test-map.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright © 2018 Ebrahim Byagowi
  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. #include "hb-test.h"
  25. /* Unit tests for hb-map.h */
  26. static void
  27. test_map_basic (void)
  28. {
  29. hb_map_t *empty = hb_map_get_empty ();
  30. hb_map_t *m;
  31. g_assert (hb_map_is_empty (empty));
  32. g_assert (!hb_map_allocation_successful (empty));
  33. hb_map_destroy (empty);
  34. m = hb_map_create ();
  35. g_assert (hb_map_allocation_successful (m));
  36. g_assert (hb_map_is_empty (m));
  37. hb_map_set (m, 213, 223);
  38. hb_map_set (m, 643, 675);
  39. g_assert_cmpint (hb_map_get_population (m), ==, 2);
  40. g_assert_cmpint (hb_map_get (m, 213), ==, 223);
  41. g_assert (!hb_map_has (m, 123));
  42. g_assert (hb_map_has (m, 213));
  43. hb_map_del (m, 213);
  44. g_assert (!hb_map_has (m, 213));
  45. g_assert_cmpint (hb_map_get (m, 643), ==, 675);
  46. hb_map_set (m, 237, 673);
  47. g_assert (hb_map_has (m, 237));
  48. hb_map_clear (m);
  49. g_assert (!hb_map_has (m, 237));
  50. g_assert (!hb_map_has (m, 643));
  51. g_assert_cmpint (hb_map_get_population (m), ==, 0);
  52. hb_map_destroy (m);
  53. }
  54. static void
  55. test_map_userdata (void)
  56. {
  57. hb_map_t *m = hb_map_create ();
  58. hb_user_data_key_t key[2];
  59. int *data = (int *) malloc (sizeof (int));
  60. int *data2;
  61. *data = 3123;
  62. hb_map_set_user_data (m, &key[0], data, free, TRUE);
  63. g_assert_cmpint (*((int *) hb_map_get_user_data (m, &key[0])), ==, 3123);
  64. data2 = (int *) malloc (sizeof (int));
  65. *data2 = 6343;
  66. hb_map_set_user_data (m, &key[0], data2, free, FALSE);
  67. g_assert_cmpint (*((int *) hb_map_get_user_data (m, &key[0])), ==, 3123);
  68. hb_map_set_user_data (m, &key[0], data2, free, TRUE);
  69. g_assert_cmpint (*((int *) hb_map_get_user_data (m, &key[0])), ==, 6343);
  70. hb_map_destroy (m);
  71. }
  72. static void
  73. test_map_refcount (void)
  74. {
  75. hb_map_t *m = hb_map_create ();
  76. hb_map_t *m2;
  77. hb_map_set (m, 213, 223);
  78. g_assert_cmpint (hb_map_get (m, 213), ==, 223);
  79. m2 = hb_map_reference (m);
  80. hb_map_destroy (m);
  81. /* We copied its reference so it is still usable after one destroy */
  82. g_assert (hb_map_has (m, 213));
  83. g_assert (hb_map_has (m2, 213));
  84. hb_map_destroy (m2);
  85. /* Now you can't access them anymore */
  86. }
  87. static void
  88. test_map_get_population (void)
  89. {
  90. hb_map_t *m = hb_map_create ();
  91. hb_map_set (m, 12, 21);
  92. g_assert_cmpint (hb_map_get_population (m), ==, 1);
  93. hb_map_set (m, 78, 87);
  94. g_assert_cmpint (hb_map_get_population (m), ==, 2);
  95. hb_map_set (m, 78, 87);
  96. g_assert_cmpint (hb_map_get_population (m), ==, 2);
  97. hb_map_set (m, 78, 13);
  98. g_assert_cmpint (hb_map_get_population (m), ==, 2);
  99. hb_map_set (m, 95, 56);
  100. g_assert_cmpint (hb_map_get_population (m), ==, 3);
  101. hb_map_del (m, 78);
  102. g_assert_cmpint (hb_map_get_population (m), ==, 2);
  103. hb_map_del (m, 103);
  104. g_assert_cmpint (hb_map_get_population (m), ==, 2);
  105. hb_map_destroy (m);
  106. }
  107. int
  108. main (int argc, char **argv)
  109. {
  110. hb_test_init (&argc, &argv);
  111. hb_test_add (test_map_basic);
  112. hb_test_add (test_map_userdata);
  113. hb_test_add (test_map_refcount);
  114. hb_test_add (test_map_get_population);
  115. return hb_test_run();
  116. }