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

            
5
typedef struct
6
{
7
  guint grid_offset;
8
  IPuzDeliminator delim;
9
  gboolean final_word;
10
} TestForeach;
11

            
12
typedef struct
13
{
14
  TestForeach *foreach_targets;
15
  guint n_targets;
16
  guint cur;
17
} TestForeachTuple;
18

            
19
static void
20
30
test_foreach_func (IPuzDeliminator delim,
21
                   guint           grid_offset,
22
                   gboolean        final_word,
23
                   gpointer        user_data)
24
{
25
30
  TestForeachTuple *tuple = user_data;
26

            
27
  //  g_print ("testing: %d %u\n", delim, grid_offset);
28
30
  g_assert (user_data);
29
30
  g_assert (tuple->cur < tuple->n_targets);
30
30
  g_assert (tuple->foreach_targets[tuple->cur].delim == delim);
31
30
  g_assert (tuple->foreach_targets[tuple->cur].grid_offset == grid_offset);
32
30
  g_assert (tuple->foreach_targets[tuple->cur].final_word == final_word);
33
30
  tuple->cur++;
34
30
}
35

            
36

            
37
static void
38
18
test_enumeration (const gchar *src,
39
                  const gchar *target_display,
40
                  TestForeach *foreach_targets,
41
                  guint        n_targets)
