mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-28 00:59:16 +01:00
* [algs] Declare ARRAY_LENGTH also constexpr so can be used in static contexts * [algs] Remove more use of ARRAY_LENGTH_CONST And rename it to ARRAY_LENGTH_UNCHECKED so it will be more obvious this is the last resort. * [bit] Avoid use of ARRAY_LENGTH_UNCHECKED * [algs] Move ARRAY_LENGTH_UNCHECKED to its only caller * [algs] Return size_t from ARRAY_LENGTH * [algs] Remove last use of ARRAY_LENGTH_UNCHECKED * [algs] Use std::extent to get template parameter array length It makes more sense I think. * [algs] Make ARRAY_LENGTH usable through template reference parameters Evaluating ARRAY_LENGTH on a reference parameter is not a constant expression in C++11. Return std::integral_constant from a function object so callers can extract the length from the return type without evaluating the array expression. Ordinary calls still convert to size_t. Use this form for all three Arabic fallback ligature table dimensions, replacing std::extent. Document both forms and add compile-time checks for ordinary calls, string literals, and nested template array arguments. Validated with GCC and Clang C++11 syntax checks, a full Meson build, and meson test -C build (296 passed, 1 skipped). Assisted-by: OpenAI Codex --------- Co-authored-by: Ebrahim <ebrahim@gnu.org>
237 lines
7.6 KiB
C++
237 lines
7.6 KiB
C++
/*
|
|
* 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;
|
|
};
|
|
|
|
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 (decltype (ARRAY_LENGTH (array))::value == 3, "");
|
|
static_assert (decltype (ARRAY_LENGTH (array[0]))::value == 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);
|
|
|
|
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)}));
|
|
|
|
test_qsort_adversary ();
|
|
|
|
return 0;
|
|
}
|