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

            
5

            
6
static void
7
1
test_acrostic_load (void)
8
{
9
1
  g_autoptr (IPuzPuzzle) puzzle = NULL;
10
1
  GError *error = NULL;
11
  gchar *path;
12

            
13
1
  setlocale(LC_ALL, "en_US.utf8");
14

            
15
1
  path = g_test_build_filename (G_TEST_DIST, "acrostic1.ipuz", NULL);
16
1
  puzzle = ipuz_puzzle_new_from_file (path, &error);
17
1
  g_free (path);
18

            
19
1
  if (error != NULL)
20
    {
21
      g_print ("Error: %s\n", error->message);
22
    }
23
1
  ipuz_crossword_print (IPUZ_CROSSWORD (puzzle));
24
1
  g_assert (puzzle != NULL);
25
1
}
26

            
27
static void
28
1
test_acrostic_load2 (void)
29
{
30
1
  g_autoptr (IPuzPuzzle) puzzle = NULL;
31
1
  GError *error = NULL;
32
  gchar *path;
33

            
34
1
  setlocale(LC_ALL, "en_US.utf8");
35

            
36
1
  path = g_test_build_filename (G_TEST_DIST, "acrostic2.ipuz", NULL);
37
1
  puzzle = ipuz_puzzle_new_from_file (path, &error);
38
1
  g_free (path);
39

            
40
1
  if (error != NULL)
41
    {
42
      g_print ("Error: %s\n", error->message);
43
    }
44

            
45
1
  g_assert (puzzle != NULL);
46
1
}
47

            
48
static void
49
1
test_acrostic_quote_clue (void)
50
{
51
1
  g_autoptr (IPuzPuzzle) puzzle_a = NULL;
52
1
  g_autoptr (IPuzPuzzle) puzzle_b = NULL;
53
  IPuzClue *clue_a;
54
  IPuzClue *clue_b;
55
1
  GError *error = NULL;
56
  gchar *path;
57

            
58
1
  setlocale(LC_ALL, "en_US.utf8");
59

            
60
  /* Load file with quote clue */
61
1
  path = g_test_build_filename (G_TEST_DIST, "acrostic2.ipuz", NULL);
62
1
  puzzle_a = ipuz_puzzle_new_from_file (path, &error);
63
1
  g_assert (error == NULL);
64
1
  g_free (path);
65

            
66
  /* same file without quote clue */
67
1
  path = g_test_build_filename (G_TEST_DIST, "acrostic2-no-quote-clue.ipuz", NULL);
68
1
  puzzle_b = ipuz_puzzle_new_from_file (path, &error);
69
1
  g_assert (error == NULL);
70
1
  g_free (path);
71

            
72
1
  clue_a = ipuz_acrostic_get_quote_clue (IPUZ_ACROSTIC (puzzle_a));
73
1
  clue_b = ipuz_acrostic_get_quote_clue (IPUZ_ACROSTIC (puzzle_b));
74

            
75
  /* These clues will have different values of clue_set, so we can't
76
   * just compare that they're equal. We do want to show that they
77
   * have the same cells, though */
78
1
  g_assert (clue_a != NULL);
79
1
  g_assert (clue_b != NULL);
80
1
  g_assert (clue_a->cells != NULL);
81
1
  g_assert (clue_b->cells != NULL);
82
1
  g_assert (clue_a->cells->len == clue_b->cells->len);
83

            
84
1
  g_assert (memcmp (clue_a->cells->data,
85
                    clue_b->cells->data,
86
                    clue_a->cells->len * sizeof (IPuzCellCoord)) == 0);
87
1
}
88

            
89
static void
90
1
test_acrostic_save (void)
91
{
92
1
  g_autoptr (IPuzPuzzle) puzzle_a = NULL;
93
1
  g_autoptr (IPuzPuzzle) puzzle_b = NULL;
94
1
  GError *error = NULL;
95
  gchar *path;
96
1
  g_autofree gchar *data = NULL;
97
  gsize length;
98

            
99
1
  setlocale(LC_ALL, "en_US.utf8");
100

            
101
1
  path = g_test_build_filename (G_TEST_DIST, "acrostic1.ipuz", NULL);
102
1
  puzzle_a = ipuz_puzzle_new_from_file (path, &error);
103
1
  g_assert (error == NULL);
104
1
  g_free (path);
105

            
106
1
  data = ipuz_puzzle_save_to_data (puzzle_a, &length);
107
1
  g_print ("%s", data);
108
1
  puzzle_b = ipuz_puzzle_new_from_data (data, length, &error);
109
1
  g_assert (error == NULL);
110

            
111
1
  g_assert (ipuz_puzzle_equal (puzzle_a, puzzle_b));
112
1
}
113

            
114
static void
115
1
test_acrostic_deep_copy (void)
116
{
117
1
  g_autoptr (IPuzPuzzle) puzzle_a = NULL;
118
1
  g_autoptr (IPuzPuzzle) puzzle_b = NULL;
119
1
  GError *error = NULL;
120
  gchar *path;
121

            
122
1
  setlocale(LC_ALL, "en_US.utf8");
123

            
124
1
  path = g_test_build_filename (G_TEST_DIST, "acrostic1.ipuz", NULL);
125
1
  puzzle_a = ipuz_puzzle_new_from_file (path, &error);
126
1
  g_assert (error == NULL);
127
1
  g_free (path);
128

            
129
1
  puzzle_b = ipuz_puzzle_deep_copy (puzzle_a);
130

            
131
1
  g_assert (ipuz_puzzle_equal (puzzle_a, puzzle_b));
132
1
}
133

            
134
static void
135
1
test_acrostic_quote_str (void)
136
{
137
1
  g_autoptr (IPuzPuzzle) puzzle = NULL;
138

            
139
1
  puzzle = ipuz_acrostic_new ();
140

            
141
1
  g_assert_cmpstr (ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)), ==, NULL);
