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 33 test_empty (hb_set_t *s) 34 { 35 hb_codepoint_t next = (hb_codepoint_t) -1; 36 g_assert_cmpint (hb_set_get_population (s), ==, 0); 37 g_assert_cmpint (hb_set_get_min (s), ==, (hb_codepoint_t) -1); 38 g_assert_cmpint (hb_set_get_max (s), ==, (hb_codepoint_t) -1); 39 g_assert (!hb_set_has (s, 13)); 40 g_assert (!hb_set_next (s, &next)); 41 g_assert_cmpint (next, ==, (hb_codepoint_t) -1); 42 } 43 44 static void 45 test_not_empty (hb_set_t *s) 46 { 47 hb_codepoint_t next = (hb_codepoint_t) -1; 48 g_assert_cmpint (hb_set_get_population (s), !=, 0); 49 g_assert_cmpint (hb_set_get_min (s), !=, (hb_codepoint_t) -1); 50 g_assert_cmpint (hb_set_get_max (s), !=, (hb_codepoint_t) -1); 51 g_assert (hb_set_next (s, &next)); 52 g_assert_cmpint (next, !=, (hb_codepoint_t) -1); 53 } 54 55 static void 56 test_set_basic (void) 57 { 58 hb_set_t *s = hb_set_create (); 59 60 test_empty (s); 61 hb_set_add (s, 13); 62 test_not_empty (s); 63 64 hb_set_clear (s); 65 test_empty (s); 66 67 hb_set_add_range (s, 10, 29); 68 test_not_empty (s); 69 g_assert (hb_set_has (s, 13)); 70 g_assert_cmpint (hb_set_get_population (s), ==, 20); 71 g_assert_cmpint (hb_set_get_min (s), ==, 10); 72 g_assert_cmpint (hb_set_get_max (s), ==, 29); 73 74 hb_set_invert (s); 75 test_not_empty (s); 76 g_assert (!hb_set_has (s, 13)); 77 g_assert_cmpint (hb_set_get_min (s), ==, 0); 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_population (s), ==, 20); 83 g_assert_cmpint (hb_set_get_min (s), ==, 10); 84 g_assert_cmpint (hb_set_get_max (s), ==, 29); 85 86 hb_set_del_range (s, 10, 18); 87 test_not_empty (s); 88 g_assert (!hb_set_has (s, 13)); 89 } 90 91 static void 92 test_set_algebra (void) 93 { 94 hb_set_t *s = hb_set_create (); 95 hb_set_t *o = hb_set_create (); 96 97 hb_set_add (o, 13); 98 hb_set_add (o, 19); 99 100 test_empty (s); 101 g_assert (!hb_set_is_equal (s, o)); 102 hb_set_set (s, o); 103 g_assert (hb_set_is_equal (s, o)); 104 test_not_empty (s); 105 g_assert_cmpint (hb_set_get_population (s), ==, 2); 106 107 hb_set_clear (s); 108 test_empty (s); 109 hb_set_add (s, 10); 110 g_assert_cmpint (hb_set_get_population (s), ==, 1); 111 hb_set_union (s, o); 112 g_assert_cmpint (hb_set_get_population (s), ==, 3); 113 g_assert (hb_set_has (s, 10)); 114 g_assert (hb_set_has (s, 13)); 115 116 hb_set_clear (s); 117 test_empty (s); 118 hb_set_add_range (s, 10, 17); 119 g_assert (!hb_set_is_equal (s, o)); 120 hb_set_intersect (s, o); 121 g_assert (!hb_set_is_equal (s, o)); 122 test_not_empty (s); 123 g_assert_cmpint (hb_set_get_population (s), ==, 1); 124 g_assert (!hb_set_has (s, 10)); 125 g_assert (hb_set_has (s, 13)); 126 127 hb_set_clear (s); 128 test_empty (s); 129 hb_set_add_range (s, 10, 17); 130 g_assert (!hb_set_is_equal (s, o)); 131 hb_set_subtract (s, o); 132 g_assert (!hb_set_is_equal (s, o)); 133 test_not_empty (s); 134 g_assert_cmpint (hb_set_get_population (s), ==, 7); 135 g_assert (hb_set_has (s, 12)); 136 g_assert (!hb_set_has (s, 13)); 137 g_assert (!hb_set_has (s, 19)); 138 139 hb_set_clear (s); 140 test_empty (s); 141 hb_set_add_range (s, 10, 17); 142 g_assert (!hb_set_is_equal (s, o)); 143 hb_set_symmetric_difference (s, o); 144 g_assert (!hb_set_is_equal (s, o)); 145 test_not_empty (s); 146 g_assert_cmpint (hb_set_get_population (s), ==, 8); 147 g_assert (hb_set_has (s, 12)); 148 g_assert (!hb_set_has (s, 13)); 149 g_assert (hb_set_has (s, 19)); 150 } 151 152 static void 153 test_set_iter (void) 154 { 155 hb_codepoint_t next, first, last; 156 hb_set_t *s = hb_set_create (); 157 158 hb_set_add (s, 13); 159 hb_set_add_range (s, 6, 6); 160 hb_set_add_range (s, 10, 15); 161 hb_set_add (s, 20005); 162 163 test_not_empty (s); 164 165 next = (hb_codepoint_t) -1; 166 g_assert (hb_set_next (s, &next)); 167 g_assert_cmpint (next, ==, 6); 168 g_assert (hb_set_next (s, &next)); 169 g_assert_cmpint (next, ==, 10); 170 g_assert (hb_set_next (s, &next)); 171 g_assert (hb_set_next (s, &next)); 172 g_assert (hb_set_next (s, &next)); 173 g_assert_cmpint (next, ==, 13); 174 g_assert (hb_set_next (s, &next)); 175 g_assert (hb_set_next (s, &next)); 176 g_assert_cmpint (next, ==, 15); 177 g_assert (hb_set_next (s, &next)); 178 g_assert_cmpint (next, ==, 20005); 179 g_assert (!hb_set_next (s, &next)); 180 g_assert_cmpint (next, ==, 20005); 181 182 first = last = (hb_codepoint_t) -1; 183 g_assert (hb_set_next_range (s, &first, &last)); 184 g_assert_cmpint (first, ==, 6); 185 g_assert_cmpint (last, ==, 6); 186 g_assert (hb_set_next_range (s, &first, &last)); 187 g_assert_cmpint (first, ==, 10); 188 g_assert_cmpint (last, ==, 15); 189 g_assert (hb_set_next_range (s, &first, &last)); 190 g_assert_cmpint (first, ==, 20005); 191 g_assert_cmpint (last, ==, 20005); 192 g_assert (!hb_set_next_range (s, &first, &last)); 193 g_assert_cmpint (first, ==, 20005); 194 g_assert_cmpint (last, ==, 20005); 195 } 196 197 static void 198 test_set_empty (void) 199 { 200 hb_set_t *b = hb_set_get_empty (); 201 202 g_assert (hb_set_get_empty ()); 203 g_assert (hb_set_get_empty () == b); 204 205 g_assert (!hb_set_allocation_successful (b)); 206 207 test_empty (b); 208 209 hb_set_add (b, 13); 210 211 test_empty (b); 212 213 hb_set_invert (b); 214 215 test_empty (b); 216 217 g_assert (!hb_set_allocation_successful (b)); 218 219 hb_set_clear (b); 220 221 test_empty (b); 222 223 g_assert (!hb_set_allocation_successful (b)); 224 } 225 226 int 227 main (int argc, char **argv) 228 { 229 hb_test_init (&argc, &argv); 230 231 hb_test_add (test_set_basic); 232 hb_test_add (test_set_algebra); 233 hb_test_add (test_set_iter); 234 hb_test_add (test_set_empty); 235 236 return hb_test_run(); 237 } 238