| /* | |
| * Copyright © 2019 Facebook, Inc. | |
| * | |
| * This is part of HarfBuzz, a text shaping library. | |
| * | |
| * Permission is hereby granted, without written agreement and without | |
| * license or royalty fees, to use, copy, modify, and distribute this | |
| * software and its documentation for any purpose, provided that the | |
| * above copyright notice and the following two paragraphs appear in | |
| * all copies of this software. | |
| * | |
| * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR | |
| * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES | |
| * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN | |
| * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH | |
| * DAMAGE. | |
| * | |
| * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, | |
| * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS | |
| * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO | |
| * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
| * | |
| * Facebook Author(s): Behdad Esfahbod | |
| */ | |
| #include "hb.hh" | |
| #include "hb-algs.hh" | |
| #include "hb-set.hh" | |
| static char * | |
| test_func (int a, char **b) | |
| { | |
| return b ? b[a] : nullptr; | |
| } | |
| struct A | |
| { | |
| void a () {} | |
| }; | |
| /* McIlroy's "killer adversary" for quicksort: hands out keys lazily, | |
| * always giving the sort's current pivot candidate the smallest key | |
| * left, so every partition splits off almost nothing. Drives any | |
| * deterministic quicksort without a depth limit to O(n^2). */ | |
| struct qsort_adversary_t | |
| { | |
| qsort_adversary_t (unsigned n) : n (n) | |
| { | |
| keys.resize (n); | |
| for (unsigned i = 0; i < n; i++) | |
| keys[i] = n; /* Unassigned; compares greater than any key. */ | |
| } | |
| int operator () (const unsigned &a, const unsigned &b) | |
| { | |
| if (keys[a] == n && keys[b] == n) | |
| keys[a == candidate ? a : b] = next++; | |
| if (keys[a] == n) candidate = a; | |
| else if (keys[b] == n) candidate = b; | |
| return (int) keys[a] - (int) keys[b]; | |
| } | |
| unsigned n; | |
| hb_vector_t<unsigned> keys; | |
| unsigned candidate = 0; | |
| unsigned next = 0; | |
| }; | |
| /* Hashes a key the way multi-field keys are: fields gathered into a local | |
| * buffer holding objects of another type than the words fasthash reads. */ | |
| static HB_NEVER_INLINE uint32_t | |
| hash_fields (uint32_t a, uint32_t b, uint32_t c) | |
| { | |
| const uint32_t fields[] = {a, b, c, a, b, c}; | |
| return fasthash32 (fields, sizeof (fields), 0xf437ffe6); | |
| } | |
| static void | |
| test_qsort_adversary () | |
| { | |
| const unsigned n = 1u << 15; | |
| hb_vector_t<unsigned> items; | |
| items.resize (n); | |
| for (unsigned i = 0; i < n; i++) | |
| items[i] = i; | |
| /* First run: let the adversary pick the worst order for our sort. */ | |
| qsort_adversary_t adversary (n); | |
| hb_qsort_inline (items.arrayZ, items.length, | |
| [&] (const unsigned &a, const unsigned &b) { return adversary (a, b); }); | |
| for (unsigned i = 0; i < n; i++) | |
| if (adversary.keys[i] == n) | |
| adversary.keys[i] = adversary.next++; | |
| /* Second run: the same input, now with the keys the adversary chose. */ | |
| for (unsigned i = 0; i < n; i++) | |
| items[i] = i; | |
| size_t comparisons = 0; | |
| hb_qsort_inline (items.arrayZ, items.length, | |
| [&] (const unsigned &a, const unsigned &b) | |
| { | |
| comparisons++; | |
| return (int) adversary.keys[a] - (int) adversary.keys[b]; | |
| }); | |
| for (unsigned i = 1; i < n; i++) | |
| hb_always_assert (adversary.keys[items[i - 1]] < adversary.keys[items[i]]); | |
| /* Introsort: stays within a small multiple of n log n; a plain | |
| * quicksort would take about n^2 / 4 comparisons here. */ | |
| hb_always_assert (comparisons < 16 * (size_t) n * hb_bit_storage (n)); | |
| } | |
| template <typename T> | |
| static void | |
| test_array_length (const T &array) | |
| { | |
| static_assert (ARRAY_LENGTH (array) == 3, ""); | |
| static_assert (ARRAY_LENGTH (array[0]) == 2, ""); | |
| } | |
| int | |
| main (int argc, char **argv) | |
| { | |
| int array[3][2] = {}; | |
| static_assert (ARRAY_LENGTH (array) == 3, ""); | |
| static_assert (ARRAY_LENGTH ("abc") == 4, ""); | |
| test_array_length (array); | |
| int index = 0; | |
| static_assert (ARRAY_LENGTH (array[index++]) == 2, ""); | |
| hb_always_assert (index == 0); | |
| hb_always_assert (roundf (0.5f) == 1.f); | |
| hb_always_assert (roundf (-0.5f) == 0.f); | |
| hb_always_assert (roundf (1.5f) == 2.f); | |
| hb_always_assert (roundf (-1.5f) == -1.f); | |
| hb_always_assert (roundf (nextafterf (0.5f, 0.f)) == 0.f); | |
| hb_always_assert (roundf (nextafterf (-0.5f, -1.f)) == -1.f); | |
| hb_always_assert (roundf (nextafterf (1.5f, 1.f)) == 1.f); | |
| hb_always_assert (roundf (nextafterf (-1.5f, -2.f)) == -2.f); | |
| hb_always_assert (roundf (15383929.f) == 15383929.f); | |
| hb_always_assert (roundf (-15383929.f) == -15383929.f); | |
| hb_always_assert (roundf (8388609.f) == 8388609.f); | |
| hb_always_assert (roundf (-8388609.f) == -8388609.f); | |
| int i = 1; | |
| auto p = hb_pair (1, i); | |
| p.second = 2; | |
| hb_always_assert (i == 2); | |
| const int c = 3; | |
| auto pc = hb_pair (1, c); | |
| hb_always_assert (pc.second == 3); | |
| auto q = p; | |
| hb_always_assert (&q != &p); | |
| q.second = 4; | |
| hb_always_assert (i == 4); | |
| hb_invoke (test_func, 0, nullptr); | |
| A a; | |
| hb_invoke (&A::a, a); | |
| hb_always_assert (1 == hb_min (8, 1)); | |
| hb_always_assert (8 == hb_max (8, 1)); | |
| int x = 1, y = 2; | |
| hb_min (x, 3); | |
| hb_min (3, x); | |
| hb_min (x, 4 + 3); | |
| int &z = hb_min (x, y); | |
| z = 3; | |
| hb_always_assert (x == 3); | |
| hb_pair_t<const int*, int> xp = hb_pair_t<int *, long> (nullptr, 0); | |
| xp = hb_pair_t<int *, double> (nullptr, 1); | |
| xp = hb_pair_t<const int*, int> (nullptr, 1); | |
| hb_always_assert (3 == hb_partial (hb_min, 3) (4)); | |
| hb_always_assert (3 == hb_partial<1> (hb_min, 4) (3)); | |
| auto M0 = hb_partial<2> (hb_max, 0); | |
| hb_always_assert (M0 (-2) == 0); | |
| hb_always_assert (M0 (+2) == 2); | |
| hb_always_assert (hb_add (2) (5) == 7); | |
| hb_always_assert (hb_add (5) (2) == 7); | |
| hb_always_assert (hb_saturate_add (1, 2) == 3); | |
| hb_always_assert (hb_saturate_add (INT_MAX, 1) == INT_MAX); | |
| hb_always_assert (hb_saturate_add (INT_MIN, -1) == INT_MIN); | |
| hb_always_assert (hb_saturate_add (INT_MAX, INT_MIN) == -1); | |
| hb_always_assert (hb_saturate_sub (3, 2) == 1); | |
| hb_always_assert (hb_saturate_sub (INT_MAX, -1) == INT_MAX); | |
| hb_always_assert (hb_saturate_sub (INT_MIN, 1) == INT_MIN); | |
| hb_always_assert (hb_saturate_sub (INT_MIN, INT_MIN) == 0); | |
| hb_always_assert (hb_saturate_neg (2) == -2); | |
| hb_always_assert (hb_saturate_neg (-2) == 2); | |
| hb_always_assert (hb_saturate_neg (INT_MIN) == INT_MAX); | |
| hb_always_assert (hb_clamp_to<int> (1.) == 1); | |
| hb_always_assert (hb_clamp_to<int> ((double) INT_MAX + 1.) == INT_MAX); | |
| hb_always_assert (hb_clamp_to<int> ((double) INT_MIN - 1.) == INT_MIN); | |
| hb_always_assert (hb_clamp_to<int> ((int64_t) 1) == 1); | |
| hb_always_assert (hb_clamp_to<int> ((int64_t) INT_MAX + 1) == INT_MAX); | |
| hb_always_assert (hb_clamp_to<int> ((int64_t) INT_MIN - 1) == INT_MIN); | |
| int64_t budget = 10; | |
| hb_always_assert (hb_budget_spend (budget, 4, 2)); | |
| hb_always_assert (budget == 2); | |
| hb_always_assert (!hb_budget_spend (budget, 3)); | |
| hb_always_assert (budget == -1); | |
| hb_always_assert (!hb_budget_spend (budget, 1)); | |
| hb_always_assert (budget == -2); | |
| budget = 0; | |
| hb_always_assert (hb_budget_spend (budget, 0)); | |
| hb_always_assert (!hb_budget_spend (budget, 1)); | |
| hb_always_assert (budget == -1); | |
| budget = INT64_MAX; | |
| hb_always_assert (hb_budget_spend (budget, 1024)); | |
| hb_always_assert (budget == INT64_MAX - 1024); | |
| x = 1; | |
| hb_always_assert (++hb_inc (x) == 3); | |
| hb_always_assert (x == 3); | |
| hb_set_t set1 {1}; | |
| hb_set_t set2 {2}; | |
| hb_always_assert (hb_hash (set1) != hb_hash (set2)); | |
| hb_always_assert (hb_hash (set1) == hb_hash (hb_set_t {1})); | |
| hb_always_assert (hb_hash (set1) != hb_hash (hb_set_t {})); | |
| hb_always_assert (hb_hash (set1) != hb_hash (hb_set_t {2})); | |
| hb_always_assert (hb_hash (set2) == hb_hash (hb_set_t {2})); | |
| /* hb_hash, unlike std::hash, dereferences pointers. */ | |
| hb_always_assert (hb_hash (set1) == hb_hash (&set1)); | |
| hb_always_assert (hb_hash (set1) == hb_hash (hb::shared_ptr<hb_set_t> {hb_set_reference (&set1)})); | |
| hb_always_assert (hb_hash (set1) == hb_hash (hb::unique_ptr<hb_set_t> {hb_set_reference (&set1)})); | |
| /* The hash is of what was just stored in the buffer, even though | |
| * fasthash reads it as words rather than as the objects it holds. Each | |
| * field repeats one byte, so that the same buffer can be spelled out | |
| * in bytes whatever the byte order. */ | |
| for (unsigned i = 1; i < 16; i++) | |
| { | |
| uint8_t x = i, y = i + 16, z = i + 32; | |
| const uint8_t bytes[] = {x, x, x, x, y, y, y, y, z, z, z, z, | |
| x, x, x, x, y, y, y, y, z, z, z, z}; | |
| hb_always_assert (hash_fields (x * 0x01010101u, y * 0x01010101u, z * 0x01010101u) == | |
| fasthash32 (bytes, sizeof (bytes), 0xf437ffe6)); | |
| } | |
| test_qsort_adversary (); | |
| return 0; | |
| } |