142

            
143
1
  ipuz_acrostic_set_quote_str (IPUZ_ACROSTIC (puzzle), "QUOTE STRING");
144

            
145
1
  g_assert_cmpstr (ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)), ==, "QUOTE STRING");
146

            
147
1
  ipuz_acrostic_set_quote_str (IPUZ_ACROSTIC (puzzle), "  Some Garbage#$");
148

            
149
1
  g_assert_cmpstr (ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)), ==, "SOME GARBAGE");
150

            
151
1
  ipuz_acrostic_set_quote_str (IPUZ_ACROSTIC (puzzle), "  multi  space");
152

            
153
1
  g_assert_cmpstr (ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)), ==, "MULTI  SPACE");
154
1
}
155

            
156
static void
157
1
test_acrostic_sync_quote_str_to_grid (void)
158
{
159
1
  g_autoptr (IPuzPuzzle) puzzle = NULL;
160
1
  IPuzCell *cell = NULL;
161
1
  IPuzCellCoord coord = {
162
    .row = 0,
163
    .column = 0,
164
  };
165

            
166
1
  puzzle = ipuz_acrostic_new ();
167

            
168
1
  ipuz_acrostic_set_quote_str (IPUZ_ACROSTIC (puzzle), "QUOTE STRING");
169

            
170
  /* sets board size to 4 * 3 */
171
  /*
172
   *  +---+---+---+---+
173
   *  | Q | U | O | T |
174
   *  +---+---+---+---+
175
   *  | E | # | S | T |
176
   *  +---+---+---+---+
177
   *  | R | I | N | G |
178
   *  +---+---+---+---+
179
   *
180
   */
181

            
182
1
  ipuz_crossword_fix_all (IPUZ_CROSSWORD (puzzle),
183
                          "sync-direction", IPUZ_ACROSTIC_SYNC_QUOTE_STR_TO_GRID,
184
                          NULL);
185

            
186
1
  g_assert_cmpint (ipuz_crossword_get_width (IPUZ_CROSSWORD (puzzle)), ==, 4);
187
1
  g_assert_cmpint (ipuz_crossword_get_height (IPUZ_CROSSWORD (puzzle)), ==, 3);
188

            
189
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
190
1
  g_assert_cmpstr (ipuz_cell_get_solution (cell), ==, "Q");
191
1
  g_assert_cmpint (ipuz_cell_get_cell_type (cell), ==, IPUZ_CELL_NORMAL);
192

            
193
1
  coord.row = 1;
194
1
  coord.column = 0;
195
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
196
1
  g_assert_cmpstr (ipuz_cell_get_solution (cell), ==, "E");
197
1
  g_assert_cmpint (ipuz_cell_get_cell_type (cell), ==, IPUZ_CELL_NORMAL);
198

            
199
1
  coord.row = 1;
200
1
  coord.column = 1;
201
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
202
1
  g_assert_cmpstr (ipuz_cell_get_solution (cell), ==, NULL);
203
1
  g_assert_cmpint (ipuz_cell_get_cell_type (cell), ==, IPUZ_CELL_BLOCK);
204

            
205
1
  coord.row = 2;
206
1
  coord.column = 3;
207
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
208
1
  g_assert_cmpstr (ipuz_cell_get_solution (cell), ==, "G");
209
1
  g_assert_cmpint (ipuz_cell_get_cell_type (cell), ==, IPUZ_CELL_NORMAL);
210
1
}
211

            
212
static void
213
1
test_acrostic_sync_grid_to_quote_str (void)
214
{
215
1
  g_autoptr (IPuzPuzzle) puzzle = NULL;
216
1
  IPuzCell *cell = NULL;
217
1
  IPuzCellCoord coord = {
218
    .row = 0,
219
    .column = 2,
220
  };
221

            
222
1
  puzzle = ipuz_acrostic_new ();
223

            
224
1
  ipuz_acrostic_set_quote_str (IPUZ_ACROSTIC (puzzle), "QUOTE STRING");
225

            
226
  /* sets board size to 4 * 3 */
227
  /*
228
   *  +---+---+---+---+
229
   *  | Q | U | O | T |
230
   *  +---+---+---+---+
231
   *  | E | # | S | T |
232
   *  +---+---+---+---+
233
   *  | R | I | N | G |
234
   *  +---+---+---+---+
235
   *
236
   */
237

            
238
1
  ipuz_crossword_fix_all (IPUZ_CROSSWORD (puzzle),
239
                          "sync-direction", IPUZ_ACROSTIC_SYNC_QUOTE_STR_TO_GRID,
240
                          NULL);
241

            
242
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
243
1
  ipuz_cell_set_solution (cell, "I");
244

            
245
1
  coord.row = 2;
246
1
  coord.column = 0;
247
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
248
1
  ipuz_cell_set_solution (cell, "U");
249

            
250
1
  coord.row = 2;
251
1
  coord.column = 1;
252
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
253
1
  ipuz_cell_set_solution (cell, "N");
254

            
255
1
  coord.row = 2;
256
1
  coord.column = 2;
257
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
258
1
  ipuz_cell_set_solution (cell, "G");
259

            
260
1
  coord.row = 2;
261
1
  coord.column = 3;
262
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
263
1
  ipuz_cell_set_cell_type (cell, IPUZ_CELL_BLOCK);
264

            
265

            
266
1
  ipuz_crossword_fix_all (IPUZ_CROSSWORD (puzzle),
267
                          "sync-direction", IPUZ_ACROSTIC_SYNC_GRID_TO_QUOTE_STR,
268
                          NULL);
269

            
270
1
  g_assert_cmpstr (ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)), ==, "QUITE STUNG");
