2020-10-29 01:49:09 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2020 Google, 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.
|
|
|
|
*
|
|
|
|
* Google Author(s): Garret Rieger
|
|
|
|
*/
|
|
|
|
|
2020-11-02 23:51:39 +01:00
|
|
|
#include <string>
|
|
|
|
|
2020-10-29 01:49:09 +01:00
|
|
|
#include "hb-repacker.hh"
|
|
|
|
#include "hb-open-type.hh"
|
|
|
|
|
2020-10-29 22:58:34 +01:00
|
|
|
static void start_object(const char* tag,
|
|
|
|
unsigned len,
|
|
|
|
hb_serialize_context_t* c)
|
2020-10-29 01:49:09 +01:00
|
|
|
{
|
|
|
|
c->push ();
|
2020-10-29 22:58:34 +01:00
|
|
|
char* obj = c->allocate_size<char> (len);
|
|
|
|
strncpy (obj, tag, len);
|
|
|
|
}
|
2020-10-29 01:49:09 +01:00
|
|
|
|
|
|
|
|
2020-10-29 22:58:34 +01:00
|
|
|
static unsigned add_object(const char* tag,
|
|
|
|
unsigned len,
|
|
|
|
hb_serialize_context_t* c)
|
|
|
|
{
|
|
|
|
start_object (tag, len, c);
|
|
|
|
return c->pop_pack (false);
|
|
|
|
}
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-10-29 22:58:34 +01:00
|
|
|
|
|
|
|
static void add_offset (unsigned id,
|
|
|
|
hb_serialize_context_t* c)
|
|
|
|
{
|
2020-10-29 01:49:09 +01:00
|
|
|
OT::Offset16* offset = c->start_embed<OT::Offset16> ();
|
|
|
|
c->extend_min (offset);
|
2020-10-29 22:58:34 +01:00
|
|
|
c->add_link (*offset, id);
|
|
|
|
}
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2021-09-09 00:07:02 +02:00
|
|
|
static void add_wide_offset (unsigned id,
|
|
|
|
hb_serialize_context_t* c)
|
|
|
|
{
|
|
|
|
OT::Offset32* offset = c->start_embed<OT::Offset32> ();
|
|
|
|
c->extend_min (offset);
|
|
|
|
c->add_link (*offset, id);
|
|
|
|
}
|
|
|
|
|
2020-10-29 22:58:34 +01:00
|
|
|
static void
|
|
|
|
populate_serializer_simple (hb_serialize_context_t* c)
|
|
|
|
{
|
|
|
|
c->start_serialize<char> ();
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-10-29 22:58:34 +01:00
|
|
|
unsigned obj_1 = add_object ("ghi", 3, c);
|
|
|
|
unsigned obj_2 = add_object ("def", 3, c);
|
|
|
|
|
|
|
|
start_object ("abc", 3, c);
|
|
|
|
add_offset (obj_2, c);
|
|
|
|
add_offset (obj_1, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
c->pop_pack (false);
|
2020-10-29 01:49:09 +01:00
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
|
|
|
|
2020-11-02 23:51:39 +01:00
|
|
|
static void
|
|
|
|
populate_serializer_with_overflow (hb_serialize_context_t* c)
|
|
|
|
{
|
2020-11-06 01:39:23 +01:00
|
|
|
std::string large_string(50000, 'a');
|
2020-11-02 23:51:39 +01:00
|
|
|
c->start_serialize<char> ();
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
unsigned obj_1 = add_object (large_string.c_str(), 10000, c);
|
|
|
|
unsigned obj_2 = add_object (large_string.c_str(), 20000, c);
|
|
|
|
unsigned obj_3 = add_object (large_string.c_str(), 50000, c);
|
2020-11-02 23:51:39 +01:00
|
|
|
|
|
|
|
start_object ("abc", 3, c);
|
|
|
|
add_offset (obj_3, c);
|
|
|
|
add_offset (obj_2, c);
|
|
|
|
add_offset (obj_1, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
c->pop_pack (false);
|
2020-11-02 23:51:39 +01:00
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
static void
|
|
|
|
populate_serializer_with_dedup_overflow (hb_serialize_context_t* c)
|
|
|
|
{
|
|
|
|
std::string large_string(70000, 'a');
|
|
|
|
c->start_serialize<char> ();
|
|
|
|
|
|
|
|
unsigned obj_1 = add_object ("def", 3, c);
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 60000, c);
|
|
|
|
add_offset (obj_1, c);
|
|
|
|
unsigned obj_2 = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 10000, c);
|
|
|
|
add_offset (obj_2, c);
|
|
|
|
add_offset (obj_1, c);
|
|
|
|
c->pop_pack (false);
|
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
|
|
|
|
2021-09-09 00:07:02 +02:00
|
|
|
static void
|
|
|
|
populate_serializer_with_isolation_overflow (hb_serialize_context_t* c)
|
|
|
|
{
|
|
|
|
std::string large_string(70000, 'a');
|
|
|
|
c->start_serialize<char> ();
|
|
|
|
|
|
|
|
unsigned obj_4 = add_object ("4", 1, c);
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 60000, c);
|
|
|
|
add_offset (obj_4, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
unsigned obj_3 = c->pop_pack (false);
|
2021-09-09 00:07:02 +02:00
|
|
|
|
|
|
|
start_object (large_string.c_str(), 10000, c);
|
|
|
|
add_offset (obj_4, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
unsigned obj_2 = c->pop_pack (false);
|
2021-09-09 00:07:02 +02:00
|
|
|
|
|
|
|
start_object ("1", 1, c);
|
|
|
|
add_wide_offset (obj_3, c);
|
|
|
|
add_offset (obj_2, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
c->pop_pack (false);
|
2021-09-09 00:07:02 +02:00
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
|
|
|
|
2021-09-09 19:25:43 +02:00
|
|
|
static void
|
|
|
|
populate_serializer_with_isolation_overflow_complex (hb_serialize_context_t* c)
|
|
|
|
{
|
|
|
|
std::string large_string(70000, 'a');
|
|
|
|
c->start_serialize<char> ();
|
|
|
|
|
|
|
|
unsigned obj_f = add_object ("f", 1, c);
|
|
|
|
|
|
|
|
start_object ("e", 1, c);
|
|
|
|
add_offset (obj_f, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
unsigned obj_e = c->pop_pack (false);
|
2021-09-09 19:25:43 +02:00
|
|
|
|
2021-09-25 01:28:34 +02:00
|
|
|
start_object ("cc", 2, c);
|
2021-09-09 19:25:43 +02:00
|
|
|
add_offset (obj_e, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
unsigned obj_c = c->pop_pack (false);
|
2021-09-09 19:25:43 +02:00
|
|
|
|
|
|
|
start_object ("d", 1, c);
|
|
|
|
add_offset (obj_e, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
unsigned obj_d = c->pop_pack (false);
|
2021-09-09 19:25:43 +02:00
|
|
|
|
|
|
|
start_object (large_string.c_str(), 60000, c);
|
|
|
|
add_offset (obj_d, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
unsigned obj_h = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 60000, c);
|
|
|
|
add_offset (obj_c, c);
|
|
|
|
add_offset (obj_h, c);
|
|
|
|
unsigned obj_b = c->pop_pack (false);
|
2021-09-09 19:25:43 +02:00
|
|
|
|
|
|
|
start_object (large_string.c_str(), 10000, c);
|
|
|
|
add_offset (obj_d, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
unsigned obj_g = c->pop_pack (false);
|
2021-09-09 19:25:43 +02:00
|
|
|
|
|
|
|
start_object ("a", 1, c);
|
|
|
|
add_wide_offset (obj_b, c);
|
|
|
|
add_offset (obj_g, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
c->pop_pack (false);
|
2021-09-09 19:25:43 +02:00
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
|
|
|
|
2021-09-25 01:28:34 +02:00
|
|
|
static void
|
|
|
|
populate_serializer_with_isolation_overflow_complex_expected (hb_serialize_context_t* c)
|
|
|
|
{
|
|
|
|
std::string large_string(70000, 'a');
|
|
|
|
c->start_serialize<char> ();
|
|
|
|
|
|
|
|
// 32 bit subgraph
|
|
|
|
unsigned obj_f_prime = add_object ("f", 1, c);
|
|
|
|
|
|
|
|
start_object ("e", 1, c);
|
|
|
|
add_offset (obj_f_prime, c);
|
|
|
|
unsigned obj_e_prime = c->pop_pack ();
|
|
|
|
|
|
|
|
start_object ("cc", 2, c);
|
|
|
|
add_offset (obj_e_prime, c);
|
|
|
|
unsigned obj_c = c->pop_pack ();
|
|
|
|
|
|
|
|
start_object ("d", 1, c);
|
|
|
|
add_offset (obj_e_prime, c);
|
|
|
|
unsigned obj_d_prime = c->pop_pack ();
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 60000, c);
|
|
|
|
add_offset (obj_c, c);
|
|
|
|
add_offset (obj_d_prime, c);
|
|
|
|
unsigned obj_b = c->pop_pack ();
|
|
|
|
|
|
|
|
// 16 bit subgraph
|
|
|
|
unsigned obj_f = add_object ("f", 1, c);
|
|
|
|
|
|
|
|
start_object ("e", 1, c);
|
|
|
|
add_offset (obj_f, c);
|
|
|
|
unsigned obj_e = c->pop_pack ();
|
|
|
|
|
|
|
|
start_object ("d", 1, c);
|
|
|
|
add_offset (obj_e, c);
|
|
|
|
unsigned obj_d = c->pop_pack ();
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 10000, c);
|
|
|
|
add_offset (obj_d, c);
|
|
|
|
unsigned obj_g = c->pop_pack ();
|
|
|
|
|
|
|
|
start_object ("a", 1, c);
|
|
|
|
add_wide_offset (obj_b, c);
|
|
|
|
add_offset (obj_g, c);
|
|
|
|
c->pop_pack ();
|
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
2021-09-09 19:25:43 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
populate_serializer_with_isolation_overflow_spaces (hb_serialize_context_t* c)
|
|
|
|
{
|
|
|
|
std::string large_string(70000, 'a');
|
|
|
|
c->start_serialize<char> ();
|
|
|
|
|
|
|
|
unsigned obj_d = add_object ("f", 1, c);
|
|
|
|
unsigned obj_e = add_object ("f", 1, c);
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 60000, c);
|
|
|
|
add_offset (obj_d, c);
|
|
|
|
unsigned obj_b = c->pop_pack ();
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 60000, c);
|
|
|
|
add_offset (obj_e, c);
|
|
|
|
unsigned obj_c = c->pop_pack ();
|
|
|
|
|
|
|
|
|
|
|
|
start_object ("a", 1, c);
|
|
|
|
add_wide_offset (obj_b, c);
|
|
|
|
add_wide_offset (obj_c, c);
|
|
|
|
c->pop_pack ();
|
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
|
|
|
|
2021-09-28 21:08:18 +02:00
|
|
|
static void
|
|
|
|
populate_serializer_spaces (hb_serialize_context_t* c, bool with_overflow)
|
|
|
|
{
|
|
|
|
std::string large_string(70000, 'a');
|
|
|
|
c->start_serialize<char> ();
|
|
|
|
|
|
|
|
unsigned obj_i;
|
|
|
|
|
|
|
|
if (with_overflow)
|
|
|
|
obj_i = add_object ("i", 1, c);
|
|
|
|
|
|
|
|
// Space 2
|
|
|
|
unsigned obj_h = add_object ("h", 1, c);
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 30000, c);
|
|
|
|
add_offset (obj_h, c);
|
|
|
|
unsigned obj_e = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object ("b", 1, c);
|
|
|
|
add_offset (obj_e, c);
|
|
|
|
unsigned obj_b = c->pop_pack (false);
|
|
|
|
|
|
|
|
// Space 1
|
|
|
|
if (!with_overflow)
|
|
|
|
obj_i = add_object ("i", 1, c);
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 30000, c);
|
|
|
|
add_offset (obj_i, c);
|
|
|
|
unsigned obj_g = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object (large_string.c_str(), 30000, c);
|
|
|
|
add_offset (obj_i, c);
|
|
|
|
unsigned obj_f = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object ("d", 1, c);
|
|
|
|
add_offset (obj_g, c);
|
|
|
|
unsigned obj_d = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object ("c", 1, c);
|
|
|
|
add_offset (obj_f, c);
|
|
|
|
unsigned obj_c = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object ("a", 1, c);
|
|
|
|
add_wide_offset (obj_b, c);
|
|
|
|
add_wide_offset (obj_c, c);
|
|
|
|
add_wide_offset (obj_d, c);
|
|
|
|
c->pop_pack (false);
|
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
|
|
|
|
2020-10-29 22:58:34 +01:00
|
|
|
static void
|
2020-10-30 18:29:51 +01:00
|
|
|
populate_serializer_complex_1 (hb_serialize_context_t* c)
|
2020-10-29 22:58:34 +01:00
|
|
|
{
|
|
|
|
c->start_serialize<char> ();
|
|
|
|
|
|
|
|
unsigned obj_4 = add_object ("jkl", 3, c);
|
|
|
|
unsigned obj_3 = add_object ("ghi", 3, c);
|
|
|
|
|
|
|
|
start_object ("def", 3, c);
|
|
|
|
add_offset (obj_3, c);
|
|
|
|
unsigned obj_2 = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object ("abc", 3, c);
|
|
|
|
add_offset (obj_2, c);
|
|
|
|
add_offset (obj_4, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
c->pop_pack (false);
|
2020-10-29 22:58:34 +01:00
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:29:51 +01:00
|
|
|
static void
|
|
|
|
populate_serializer_complex_2 (hb_serialize_context_t* c)
|
|
|
|
{
|
|
|
|
c->start_serialize<char> ();
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
unsigned obj_5 = add_object ("mn", 2, c);
|
2020-10-30 18:29:51 +01:00
|
|
|
|
|
|
|
unsigned obj_4 = add_object ("jkl", 3, c);
|
|
|
|
|
|
|
|
start_object ("ghi", 3, c);
|
|
|
|
add_offset (obj_4, c);
|
|
|
|
unsigned obj_3 = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object ("def", 3, c);
|
|
|
|
add_offset (obj_3, c);
|
|
|
|
unsigned obj_2 = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object ("abc", 3, c);
|
|
|
|
add_offset (obj_2, c);
|
|
|
|
add_offset (obj_4, c);
|
|
|
|
add_offset (obj_5, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
c->pop_pack (false);
|
2020-10-30 18:29:51 +01:00
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
static void
|
|
|
|
populate_serializer_complex_3 (hb_serialize_context_t* c)
|
|
|
|
{
|
|
|
|
c->start_serialize<char> ();
|
|
|
|
|
|
|
|
unsigned obj_6 = add_object ("opqrst", 6, c);
|
|
|
|
|
|
|
|
unsigned obj_5 = add_object ("mn", 2, c);
|
|
|
|
|
|
|
|
start_object ("jkl", 3, c);
|
|
|
|
add_offset (obj_6, c);
|
|
|
|
unsigned obj_4 = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object ("ghi", 3, c);
|
|
|
|
add_offset (obj_4, c);
|
|
|
|
unsigned obj_3 = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object ("def", 3, c);
|
|
|
|
add_offset (obj_3, c);
|
|
|
|
unsigned obj_2 = c->pop_pack (false);
|
|
|
|
|
|
|
|
start_object ("abc", 3, c);
|
|
|
|
add_offset (obj_2, c);
|
|
|
|
add_offset (obj_4, c);
|
|
|
|
add_offset (obj_5, c);
|
2021-09-28 21:08:18 +02:00
|
|
|
c->pop_pack (false);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
|
|
|
c->end_serialize();
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:29:51 +01:00
|
|
|
static void test_sort_kahn_1 ()
|
2020-10-29 22:58:34 +01:00
|
|
|
{
|
|
|
|
size_t buffer_size = 100;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
2020-10-30 18:29:51 +01:00
|
|
|
populate_serializer_complex_1 (&c);
|
2020-10-29 22:58:34 +01:00
|
|
|
|
|
|
|
graph_t graph (c.object_graph ());
|
2020-10-30 18:29:51 +01:00
|
|
|
graph.sort_kahn ();
|
2020-10-29 22:58:34 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (3).head, "abc", 3) == 0);
|
|
|
|
assert(graph.object (3).links.length == 2);
|
|
|
|
assert(graph.object (3).links[0].objidx == 2);
|
|
|
|
assert(graph.object (3).links[1].objidx == 1);
|
2020-10-29 22:58:34 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (2).head, "def", 3) == 0);
|
|
|
|
assert(graph.object (2).links.length == 1);
|
|
|
|
assert(graph.object (2).links[0].objidx == 0);
|
2020-10-29 22:58:34 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (1).head, "jkl", 3) == 0);
|
|
|
|
assert(graph.object (1).links.length == 0);
|
2020-10-29 22:58:34 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (0).head, "ghi", 3) == 0);
|
|
|
|
assert(graph.object (0).links.length == 0);
|
2020-11-11 22:55:35 +01:00
|
|
|
|
|
|
|
free (buffer);
|
2020-10-29 22:58:34 +01:00
|
|
|
}
|
|
|
|
|
2020-10-30 18:29:51 +01:00
|
|
|
static void test_sort_kahn_2 ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 100;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_complex_2 (&c);
|
|
|
|
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
graph.sort_kahn ();
|
|
|
|
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (4).head, "abc", 3) == 0);
|
|
|
|
assert(graph.object (4).links.length == 3);
|
|
|
|
assert(graph.object (4).links[0].objidx == 3);
|
|
|
|
assert(graph.object (4).links[1].objidx == 0);
|
|
|
|
assert(graph.object (4).links[2].objidx == 2);
|
2020-10-30 18:29:51 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (3).head, "def", 3) == 0);
|
|
|
|
assert(graph.object (3).links.length == 1);
|
|
|
|
assert(graph.object (3).links[0].objidx == 1);
|
2020-10-30 18:29:51 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (2).head, "mn", 2) == 0);
|
|
|
|
assert(graph.object (2).links.length == 0);
|
2020-10-30 18:29:51 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (1).head, "ghi", 3) == 0);
|
|
|
|
assert(graph.object (1).links.length == 1);
|
|
|
|
assert(graph.object (1).links[0].objidx == 0);
|
2020-10-30 18:29:51 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (0).head, "jkl", 3) == 0);
|
|
|
|
assert(graph.object (0).links.length == 0);
|
2020-11-11 22:55:35 +01:00
|
|
|
|
|
|
|
free (buffer);
|
2020-10-30 18:29:51 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:16:27 +01:00
|
|
|
static void test_sort_shortest ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 100;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_complex_2 (&c);
|
|
|
|
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
graph.sort_shortest_distance ();
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (4).head, "abc", 3) == 0);
|
|
|
|
assert(graph.object (4).links.length == 3);
|
|
|
|
assert(graph.object (4).links[0].objidx == 2);
|
|
|
|
assert(graph.object (4).links[1].objidx == 0);
|
|
|
|
assert(graph.object (4).links[2].objidx == 3);
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (3).head, "mn", 2) == 0);
|
|
|
|
assert(graph.object (3).links.length == 0);
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (2).head, "def", 3) == 0);
|
|
|
|
assert(graph.object (2).links.length == 1);
|
|
|
|
assert(graph.object (2).links[0].objidx == 1);
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (1).head, "ghi", 3) == 0);
|
|
|
|
assert(graph.object (1).links.length == 1);
|
|
|
|
assert(graph.object (1).links[0].objidx == 0);
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (0).head, "jkl", 3) == 0);
|
|
|
|
assert(graph.object (0).links.length == 0);
|
2020-11-11 22:55:35 +01:00
|
|
|
|
|
|
|
free (buffer);
|
2020-11-03 01:16:27 +01:00
|
|
|
}
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
static void test_duplicate_leaf ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 100;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_complex_2 (&c);
|
|
|
|
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
graph.duplicate (4, 1);
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (5).head, "abc", 3) == 0);
|
|
|
|
assert(graph.object (5).links.length == 3);
|
|
|
|
assert(graph.object (5).links[0].objidx == 3);
|
|
|
|
assert(graph.object (5).links[1].objidx == 4);
|
|
|
|
assert(graph.object (5).links[2].objidx == 0);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (4).head, "jkl", 3) == 0);
|
|
|
|
assert(graph.object (4).links.length == 0);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (3).head, "def", 3) == 0);
|
|
|
|
assert(graph.object (3).links.length == 1);
|
|
|
|
assert(graph.object (3).links[0].objidx == 2);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (2).head, "ghi", 3) == 0);
|
|
|
|
assert(graph.object (2).links.length == 1);
|
|
|
|
assert(graph.object (2).links[0].objidx == 1);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (1).head, "jkl", 3) == 0);
|
|
|
|
assert(graph.object (1).links.length == 0);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (0).head, "mn", 2) == 0);
|
|
|
|
assert(graph.object (0).links.length == 0);
|
2020-11-11 22:55:35 +01:00
|
|
|
|
|
|
|
free (buffer);
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_duplicate_interior ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 100;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_complex_3 (&c);
|
|
|
|
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
graph.duplicate (3, 2);
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (6).head, "abc", 3) == 0);
|
|
|
|
assert(graph.object (6).links.length == 3);
|
|
|
|
assert(graph.object (6).links[0].objidx == 4);
|
|
|
|
assert(graph.object (6).links[1].objidx == 2);
|
|
|
|
assert(graph.object (6).links[2].objidx == 1);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (5).head, "jkl", 3) == 0);
|
|
|
|
assert(graph.object (5).links.length == 1);
|
|
|
|
assert(graph.object (5).links[0].objidx == 0);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (4).head, "def", 3) == 0);
|
|
|
|
assert(graph.object (4).links.length == 1);
|
|
|
|
assert(graph.object (4).links[0].objidx == 3);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (3).head, "ghi", 3) == 0);
|
|
|
|
assert(graph.object (3).links.length == 1);
|
|
|
|
assert(graph.object (3).links[0].objidx == 5);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (2).head, "jkl", 3) == 0);
|
|
|
|
assert(graph.object (2).links.length == 1);
|
|
|
|
assert(graph.object (2).links[0].objidx == 0);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (1).head, "mn", 2) == 0);
|
|
|
|
assert(graph.object (1).links.length == 0);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
assert(strncmp (graph.object (0).head, "opqrst", 6) == 0);
|
|
|
|
assert(graph.object (0).links.length == 0);
|
2020-11-11 22:55:35 +01:00
|
|
|
|
|
|
|
free (buffer);
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
|
|
|
|
2020-10-29 01:49:09 +01:00
|
|
|
static void
|
|
|
|
test_serialize ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 100;
|
|
|
|
void* buffer_1 = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c1 (buffer_1, buffer_size);
|
2020-10-29 22:58:34 +01:00
|
|
|
populate_serializer_simple (&c1);
|
2020-10-29 01:49:09 +01:00
|
|
|
hb_bytes_t expected = c1.copy_bytes ();
|
|
|
|
|
|
|
|
void* buffer_2 = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c2 (buffer_2, buffer_size);
|
|
|
|
|
|
|
|
graph_t graph (c1.object_graph ());
|
|
|
|
graph.serialize (&c2);
|
|
|
|
hb_bytes_t actual = c2.copy_bytes ();
|
|
|
|
|
|
|
|
assert (actual == expected);
|
|
|
|
|
2021-07-08 18:53:45 +02:00
|
|
|
actual.fini ();
|
|
|
|
expected.fini ();
|
2020-10-29 01:49:09 +01:00
|
|
|
free (buffer_1);
|
|
|
|
free (buffer_2);
|
|
|
|
}
|
|
|
|
|
2020-11-02 23:51:39 +01:00
|
|
|
static void test_will_overflow_1 ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 100;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_complex_2 (&c);
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
assert (!graph.will_overflow (nullptr));
|
2020-11-11 22:55:35 +01:00
|
|
|
|
|
|
|
free (buffer);
|
2020-11-02 23:51:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_will_overflow_2 ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 160000;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_with_overflow (&c);
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
assert (graph.will_overflow (nullptr));
|
2020-11-11 22:55:35 +01:00
|
|
|
|
|
|
|
free (buffer);
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_will_overflow_3 ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 160000;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_with_dedup_overflow (&c);
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
|
|
|
|
assert (graph.will_overflow (nullptr));
|
2020-11-11 22:55:35 +01:00
|
|
|
|
|
|
|
free (buffer);
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_resolve_overflows_via_sort ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 160000;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_with_overflow (&c);
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
|
|
|
|
void* out_buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t out (out_buffer, buffer_size);
|
|
|
|
|
2021-09-23 01:00:02 +02:00
|
|
|
hb_resolve_overflows (c.object_graph (), HB_TAG_NONE, &out);
|
2021-03-17 23:53:10 +01:00
|
|
|
assert (!out.offset_overflow ());
|
2020-11-06 01:39:23 +01:00
|
|
|
hb_bytes_t result = out.copy_bytes ();
|
|
|
|
assert (result.length == (80000 + 3 + 3 * 2));
|
|
|
|
|
2021-07-08 18:53:45 +02:00
|
|
|
result.fini ();
|
2020-11-06 01:39:23 +01:00
|
|
|
free (buffer);
|
|
|
|
free (out_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_resolve_overflows_via_duplication ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 160000;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_with_dedup_overflow (&c);
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
|
|
|
|
void* out_buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t out (out_buffer, buffer_size);
|
|
|
|
|
2021-09-23 01:00:02 +02:00
|
|
|
hb_resolve_overflows (c.object_graph (), HB_TAG_NONE, &out);
|
2021-03-17 23:53:10 +01:00
|
|
|
assert (!out.offset_overflow ());
|
2020-11-06 01:39:23 +01:00
|
|
|
hb_bytes_t result = out.copy_bytes ();
|
|
|
|
assert (result.length == (10000 + 2 * 2 + 60000 + 2 + 3 * 2));
|
|
|
|
|
2021-07-08 18:53:45 +02:00
|
|
|
result.fini ();
|
2020-11-06 01:39:23 +01:00
|
|
|
free (buffer);
|
|
|
|
free (out_buffer);
|
2020-11-02 23:51:39 +01:00
|
|
|
}
|
|
|
|
|
2021-09-28 21:08:18 +02:00
|
|
|
static void test_resolve_overflows_via_space_assignment ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 160000;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_spaces (&c, true);
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
|
|
|
|
void* out_buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t out (out_buffer, buffer_size);
|
|
|
|
|
|
|
|
assert (c.offset_overflow ());
|
|
|
|
hb_resolve_overflows (c.object_graph (), HB_TAG ('G', 'S', 'U', 'B'), &out, 0);
|
|
|
|
assert (!out.offset_overflow ());
|
|
|
|
hb_bytes_t result = out.copy_bytes ();
|
|
|
|
|
|
|
|
void* expected_buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t e (expected_buffer, buffer_size);
|
|
|
|
assert (!e.offset_overflow ());
|
|
|
|
populate_serializer_spaces (&e, false);
|
|
|
|
hb_bytes_t expected_result = e.copy_bytes ();
|
|
|
|
|
|
|
|
assert (result.length == expected_result.length);
|
|
|
|
for (unsigned i = 0; i < result.length; i++)
|
|
|
|
assert (result[i] == expected_result[i]);
|
|
|
|
|
|
|
|
result.fini ();
|
|
|
|
expected_result.fini ();
|
|
|
|
free (buffer);
|
|
|
|
free (expected_buffer);
|
|
|
|
free (out_buffer);
|
|
|
|
}
|
2021-09-09 00:07:02 +02:00
|
|
|
|
|
|
|
static void test_resolve_overflows_via_isolation ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 160000;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_with_isolation_overflow (&c);
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
|
|
|
|
void* out_buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t out (out_buffer, buffer_size);
|
|
|
|
|
|
|
|
assert (c.offset_overflow ());
|
2021-09-28 21:08:18 +02:00
|
|
|
hb_resolve_overflows (c.object_graph (), HB_TAG ('G', 'S', 'U', 'B'), &out, 1);
|
2021-09-09 00:07:02 +02:00
|
|
|
assert (!out.offset_overflow ());
|
|
|
|
hb_bytes_t result = out.copy_bytes ();
|
|
|
|
assert (result.length == (1 + 10000 + 60000 + 1 + 1
|
|
|
|
+ 4 + 3 * 2));
|
|
|
|
|
|
|
|
result.fini ();
|
|
|
|
free (buffer);
|
|
|
|
free (out_buffer);
|
|
|
|
}
|
|
|
|
|
2021-09-09 19:25:43 +02:00
|
|
|
static void test_resolve_overflows_via_isolation_with_recursive_duplication ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 160000;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_with_isolation_overflow_complex (&c);
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
|
|
|
|
void* out_buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t out (out_buffer, buffer_size);
|
|
|
|
|
|
|
|
assert (c.offset_overflow ());
|
2021-09-28 21:08:18 +02:00
|
|
|
hb_resolve_overflows (c.object_graph (), HB_TAG ('G', 'S', 'U', 'B'), &out, 1);
|
2021-09-09 19:25:43 +02:00
|
|
|
assert (!out.offset_overflow ());
|
|
|
|
hb_bytes_t result = out.copy_bytes ();
|
|
|
|
|
2021-09-25 01:28:34 +02:00
|
|
|
void* expected_buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t e (expected_buffer, buffer_size);
|
|
|
|
assert (!e.offset_overflow ());
|
|
|
|
populate_serializer_with_isolation_overflow_complex_expected (&e);
|
|
|
|
hb_bytes_t expected_result = e.copy_bytes ();
|
|
|
|
|
|
|
|
assert (result.length == expected_result.length);
|
|
|
|
for (unsigned i = 0; i < result.length; i++)
|
|
|
|
assert (result[i] == expected_result[i]);
|
2021-09-09 19:25:43 +02:00
|
|
|
|
|
|
|
result.fini ();
|
2021-09-25 01:28:34 +02:00
|
|
|
expected_result.fini ();
|
2021-09-09 19:25:43 +02:00
|
|
|
free (buffer);
|
2021-09-25 01:28:34 +02:00
|
|
|
free (expected_buffer);
|
2021-09-09 19:25:43 +02:00
|
|
|
free (out_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_resolve_overflows_via_isolation_spaces ()
|
|
|
|
{
|
|
|
|
size_t buffer_size = 160000;
|
|
|
|
void* buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t c (buffer, buffer_size);
|
|
|
|
populate_serializer_with_isolation_overflow_spaces (&c);
|
|
|
|
graph_t graph (c.object_graph ());
|
|
|
|
|
|
|
|
void* out_buffer = malloc (buffer_size);
|
|
|
|
hb_serialize_context_t out (out_buffer, buffer_size);
|
|
|
|
|
|
|
|
assert (c.offset_overflow ());
|
|
|
|
hb_resolve_overflows (c.object_graph (), HB_TAG ('G', 'S', 'U', 'B'), &out, 0);
|
|
|
|
assert (!out.offset_overflow ());
|
|
|
|
hb_bytes_t result = out.copy_bytes ();
|
|
|
|
|
|
|
|
unsigned expected_length = 3 + 2 * 60000; // objects
|
|
|
|
expected_length += 2 * 4 + 2 * 2; // links
|
|
|
|
assert (result.length == expected_length);
|
|
|
|
|
|
|
|
result.fini ();
|
|
|
|
free (buffer);
|
|
|
|
free (out_buffer);
|
|
|
|
}
|
2021-09-09 01:08:48 +02:00
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
// TODO(garretrieger): update will_overflow tests to check the overflows array.
|
2020-11-07 01:22:48 +01:00
|
|
|
// TODO(garretrieger): add tests for priority raising.
|
2020-11-05 18:21:25 +01:00
|
|
|
|
2020-10-29 01:49:09 +01:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
test_serialize ();
|
2020-10-30 18:29:51 +01:00
|
|
|
test_sort_kahn_1 ();
|
|
|
|
test_sort_kahn_2 ();
|
2020-11-03 01:16:27 +01:00
|
|
|
test_sort_shortest ();
|
2020-11-02 23:51:39 +01:00
|
|
|
test_will_overflow_1 ();
|
|
|
|
test_will_overflow_2 ();
|
2020-11-06 01:39:23 +01:00
|
|
|
test_will_overflow_3 ();
|
|
|
|
test_resolve_overflows_via_sort ();
|
|
|
|
test_resolve_overflows_via_duplication ();
|
2021-09-28 21:08:18 +02:00
|
|
|
test_resolve_overflows_via_space_assignment ();
|
2021-09-09 00:07:02 +02:00
|
|
|
test_resolve_overflows_via_isolation ();
|
2021-09-09 19:25:43 +02:00
|
|
|
test_resolve_overflows_via_isolation_with_recursive_duplication ();
|
|
|
|
test_resolve_overflows_via_isolation_spaces ();
|
2020-11-06 01:39:23 +01:00
|
|
|
test_duplicate_leaf ();
|
|
|
|
test_duplicate_interior ();
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|