diff mbox series

[RFC,v4,4/4] testsuite: Add tests for __lengthof__

Message ID 20240806122218.3827577-5-alx@kernel.org
State New
Headers show
Series c: Add __lengthof__ operator | expand

Commit Message

Alejandro Colomar Aug. 6, 2024, 12:22 p.m. UTC
I've compiled those files manually, and they behave as expected.  But
within the test-suite, they don't seem to work:

	FAIL: gcc.dg/lengthof-compile.c (test for excess errors)
	FAIL: gcc.dg/lengthof.c (test for excess errors)

Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
 gcc/testsuite/gcc.dg/lengthof-compile.c |  48 +++++++++
 gcc/testsuite/gcc.dg/lengthof.c         | 126 ++++++++++++++++++++++++
 2 files changed, 174 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/lengthof-compile.c
 create mode 100644 gcc/testsuite/gcc.dg/lengthof.c
diff mbox series

Patch

diff --git a/gcc/testsuite/gcc.dg/lengthof-compile.c b/gcc/testsuite/gcc.dg/lengthof-compile.c
new file mode 100644
index 00000000000..b5ca8978a99
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/lengthof-compile.c
@@ -0,0 +1,48 @@ 
+/* { dg-do compile } */
+
+extern int x[];
+
+void
+incomplete(int p[])
+{
+  unsigned n;
+
+  n = __lengthof__(x);  /* { dg-error "incomplete" } */
+
+  /* We want to support the following one in the future,
+     but for now it should fail.  */
+  n = __lengthof__(p);  /* { dg-error "invalid" } */
+}
+
+void
+fam(void)
+{
+  struct {
+    int x;
+    int fam[];
+  } s;
+  unsigned n;
+
+  n = __lengthof__(s.fam); /* { dg-error "incomplete" } */
+}
+
+void fix_fix(int i, char (*a)[3][5], int (*x)[__lengthof__(*a)]);
+void fix_var(int i, char (*a)[3][i], int (*x)[__lengthof__(*a)]);
+void fix_uns(int i, char (*a)[3][*], int (*x)[__lengthof__(*a)]);
+
+void
+func(void)
+{
+  int  i3[3];
+  int  i5[5];
+  char c35[3][5];
+
+  fix_fix(5, &c35, &i3);
+  fix_fix(5, &c35, &i5); /* { dg-error "incompatible-pointer-types" } */
+
+  fix_var(5, &c35, &i3);
+  fix_var(5, &c35, &i5); /* { dg-error "incompatible-pointer-types" } */
+
+  fix_uns(5, &c35, &i3);
+  fix_uns(5, &c35, &i5); /* { dg-error "incompatible-pointer-types" } */
+}
diff --git a/gcc/testsuite/gcc.dg/lengthof.c b/gcc/testsuite/gcc.dg/lengthof.c
new file mode 100644
index 00000000000..6aec558749c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/lengthof.c
@@ -0,0 +1,126 @@ 
+/* { dg-do run } */
+
+#undef NDEBUG
+#include <assert.h>
+
+void
+array(void)
+{
+  short a[7];
+
+  assert(__lengthof__(a) == 7);
+  assert(__lengthof__(long [0]) == 0);
+  assert(__lengthof__(unsigned [99]) == 99);
+}
+
+void
+vla(void)
+{
+  unsigned n;
+
+  n = 99;
+  assert(__lengthof__(short [n - 10]) == 99 - 10);
+
+  int v[n / 2];
+  assert(__lengthof__(v) == 99 / 2);
+
+  n = 0;
+  int z[n];
+  assert(__lengthof__(z) == 0);
+}
+
+void
+member(void)
+{
+  struct {
+    int a[8];
+  } s;
+
+  assert(__lengthof__(s.a) == 8);
+}
+
+void
+vla_eval(void)
+{
+  int i;
+
+  i = 7;
+  assert(__lengthof__(struct {int x;}[i++]) == 7);
+  assert(i == 7 + 1);
+
+  int v[i];
+  int (*p)[i];
+  p = &v;
+  assert(__lengthof__(*p++) == i);
+  assert(p - 1 == &v);
+}
+
+void
+inner_vla_noeval(void)
+{
+  int i;
+
+  i = 3;
+  assert(__lengthof__(struct {int x[i++];}[3]) == 3);
+  assert(i == 3);
+}
+
+void
+array_noeval(void)
+{
+  long a[5];
+  long (*p)[__lengthof__(a)];
+
+  p = &a;
+  assert(__lengthof__(*p++) == 5);
+  assert(p == &a);
+}
+
+void
+matrix_zero(void)
+{
+  int i;
+
+  assert(__lengthof__(int [0][4]) == 0);
+  i = 3;
+  assert(__lengthof__(int [0][i]) == 0);
+}
+
+void
+matrix_fixed(void)
+{
+  int i;
+
+  assert(__lengthof__(int [7][4]) == 7);
+  i = 3;
+  assert(__lengthof__(int [7][i]) == 7);
+}
+
+void
+matrix_vla(void)
+{
+  int i, j;
+
+  i = 7;
+  assert(__lengthof__(int [i++][4]) == 7);
+  assert(i == 7 + 1);
+
+  i = 9;
+  j = 3;
+  assert(__lengthof__(int [i++][j]) == 9);
+  assert(i == 9 + 1);
+}
+
+int
+main(void)
+{
+  array();
+  vla();
+  member();
+  vla_eval();
+  inner_vla_noeval();
+  array_noeval();
+  matrix_zero();
+  matrix_fixed();
+  matrix_vla();
+}