commit a06cc1769a79f0a8c57415f858a97e2cef174c9e from: Benjamin Stürz date: Fri Jul 05 16:12:28 2024 UTC add test commit - aabb26f4967eaad4ba6030017a87f156bf0df14e commit + a06cc1769a79f0a8c57415f858a97e2cef174c9e blob - b01f1c55cb4f2b49ac28a0eef40040777fc3d561 blob + c3fb48590755b9e8052aa783549970a1a8d2c255 --- .gitignore +++ .gitignore @@ -1,5 +1,9 @@ destruct destruct.inc +test/data.[cho] +test/test +test/test.bin +test/main.o y.tab.h test.[ch] *.o blob - /dev/null blob + 7dee7fad484b905e9422eba3f5e2dfdf27d76e3e (mode 644) --- /dev/null +++ test/Makefile @@ -0,0 +1,22 @@ +DESTRUCT = ../destruct +ENDIAN = little + +all: test test.bin + +run: all + ./test + +clean: + rm -f test test.bin data.[ch] *.o + +test: main.o data.o + ${CC} -o $@ main.o data.o + +test.bin: + printf '%-16.16s\777\0\0\0\002\0\0\0\x55\x01\0\0\0\0\0\0' "Hello World" > $@ + +main.o: data.h + +data.c data.h: data.dst + ${DESTRUCT} -e ${ENDIAN} -H data.h -o data.c data.dst + blob - /dev/null blob + df2005624e42afc83c0af2a277981ccc0722b10a (mode 644) --- /dev/null +++ test/data.dst @@ -0,0 +1,11 @@ +struct test { + name: [u8; 16], + version: u32, + size: u8, + data: union { + byte: u8 if size == 1, + half: u16 if size == 2, + word: u32 if size == 4, + dword: u64 if size == 8, + }, +}; blob - /dev/null blob + e8797078e019b2bfedb5ab8024cacfb234a2bd3f (mode 644) --- /dev/null +++ test/main.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include "data.h" + +int main (void) +{ + struct test test; + uint8_t buffer[sizeof (test)]; + int fd; + + fd = open ("test.bin", O_RDONLY); + if (fd < 0) + err (1, "cannot open test.bin"); + + if (read (fd, buffer, sizeof (buffer)) != sizeof (buffer)) + err (1, "cannot read test.bin"); + + decode_test (&test, buffer); + + printf ("name: \"%.16s\"\n", test.name); + printf ("version: %u\n", (unsigned)test.version); + printf ("size: %u\n", (unsigned)test.size); + + switch (test.size) { + case 1: + printf ("byte: %u\n", (unsigned)test.data.byte); + break; + case 2: + printf ("half: %u\n", (unsigned)test.data.half); + break; + case 4: + printf ("word: %u\n", (unsigned)test.data.word); + break; + case 8: + printf ("dword: %llu\n", (unsigned long long)test.data.dword); + break; + } + + + close (fd); +}