1
#include <glib.h>
2
#include <libipuz/libipuz.h>
3
#include <locale.h>
4

            
5
static void
6
1
ipuz_charset_handles_ascii (void)
7
{
8
1
  IPuzCharsetBuilder *builder = ipuz_charset_builder_new ();
9
1
  ipuz_charset_builder_add_text (builder, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
10

            
11
2
  g_autoptr (IPuzCharset) charset = ipuz_charset_builder_build (builder);
12

            
13
1
  g_assert_cmpint (ipuz_charset_get_n_chars (charset), ==, 26);
14
1
  g_assert_cmpint (ipuz_charset_get_char_index (charset, 'A'), ==, 0);
15
1
  g_assert_cmpint (ipuz_charset_get_char_index (charset, 'M'), ==, 12);
16
1
  g_assert_cmpint (ipuz_charset_get_char_index (charset, 'Z'), ==, 25);
17
1
  g_assert_cmpint (ipuz_charset_get_char_index (charset, '7'), ==, -1);
18

            
19
2
  g_autofree char *serialized = ipuz_charset_serialize (charset);
20
1
  g_assert_cmpstr (serialized, ==, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
21
1
}
22

            
23
static void
24
1
ipuz_charset_handles_non_ascii (void)
25
{
26
1
  IPuzCharsetBuilder *builder = ipuz_charset_builder_new ();
27

            
28
1
  ipuz_charset_builder_add_text (builder, "ÁRBOL");
29
1
  ipuz_charset_builder_add_text (builder, "BLÅHAJ");
30
1
  ipuz_charset_builder_add_text (builder, "BORLA");
31
1
  ipuz_charset_builder_add_text (builder, "TRALALA");
32

            
33
2
  g_autoptr (IPuzCharset) charset = ipuz_charset_builder_build (builder);
34

            
35
1
  g_assert_cmpint (ipuz_charset_get_n_chars (charset), ==, 10);
36
2
  g_autofree char *serialized = ipuz_charset_serialize (charset);
37

            
38
  /* Characters are sorted by Unicode code point, not logically.  I guess that doesn't matter? */
39
1
  g_assert_cmpstr (serialized, ==, "ABHJLORTÁÅ");
40

            
41
1
  g_assert_cmpint (ipuz_charset_get_char_index (charset, 'X'), ==, -1);
42
1
}
43

            
44
static void
45
1
ipuz_charset_serialization_roundtrip (void)
46
{
47
1
  IPuzCharsetBuilder *builder = ipuz_charset_builder_new ();
48

            
49
1
  ipuz_charset_builder_add_text (builder, "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚ");
50

            
51
2
  g_autoptr (IPuzCharset) charset = ipuz_charset_builder_build (builder);
52

            
53
2
  g_autofree char *ser = ipuz_charset_serialize (charset);
54
2
  g_autoptr (IPuzCharset) deserialized = ipuz_charset_deserialize (ser);
55

            
56
2
  g_autofree char *roundtrip = ipuz_charset_serialize (deserialized);
57
1
  g_assert (strcmp (ser, roundtrip) == 0);
58
1
  g_assert (strcmp (roundtrip, "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚ") == 0);
59
1
}
60

            
61
static void
62
1
histogram_support (void)
63
{
64
1
  IPuzCharsetBuilder *builder = ipuz_charset_builder_new ();
65

            
66
1
  ipuz_charset_builder_add_text (builder, "ABBCCCDDDDEEEEEFFFFFFGGGGGGG");
67

            
68
2
  g_autoptr (IPuzCharset) charset = ipuz_charset_builder_build (builder);
69

            
70
1
  g_assert (ipuz_charset_get_char_count (charset, g_utf8_get_char("E")) == 5);
71
1
}
72

            
73
static void
74
1
test_iters (void)
75
{
76
1
  IPuzCharsetBuilder *builder = ipuz_charset_builder_new ();
77
  IPuzCharsetIter *iter;
78
1
  guint n_chars = 0;
79

            
80
1
  ipuz_charset_builder_add_text (builder, "THETIMEHASCOMEFORALLGOODMENTOCOMETOTHEAIDOFTHEIRPARTY");
81

            
82
2
  g_autoptr (IPuzCharset) charset = ipuz_charset_builder_build (builder);
83

            
84
1
  for (iter = ipuz_charset_iter_first (charset);
85
18
       iter;
86
17
       iter= ipuz_charset_iter_next (iter))
87
    {
88
      gchar str[7];
89
      guint len;
90
      IPuzCharsetIterValue value;
91

            
92
17
      value = ipuz_charset_iter_get_value (iter);
93
17
      len = g_unichar_to_utf8 (value.c, str);
94
17
      str[len] = '\0';
95

            
96
      // g_print ("Char:%s:\tCount:%u:\n", str, value.count);
97

            
98
17
      if (!g_strcmp0 (str, "E"))
99
1
        g_assert (value.count == 7);
100
17
      n_chars ++;
101
    }
102

            
103
  /* Trust... */
104
1
  g_assert (n_chars == ipuz_charset_get_n_chars (charset));
105

            
106
  /* But verify. */
107
1
  g_assert (n_chars == 17);
108
1
}
109

            
110
static void
111
1
test_charset_remove_text (void)
112
{
113
1
  IPuzCharsetBuilder *builder = ipuz_charset_builder_new ();
114

            
115
1
  ipuz_charset_builder_add_text (builder, "AABBCCDDEEFFG");
116

            
117
1
  g_assert_false (ipuz_charset_builder_remove_text (builder , "TEXT"));
118

            
119
1
  g_assert_true (ipuz_charset_builder_remove_text (builder, "ABCDEFG"));
120

            
121
2
  g_autoptr (IPuzCharset) charset = ipuz_charset_builder_build (builder);
122

            
123
1
  g_assert_cmpint (ipuz_charset_get_size (charset), ==, 6);
124
1
}
125

            
126
static void
127
1
test_charset_equal_lang (void)
128
{
129
1
  g_autoptr (IPuzCharset) ipuz_charset_a = NULL;
130
1
  g_autoptr (IPuzCharset) ipuz_charset_b = NULL;
131

            
132
1
  ipuz_charset_a = ipuz_charset_builder_build (ipuz_charset_builder_new_for_language ("C"));
133
1
  ipuz_charset_b = ipuz_charset_builder_build (ipuz_charset_builder_new_for_language ("en"));
134

            
135
1
  g_assert (ipuz_charset_compare (ipuz_charset_a, ipuz_charset_b));
136

            
137
1
  ipuz_charset_unref (ipuz_charset_b);
138
1
  ipuz_charset_b = ipuz_charset_builder_build (ipuz_charset_builder_new_for_language ("es"));
139

            
140
1
  g_assert (! ipuz_charset_compare (ipuz_charset_a, ipuz_charset_b));
141
1
}
142

            
143
int
144
1
main (int argc, char **argv)
145
{
146
1
  setlocale(LC_ALL, "en_US.utf8");
147

            
148
1
  g_test_init (&argc, &argv, NULL);
149

            
150
1
  g_test_add_func ("/charset/handles_ascii", ipuz_charset_handles_ascii);
151
1
  g_test_add_func ("/charset/handles_non_ascii", ipuz_charset_handles_non_ascii);
152
1
  g_test_add_func ("/charset/serialization_roundtrip", ipuz_charset_serialization_roundtrip);
153
1
  g_test_add_func ("/charset/histogram", histogram_support);
154
1
  g_test_add_func ("/charset/iters", test_iters);
155

            
156
1
  g_test_add_func ("/charset/remove_text", test_charset_remove_text);
157

            
158
1
  g_test_add_func ("/charset/equal_lang", test_charset_equal_lang);
159

            
160
1
  return g_test_run ();
161
}