]> git.sur5r.net Git - cc65/blob - test/val/pointed-array.c
src/ld65/exports.c: Issue an error instead of a warning for duplicate global symbols.
[cc65] / test / val / pointed-array.c
1 /*
2 ** !!DESCRIPTION!! Simple tests of pointer-to-array dereferences
3 ** !!ORIGIN!!      cc65 regression tests
4 ** !!LICENCE!!     Public Domain
5 ** !!AUTHOR!!      2015-06-29, Greg King
6 */
7
8 #include <stdio.h>
9
10 static unsigned char failures = 0;
11 static size_t Size;
12
13 typedef unsigned char array_t[4][4];
14
15 static array_t table = {
16     {12, 13, 14, 15},
17     { 8,  9, 10, 11},
18     { 4,  5,  6,  7},
19     { 0,  1,  2,  3}
20 };
21 static array_t *tablePtr = &table;
22
23 static unsigned (*vector)[2];
24
25 static unsigned char y = 0, x;
26
27 int main(void)
28 {
29     /* The indirection must convert the expression-type (from Pointer into
30     ** Array); but, it must not convert the value, because it already points
31     ** to the start of the array.
32     */
33     /* (Note:  I reduce output clutter by using a variable to prevent
34     ** compiler warnings about constant comparisons and unreachable code.
35     */
36     if ((Size = sizeof *tablePtr) != sizeof table) {
37         ++failures;
38     }
39     if (*tablePtr != table) {
40         ++failures;
41     }
42
43     /* Test fetching. */
44     do {
45         x = 0;
46         do {
47             if ((*tablePtr)[y][x] != table[y][x]) {
48                 ++failures;
49                 printf("(*tableptr)[%u][%u] (%u) != table[%u][%u] (%u).\n",
50                        y, x, (*tablePtr)[y][x],
51                        y, x, table[y][x]);
52             }
53         } while (++x < sizeof table[0]);
54     } while (++y < sizeof table / sizeof table[0]);
55
56     vector = (unsigned (*)[])table[1];
57     if ((*vector)[1] != 0x0B0A) {
58         ++failures;
59     }
60
61     /* Test storing. */
62     (*tablePtr)[2][1] = 42;
63     if (table[2][1] != 42) {
64         ++failures;
65         printf("table[2][1] == %u (should have changed from 5 to 42).\n",
66                table[2][1]);
67     }
68     x = 3;
69     y = 1;
70     (*tablePtr)[y][x] = 83;
71     if (table[1][3] != 83) {
72         ++failures;
73         printf("table[y][x] == %u (should have changed from 11 to 83).\n",
74                table[1][3]);
75     }
76
77     /* Test triple indirection.  It should compile to two indirection
78     ** operations.
79     */
80     --***tablePtr;
81     if (**table != 11) {
82         ++failures;
83         printf("**table == %u (should have changed from 12 to 11).\n",
84                table[0][0]);
85     }
86
87     if (failures != 0) {
88         printf("failures: %u\n", failures);
89     }
90     return failures;
91 }