From 010e8782861b8ac19be2e17cfbaabcfaa17373a5 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 28 Feb 2018 19:43:29 -0500 Subject: [PATCH] Wrote more of the testing framework, wrote the first test, and fixed compiler errors in opentype.c. --- common/opentype.c | 38 +++++++++++++++++++++++++++----------- common/opentype_test.c | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/common/opentype.c b/common/opentype.c index 335a2ad0..f27907f6 100644 --- a/common/opentype.c +++ b/common/opentype.c @@ -1,6 +1,6 @@ // 25 february 2018 -#include "uipriv.h" -#include "attrstr.h" +//TODO#include "uipriv.h" +//TODO#include "attrstr.h" struct feature { char a; @@ -35,9 +35,7 @@ void uiFreeOpenTypeFeatures(uiOpenTypeFeatures *otf) uiprivFree(otf); } -// uiOpenTypeFeaturesClone() makes a copy of otf and returns it. -// Changing one will not affect the other. -_UI_EXTERN uiOpenTypeFeatures *uiOpenTypeFeaturesClone(const uiOpenTypeFeatures *otf) +uiOpenTypeFeatures *uiOpenTypeFeaturesClone(const uiOpenTypeFeatures *otf) { uiOpenTypeFeatures *ret; @@ -66,12 +64,27 @@ static int featurecmp(const void *a, const void *b) return intdiff(f->d, g->d); } +static struct feature mkkey(char a, char b, char c, char d) +{ + struct feature f; + + f.a = a; + f.b = b; + f.c = c; + f.d = d; + return f; +} + +#define find(pkey, otf) bsearch(pkey, otf->data, otf->len, sizeof (struct feature), featurecmp) + void uiOpenTypeFeaturesAdd(uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t value) { struct feature *f; + struct feature key; // replace existing value if any - f = (struct feature *) bsearch(otf->data, otf->len, sizeof (struct feature), featurecmp); + key = mkkey(a, b, c, d); + f = (struct feature *) find(&key, otf); if (f != NULL) { f->value = value; return; @@ -93,13 +106,15 @@ void uiOpenTypeFeaturesAdd(uiOpenTypeFeatures *otf, char a, char b, char c, char qsort(otf->data, otf->len, sizeof (struct feature), featurecmp); } -_UI_EXTERN void uiOpenTypeFeaturesRemove(uiOpenTypeFeatures *otf, char a, char b, char c, char d) +void uiOpenTypeFeaturesRemove(uiOpenTypeFeatures *otf, char a, char b, char c, char d) { struct feature *f; + struct feature key; ptrdiff_t index; size_t count; - f = (struct feature *) bsearch(otf->data, otf->len, sizeof (struct feature), featurecmp); + key = mkkey(a, b, c, d); + f = (struct feature *) find(&key, otf); if (f == NULL) return; @@ -112,8 +127,10 @@ _UI_EXTERN void uiOpenTypeFeaturesRemove(uiOpenTypeFeatures *otf, char a, char b int uiOpenTypeFeaturesGet(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t *value) { const struct feature *f; + struct feature key; - f = (const struct feature *) bsearch(otf->data, otf->len, sizeof (struct feature), featurecmp); + key = mkkey(a, b, c, d); + f = (const struct feature *) find(&key, otf); if (f == NULL) return 0; *value = f->value; @@ -128,8 +145,7 @@ void uiOpenTypeFeaturesForEach(const uiOpenTypeFeatures *otf, uiOpenTypeFeatures p = otf->data; for (n = 0; n < otf->len; n++) { - ret = (*f)(cur->a, cur->b, cur->c, cur->d, - cur->value, data); + ret = (*f)(otf, p->a, p->b, p->c, p->d, p->value, data); // TODO for all: require exact match? if (ret == uiForEachStop) return; diff --git a/common/opentype_test.c b/common/opentype_test.c index b883c53b..3fe00079 100644 --- a/common/opentype_test.c +++ b/common/opentype_test.c @@ -2,6 +2,7 @@ #ifndef TODO_TEST #error TODO this is where libui itself goes #endif +#include #include @@ -18,7 +19,7 @@ #define testingprivBadLanguageVersion #endif #ifdef testingprivBadLanguageVersion -#error sorry, TODO requires either C99 or TODO; cannot continue +#error sorry, TODO requires either C99 or C++11; cannot continue #endif #ifdef __cplusplus @@ -49,9 +50,39 @@ extern void testingprivTErrorvfFull(testingT *, const char *, int, const char *, } #endif -testingTest(Name) +#include +#include +#include +#include +typedef struct uiOpenTypeFeatures uiOpenTypeFeatures; +typedef int uiForEach; +enum { uiForEachContinue, uiForEachStop }; +typedef uiForEach (*uiOpenTypeFeaturesForEachFunc)(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t value, void *data); +#define uiprivNew(x) ((x *) malloc(sizeof (x))) +#define uiprivAlloc(x,y) malloc(x) +#define uiprivRealloc(x,y,z) realloc(x,y) +#define uiprivFree free +#include "opentype.c" + +testingTest(OpenTypeFeaturesAddGet) { - printf("in the test!\n"); + uiOpenTypeFeatures *otf; + char a, b, c, d; + uint32_t value; + + otf = uiNewOpenTypeFeatures(); + uiOpenTypeFeaturesAdd(otf, 'a', 'b', 'c', 'd', 12345); + if (!uiOpenTypeFeaturesGet(otf, 'a', 'b', 'c', 'd', &value)) { + testingTErrorf(t, "uiOpenTypeFeaturesGet() failed to get feature we added"); + goto out; + } + if (value != 12345) { + testingTErrorf(t, "feature abcd: got %" PRIu32 ", want 12345", value); + goto out; + } + +out: + uiFreeOpenTypeFeatures(otf); } int main(void)