From 946bdbe1d5dd89ab671391fbe429a1c2c48ecaa7 Mon Sep 17 00:00:00 2001 From: unknown <> Date: Mon, 17 Nov 2014 20:13:36 +0000 Subject: [PATCH] autocommit for files dated 2014-11-17 20:13:36 --- tests/function.c | 98 ++++++++++++++++++++ tests/global.c | 49 ++++++++++ tests/init.c | 218 +++++++++++++++++++++++++++++++++++++++++++++ tests/int.c | 23 +++++ tests/kandr.c | 22 +++++ tests/line.c | 35 ++++++++ tests/literal.c | 75 ++++++++++++++++ tests/number.c | 43 +++++++++ tests/offsetof.c | 27 ++++++ tests/pointer.c | 29 ++++++ tests/returnaddr.c | 25 ++++++ 11 files changed, 644 insertions(+) create mode 100644 tests/function.c create mode 100644 tests/global.c create mode 100644 tests/init.c create mode 100644 tests/int.c create mode 100644 tests/kandr.c create mode 100644 tests/line.c create mode 100644 tests/literal.c create mode 100644 tests/number.c create mode 100644 tests/offsetof.c create mode 100644 tests/pointer.c create mode 100644 tests/returnaddr.c diff --git a/tests/function.c b/tests/function.c new file mode 100644 index 0000000..19173e5 --- /dev/null +++ b/tests/function.c @@ -0,0 +1,98 @@ +// functions + +int i1() { return 42; } +int i2(void) { return 42; } + +int splat(int a, int b, int c, int d, int e, int f) { + expecti(a, 1); + expecti(b, 2); + expecti(c, 3); + expecti(d, 4); + expecti(e, 5); + expecti(f, 6); +} + +int deref(int *a) { + return *a; +} + +int proto1(); +int proto1() { return 1024; } + +int proto2(int a, int b); +int proto2(int a, int b) { + return a + b; +} + +void proto3(int a, ...); +void proto3(int a, ...) { + expecti(a, 1024); +} + +void ignore(void) { + return; +} + +void ___func___(void) { + expectstr(__func__, "___func___"); +} + +int funptr1(void) { + return 1024; +} + +int funptr2(int a) { + return funptr1() * 2; +} + +float funptr3(float a) { + return a * 2; +} + +int funptr4(int (*callback)(void), int *data) { + *data = callback(); + return *data; +} + +// emptyies +void empty1(void) { + // nothing to see here +} + +void empty2(void) { + // empty semicolons + ;;; +} + +int main(void) { + expecti(i1(), 42); + expecti(i2(), 42); + + int a = 1024; + expecti(deref(&a), 1024); + expecti(proto1(), 1024); + expecti(proto2(512,512), 1024); + + proto3(1024); + splat(1, 2, 3, 4, 5, 6); + + ignore(); + ___func___(); + + // function pointer tests + int (*ptr1)(void) = funptr1; + int (*ptr2)(int) = funptr2; + float (*ptr3)(float) = funptr3; + int (*ptr4)(int(*)(void),int*) = funptr4; + + expecti(ptr1(), 1024); + expecti(ptr2(a), 2048); + expecti(ptr4(ptr1,&a), 1024); + expecti(a, 1024); + expectf(ptr3(3.14), 6.28); + + empty1(); + empty2(); + + return 0; +} diff --git a/tests/global.c b/tests/global.c new file mode 100644 index 0000000..d74bdc0 --- /dev/null +++ b/tests/global.c @@ -0,0 +1,49 @@ +// global variables + +int a = 1024; +int *b = &a; + +int c[10]; +int d[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + +int e, f; +int g, h = 10; +int i = 11, j; + +char k[] = "hello"; +char *l = "world"; + +long m = 32; +int *n = &(int) { 64 }; + +int main(void) { + expecti(a, 1024); + + // can write to global? + a = 2048; + expecti(a, 2048); + expecti(*b, 2048); + + c[1] = 2; + expecti(c[1], 2); + expecti(d[1], 2); + + e = 7; + f = 8; + expecti(e, 7); + expecti(f, 8); + g = 9; + expecti(g, 9); + expecti(h, 10); + expecti(i, 11); + j = 12; + expecti(j, 12); + + expectstr(k, "hello"); + expectstr(l, "world"); + + expectl(m, 32); + expectl(*n, 64); + + return 0; +} diff --git a/tests/init.c b/tests/init.c new file mode 100644 index 0000000..17c4b53 --- /dev/null +++ b/tests/init.c @@ -0,0 +1,218 @@ +// initializers + +void testi(int *recieved, int *wanted, int length) { + for (int i = 0; i < length; i++) + expecti(recieved[i], wanted[i]); +} + +void tests(short *recieved, short *wanted, int length) { + for (int i = 0; i < length; i++) + expects(recieved[i], wanted[i]); +} + +void testarray(void) { + int a[] = { 1, 2, 3 }; + int b[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int c[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; + int d[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int e[] = { 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0 }; + int f[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } }; + + expecti(a[0], 1); + expecti(a[1], 2); + expecti(a[2], 3); + + testi(b, c, 9); + testi(c, d, 9); + testi(e, f, 12); + + short g[] = { + 1, 0, 0, 0, 0, + 0, 2, 3, 0, 0, + 0, 0, 4, 5, 6 + }; + + short h[4][3][2] = { + { 1 }, + { 2, 3 }, + { 4, 5, 6 } + }; + + tests(g, h, 15); +} + +void teststruct(void) { + int a[] = { + 1, 0, 0, 0, + 2, 0, 0, 0 + }; + + struct { + int a[3]; + int b; + } data[] = { { 1 }, 2 }; + + testi(&data, a, 8); +} + +void testnesting(void) { + struct { + struct { + struct { + int a; + int b; + } a; + + struct { + char a[8]; + } b; + } a; + } data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + expecti(data.a.a.a, 1); + expecti(data.a.a.b, 2); + expecti(data.a.b.a[0], 3); + expecti(data.a.b.a[1], 4); + expecti(data.a.b.a[2], 5); + expecti(data.a.b.a[3], 6); + expecti(data.a.b.a[4], 7); + expecti(data.a.b.a[5], 8); + expecti(data.a.b.a[6], 9); + expecti(data.a.b.a[7], 10); +} + +void testdesignated(void) { + struct { int a, b, c; } a = { + .a = 1024, + .b = 2048, + .c = 4096, + }; + + struct { int a, b, c; } b = { + .c = 4096, + .b = 2048, + .a = 1024 + }; + + struct { int a, b, c; } c = { + .b = 2048, + 4096, // should be c + .a = 1024 + }; + + expecti(a.a, 1024); + expecti(a.b, 2048); + expecti(a.c, 4096); + expecti(b.a, 1024); + expecti(b.b, 2048); + expecti(b.c, 4096); + expecti(c.a, 1024); + expecti(c.b, 2048); + expecti(c.c, 4096); + + // array designated initializers + int array[3] = { [1] = 1024 }; + expecti(array[0], 0); + expecti(array[1], 1024); + expecti(array[2], 0); + + // for structs too + struct { + int a, b; + } data[2] = { [1] = { 1, 2 } }; + + expecti(data[1].a, 1); + expecti(data[1].b, 2); + expecti(data[0].a, 0); + expecti(data[0].b, 0); + + // splatted too + struct { + int a, b; + } data2[3] = { [1] = 10, 20, 30, 40 }; + expecti(data2[0].a, 0); + expecti(data2[0].b, 0); + expecti(data2[1].a, 10); + expecti(data2[1].b, 20); + expecti(data2[2].a, 30); + expecti(data2[2].b, 40); + + struct { + struct { + int a, b; + } c[3]; + } again[] = { + [1].c[0].b = 10, 20, 30, 40, 50, + [0].c[2].b = 60, 70 + }; + expecti(10, again[1].c[0].b); + expecti(20, again[1].c[1].a); + expecti(30, again[1].c[1].b); + expecti(40, again[1].c[2].a); + expecti(50, again[1].c[2].b); + expecti(60, again[0].c[2].b); + expecti(70, again[1].c[0].a); + + int A[][3] = { [0][0] = 10, [1][0] = 20 }; + struct { int a, b[3]; } B = { .a = 10, .b[0] = 20, .b[1] = 30 }; + + expecti(A[0][0], 10); + expecti(A[1][0], 20); + expecti(B.a, 10); + expecti(B.b[0], 20); + expecti(B.b[1], 30); + expecti(B.b[2], 0); +} + +void testorder(void) { + int order[4] = { [2] = 30, [0] = 10, 20 }; + + expecti(order[0], 10); + expecti(order[1], 20); + expecti(order[2], 30); +} + +void testzero(void) { + struct { int a, b; } a = { 1024 }; + struct { int a, b; } b = { .b = 1024 }; + + expecti(a.a, 1024); + expecti(a.b, 0); + expecti(b.a, 0); + expecti(b.b, 1024); +} + +void testtypedef(void) { + typedef int array[]; + array a = { 1, 2, 3 }; + array b = { 4, 5, 6 }; + + expecti(sizeof(a) / sizeof(*a), 3); + expecti(sizeof(b) / sizeof(*b), 3); + + expecti(a[0], 1); + expecti(a[1], 2); + expecti(a[2], 3); + expecti(b[0], 4); + expecti(b[1], 5); + expecti(b[2], 6); +} + +int main(void) { + testarray(); + testnesting(); + teststruct(); + testdesignated(); + testzero(); + testtypedef(); + testorder(); + + int primitive = { 1024 }; + expecti(primitive, 1024); + + // test odd string initializers + char f[] = "abc"; expectstr(f, "abc"); + char g[] = { "cba" }; expectstr(g, "cba"); + + return 0; +} diff --git a/tests/int.c b/tests/int.c new file mode 100644 index 0000000..4fb2f5a --- /dev/null +++ b/tests/int.c @@ -0,0 +1,23 @@ +// integers + +int main(void) { + short a = 100; + short int b = 150; + long c = 2048; + long int d = 4096; + int e = 214748364; + + expects(a + b, 250); + expects(a + 100, 200); + + expectl(c, 2048); + expectl(d * 2, 8192); + expectl(100L, 100L); + + expectl(922337203685477807, 922337203685477807); + expectl(922337203685477806 + 1, 922337203685477807); + + expecti(e, 214748364); + + return 0; +} diff --git a/tests/kandr.c b/tests/kandr.c new file mode 100644 index 0000000..3185d9c --- /dev/null +++ b/tests/kandr.c @@ -0,0 +1,22 @@ +// K&R C + +#pragma warning_disable + +// implicit integer +function() { + return 4; +} + +int main(void) { + expecti(function(), 4); + expecti(defined_later(), 1337); +} + +defined_later() { + // this is defined later, but main can call into it + // since K&R doesn't care about declarations + + return 1337; +} + +#pragma warning_enable diff --git a/tests/line.c b/tests/line.c new file mode 100644 index 0000000..75921bf --- /dev/null +++ b/tests/line.c @@ -0,0 +1,35 @@ +// line continuation + +int \ +main \ +( \ + void // \ + \ + // one empty space to compensate +) { \ + int \ + a \ + = \ + 0, \ + *b \ + = \ + &a; \ + + expecti(*b, a); // line continuation should make the above + // int a=0,*b=&a; + + /* \ + * \ + * \\ int *e=b; / + */ + + // would error if int *e made it as code + int e = 0; // \ + comments \ + comments \ + e = 1; + + expecti(e, 0); // error if e = 1 made it + + return e; // e == 0 (success) +} diff --git a/tests/literal.c b/tests/literal.c new file mode 100644 index 0000000..201710f --- /dev/null +++ b/tests/literal.c @@ -0,0 +1,75 @@ +// literals + +struct s1 { + int a; +}; + +struct s2 { + int a; + struct s3 { + int a[2]; + } *b; +}; + + +int a = (int) { 42 }; +int *b = &(int) { 42 }; +struct s1 *c = &(struct s1) { 42 }; +struct s2 *d = &(struct s2) { 42 , &(struct s3) { 42, 42 } }; + +int main(void) { + expecti('a', 97); + expecti('A', 65); + + //expecti('\0', 0); + expecti('\a', 7); + expecti('\b', 8); + expecti('\t', 9); + expecti('\n', 10); + expecti('\v', 11); + expecti('\f', 12); + expecti('\r', 13); + + expecti('\7', 7); + expecti('\17', 15); + expecti('\235', -99); + + expecti('\x0', 0); + expecti('\xff', -1); + expecti('\x012', 18); + + + expectstr("hello world", "hello world"); + expecti("hello world"[5], ' '); + expecti("hello world"[11], 0); // null terminator + + expectstr( + (char []) { 'h', 'e', 'l', 'l', 'o', 0 }, + "hello" + ); + + expecti(L'c', 'c'); + expectstr(L"butt", "butt"); + + // lexer could fail for 'L' so we need to ensure that it can be + // used as an identifier still. + int L = 1337; + expecti(L, 1337); + int L1 = L; + expecti(L1, L); + + expecti(a, 42); + expecti(*b, 42); + expecti(c->a, 42); + expecti(d->a, 42); + expecti(d->b->a[0], 42); + expecti(d->b->a[1], 42); + + // universal (unicode / host defined) + expecti(L'\u0024', '$'); + expecti(L'\U00000024', '$'); + expectstr("\u0024", "$"); + expectstr("\U00000024", "$"); + + return 0; +} diff --git a/tests/number.c b/tests/number.c new file mode 100644 index 0000000..895e22f --- /dev/null +++ b/tests/number.c @@ -0,0 +1,43 @@ +// numeric constants + +int main(void) { + expecti(0x1, 1); + expecti(0xf, 15); + expecti(0xF, 15); + + expecti(3L, 3); + expecti(3LL, 3); + expecti(3UL, 3); + expecti(3LU, 3); + expecti(3ULL, 3); + expecti(3LU, 3); + expecti(3LLU, 3); + expecti(3l, 3); + expecti(3ll, 3); + expecti(3ul, 3); + expecti(3lu, 3); + expecti(3ull, 3); + expecti(3lu, 3); + expecti(3llu, 3); + + expectf(1.0f, 1.0); + expectf(1.2f, 1.2); + expectf(1.0f, 1.0f); + expectf(1.2f, 1.2f); + + expectd(3.14159265, 3.14159265); + expectd(2e2, 200.0); + expectd(1.55e1, 15.5); + expectd(0x0.DE488631p8, 0xDE.488631); + + expectl(0xFL, 15L); + expectl(0xFULL, 15ULL); + + expecti(0b1011, 11); + expecti(0xe0, 224); + expecti(0xE0, 224); + + expecti(sizeof(0xe0), 4); // should be integer type + + return 0; +} diff --git a/tests/offsetof.c b/tests/offsetof.c new file mode 100644 index 0000000..bbc4ee0 --- /dev/null +++ b/tests/offsetof.c @@ -0,0 +1,27 @@ +// offsetof + +#include + +struct test { + int a; // 0 + int b; // 4 + int c; // 8 + char d; // 12 + short e; // 14 + int f; // 16 +}; + +int main(void) { + expecti(offsetof(struct test, a), 0); + expecti(offsetof(struct test, b), 4); + expecti(offsetof(struct test, c), 8); + expecti(offsetof(struct test, d), 12); + expecti(offsetof(struct test, e), 14); + expecti(offsetof(struct test, f), 16); + + // it should also be a valid integer constant expression + int a[offsetof(struct test, b)]; + expecti(sizeof(a), sizeof(int) * 4); + + return 0; +} diff --git a/tests/pointer.c b/tests/pointer.c new file mode 100644 index 0000000..d734f78 --- /dev/null +++ b/tests/pointer.c @@ -0,0 +1,29 @@ +// pointers + +int main(void) { + int a = 1024; + int *b = &a; + char *c = "hi"; + char *d = "hi" + 1; + char e[] = "abc"; + char *f = e + 2; + char g[] = "abc"; + + *g = 32; + expecti(*b, 1024); + expecti(*c, 'h'); + expecti(*d, 'i'); + expecti(*f, 'c'); + expecti(*g, 32); + + int aa[] = { 1, 2, 3 }; + int *p = aa; + expecti(*p, 1); p++; + expecti(*p, 2); p++; + expecti(*p, 3); + expecti(*p, 3); p--; + expecti(*p, 2); p--; + expecti(*p, 1); + + return 0; +} diff --git a/tests/returnaddr.c b/tests/returnaddr.c new file mode 100644 index 0000000..f4c2cc0 --- /dev/null +++ b/tests/returnaddr.c @@ -0,0 +1,25 @@ +// __builtin_return_address + +static void *a(void) { + return __builtin_return_address(1); +} + +static void *b(void) { + expecti(__builtin_return_address(0), a()); + return __builtin_return_address(0); +} + +static void *c(void) { +_1: + void *this = b(); +_2: + expecti((&&_1 < this && this <= &&_2), 1); + + return this; +} + +int main(void) { + expecti(c() <= b(), 1); + + return 0; +} -- 2.40.0