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

            
5

            
6
static void
7
1
test_crossword_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, "first-crossword.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
  g_assert (puzzle != NULL);
24

            
25
1
  ipuz_crossword_print (IPUZ_CROSSWORD (puzzle));
26
1
}
27

            
28
static void
29
1
test_crossword_board_equal (void)
30
{
31
1
  g_autoptr (IPuzPuzzle) puzzle_a = NULL;
32
1
  g_autoptr (IPuzPuzzle) puzzle_b = NULL;
33
1
  GError *error = NULL;
34
  gchar *path;
35

            
36
1
  setlocale(LC_ALL, "en_US.utf8");
37

            
38
1
  path = g_test_build_filename (G_TEST_DIST, "first-crossword.ipuz", NULL);
39
1
  puzzle_a = ipuz_puzzle_new_from_file (path, &error);
40
1
  g_assert (error == NULL);
41

            
42
1
  puzzle_b = ipuz_puzzle_new_from_file (path, &error);
43
1
  g_assert (error == NULL);
44

            
45
1
  g_free (path);
46

            
47
1
  g_assert (ipuz_board_equal (ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle_a)),
48
                              ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle_b))));
49
1
}
50

            
51
static void
52
1
test_crossword_board_different (void)
53
{
54
1
  g_autoptr (IPuzPuzzle) puzzle_a = NULL;
55
1
  g_autoptr (IPuzPuzzle) puzzle_b = NULL;
56
1
  GError *error = NULL;
57
  gchar *path;
58

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

            
61
1
  path = g_test_build_filename (G_TEST_DIST, "4962.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
  /* This is the same file, with one cell field modified... */
67

            
68
1
  path = g_test_build_filename (G_TEST_DIST, "4962-modified.ipuz", NULL);
69
1
  puzzle_b = ipuz_puzzle_new_from_file (path, &error);
70
1
  g_assert (error == NULL);
71
1
  g_free (path);
72

            
73
  /* ... so it should compare as being different. */
74

            
75
1
  g_assert (!ipuz_board_equal (ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle_a)),
76
                               ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle_b))));
77
1
}
78

            
79
static void
80
1
test_crossword_deep_copy (void)
81
{
82
1
  g_autoptr (IPuzPuzzle) puzzle_a = NULL;
83
1
  g_autoptr (IPuzPuzzle) puzzle_b = NULL;
84
1
  GError *error = NULL;
85
  gchar *path;
86

            
87
1
  setlocale(LC_ALL, "en_US.utf8");
88

            
89
1
  path = g_test_build_filename (G_TEST_DIST, "first-crossword.ipuz", NULL);
90
1
  puzzle_a = ipuz_puzzle_new_from_file (path, &error);
91
1
  g_assert (error == NULL);
92

            
93
1
  puzzle_b = ipuz_puzzle_deep_copy (puzzle_a);
94

            
95
1
  g_free (path);
96

            
97
1
  g_assert (ipuz_board_equal (ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle_a)),
98
                              ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle_b))));
99
1
}
100

            
101
static void
102
1
test_crossword_guesses (void)
103
{
104
1
  g_autoptr (IPuzPuzzle) puzzle = NULL;
105
1
  GError *error = NULL;
106
1
  g_autofree gchar *path = NULL;
107
1
  g_autofree gchar *saved_path = NULL;
108
1
  g_autoptr(IPuzGuesses) guesses = NULL;
109
1
  g_autoptr(IPuzGuesses) saved_guesses = NULL;
110
  IPuzCellCoord coord;
111

            
112
1
  setlocale(LC_ALL, "en_US.utf8");
113

            
114
1
  path = g_test_build_filename (G_TEST_DIST, "simple.ipuz", NULL);
115
1
  puzzle = ipuz_puzzle_new_from_file (path, &error);
116

            
117
1
  if (error != NULL)
118
    {
119
      g_print ("Error: %s\n", error->message);
120
    }
121
1
  g_assert (puzzle != NULL);
122

            
123
1
  guesses = ipuz_guesses_new_from_board (ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle)),
124
                                         FALSE);
125
1
  g_assert (guesses);
126

            
127
1
  saved_path = g_test_build_filename (G_TEST_DIST, "simple-saved.ipuz", NULL);
