1 /*
2  * Copyright © 2013  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26 
27 #include "hb-test.h"
28 
29 /* Unit tests for hb-set.h */
30 
31 
32 static void
test_empty(hb_set_t * s)33 test_empty (hb_set_t *s)
34 {
35   hb_codepoint_t next = HB_SET_VALUE_INVALID;
36   g_assert_cmpint (hb_set_get_population (s), ==, 0);
37   g_assert_cmpint (hb_set_get_min (s), ==, HB_SET_VALUE_INVALID);
38   g_assert_cmpint (hb_set_get_max (s), ==, HB_SET_VALUE_INVALID);
39   g_assert (!hb_set_has (s, 13));
40   g_assert (!hb_set_next (s, &next));
41   g_assert_cmpint (next, ==, HB_SET_VALUE_INVALID);
42   g_assert (hb_set_is_empty (s));
43 }
44 
45 static void
test_not_empty(hb_set_t * s)46 test_not_empty (hb_set_t *s)
47 {
48   hb_codepoint_t next = HB_SET_VALUE_INVALID;
49   g_assert_cmpint (hb_set_get_population (s), !=, 0);
50   g_assert_cmpint (hb_set_get_min (s), !=, HB_SET_VALUE_INVALID);
51   g_assert_cmpint (hb_set_get_max (s), !=, HB_SET_VALUE_INVALID);
52   g_assert (hb_set_next (s, &next));
53   g_assert_cmpint (next, !=, HB_SET_VALUE_INVALID);
54 }
55 
56 static void
test_set_basic(void)57 test_set_basic (void)
58 {
59   hb_set_t *s = hb_set_create ();
60 
61   test_empty (s);
62   hb_set_add (s, 13);
63   test_not_empty (s);
64 
65   hb_set_clear (s);
66   test_empty (s);
67 
68   hb_set_add (s, 33000);
69   test_not_empty (s);
70   hb_set_clear (s);
71 
72   hb_set_add_range (s, 10, 29);
73   test_not_empty (s);
74   g_assert (hb_set_has (s, 13));
75   g_assert_cmpint (hb_set_get_population (s), ==, 20);
76   g_assert_cmpint (hb_set_get_min (s), ==, 10);
77   g_assert_cmpint (hb_set_get_max (s), ==, 29);
78 
79   hb_set_invert (s);
80   test_not_empty (s);
81   g_assert (!hb_set_has (s, 13));
82   g_assert_cmpint (hb_set_get_min (s), ==, 0);
83 
84   hb_set_invert (s);
85   test_not_empty (s);
86   g_assert (hb_set_has (s, 13));
87   g_assert_cmpint (hb_set_get_population (s), ==, 20);
88   g_assert_cmpint (hb_set_get_min (s), ==, 10);
89   g_assert_cmpint (hb_set_get_max (s), ==, 29);
90 
91   hb_set_del_range (s, 10, 18);
92   test_not_empty (s);
93   g_assert (!hb_set_has (s, 13));
94 
95   hb_set_destroy (s);
96 }
97 
98 static void
test_set_algebra(void)99 test_set_algebra (void)
100 {
101   hb_set_t *s = hb_set_create ();
102   hb_set_t *o = hb_set_create ();
103 
104   hb_set_add (o, 13);
105   hb_set_add (o, 19);
106 
107   test_empty (s);
108   g_assert (!hb_set_is_equal (s, o));
109   hb_set_set (s, o);
110   g_assert (hb_set_is_equal (s, o));
111   test_not_empty (s);
112   g_assert_cmpint (hb_set_get_population (s), ==, 2);
113 
114   hb_set_clear (s);
115   test_empty (s);
116   hb_set_add (s, 10);
117   g_assert_cmpint (hb_set_get_population (s), ==, 1);
118   hb_set_union (s, o);
119   g_assert_cmpint (hb_set_get_population (s), ==, 3);
120   g_assert (hb_set_has (s, 10));
121   g_assert (hb_set_has (s, 13));
122 
123   hb_set_clear (s);
124   test_empty (s);
125   hb_set_add_range (s, 10, 17);
126   g_assert (!hb_set_is_equal (s, o));
127   hb_set_intersect (s, o);
128   g_assert (!hb_set_is_equal (s, o));
129   test_not_empty (s);
130   g_assert_cmpint (hb_set_get_population (s), ==, 1);
131   g_assert (!hb_set_has (s, 10));
132   g_assert (hb_set_has (s, 13));
133 
134   hb_set_clear (s);
135   test_empty (s);
136   hb_set_add_range (s, 10, 17);
137   g_assert (!hb_set_is_equal (s, o));
138   hb_set_subtract (s, o);
139   g_assert (!hb_set_is_equal (s, o));
140   test_not_empty (s);
141   g_assert_cmpint (hb_set_get_population (s), ==, 7);
142   g_assert (hb_set_has (s, 12));
143   g_assert (!hb_set_has (s, 13));
144   g_assert (!hb_set_has (s, 19));
145 
146   hb_set_clear (s);
147   test_empty (s);
148   hb_set_add_range (s, 10, 17);
149   g_assert (!hb_set_is_equal (s, o));
150   hb_set_symmetric_difference (s, o);
151   g_assert (!hb_set_is_equal (s, o));
152   test_not_empty (s);
153   g_assert_cmpint (hb_set_get_population (s), ==, 8);
154   g_assert (hb_set_has (s, 12));
155   g_assert (!hb_set_has (s, 13));
156   g_assert (hb_set_has (s, 19));
157 
158   hb_set_destroy (s);
159 }
160 
161 static void
test_set_iter(void)162 test_set_iter (void)
163 {
164   hb_codepoint_t next, first, last;
165   hb_set_t *s = hb_set_create ();
166 
167   hb_set_add (s, 13);
168   hb_set_add_range (s, 6, 6);
169   hb_set_add_range (s, 10, 15);
170   hb_set_add (s, 20005);
171 
172   test_not_empty (s);
173 
174   next = HB_SET_VALUE_INVALID;
175   g_assert (hb_set_next (s, &next));
176   g_assert_cmpint (next, ==, 6);
177   g_assert (hb_set_next (s, &next));
178   g_assert_cmpint (next, ==, 10);
179   g_assert (hb_set_next (s, &next));
180   g_assert (hb_set_next (s, &next));
181   g_assert (hb_set_next (s, &next));
182   g_assert_cmpint (next, ==, 13);
183   g_assert (hb_set_next (s, &next));
184   g_assert (hb_set_next (s, &next));
185   g_assert_cmpint (next, ==, 15);
186   g_assert (hb_set_next (s, &next));
187   g_assert_cmpint (next, ==, 20005);
188   g_assert (!hb_set_next (s, &next));
189   g_assert_cmpint (next, ==, HB_SET_VALUE_INVALID);
190 
191   first = last = HB_SET_VALUE_INVALID;
192   g_assert (hb_set_next_range (s, &first, &last));
193   g_assert_cmpint (first, ==, 6);
194   g_assert_cmpint (last,  ==, 6);
195   g_assert (hb_set_next_range (s, &first, &last));
196   g_assert_cmpint (first, ==, 10);
197   g_assert_cmpint (last,  ==, 15);
198   g_assert (hb_set_next_range (s, &first, &last));
199   g_assert_cmpint (first, ==, 20005);
200   g_assert_cmpint (last,  ==, 20005);
201   g_assert (!hb_set_next_range (s, &first, &last));
202   g_assert_cmpint (first, ==, HB_SET_VALUE_INVALID);
203   g_assert_cmpint (last,  ==, HB_SET_VALUE_INVALID);
204 
205   hb_set_destroy (s);
206 }
207 
208 static void
test_set_empty(void)209 test_set_empty (void)
210 {
211   hb_set_t *b = hb_set_get_empty ();
212 
213   g_assert (hb_set_get_empty ());
214   g_assert (hb_set_get_empty () == b);
215 
216   g_assert (!hb_set_allocation_successful (b));
217 
218   test_empty (b);
219 
220   hb_set_add (b, 13);
221 
222   test_empty (b);
223 
224   hb_set_invert (b);
225 
226   test_empty (b);
227 
228   g_assert (!hb_set_allocation_successful (b));
229 
230   hb_set_clear (b);
231 
232   test_empty (b);
233 
234   g_assert (!hb_set_allocation_successful (b));
235 
236   hb_set_destroy (b);
237 }
238 
239 int
main(int argc,char ** argv)240 main (int argc, char **argv)
241 {
242   hb_test_init (&argc, &argv);
243 
244   hb_test_add (test_set_basic);
245   hb_test_add (test_set_algebra);
246   hb_test_add (test_set_iter);
247   hb_test_add (test_set_empty);
248 
249   return hb_test_run();
250 }
251