Behdad Esfahbod | 0d7af5f | 2019-02-14 19:37:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2019 Facebook, 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 | * Facebook Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
| 27 | #include "hb.hh" |
| 28 | #include "hb-algs.hh" |
Behdad Esfahbod | f3a8b7f | 2022-06-03 02:21:46 -0600 | [diff] [blame] | 29 | #include "hb-set.hh" |
Behdad Esfahbod | 0d7af5f | 2019-02-14 19:37:57 -0800 | [diff] [blame] | 30 | |
| 31 | |
Behdad Esfahbod | 12017db | 2019-04-24 09:24:38 -0400 | [diff] [blame] | 32 | static char * |
| 33 | test_func (int a, char **b) |
| 34 | { |
| 35 | return b ? b[a] : nullptr; |
| 36 | } |
| 37 | |
Behdad Esfahbod | 0268db1 | 2019-04-24 10:43:40 -0400 | [diff] [blame] | 38 | struct A |
| 39 | { |
| 40 | void a () {} |
| 41 | }; |
| 42 | |
Behdad Esfahbod | 0d7af5f | 2019-02-14 19:37:57 -0800 | [diff] [blame] | 43 | int |
| 44 | main (int argc, char **argv) |
| 45 | { |
| 46 | int i = 1; |
| 47 | auto p = hb_pair (1, i); |
| 48 | |
| 49 | p.second = 2; |
| 50 | assert (i == 2); |
| 51 | |
| 52 | const int c = 3; |
| 53 | auto pc = hb_pair (1, c); |
| 54 | assert (pc.second == 3); |
| 55 | |
Behdad Esfahbod | fa37358 | 2019-02-14 20:15:07 -0800 | [diff] [blame] | 56 | auto q = p; |
Behdad Esfahbod | 1558a43 | 2019-02-14 20:46:13 -0800 | [diff] [blame] | 57 | assert (&q != &p); |
Behdad Esfahbod | fa37358 | 2019-02-14 20:15:07 -0800 | [diff] [blame] | 58 | q.second = 4; |
| 59 | assert (i == 4); |
| 60 | |
Behdad Esfahbod | 12017db | 2019-04-24 09:24:38 -0400 | [diff] [blame] | 61 | hb_invoke (test_func, 0, nullptr); |
| 62 | |
Behdad Esfahbod | 0268db1 | 2019-04-24 10:43:40 -0400 | [diff] [blame] | 63 | A a; |
| 64 | hb_invoke (&A::a, a); |
| 65 | |
Behdad Esfahbod | 5266ca8 | 2019-05-15 17:59:00 -0700 | [diff] [blame] | 66 | assert (1 == hb_min (8, 1)); |
| 67 | assert (8 == hb_max (8, 1)); |
Behdad Esfahbod | 6fa1f38 | 2019-05-07 21:33:26 -0700 | [diff] [blame] | 68 | |
| 69 | int x = 1, y = 2; |
Behdad Esfahbod | 2c7093e | 2019-05-07 23:10:59 -0700 | [diff] [blame] | 70 | hb_min (x, 3); |
Behdad Esfahbod | 5266ca8 | 2019-05-15 17:59:00 -0700 | [diff] [blame] | 71 | hb_min (3, x); |
| 72 | hb_min (x, 4 + 3); |
Behdad Esfahbod | 6fa1f38 | 2019-05-07 21:33:26 -0700 | [diff] [blame] | 73 | int &z = hb_min (x, y); |
| 74 | z = 3; |
| 75 | assert (x == 3); |
| 76 | |
Behdad Esfahbod | 790315e | 2019-05-09 15:31:24 -0700 | [diff] [blame] | 77 | hb_pair_t<const int*, int> xp = hb_pair_t<int *, long> (nullptr, 0); |
| 78 | xp = hb_pair_t<int *, double> (nullptr, 1); |
Behdad Esfahbod | 243a5a6 | 2019-05-15 19:03:59 -0700 | [diff] [blame] | 79 | xp = hb_pair_t<const int*, int> (nullptr, 1); |
Behdad Esfahbod | 790315e | 2019-05-09 15:31:24 -0700 | [diff] [blame] | 80 | |
Behdad Esfahbod | 6f51e55 | 2019-05-15 21:41:12 -0700 | [diff] [blame] | 81 | assert (3 == hb_partial (hb_min, 3) (4)); |
| 82 | assert (3 == hb_partial<1> (hb_min, 4) (3)); |
Behdad Esfahbod | 16a3540 | 2019-05-15 20:48:20 -0700 | [diff] [blame] | 83 | |
Behdad Esfahbod | 6f51e55 | 2019-05-15 21:41:12 -0700 | [diff] [blame] | 84 | auto M0 = hb_partial<2> (hb_max, 0); |
Behdad Esfahbod | 16a3540 | 2019-05-15 20:48:20 -0700 | [diff] [blame] | 85 | assert (M0 (-2) == 0); |
| 86 | assert (M0 (+2) == 2); |
| 87 | |
Behdad Esfahbod | edc69ec | 2019-05-15 21:09:56 -0700 | [diff] [blame] | 88 | assert (hb_add (2) (5) == 7); |
| 89 | assert (hb_add (5) (2) == 7); |
| 90 | |
Behdad Esfahbod | 3bc86fb | 2019-08-30 16:39:52 -0500 | [diff] [blame] | 91 | x = 1; |
| 92 | assert (++hb_inc (x) == 3); |
| 93 | assert (x == 3); |
| 94 | |
Behdad Esfahbod | f3a8b7f | 2022-06-03 02:21:46 -0600 | [diff] [blame] | 95 | hb_set_t set1 {1}; |
| 96 | hb_set_t set2 {2}; |
| 97 | |
| 98 | assert (hb_hash (set1) != hb_hash (set2)); |
| 99 | assert (hb_hash (set1) == hb_hash (hb_set_t {1})); |
| 100 | assert (hb_hash (set1) != hb_hash (hb_set_t {})); |
| 101 | assert (hb_hash (set1) != hb_hash (hb_set_t {2})); |
| 102 | assert (hb_hash (set2) == hb_hash (hb_set_t {2})); |
| 103 | |
| 104 | /* hb_hash, unlike std::hash, dereferences pointers. */ |
| 105 | assert (hb_hash (set1) == hb_hash (&set1)); |
| 106 | assert (hb_hash (set1) == hb_hash (hb::shared_ptr<hb_set_t> {hb_set_reference (&set1)})); |
| 107 | assert (hb_hash (set1) == hb_hash (hb::unique_ptr<hb_set_t> {hb_set_reference (&set1)})); |
| 108 | |
Behdad Esfahbod | 0d7af5f | 2019-02-14 19:37:57 -0800 | [diff] [blame] | 109 | return 0; |
| 110 | } |