]> pd.if.org Git - lice/blobdiff - tests/typeof.c
autocommit for files dated 2014-11-17 20:13:38
[lice] / tests / typeof.c
diff --git a/tests/typeof.c b/tests/typeof.c
new file mode 100644 (file)
index 0000000..91f1563
--- /dev/null
@@ -0,0 +1,56 @@
+// typeof keyword
+
+int main(void) {
+    // basic usage of it
+    typeof(int) a = 1024;
+    expecti(a, 1024);
+
+    typeof(a) b = 2048;
+    expecti(b, 2048);
+
+    __typeof__(int) aa = 1024;
+    expecti(aa, 1024);
+
+    __typeof__(aa) bb = 2048;
+    expecti(bb, 2048);
+
+
+    // arrays?
+    char c[] = "hello";
+    typeof(c) d = "world";
+
+    expectstr(d, "world");
+    expecti(sizeof(d), 6);
+
+    typeof(typeof (char *)[4]) odd;
+    expecti(sizeof(odd)/sizeof(*odd), 4);
+
+
+    char cc[] = "hello";
+    __typeof__(cc) dd = "world";
+
+    expectstr(dd, "world");
+    expecti(sizeof(dd), 6);
+
+    __typeof__(__typeof__ (char *)[4]) oddd;
+    expecti(sizeof(oddd)/sizeof(*oddd), 4);
+
+    // struct union enum
+    typeof(struct { int a; }) __1 = { .a = 1 };
+    typeof(union  { int a; }) __2 = { .a = 1 };
+    typeof(enum   { A1,B2  }) __3 = { B2 };
+
+    expecti(__1.a, 1);
+    expecti(__2.a, 1);
+    expecti(__3,   B2);
+
+    __typeof__(struct { int a;  }) __11 = { .a = 1 };
+    __typeof__(union  { int a;  }) __22 = { .a = 1 };
+    __typeof__(enum   { A11,B22 }) __33 = { B22 };
+
+    expecti(__11.a, 1);
+    expecti(__22.a, 1);
+    expecti(__33,   B22);
+
+    return 0;
+}