From 0e6008e9e6dfd312dd6c8f6e4b514728943b6e35 Mon Sep 17 00:00:00 2001 From: Greg King Date: Wed, 1 Jul 2015 06:54:05 -0400 Subject: [PATCH] Added a regression test for pointer-to-array dereferences. --- test/val/pointed-array.c | 91 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 test/val/pointed-array.c diff --git a/test/val/pointed-array.c b/test/val/pointed-array.c new file mode 100644 index 000000000..ddbc1a502 --- /dev/null +++ b/test/val/pointed-array.c @@ -0,0 +1,91 @@ +/* +** !!DESCRIPTION!! Simple tests of pointer-to-array dereferences +** !!ORIGIN!! cc65 regression tests +** !!LICENCE!! Public Domain +** !!AUTHOR!! 2015-06-29, Greg King +*/ + +#include + +static unsigned char failures = 0; +static size_t Size; + +typedef unsigned char array_t[4][4]; + +static array_t table = { + {12, 13, 14, 15}, + { 8, 9, 10, 11}, + { 4, 5, 6, 7}, + { 0, 1, 2, 3} +}; +static array_t *tablePtr = &table; + +static unsigned (*vector)[2]; + +static unsigned char y = 0, x; + +int main(void) +{ + /* The indirection must convert the expression-type (from Pointer into + ** Array); but, it must not convert the value, because it already points + ** to the start of the array. + */ + /* (Note: I reduce output clutter by using a variable to prevent + ** compiler warnings about constant comparisons and unreachable code. + */ + if ((Size = sizeof *tablePtr) != sizeof table) { + ++failures; + } + if (*tablePtr != table) { + ++failures; + } + + /* Test fetching. */ + do { + x = 0; + do { + if ((*tablePtr)[y][x] != table[y][x]) { + ++failures; + printf("(*tableptr)[%u][%u] (%u) != table[%u][%u] (%u).\n", + y, x, (*tablePtr)[y][x], + y, x, table[y][x]); + } + } while (++x < sizeof table[0]); + } while (++y < sizeof table / sizeof table[0]); + + vector = (unsigned (*)[])table[1]; + if ((*vector)[1] != 0x0B0A) { + ++failures; + } + + /* Test storing. */ + (*tablePtr)[2][1] = 42; + if (table[2][1] != 42) { + ++failures; + printf("table[2][1] == %u (should have changed from 5 to 42).\n", + table[2][1]); + } + x = 3; + y = 1; + (*tablePtr)[y][x] = 83; + if (table[1][3] != 83) { + ++failures; + printf("table[y][x] == %u (should have changed from 11 to 83).\n", + table[1][3]); + } + + /* Test triple indirection. It should compile to two indirection + ** operations. + */ + --***tablePtr; + if (**table != 11) { + ++failures; + printf("**table == %u (should have changed from 12 to 11).\n", + table[0][0]); + } + + if (failures != 0) { + printf("failures: %u\n", failures); + } + return failures; +}