[test] Free memory in hb-aots-tester

This commit is contained in:
Khaled Hosny 2021-07-29 18:20:03 +02:00 committed by Behdad Esfahbod
parent 895162d6f3
commit 72489f3e0e
1 changed files with 55 additions and 51 deletions

View File

@ -23,16 +23,36 @@ ____________________________________________________________________________*/
static const bool verbose = true;
struct TestData
{
TestData(hb_buffer_t *buffer_,
hb_face_t *face_,
hb_font_t *font_,
hb_feature_t *features_,
int num_features_)
: buffer(buffer_), face(face_), font(font_),
features(features_), num_features(num_features_)
{ }
~TestData()
{
free (features);
hb_face_destroy (face);
hb_font_destroy (font);
hb_buffer_destroy (buffer);
}
hb_feature_t *gFeatures;
hb_font_t *gFont;
hb_face_t *gFace;
int gNbFeatures;
hb_buffer_t *buffer;
hb_face_t *face;
hb_font_t *font;
hb_feature_t *features;
int num_features;
};
hb_buffer_t *runTest(const char *testName,
const char *fontfileName,
unsigned int *in, int nbIn,
unsigned int *select, int nbSelect)
TestData
runTest(const char *testName,
const char *fontfileName,
unsigned int *in, int nbIn,
unsigned int *select, int nbSelect)
{
FILE *f = fopen (fontfileName, "rb");
fseek(f, 0, SEEK_END);
@ -94,20 +114,13 @@ int gNbFeatures;
}
}
}
gFeatures = features;
gNbFeatures = nbFeatures;
gFont = font;
gFace = face;
// shape
hb_shape(font, buffer, features, nbFeatures);
hb_blob_destroy(blob);
//hb_font_destroy(font);
//hb_face_destroy(face);
//free(features);
return buffer;
return TestData(buffer, face, font, features, nbFeatures);
}
@ -131,14 +144,14 @@ bool gsub_test(const char *testName,
int nbSelect, unsigned int *select,
int nbExpected, unsigned int *expected)
{
hb_buffer_t *buffer = runTest(testName,
fontfileName,
in, nbIn,
select, nbSelect);
TestData data = runTest(testName,
fontfileName,
in, nbIn,
select, nbSelect);
// verify
hb_glyph_info_t *actual = hb_buffer_get_glyph_infos(buffer, 0);
unsigned int nbActual = hb_buffer_get_length(buffer);
hb_glyph_info_t *actual = hb_buffer_get_glyph_infos(data.buffer, 0);
unsigned int nbActual = hb_buffer_get_length(data.buffer);
bool ok = true;
@ -159,14 +172,13 @@ bool gsub_test(const char *testName,
FILE *tests_file = fopen (test_name, "a+");
if (!ok) fprintf (tests_file, "#");
fprintf (tests_file, "../fonts/%s:--features=\"", fontfileName + 9);
for (unsigned int i = 0; i < gNbFeatures; i++)
for (unsigned int i = 0; i < data.num_features; i++)
{
if (i != 0) fprintf (tests_file, ",");
char buf[255];
hb_feature_to_string (&gFeatures[i], buf, sizeof (buf));
hb_feature_to_string (&data.features[i], buf, sizeof (buf));
fprintf (tests_file, "%s", buf);
}
free (gFeatures);
fprintf (tests_file, "\" --no-clusters --no-glyph-names --no-positions:");
for (unsigned int i = 0; i < nbIn; i++)
@ -202,8 +214,6 @@ bool gsub_test(const char *testName,
}
hb_buffer_destroy(buffer);
return ok;
}
@ -213,14 +223,14 @@ bool cmap_test(const char *testName,
int nbSelect, unsigned int *select,
int nbExpected, unsigned int *expected)
{
hb_buffer_t *buffer = runTest(testName,
fontfileName,
in, nbIn,
select, nbSelect);
TestData data = runTest(testName,
fontfileName,
in, nbIn,
select, nbSelect);
// verify
hb_glyph_info_t *actual = hb_buffer_get_glyph_infos(buffer, 0);
unsigned int nbActual = hb_buffer_get_length(buffer);
hb_glyph_info_t *actual = hb_buffer_get_glyph_infos(data.buffer, 0);
unsigned int nbActual = hb_buffer_get_length(data.buffer);
bool ok = true;
@ -241,14 +251,13 @@ bool cmap_test(const char *testName,
FILE *tests_file = fopen (test_name, "a+");
if (!ok) fprintf (tests_file, "#");
fprintf (tests_file, "../fonts/%s:--features=\"", fontfileName + 9);
for (unsigned int i = 0; i < gNbFeatures; i++)
for (unsigned int i = 0; i < data.num_features; i++)
{
if (i != 0) fprintf (tests_file, ",");
char buf[255];
hb_feature_to_string (&gFeatures[i], buf, sizeof (buf));
hb_feature_to_string (&data.features[i], buf, sizeof (buf));
fprintf (tests_file, "%s", buf);
}
free (gFeatures);
fprintf (tests_file, "\" --no-clusters --no-glyph-names --no-positions --font-funcs=ot:");
for (unsigned int i = 0; i < nbIn; i++)
@ -284,8 +293,6 @@ bool cmap_test(const char *testName,
}
hb_buffer_destroy(buffer);
return ok;
}
@ -298,15 +305,15 @@ bool gpos_test(const char *testName,
int *x,
int *y)
{
hb_buffer_t *buffer = runTest(testName,
fontfileName,
in, nbIn,
0, 0);
TestData data = runTest(testName,
fontfileName,
in, nbIn,
0, 0);
// verify
unsigned int nbActual;
hb_glyph_info_t *actual = hb_buffer_get_glyph_infos(buffer, &nbActual);
hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL);
hb_glyph_info_t *actual = hb_buffer_get_glyph_infos(data.buffer, &nbActual);
hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (data.buffer, NULL);
unsigned int *actualG = (unsigned int *) malloc(sizeof(*actualG) * nbActual);
int *actualX = (int *) malloc(sizeof(*actualX) * nbActual);
@ -319,7 +326,7 @@ bool gpos_test(const char *testName,
actualY[i] = curY + pos[i].y_offset;
curX += pos[i].x_advance;
if (hb_ot_layout_get_glyph_class (gFace, actualG[i]) != HB_OT_LAYOUT_GLYPH_CLASS_MARK)
if (hb_ot_layout_get_glyph_class (data.face, actualG[i]) != HB_OT_LAYOUT_GLYPH_CLASS_MARK)
curX -= 1500;
curY += pos[i].y_advance;
}
@ -374,14 +381,13 @@ bool gpos_test(const char *testName,
FILE *tests_file = fopen (test_name, "a+");
if (!ok) fprintf (tests_file, "#");
fprintf (tests_file, "../fonts/%s:--features=\"", fontfileName + 9);
for (unsigned int i = 0; i < gNbFeatures; i++)
for (unsigned int i = 0; i < data.num_features; i++)
{
if (i != 0) fprintf (tests_file, ",");
char buf[255];
hb_feature_to_string (&gFeatures[i], buf, sizeof (buf));
hb_feature_to_string (&data.features[i], buf, sizeof (buf));
fprintf (tests_file, "%s", buf);
}
free (gFeatures);
fprintf (tests_file, "\" --no-clusters --no-glyph-names --ned:");
for (unsigned int i = 0; i < nbIn; i++)
@ -400,7 +406,7 @@ bool gpos_test(const char *testName,
int expected_x = x[i] + accumlatedAdvance;
int expected_y = y[i];
if (expected_x || expected_y) fprintf (tests_file, "@%d,%d", expected_x, expected_y);
if (hb_ot_layout_get_glyph_class (gFace, actualG[i]) != HB_OT_LAYOUT_GLYPH_CLASS_MARK)
if (hb_ot_layout_get_glyph_class (data.face, actualG[i]) != HB_OT_LAYOUT_GLYPH_CLASS_MARK)
accumlatedAdvance += 1500;
}
fprintf (tests_file, "]");
@ -409,8 +415,6 @@ bool gpos_test(const char *testName,
fclose (tests_file);
hb_buffer_destroy(buffer);
free(actualG);
free(actualX);
free(actualY);