From 5e4590bc2c99b586defcd425a5b1e3c479c27832 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sat, 12 Sep 2020 00:52:52 +0100 Subject: unpack_test: Rename to a more generic name --- .gitignore | 4 +- all.do | 2 +- scripts/clean | 2 +- test.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test.do | 3 ++ test.inc.do | 3 ++ test.o.do | 4 ++ unpack_test.c | 106 ----------------------------------------------------- unpack_test.do | 3 -- unpack_test.inc.do | 3 -- unpack_test.o.do | 4 -- 11 files changed, 120 insertions(+), 120 deletions(-) create mode 100644 test.c create mode 100644 test.do create mode 100644 test.inc.do create mode 100644 test.o.do delete mode 100644 unpack_test.c delete mode 100644 unpack_test.do delete mode 100644 unpack_test.inc.do delete mode 100644 unpack_test.o.do diff --git a/.gitignore b/.gitignore index c29b68d..4dc4c13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ *.d *.o -*_test *.inc -*_gen *.args +test +test_gen diff --git a/all.do b/all.do index ff9812a..0c001aa 100644 --- a/all.do +++ b/all.do @@ -1,2 +1,2 @@ #!/bin/sh -redo-ifchange "$0" "unpack_test" +redo-ifchange "$0" "test" diff --git a/scripts/clean b/scripts/clean index 97d7b0f..42574c9 100755 --- a/scripts/clean +++ b/scripts/clean @@ -1,2 +1,2 @@ #!/bin/sh -find . \( -name '*.d' -o -name '*.o' -o -name '*_gen' -o -name '*.inc' \) -delete +find . \( -name '*.d' -o -name '*.o' -o -name '*.inc' -o -name 'test_gen' -o -name 'test' \) -delete diff --git a/test.c b/test.c new file mode 100644 index 0000000..08b03b3 --- /dev/null +++ b/test.c @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2020 Tomasz Kramkowski + * SPDX-License-Identifier: MIT + */ +#include +#include +#include +#include + +#include "pack.h" + +typedef bool test_func(void); + +struct test { + test_func *func; + char *desc; +}; + +#define TEST(name, desc) static bool test_##name(void); \ + static struct test testinfo_##name \ + __attribute__((__section__("tests"))) \ + __attribute__((__used__)) = \ + { test_##name, desc }; \ + static bool test_##name(void) +#define DATA(...) (unsigned char []){ __VA_ARGS__ }, sizeof (unsigned char []){ __VA_ARGS__ } +#define CHECK(test) if (!(test)) { puts("! " #test); return false; } + +#define CHECK_UNPACK(data, fmt, ...) do { \ + enum pack_status CHECK_UNPACK_s = unpack(data, fmt, __VA_ARGS__); \ + if (CHECK_UNPACK_s != PACK_OK) { \ + printf(__FILE__ ":%d unpack(" #data ", " #fmt ", ...) -> %s (%d)\n", __LINE__, pack_strerror(CHECK_UNPACK_s), CHECK_UNPACK_s); \ + return false; \ + } \ +} while (0) +#define CHECK_EQUAL(f, a, b) if (a != b) { printf(__FILE__ ":%d %"f" != %"f"\n", __LINE__, a, b); return false; } + +#include "test.inc" + +TEST(simple0_float, "simple unpack float") +{ + float v[1] = { __LINE__ }; + + CHECK_UNPACK(DATA(0x00, 0x00, 0x00, 0x00), "f", &v); + CHECK_EQUAL("f", v[0], 0.0f); + CHECK_UNPACK(DATA(0x3f, 0x80, 0x00, 0x00), "f", &v); + CHECK_EQUAL("f", v[0], 1.0f); + CHECK_UNPACK(DATA(0x41, 0x00, 0x00, 0x00), "f", &v); + CHECK_EQUAL("f", v[0], 8.0f); + + return true; +} + +TEST(simple0_double, "simple unpack double") +{ + double v[1] = { __LINE__ }; + + CHECK_UNPACK(DATA(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), "d", &v); + CHECK_EQUAL("f", v[0], 0.0f); + CHECK_UNPACK(DATA(0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), "d", &v); + CHECK_EQUAL("f", v[0], 1.0f); + CHECK_UNPACK(DATA(0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), "d", &v); + CHECK_EQUAL("f", v[0], 8.0f); + + return true; +} + +TEST(simple_padding, "simple unpack padding") +{ + struct { + int i; + unsigned char c; + float f; + } v = { __LINE__, __LINE__ + 1, __LINE__ + 2 }; + + CHECK_UNPACK(DATA(0xff, 0x85, + 0x00, 0x00, 0x00, + 0x16, + 0x00, 0x00, + 0x40, 0x44, 0x58, 0x00), + ">i3xBxxf", &v.i, &v.c, &v.f); + CHECK_EQUAL("d", v.i, -123); + CHECK_EQUAL("d", v.c, 22); + CHECK_EQUAL("f", v.f, 0x1.88bp+1); + + return true; +} + +int main(void) +{ + extern struct test __start_tests, __stop_tests; + + pack_trace = PACK_TRACE_OFF; + + for (struct test *t = &__start_tests; t < &__stop_tests; t++) { + if (t->func()) { + printf(" OK %s\n", t->desc); + continue; + } + pack_trace = PACK_TRACE_ALL; + fprintf(stderr, ">>> Test failure trace for '%s'\n", t->desc); + t->func(); + fprintf(stderr, "<<<\n"); + pack_trace = PACK_TRACE_OFF; + printf("FAIL %s\n", t->desc); + } +} diff --git a/test.do b/test.do new file mode 100644 index 0000000..23f1a0d --- /dev/null +++ b/test.do @@ -0,0 +1,3 @@ +#!/bin/bash +deps=(test.o common.o unpack.o trace.o) +. ./link diff --git a/test.inc.do b/test.inc.do new file mode 100644 index 0000000..c6d5c1b --- /dev/null +++ b/test.inc.do @@ -0,0 +1,3 @@ +#!/bin/sh +redo-ifchange "$0" "test_gen" +./test_gen diff --git a/test.o.do b/test.o.do new file mode 100644 index 0000000..f90e621 --- /dev/null +++ b/test.o.do @@ -0,0 +1,4 @@ +#!/bin/bash +redo-ifchange "test.inc" +set -- "$1" "${2%.o}" "$3" +. ./default.o.do diff --git a/unpack_test.c b/unpack_test.c deleted file mode 100644 index 28cafdc..0000000 --- a/unpack_test.c +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2020 Tomasz Kramkowski - * SPDX-License-Identifier: MIT - */ -#include -#include -#include -#include - -#include "pack.h" - -typedef bool test_func(void); - -struct test { - test_func *func; - char *desc; -}; - -#define TEST(name, desc) static bool test_##name(void); \ - static struct test testinfo_##name \ - __attribute__((__section__("tests"))) \ - __attribute__((__used__)) = \ - { test_##name, desc }; \ - static bool test_##name(void) -#define DATA(...) (unsigned char []){ __VA_ARGS__ }, sizeof (unsigned char []){ __VA_ARGS__ } -#define CHECK(test) if (!(test)) { puts("! " #test); return false; } - -#define CHECK_UNPACK(data, fmt, ...) do { \ - enum pack_status CHECK_UNPACK_s = unpack(data, fmt, __VA_ARGS__); \ - if (CHECK_UNPACK_s != PACK_OK) { \ - printf(__FILE__ ":%d unpack(" #data ", " #fmt ", ...) -> %s (%d)\n", __LINE__, pack_strerror(CHECK_UNPACK_s), CHECK_UNPACK_s); \ - return false; \ - } \ -} while (0) -#define CHECK_EQUAL(f, a, b) if (a != b) { printf(__FILE__ ":%d %"f" != %"f"\n", __LINE__, a, b); return false; } - -#include "unpack_test.inc" - -TEST(simple0_float, "simple unpack float") -{ - float v[1] = { __LINE__ }; - - CHECK_UNPACK(DATA(0x00, 0x00, 0x00, 0x00), "f", &v); - CHECK_EQUAL("f", v[0], 0.0f); - CHECK_UNPACK(DATA(0x3f, 0x80, 0x00, 0x00), "f", &v); - CHECK_EQUAL("f", v[0], 1.0f); - CHECK_UNPACK(DATA(0x41, 0x00, 0x00, 0x00), "f", &v); - CHECK_EQUAL("f", v[0], 8.0f); - - return true; -} - -TEST(simple0_double, "simple unpack double") -{ - double v[1] = { __LINE__ }; - - CHECK_UNPACK(DATA(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), "d", &v); - CHECK_EQUAL("f", v[0], 0.0f); - CHECK_UNPACK(DATA(0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), "d", &v); - CHECK_EQUAL("f", v[0], 1.0f); - CHECK_UNPACK(DATA(0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), "d", &v); - CHECK_EQUAL("f", v[0], 8.0f); - - return true; -} - -TEST(simple_padding, "simple unpack padding") -{ - struct { - int i; - unsigned char c; - float f; - } v = { __LINE__, __LINE__ + 1, __LINE__ + 2 }; - - CHECK_UNPACK(DATA(0xff, 0x85, - 0x00, 0x00, 0x00, - 0x16, - 0x00, 0x00, - 0x40, 0x44, 0x58, 0x00), - ">i3xBxxf", &v.i, &v.c, &v.f); - CHECK_EQUAL("d", v.i, -123); - CHECK_EQUAL("d", v.c, 22); - CHECK_EQUAL("f", v.f, 0x1.88bp+1); - - return true; -} - -int main(void) -{ - extern struct test __start_tests, __stop_tests; - - pack_trace = PACK_TRACE_OFF; - - for (struct test *t = &__start_tests; t < &__stop_tests; t++) { - if (t->func()) { - printf(" OK %s\n", t->desc); - continue; - } - pack_trace = PACK_TRACE_ALL; - fprintf(stderr, ">>> Test failure trace for '%s'\n", t->desc); - t->func(); - fprintf(stderr, "<<<\n"); - pack_trace = PACK_TRACE_OFF; - printf("FAIL %s\n", t->desc); - } -} diff --git a/unpack_test.do b/unpack_test.do deleted file mode 100644 index 9c810c6..0000000 --- a/unpack_test.do +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -deps=(unpack_test.o common.o unpack.o trace.o) -. ./link diff --git a/unpack_test.inc.do b/unpack_test.inc.do deleted file mode 100644 index c6d5c1b..0000000 --- a/unpack_test.inc.do +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -redo-ifchange "$0" "test_gen" -./test_gen diff --git a/unpack_test.o.do b/unpack_test.o.do deleted file mode 100644 index a7c32b8..0000000 --- a/unpack_test.o.do +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -redo-ifchange "unpack_test.inc" -set -- "$1" "${2%.o}" "$3" -. ./default.o.do -- cgit v1.2.3-54-g00ecf