[ot-glyph][test] Avoid sprintf use

This commit is contained in:
Ebrahim Byagowi 2020-01-26 22:10:47 +03:30
parent 99f8e52d5e
commit 9bb1c79a2c
1 changed files with 116 additions and 44 deletions

View File

@ -26,26 +26,92 @@
#include <hb-ot.h>
typedef struct user_data_t {
typedef struct user_data_t
{
char *str;
unsigned size;
unsigned consumed;
} user_data_t;
/* Our modified itoa, why not using libc's? it is going to be used
in harfbuzzjs where libc isn't available */
static void _hb_reverse (char *buf, unsigned int len)
{
unsigned start = 0, end = len - 1;
while (start < end)
{
char c = buf[end];
buf[end] = buf[start];
buf[start] = c;
start++; end--;
}
}
static unsigned _hb_itoa (int32_t num, char *buf)
{
unsigned int i = 0;
hb_bool_t is_negative = num < 0;
if (is_negative) num = -num;
do
{
buf[i++] = '0' + num % 10;
num /= 10;
} while (num);
if (is_negative) buf[i++] = '-';
_hb_reverse (buf, i);
buf[i] = '\0';
return i;
}
#define ITOA_BUF_SIZE 12 // 10 digits in int32, 1 for negative sign, 1 for \0
static void
test_itoa (void)
{
char s[] = "12345";
_hb_reverse (s, 5);
g_assert_cmpmem (s, 5, "54321", 5);
{
unsigned num = 12345;
char buf[ITOA_BUF_SIZE];
unsigned len = _hb_itoa (num, buf);
g_assert_cmpmem (buf, len, "12345", 5);
}
{
unsigned num = 3152;
char buf[ITOA_BUF_SIZE];
unsigned len = _hb_itoa (num, buf);
g_assert_cmpmem (buf, len, "3152", 4);
}
{
int num = -6457;
char buf[ITOA_BUF_SIZE];
unsigned len = _hb_itoa (num, buf);
g_assert_cmpmem (buf, len, "-6457", 5);
}
}
static void
move_to (hb_position_t to_x, hb_position_t to_y, user_data_t *user_data)
{
user_data->consumed += snprintf (user_data->str + user_data->consumed,
user_data->size - user_data->consumed,
"M%d,%d", to_x, to_y);
/* 4 = command character space + comma + array starts with 0 index + nul character space */
if (user_data->consumed + 2 * ITOA_BUF_SIZE + 4 > user_data->size) return;
user_data->str[user_data->consumed++] = 'M';
user_data->consumed += _hb_itoa (to_x, user_data->str + user_data->consumed);
user_data->str[user_data->consumed++] = ',';
user_data->consumed += _hb_itoa (to_y, user_data->str + user_data->consumed);
}
static void
line_to (hb_position_t to_x, hb_position_t to_y, user_data_t *user_data)
{
user_data->consumed += snprintf (user_data->str + user_data->consumed,
user_data->size - user_data->consumed,
"L%d,%d", to_x, to_y);
if (user_data->consumed + 2 * ITOA_BUF_SIZE + 4 > user_data->size) return;
user_data->str[user_data->consumed++] = 'L';
user_data->consumed += _hb_itoa (to_x, user_data->str + user_data->consumed);
user_data->str[user_data->consumed++] = ',';
user_data->consumed += _hb_itoa (to_y, user_data->str + user_data->consumed);
}
static void
@ -53,11 +119,16 @@ conic_to (hb_position_t control_x, hb_position_t control_y,
hb_position_t to_x, hb_position_t to_y,
user_data_t *user_data)
{
user_data->consumed += snprintf (user_data->str + user_data->consumed,
user_data->size - user_data->consumed,
"Q%d,%d %d,%d",
control_x, control_y,
to_x, to_y);
if (user_data->consumed + 4 * ITOA_BUF_SIZE + 6 > user_data->size) return;
user_data->str[user_data->consumed++] = 'Q';
user_data->consumed += _hb_itoa (control_x, user_data->str + user_data->consumed);
user_data->str[user_data->consumed++] = ',';
user_data->consumed += _hb_itoa (control_y, user_data->str + user_data->consumed);
user_data->str[user_data->consumed++] = ' ';
user_data->consumed += _hb_itoa (to_x, user_data->str + user_data->consumed);
user_data->str[user_data->consumed++] = ',';
user_data->consumed += _hb_itoa (to_y, user_data->str + user_data->consumed);
}
static void
@ -66,20 +137,26 @@ cubic_to (hb_position_t control1_x, hb_position_t control1_y,
hb_position_t to_x, hb_position_t to_y,
user_data_t *user_data)
{
user_data->consumed += snprintf (user_data->str + user_data->consumed,
user_data->size - user_data->consumed,
"C%d,%d %d,%d %d,%d",
control1_x, control1_y,
control2_x, control2_y,
to_x, to_y);
if (user_data->consumed + 6 * ITOA_BUF_SIZE + 8 > user_data->size) return;
user_data->str[user_data->consumed++] = 'C';
user_data->consumed += _hb_itoa (control1_x, user_data->str + user_data->consumed);
user_data->str[user_data->consumed++] = ',';
user_data->consumed += _hb_itoa (control1_y, user_data->str + user_data->consumed);
user_data->str[user_data->consumed++] = ' ';
user_data->consumed += _hb_itoa (control2_x, user_data->str + user_data->consumed);
user_data->str[user_data->consumed++] = ',';
user_data->consumed += _hb_itoa (control2_y, user_data->str + user_data->consumed);
user_data->str[user_data->consumed++] = ' ';
user_data->consumed += _hb_itoa (to_x, user_data->str + user_data->consumed);
user_data->str[user_data->consumed++] = ',';
user_data->consumed += _hb_itoa (to_y, user_data->str + user_data->consumed);
}
static void
close_path (user_data_t *user_data)
{
user_data->consumed += snprintf (user_data->str + user_data->consumed,
user_data->size - user_data->consumed,
"Z");
if (user_data->consumed + 2 > user_data->size) return;
user_data->str[user_data->consumed++] = 'Z';
}
static hb_ot_glyph_decompose_funcs_t *funcs;
@ -97,13 +174,16 @@ test_hb_ot_glyph_glyf (void)
hb_font_t *font = hb_font_create (face);
hb_face_destroy (face);
char str[1024] = {0};
char str[1024];
user_data_t user_data = {
.str = str,
.size = sizeof (str),
.consumed = 0
};
user_data.consumed = 0;
g_assert (!hb_ot_glyph_decompose (font, 4, funcs, &user_data));
user_data.consumed = 0;
g_assert (hb_ot_glyph_decompose (font, 3, funcs, &user_data));
char expected[] = "M275,442L275,442Q232,442 198,420Q164,397 145,353Q126,309 126,245L126,245"
"Q126,182 147,139Q167,95 204,73Q240,50 287,50L287,50Q330,50 367,70"
@ -118,20 +198,15 @@ test_hb_ot_glyph_glyf (void)
var.value = 800;
hb_font_set_variations (font, &var, 1);
char str2[1024] = {0};
user_data_t user_data2 = {
.str = str2,
.size = sizeof (str2),
.consumed = 0
};
g_assert (hb_ot_glyph_decompose (font, 3, funcs, &user_data2));
user_data.consumed = 0;
g_assert (hb_ot_glyph_decompose (font, 3, funcs, &user_data));
char expected2[] = "M323,448L323,448Q297,448 271,430Q244,412 227,371"
"Q209,330 209,261L209,261Q209,204 226,166Q242,127 273,107Q303,86 344,86L344,86Q378,86 404,101"
"Q430,115 451,137L451,137L488,103Q458,42 404,13Q350,-16 279,-16L279,-16Q211,-16 153,13Q95,41 60,99"
"Q25,156 25,241L25,241Q25,323 62,382Q99,440 163,471Q226,501 303,501L303,501Q357,501 399,481"
"Q440,460 464,426Q488,392 492,352L492,352Q475,297 420,297L420,297Q390,297 366,320"
"Q342,342 339,401L339,401L333,469L411,427Q387,438 368,443Q348,448 323,448Z";
g_assert_cmpmem (str2, user_data2.consumed, expected2, sizeof (expected2) - 1);
g_assert_cmpmem (str, user_data.consumed, expected2, sizeof (expected2) - 1);
hb_font_destroy (font);
}
@ -143,7 +218,7 @@ test_hb_ot_glyph_cff1 (void)
hb_font_t *font = hb_font_create (face);
hb_face_destroy (face);
char str[1024] = {0};
char str[1024];
user_data_t user_data = {
.str = str,
.size = sizeof (str),
@ -166,7 +241,7 @@ test_hb_ot_glyph_cff1_rline (void)
hb_font_t *font = hb_font_create (face);
hb_face_destroy (face);
char str[1024] = {0};
char str[1024];
user_data_t user_data = {
.str = str,
.size = sizeof (str),
@ -190,12 +265,13 @@ test_hb_ot_glyph_cff2 (void)
hb_font_t *font = hb_font_create (face);
hb_face_destroy (face);
char str[1024] = {0};
char str[1024];
user_data_t user_data = {
.str = str,
.size = sizeof (str),
.consumed = 0
.size = sizeof (str)
};
user_data.consumed = 0;
g_assert (hb_ot_glyph_decompose (font, 3, funcs, &user_data));
char expected[] = "M275,442C303,442 337,435 371,417L325,454L350,366"
"C357,341 370,321 403,321C428,321 443,333 448,358"
@ -209,19 +285,14 @@ test_hb_ot_glyph_cff2 (void)
var.value = 800;
hb_font_set_variations (font, &var, 1);
char str2[1024] = {0};
user_data_t user_data2 = {
.str = str2,
.size = sizeof (str2),
.consumed = 0
};
g_assert (hb_ot_glyph_decompose (font, 3, funcs, &user_data2));
user_data.consumed = 0;
g_assert (hb_ot_glyph_decompose (font, 3, funcs, &user_data));
char expected2[] = "M323,448C356,448 380,441 411,427L333,469L339,401"
"C343,322 379,297 420,297C458,297 480,314 492,352"
"C486,433 412,501 303,501C148,501 25,406 25,241"
"C25,70 143,-16 279,-16C374,-16 447,22 488,103L451,137"
"C423,107 390,86 344,86C262,86 209,148 209,261C209,398 271,448 323,448Z";
g_assert_cmpmem (str2, user_data2.consumed, expected2, sizeof (expected2) - 1);
g_assert_cmpmem (str, user_data.consumed, expected2, sizeof (expected2) - 1);
hb_font_destroy (font);
}
@ -322,7 +393,7 @@ static void
test_hb_ot_glyph_font_kit_variations_tests (void)
{
/* https://github.com/foliojs/fontkit/blob/b310db5/test/variations.js */
char str[4096] = {0};
char str[2048];
user_data_t user_data = {
.str = str,
.size = sizeof (str)
@ -569,6 +640,7 @@ main (int argc, char **argv)
hb_ot_glyph_decompose_funcs_set_close_path_func (funcs, (hb_ot_glyph_decompose_close_path_func_t) close_path);
hb_test_init (&argc, &argv);
hb_test_add (test_itoa);
hb_test_add (test_hb_ot_glyph_empty);
hb_test_add (test_hb_ot_glyph_glyf);
hb_test_add (test_hb_ot_glyph_cff1);