42
{
43
18
  g_autoptr (IPuzEnumeration) enumeration = NULL;
44
18
  g_autofree gchar *display = NULL;
45
18
  TestForeachTuple tuple = {
46
    .foreach_targets = foreach_targets,
47
    .n_targets = n_targets,
48
    .cur = 0,
49
  };
50

            
51
18
  enumeration = ipuz_enumeration_new (src, IPUZ_VERBOSITY_STANDARD);
52
18
  display = ipuz_enumeration_get_display (enumeration);
53
18
  g_print ("Testing (%s): \"%s\" vs \"%s\"\n",
54
           src, display, target_display);
55
18
  g_assert (g_strcmp0 (display, target_display) == 0);
56
18
  ipuz_enumeration_delim_foreach (enumeration, test_foreach_func, &tuple);
57
  /* Make sure we hit exactly the right number of enumerations */
58
18
  g_assert (n_targets == tuple.cur);
59
18
  if (n_targets == 0)
60
8
    g_assert (!ipuz_enumeration_get_has_delim (enumeration));
61
18
}
62

            
63

            
64
static TestForeach test1_foreach[] = {
65
    {8, IPUZ_DELIMINATOR_WORD_BREAK, FALSE},
66
    {16, IPUZ_DELIMINATOR_WORD_BREAK, TRUE},
67
};
68

            
69
static TestForeach test2_foreach[] = {
70
    {8, IPUZ_DELIMINATOR_WORD_BREAK, FALSE},
71
    {20, IPUZ_DELIMINATOR_WORD_BREAK, TRUE},
72
};
73

            
74
static TestForeach test3_foreach[] = {
75
    {2, IPUZ_DELIMINATOR_DASH, FALSE},
76
    {10, IPUZ_DELIMINATOR_WORD_BREAK, TRUE},
77
};
78

            
79
static TestForeach test4_foreach[] = {
80
    {8, IPUZ_DELIMINATOR_WORD_BREAK, FALSE},
81
    {16, IPUZ_DELIMINATOR_WORD_BREAK, FALSE},
82
    {22, IPUZ_DELIMINATOR_DASH, FALSE},
83
    {26, IPUZ_DELIMINATOR_WORD_BREAK, TRUE},
84
};
85
static TestForeach test5_foreach[] = {
86
    {2, IPUZ_DELIMINATOR_DASH, FALSE},
87
    {10, IPUZ_DELIMINATOR_WORD_BREAK, FALSE},
88
    {11, IPUZ_DELIMINATOR_FOREIGN, FALSE},
89
    {16, IPUZ_DELIMINATOR_WORD_BREAK, TRUE},
90
};
91
static TestForeach test6_foreach[] = {
92
    {6, IPUZ_DELIMINATOR_WORD_BREAK, FALSE},
93
    {12, IPUZ_DELIMINATOR_WORD_BREAK, FALSE},
94
    {20, IPUZ_DELIMINATOR_WORD_BREAK, FALSE},
95
    {22, IPUZ_DELIMINATOR_PERIOD, FALSE},
96
    {24, IPUZ_DELIMINATOR_PERIOD, FALSE},
97
    {26, IPUZ_DELIMINATOR_PERIOD, FALSE},
98
    {28, IPUZ_DELIMINATOR_PERIOD, FALSE},
99
    {30, IPUZ_DELIMINATOR_WORD_BREAK, TRUE},
100
};
101
static TestForeach test7_foreach[] = {
102
    {10, IPUZ_DELIMINATOR_DASH, FALSE},
103
    {10, IPUZ_DELIMINATOR_WORD_BREAK, TRUE},
104
};
105

            
106
static TestForeach test8_foreach[] = {
107
    {0, IPUZ_DELIMINATOR_DASH, FALSE},
108
    {10, IPUZ_DELIMINATOR_WORD_BREAK, TRUE},
109
};
110

            
111
static TestForeach test9_foreach[] = {
112
  {1, IPUZ_DELIMINATOR_ALLCAPS, FALSE},
113
  {10, IPUZ_DELIMINATOR_WORD_BREAK, TRUE},
114
};
115

            
116
static TestForeach test10_foreach[] = {
117
  {1, IPUZ_DELIMINATOR_CAPITALIZED, FALSE},
118
  {10, IPUZ_DELIMINATOR_WORD_BREAK, TRUE},
119
};
120

            
121
static void
122
1
test_enumeration_parse (void)
123
{
124
1
  setlocale(LC_ALL, "en_US.utf8");
125

            
126
1
  test_enumeration ("", "", NULL, 0);
127
1
  test_enumeration ("two words", "two words", NULL, 0);
128
1
  test_enumeration ("4 4", "4,4", test1_foreach, G_N_ELEMENTS (test1_foreach));
129
1
  test_enumeration ("4,6", "4,6", test2_foreach, G_N_ELEMENTS (test2_foreach));
130
1
  test_enumeration ("1-4", "1-4", test3_foreach, G_N_ELEMENTS (test3_foreach));
131
1
  test_enumeration ("1-.,4", "1-.,4", NULL, 0);
132
1
  test_enumeration ("4 4 3-2", "4,4,3-2", test4_foreach, G_N_ELEMENTS (test4_foreach));
133
1
  test_enumeration ("1-4 +3", "1-4,3 (foreign)", test5_foreach, G_N_ELEMENTS (test5_foreach));
134
1
  test_enumeration ("3,3,4,1.1.1.1.1", "3,3,4,1.1.1.1.1", test6_foreach, G_N_ELEMENTS (test6_foreach));
135
1
  test_enumeration ("5-", "5-", test7_foreach, G_N_ELEMENTS (test7_foreach));
136
1
  test_enumeration ("-5", "-5", test8_foreach, G_N_ELEMENTS (test8_foreach));
137
1
  test_enumeration ("5*,5", "5*,5", NULL, 0);
138
1
  test_enumeration ("5,5,*", "5,5,*", NULL, 0);
139
1
  test_enumeration ("5,*+5", "5,*+5", NULL, 0);
140
1
  test_enumeration ("*5", "5 (allcaps)", test9_foreach, G_N_ELEMENTS (test9_foreach));
141
1
  test_enumeration ("^5", "5 (capitalized)", test10_foreach, G_N_ELEMENTS (test10_foreach));
142
1
  test_enumeration ("0-4-2", "0-4-2", NULL, 0);
143
1
  test_enumeration ("1000000000000", "1000000000000", NULL, 0);
144
1
}
145

            
146

            
147
int
148
1
main (int   argc,
149
      char *argv[])
150
{
151
1
  g_test_init (&argc, &argv, NULL);
152

            
153
1
  g_test_add_func ("/enumerations/parse", test_enumeration_parse);
154

            
155
1
  return g_test_run ();
156
}
157
/* vi: set et sw=2: */