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