blob: 450a7c439b7700611518ee7f30602417dc6151f9 [file] [log] [blame]
Behdad Esfahbod0d7af5f2019-02-14 19:37:57 -08001/*
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 Esfahbodf3a8b7f2022-06-03 02:21:46 -060029#include "hb-set.hh"
Behdad Esfahbod0d7af5f2019-02-14 19:37:57 -080030
31
Behdad Esfahbod12017db2019-04-24 09:24:38 -040032static char *
33test_func (int a, char **b)
34{
35 return b ? b[a] : nullptr;
36}
37
Behdad Esfahbod0268db12019-04-24 10:43:40 -040038struct A
39{
40 void a () {}
41};
42
Behdad Esfahbod0d7af5f2019-02-14 19:37:57 -080043int
44main (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 Esfahbodfa373582019-02-14 20:15:07 -080056 auto q = p;
Behdad Esfahbod1558a432019-02-14 20:46:13 -080057 assert (&q != &p);
Behdad Esfahbodfa373582019-02-14 20:15:07 -080058 q.second = 4;
59 assert (i == 4);
60
Behdad Esfahbod12017db2019-04-24 09:24:38 -040061 hb_invoke (test_func, 0, nullptr);
62
Behdad Esfahbod0268db12019-04-24 10:43:40 -040063 A a;
64 hb_invoke (&A::a, a);
65
Behdad Esfahbod5266ca82019-05-15 17:59:00 -070066 assert (1 == hb_min (8, 1));
67 assert (8 == hb_max (8, 1));
Behdad Esfahbod6fa1f382019-05-07 21:33:26 -070068
69 int x = 1, y = 2;
Behdad Esfahbod2c7093e2019-05-07 23:10:59 -070070 hb_min (x, 3);
Behdad Esfahbod5266ca82019-05-15 17:59:00 -070071 hb_min (3, x);
72 hb_min (x, 4 + 3);
Behdad Esfahbod6fa1f382019-05-07 21:33:26 -070073 int &z = hb_min (x, y);
74 z = 3;
75 assert (x == 3);
76
Behdad Esfahbod790315e2019-05-09 15:31:24 -070077 hb_pair_t<const int*, int> xp = hb_pair_t<int *, long> (nullptr, 0);
78 xp = hb_pair_t<int *, double> (nullptr, 1);
Behdad Esfahbod243a5a62019-05-15 19:03:59 -070079 xp = hb_pair_t<const int*, int> (nullptr, 1);
Behdad Esfahbod790315e2019-05-09 15:31:24 -070080
Behdad Esfahbod6f51e552019-05-15 21:41:12 -070081 assert (3 == hb_partial (hb_min, 3) (4));
82 assert (3 == hb_partial<1> (hb_min, 4) (3));
Behdad Esfahbod16a35402019-05-15 20:48:20 -070083
Behdad Esfahbod6f51e552019-05-15 21:41:12 -070084 auto M0 = hb_partial<2> (hb_max, 0);
Behdad Esfahbod16a35402019-05-15 20:48:20 -070085 assert (M0 (-2) == 0);
86 assert (M0 (+2) == 2);
87
Behdad Esfahbodedc69ec2019-05-15 21:09:56 -070088 assert (hb_add (2) (5) == 7);
89 assert (hb_add (5) (2) == 7);
90
Behdad Esfahbod3bc86fb2019-08-30 16:39:52 -050091 x = 1;
92 assert (++hb_inc (x) == 3);
93 assert (x == 3);
94
Behdad Esfahbodf3a8b7f2022-06-03 02:21:46 -060095 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 Esfahbod0d7af5f2019-02-14 19:37:57 -0800109 return 0;
110}