mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-24 08:21:32 +01:00
Use hb_bit_vector_t and hb_min_max_t as Coverage sinks for the GDEF mark-set cache. Preserve the exact-cache span limit and fall back to the digest and table when allocation fails. Pack the repacker's Dijkstra visited flags into one bit per vertex. Fix singleton ranges and compute bit masks relative to the vector's minimum. Avoid overflow when sizing inclusive ranges, expose allocation failure, and add the Coverage sink methods needed by GDEF. Add regression tests for biased ranges, singleton and allocation boundaries, failing allocation, moves, and GDEF cache/table parity. Testing: full meson test suite (303 passed, 1 skipped); bitvector tests with AddressSanitizer and UndefinedBehaviorSanitizer; HB_TINY amalgamation syntax check. Assisted-by: OpenAI Codex
120 lines
4.5 KiB
C++
120 lines
4.5 KiB
C++
/*
|
|
* Copyright © 2026 Behdad Esfahbod
|
|
*
|
|
* 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.
|
|
*
|
|
* Author(s): Behdad Esfahbod
|
|
*/
|
|
|
|
#define hb_calloc bit_vector_test_calloc
|
|
|
|
#include "hb-bit-vector.hh"
|
|
|
|
static bool fail_allocation;
|
|
static size_t last_allocation;
|
|
|
|
extern "C" void *
|
|
bit_vector_test_calloc (size_t count, size_t size)
|
|
{
|
|
last_allocation = count * size;
|
|
return fail_allocation ? nullptr : calloc (count, size);
|
|
}
|
|
|
|
int
|
|
main ()
|
|
{
|
|
/* Check every bit offset, including ranges that wrap within a word
|
|
* or cross a word boundary relative to an unaligned minimum. */
|
|
for (unsigned bias = 0; bias < 64; bias++)
|
|
for (unsigned first = 0; first < 130; first++)
|
|
for (unsigned last = first; last < 130; last++)
|
|
{
|
|
hb_bit_vector_t<> bits (bias, bias + 129);
|
|
hb_always_assert (!bits.in_error ());
|
|
hb_always_assert (bits.add_range (bias + first, bias + last));
|
|
for (unsigned i = 0; i < 130; i++)
|
|
hb_always_assert (bits.has (bias + i) == (first <= i && i <= last));
|
|
bits.del_range (bias + first, bias + last);
|
|
for (unsigned i = 0; i < 130; i++)
|
|
hb_always_assert (!bits.has (bias + i));
|
|
bits.add_range (bias, bias + 129);
|
|
bits.del_range (bias + first, bias + last);
|
|
for (unsigned i = 0; i < 130; i++)
|
|
hb_always_assert (bits.has (bias + i) == (i < first || i > last));
|
|
}
|
|
|
|
for (unsigned value : {0u, 37u, UINT_MAX})
|
|
{
|
|
hb_bit_vector_t<> bits (value, value);
|
|
hb_always_assert (!bits.in_error ());
|
|
hb_always_assert (bits.count == 1 && last_allocation == 8);
|
|
hb_always_assert (!bits.has (value));
|
|
bits.add (value);
|
|
hb_always_assert (bits.has (value));
|
|
bits.del (value);
|
|
hb_always_assert (!bits.has (value));
|
|
}
|
|
|
|
for (unsigned size : {1u, 64u, 65u, 512u})
|
|
{
|
|
hb_bit_vector_t<> bits (37, 37 + size - 1);
|
|
hb_always_assert (last_allocation == (size + 63) / 64 * 8);
|
|
bits.add_range (37, 37 + size - 1);
|
|
hb_always_assert (!bits.has (36) && !bits.has (37 + size));
|
|
hb_always_assert (!bits.has (UINT_MAX));
|
|
bits.add (36);
|
|
bits.del (37 + size);
|
|
hb_always_assert (bits.has (37) && bits.has (37 + size - 1));
|
|
hb_bit_vector_t<> moved (std::move (bits));
|
|
hb_always_assert (bits.in_error () && moved.has (37));
|
|
bits = std::move (moved);
|
|
hb_always_assert (bits.has (37) && moved.in_error ());
|
|
}
|
|
|
|
/* Empty/reversed and unrepresentable inclusive ranges must not allocate. */
|
|
last_allocation = 0;
|
|
hb_bit_vector_t<> empty (1, 0);
|
|
hb_bit_vector_t<> overflow (0, UINT_MAX);
|
|
hb_always_assert (empty.in_error () && overflow.in_error ());
|
|
hb_always_assert (!last_allocation);
|
|
|
|
fail_allocation = true;
|
|
hb_bit_vector_t<> failed (37, 102);
|
|
hb_always_assert (failed.in_error () && !failed.count);
|
|
hb_always_assert (!failed.has (37));
|
|
failed.add (37);
|
|
hb_always_assert (!failed.has (37));
|
|
hb_always_assert (!failed.add_range (37, 102));
|
|
fail_allocation = false;
|
|
|
|
const unsigned values[] = {37, 63, 64, 65, 100};
|
|
auto array = hb_sorted_array (values);
|
|
hb_min_max_t extents;
|
|
hb_always_assert (extents.add_range (100, 37));
|
|
hb_always_assert (extents.get_min () == HB_CODEPOINT_INVALID);
|
|
hb_always_assert (extents.add_sorted_array (array));
|
|
hb_always_assert (extents.get_min () == 37 && extents.get_max () == 100);
|
|
hb_bit_vector_t<> bits (extents.get_min (), extents.get_max ());
|
|
hb_always_assert (bits.add_sorted_array (array));
|
|
for (unsigned i = 0; i < 110; i++)
|
|
hb_always_assert (bits.has (i) == (i == 37 || i == 63 || i == 64 || i == 65 || i == 100));
|
|
}
|