128
1
  saved_guesses = ipuz_guesses_new_from_file (saved_path, &error);
129
1
  g_assert (saved_guesses);
130

            
131
  /* These should be different to start */
132
1
  g_assert (!ipuz_guesses_equal (saved_guesses, guesses));
133

            
134
1
  coord.row = 0; coord.column = 0;
135
1
  ipuz_guesses_set_guess (guesses, coord, "C");
136

            
137
1
  coord.row = 2; coord.column = 1;
138
1
  ipuz_guesses_set_guess (guesses, coord, "Q");
139

            
140
1
  coord.row = 2; coord.column = 2;
141
1
  ipuz_guesses_set_guess (guesses, coord, "O");
142

            
143
  /* Now they should be the same */
144
1
  g_assert (ipuz_guesses_equal (saved_guesses, guesses));
145

            
146
  /* FIXME: Figure out where I'm supposed to write files during tests */
147
  //  g_assert (ipuz_guesses_save_to_file (saved_guesses, "/tmp/test.json", &error));
148
1
}
149

            
150

            
151
static void
152
1
test_crossword_calc_clues (void)
153
{
154
1
  g_autoptr (IPuzPuzzle) puzzle_a = NULL;
155
1
  g_autoptr (IPuzPuzzle) puzzle_b = NULL;
156
1
  GError *error = NULL;
157
  gchar *path;
158

            
159
1
  setlocale(LC_ALL, "en_US.utf8");
160

            
161
  /* Load a file with no clue numbers */
162
1
  path = g_test_build_filename (G_TEST_DIST, "calc.ipuz", NULL);
163
1
  puzzle_a = ipuz_puzzle_new_from_file (path, &error);
164
1
  g_assert (error == NULL);
165
1
  g_free (path);
166

            
167

            
168
  /* same puzzle but with the correct clue nubmers */
169
1
  path = g_test_build_filename (G_TEST_DIST, "calc-corrected.ipuz", NULL);
170
1
  puzzle_b = ipuz_puzzle_new_from_file (path, &error);
171
1
  g_assert (error == NULL);
172
1
  g_free (path);
173

            
174
  /* it should compare as being different. */
175
1
  g_assert (!ipuz_board_equal (ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle_a)),
176
                               ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle_b))));
177

            
178
1
  ipuz_crossword_fix_all (IPUZ_CROSSWORD (puzzle_a), NULL);
179

            
180
  /* And now they should compare as being the same. */
181
1
  g_assert (ipuz_board_equal (ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle_a)),
182
                              ipuz_crossword_get_board (IPUZ_CROSSWORD (puzzle_b))));