271

            
272
1
}
273

            
274
static void
275
1
test_acrostic_update_quote_str (void)
276
{
277
1
  IPuzCellCoord coord = {
278
    .row = 0,
279
    .column = 0,
280
  };
281
  IPuzCell *cell;
282

            
283
1
  g_autoptr (IPuzPuzzle) puzzle = NULL;
284
1
  g_autofree gchar *quote_str = NULL;
285

            
286
1
  puzzle = ipuz_acrostic_new ();
287
1
  ipuz_acrostic_set_quote_str (IPUZ_ACROSTIC (puzzle), "  quote string       ");
288
1
  ipuz_acrostic_fix_quote_str (IPUZ_ACROSTIC (puzzle), IPUZ_ACROSTIC_SYNC_QUOTE_STR_TO_GRID);
289
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
290
1
  g_assert_cmpstr (ipuz_cell_get_solution (cell), ==, "Q");
291

            
292
  /* Set a new quote str and make sure it updates corectly */
293
1
  ipuz_acrostic_set_quote_str (IPUZ_ACROSTIC (puzzle), " longer quote #string  ");
294
1
  ipuz_acrostic_fix_quote_str (IPUZ_ACROSTIC (puzzle), IPUZ_ACROSTIC_SYNC_QUOTE_STR_TO_GRID);
295
1
  g_assert_cmpint (ipuz_crossword_get_width (IPUZ_CROSSWORD (puzzle)), ==, 5);
296
1
  g_assert_cmpint (ipuz_crossword_get_height (IPUZ_CROSSWORD (puzzle)), ==, 4);
297

            
298
1
  cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
299
1
  g_assert_cmpstr (ipuz_cell_get_solution (cell), ==, "L");
300

            
301
1
}
302

            
303
static void
304
1
test_acrostic_non_english (void)
305
{
306
1
  g_autoptr (IPuzPuzzle) puzzle = NULL;
307
  IPuzCharsetBuilder *builder;
308
  IPuzCharset *charset;
309
1
  gchar *quote_str = NULL;
310

            
311
1
  puzzle = ipuz_acrostic_new ();
312

            
313
  /* Test for ES */
314
1
  builder = ipuz_charset_builder_new_for_language ("es");
315
1
  charset = ipuz_charset_builder_build (builder);
316
1
  builder = NULL;
317
1
  g_object_set (puzzle, "lang-charset", charset, NULL);
318

            
319
1
  ipuz_acrostic_set_quote_str (IPUZ_ACROSTIC (puzzle), "QUOTE STRIÑG");
320

            
321
1
  g_assert_cmpstr (ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)), ==, "QUOTE STRIÑG");
