From 0051a28996161834e87fce34165f099492c3966a Mon Sep 17 00:00:00 2001 From: Boris Faure Date: Mon, 25 May 2020 22:38:39 +0200 Subject: [PATCH] termiolink: add tests on some parsing functions --- src/bin/sb.h | 2 + src/bin/termiolink.c | 205 ++++++++++++++++++++++++++++++++++++++++++- src/bin/tytest.c | 4 + src/bin/unit_tests.h | 4 + 4 files changed, 214 insertions(+), 1 deletion(-) diff --git a/src/bin/sb.h b/src/bin/sb.h index 4cdc8e1f..f1851044 100644 --- a/src/bin/sb.h +++ b/src/bin/sb.h @@ -11,9 +11,11 @@ struct ty_sb { }; int ty_sb_add(struct ty_sb *sb, const char *s, size_t len); +#define TY_SB_ADD(_SB, _S) ty_sb_add(_SB, _S, strlen(_S)) void ty_sb_spaces_rtrim(struct ty_sb *sb); void ty_sb_spaces_ltrim(struct ty_sb *sb); int ty_sb_prepend(struct ty_sb *sb, const char *s, size_t len); +#define TY_SB_PREPEND(_SB, _S) ty_sb_prepend(_SB, _S, strlen(_S)) char *ty_sb_steal_buf(struct ty_sb *sb); void ty_sb_lskip(struct ty_sb *sb, size_t len); void ty_sb_rskip(struct ty_sb *sb, size_t len); diff --git a/src/bin/termiolink.c b/src/bin/termiolink.c index 11e97029..12004e01 100644 --- a/src/bin/termiolink.c +++ b/src/bin/termiolink.c @@ -734,7 +734,9 @@ _parse_sharp_color(struct ty_sb *sb, { uint8_t r, g, b, a = 255; - /* skip sharp */ + /* sharp */ + if (sb->buf[0] != '#') + return EINA_FALSE; ty_sb_lskip(sb, 1); switch (sb->len) { @@ -1094,3 +1096,204 @@ end: } return found; } + + +#if defined(BINARY_TYTEST) +int +tytest_color_parse_hex(void) +{ + uint8_t v = 42; + + assert(_parse_hex('3', &v) == EINA_TRUE); + assert(v == 3); + + assert(_parse_hex('b', &v) == EINA_TRUE); + assert(v == 11); + + assert(_parse_hex('F', &v) == EINA_TRUE); + assert(v == 15); + + assert(_parse_hex('@', &v) == EINA_FALSE); + assert(_parse_hex('#', &v) == EINA_FALSE); + assert(_parse_hex('G', &v) == EINA_FALSE); + assert(_parse_hex('_', &v) == EINA_FALSE); + assert(_parse_hex('g', &v) == EINA_FALSE); + assert(_parse_hex('~', &v) == EINA_FALSE); + + return 0; +} + +int +tytest_color_parse_2hex(void) +{ + uint8_t v = 42; + + assert(_parse_2hex("03", &v) == EINA_TRUE); + assert(v == 3); + + assert(_parse_2hex("aF", &v) == EINA_TRUE); + assert(v == 175); + + assert(_parse_2hex("44", &v) == EINA_TRUE); + assert(v == 68); + + assert(_parse_2hex("gg", &v) == EINA_FALSE); + assert(_parse_2hex("@3", &v) == EINA_FALSE); + assert(_parse_2hex("#3", &v) == EINA_FALSE); + assert(_parse_2hex("G3", &v) == EINA_FALSE); + assert(_parse_2hex("_3", &v) == EINA_FALSE); + assert(_parse_2hex("~3", &v) == EINA_FALSE); + return 0; +} + +int +tytest_color_parse_sharp(void) +{ + struct ty_sb sb = {}; + uint8_t r = 0, g = 0, b = 0, a = 0; + + /* 3 letters */ + assert(TY_SB_ADD(&sb, "#fab") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 240 && g == 160 && b == 176 && a == 255); + ty_sb_free(&sb); + + /* 4 letters */ + assert(TY_SB_ADD(&sb, "#2345") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 32 && g == 48 && b == 64 && a == 80); + ty_sb_free(&sb); + + /* 6 letters */ + assert(TY_SB_ADD(&sb, "#decfab") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 222 && g == 207 && b == 171 && a == 255); + ty_sb_free(&sb); + + /* 8 letters */ + assert(TY_SB_ADD(&sb, "#fabdec86") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 250 && g == 189 && b == 236 && a == 134); + ty_sb_free(&sb); + + /* upper case */ + assert(TY_SB_ADD(&sb, "#DECFAB") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 222 && g == 207 && b == 171 && a == 255); + ty_sb_free(&sb); + + /* mixed case */ + assert(TY_SB_ADD(&sb, "#fAbDeC") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 250 && g == 189 && b == 236 && a == 255); + ty_sb_free(&sb); + + /* Invalid*/ + /* improper start */ + assert(TY_SB_ADD(&sb, "~decfab") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + /* improper ending */ + assert(TY_SB_ADD(&sb, "#decfab;") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + /* invalid hex */ + assert(TY_SB_ADD(&sb, "#dgc") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "#dgca") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "#dgcaca") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "#dgcacaca") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "#dgcacaca") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + /* invalid length */ + assert(TY_SB_ADD(&sb, "#a") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "#da") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "#dadad") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "#dadadad") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "#dadadadad") == 0); + assert(_parse_sharp_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + return 0; +} + +int +tytest_color_parse_uint8(void) +{ + struct ty_sb sb = {}; + uint8_t v = 0; + + /* 1 digit (with/without ending) */ + assert(TY_SB_ADD(&sb, "3 ") == 0); + assert(_parse_uint8(&sb, &v) == EINA_TRUE); + assert(v == 3); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "4") == 0); + assert(_parse_uint8(&sb, &v) == EINA_TRUE); + assert(v == 4); + ty_sb_free(&sb); + + /* 2 digit (with/without ending) */ + assert(TY_SB_ADD(&sb, "12 ") == 0); + assert(_parse_uint8(&sb, &v) == EINA_TRUE); + assert(v == 12); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "34") == 0); + assert(_parse_uint8(&sb, &v) == EINA_TRUE); + assert(v == 34); + ty_sb_free(&sb); + + /* 3 digit (with/without ending) */ + assert(TY_SB_ADD(&sb, "123 ") == 0); + assert(_parse_uint8(&sb, &v) == EINA_TRUE); + assert(v == 123); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "234") == 0); + assert(_parse_uint8(&sb, &v) == EINA_TRUE); + assert(v == 234); + ty_sb_free(&sb); + + /* 0 padded */ + assert(TY_SB_ADD(&sb, "012") == 0); + assert(_parse_uint8(&sb, &v) == EINA_TRUE); + assert(v == 12); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, "007") == 0); + assert(_parse_uint8(&sb, &v) == EINA_TRUE); + assert(v == 7); + ty_sb_free(&sb); + + /* too large */ + assert(TY_SB_ADD(&sb, "256") == 0); + assert(_parse_uint8(&sb, &v) == EINA_FALSE); + ty_sb_free(&sb); + /* too long */ + assert(TY_SB_ADD(&sb, "1234") == 0); + assert(_parse_uint8(&sb, &v) == EINA_FALSE); + ty_sb_free(&sb); + /* not a digit */ + assert(TY_SB_ADD(&sb, "a") == 0); + assert(_parse_uint8(&sb, &v) == EINA_FALSE); + ty_sb_free(&sb); + assert(TY_SB_ADD(&sb, ".") == 0); + assert(_parse_uint8(&sb, &v) == EINA_FALSE); + ty_sb_free(&sb); + + return 0; +} +#endif diff --git a/src/bin/tytest.c b/src/bin/tytest.c index 0853d6a9..946b1234 100644 --- a/src/bin/tytest.c +++ b/src/bin/tytest.c @@ -33,6 +33,10 @@ static struct { { "sb_trim", tytest_sb_trim}, { "sb_gap", tytest_sb_gap}, { "sb_steal", tytest_sb_steal}, + { "color_parse_hex", tytest_color_parse_hex}, + { "color_parse_2hex", tytest_color_parse_2hex}, + { "color_parse_sharp", tytest_color_parse_sharp}, + { "color_parse_uint8", tytest_color_parse_uint8}, { NULL, NULL}, }; diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h index 813441cb..a9cc4ba4 100644 --- a/src/bin/unit_tests.h +++ b/src/bin/unit_tests.h @@ -10,5 +10,9 @@ int tytest_sb_skip(void); int tytest_sb_trim(void); int tytest_sb_gap(void); int tytest_sb_steal(void); +int tytest_color_parse_hex(void); +int tytest_color_parse_2hex(void); +int tytest_color_parse_sharp(void); +int tytest_color_parse_uint8(void); #endif