183
1
}
184

            
185
static IPuzStyle *
186
8
get_style (IPuzPuzzle *puzzle, IPuzCellCoord coord)
187
{
188
8
  IPuzCell *cell = ipuz_crossword_get_cell (IPUZ_CROSSWORD (puzzle), coord);
189
8
  g_assert (cell != NULL);
190
8
  return ipuz_cell_get_style (cell);
191
}
192

            
193
static IPuzStyleShape
194
5
get_shapebg (IPuzPuzzle *puzzle, IPuzCellCoord coord)
195
{
196
5
  IPuzStyle *style = get_style (puzzle, coord);
197
5
  g_assert (style != NULL);
198
5
  return ipuz_style_get_shapebg (style);
199
}
200

            
201
static IPuzStyleSides
202
2
get_barred (IPuzPuzzle *puzzle, IPuzCellCoord coord)
203
{
204
2
  IPuzStyle *style = get_style (puzzle, coord);
205
2
  g_assert (style != NULL);
206
2
  return ipuz_style_get_barred (style);
207
}
208

            
209
static void
210
1
test_crossword_load_styles (void)
211
{
212
1
  g_autoptr (IPuzPuzzle) puzzle = NULL;
213
1
  GError *error = NULL;
214
  gchar *path;
215

            
216
1
  setlocale(LC_ALL, "en_US.utf8");
217

            
218
  /* this file has styles in it */
219
1
  path = g_test_build_filename (G_TEST_DIST, "cloud.ipuz", NULL);
220
1
  puzzle = ipuz_puzzle_new_from_file (path, &error);
221
1
  g_assert_no_error (error);
222

            
223
1
  IPuzCellCoord coord = {
224
    .row = 1,
225
    .column = 9,
226
  };
227
1
  g_assert_cmpint (get_shapebg (puzzle, coord), ==, IPUZ_STYLE_SHAPE_CIRCLE);
228

            
229
  // Test backslash to see if quoting is done correctly.
230

            
231
  // test style by reference
232
1
  coord.column = 10;
233
1
  g_assert_cmpint (get_shapebg (puzzle, coord), ==, IPUZ_STYLE_SHAPE_BACKSLASH);
234

            
235
  // test style inline
236
1
  coord.column = 11;
237
1
  g_assert_cmpint (get_shapebg (puzzle, coord), ==, IPUZ_STYLE_SHAPE_BACKSLASH);
238

            
239
  // style with no shapebg
240
1
  coord.column = 12;
241
1
  g_assert_cmpint (get_shapebg (puzzle, coord), ==, IPUZ_STYLE_SHAPE_NONE);
242

            
243
  // no style
244
1
  coord.row = 2;
245
1
  coord.column = 7;
246
1
  g_assert_null (get_style (puzzle, coord));
247

            
248
  // unsupported shapebg, falls back to NONE
249
1
  coord.column = 8;
250
1
  g_assert_cmpint (get_shapebg (puzzle, coord), ==, IPUZ_STYLE_SHAPE_NONE);
251

            
252
  // barred style
253
1
  coord.row = 15;
254
1
  coord.column = 0;
255
1
  g_assert_cmpint (get_barred (puzzle, coord), ==, IPUZ_STYLE_SIDES_TOP | IPUZ_STYLE_SIDES_RIGHT);
256

            
257
1
  coord.row = 15;
258
1
  coord.column = 1;
259
1
  g_assert_cmpint (get_barred (puzzle, coord), ==, IPUZ_STYLE_SIDES_BOTTOM | IPUZ_STYLE_SIDES_LEFT);
260

            
261
1
  g_free (path);
262
1
}
263

            
264

            
265
static void
266
1
test_crossword_save (void)
267
{
268
1
  g_autoptr (IPuzPuzzle) puzzle_a = NULL;
269
1
  g_autoptr (IPuzPuzzle) puzzle_b = NULL;
270
1
  GError *error = NULL;
271
  gchar *path;
272
1
  g_autofree gchar *data = NULL;
273
  gsize length;
274

            
275
1
  setlocale(LC_ALL, "en_US.utf8");
276

            
277
1
  path = g_test_build_filename (G_TEST_DIST, "first-crossword.ipuz", NULL);
278
1
  puzzle_a = ipuz_puzzle_new_from_file (path, &error);
279
1
  g_assert (error == NULL);
280
1
  g_free (path);
281

            
282
1
  data = ipuz_puzzle_save_to_data (puzzle_a, &length);
283
1
  g_print ("%s", data);
284
1
  puzzle_b = ipuz_puzzle_new_from_data (data, length, &error);
285
1
  g_assert (error == NULL);
286

            
287
1
  g_assert (ipuz_puzzle_equal (puzzle_a, puzzle_b));
288
1
}
289

            
290
int
291
1
main (int   argc,
292
      char *argv[])
293
{
294
1
  g_test_init (&argc, &argv, NULL);
295

            
296
1
  g_test_add_func ("/crossword/load", test_crossword_load);
297
1
  g_test_add_func ("/crossword/board_equal", test_crossword_board_equal);
298
1
  g_test_add_func ("/crossword/board_different", test_crossword_board_different);
299
1
  g_test_add_func ("/crossword/deep_copy", test_crossword_deep_copy);
300
1
  g_test_add_func ("/crossword/guesses", test_crossword_guesses);
301
1
  g_test_add_func ("/crossword/calc_clues", test_crossword_calc_clues);
302
1
  g_test_add_func ("/crossword/load_styles", test_crossword_load_styles);
303
1
  g_test_add_func ("/crossword/save", test_crossword_save);
304

            
305
1
  return g_test_run ();
306
}
307