322

            
323
1
  ipuz_acrostic_fix_quote_str (IPUZ_ACROSTIC (puzzle), IPUZ_ACROSTIC_SYNC_QUOTE_STR_TO_GRID);
324

            
325
1
  quote_str = g_strdup (ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)));
326

            
327
1
  ipuz_acrostic_fix_quote_str (IPUZ_ACROSTIC (puzzle), IPUZ_ACROSTIC_SYNC_GRID_TO_QUOTE_STR);
328

            
329
1
  g_assert_cmpstr (quote_str, ==, ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)));
330

            
331
1
  g_free (quote_str);
332

            
333
  /* Test for IT */
334
1
  builder = ipuz_charset_builder_new_for_language ("it");
335
1
  charset = ipuz_charset_builder_build (builder);
336
1
  builder = NULL;
337
1
  g_object_set (puzzle, "lang-charset", charset, NULL);
338

            
339
1
  ipuz_acrostic_set_quote_str (IPUZ_ACROSTIC (puzzle), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
340

            
341
1
  g_assert_cmpstr (ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)), ==, "ABCDEFGHI  LMNOPQRSTUV   Z");
342

            
343
1
  ipuz_acrostic_fix_quote_str (IPUZ_ACROSTIC (puzzle), IPUZ_ACROSTIC_SYNC_GRID_TO_QUOTE_STR);
344

            
345
1
  quote_str = g_strdup (ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)));
346

            
347
1
  ipuz_acrostic_fix_quote_str (IPUZ_ACROSTIC (puzzle), IPUZ_ACROSTIC_SYNC_QUOTE_STR_TO_GRID);
348

            
349
1
  g_assert_cmpstr (quote_str, ==, ipuz_acrostic_get_quote_str (IPUZ_ACROSTIC (puzzle)));
350

            
351
1
  g_free (quote_str);
352
1
}
353

            
354
int
355
1
main (int   argc,
356
      char *argv[])
357
{
358
1
  g_test_init (&argc, &argv, NULL);
359

            
360
1
  g_test_add_func ("/acrostic/load", test_acrostic_load);
361
1
  g_test_add_func ("/acrostic/load2", test_acrostic_load2);
362
1
  g_test_add_func ("/acrostic/quote_clue", test_acrostic_quote_clue);
363
1
  g_test_add_func ("/acrostic/save", test_acrostic_save);
364
1
  g_test_add_func ("/acrostic/deep_copy", test_acrostic_deep_copy);
365
1
  g_test_add_func ("/acrostic/quote_str", test_acrostic_quote_str);
366
1
  g_test_add_func ("/acrostic/sync_quote_str_to_grid", test_acrostic_sync_quote_str_to_grid);
367
1
  g_test_add_func ("/acrostic/sync_grid_to_quote_str", test_acrostic_sync_grid_to_quote_str);
368
1
  g_test_add_func ("/acrostic/update_quote_str", test_acrostic_update_quote_str);
369
1
  g_test_add_func ("/acrostic/non_english", test_acrostic_non_english);
370

            
371
1
  return g_test_run ();
372
}