This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
test for strncpy
- From: Joern Rennecke <joern dot rennecke at superh dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 25 Sep 2003 18:35:11 +0100 (BST)
- Subject: test for strncpy
I've written a test for strncpy - using the strcpy one as a starting point.
Unfortunately, there are more variables to consider, so this test takes
1.7 seconds natively on a 1.4 GHz Athlon; 6 minutes 13 seconds to execute
it under the sh64-elf simulator on the same machine for the -O0 version.
The -O and -O2 versions takes 1 minute 16 seconds and 1 minute 13 seconds,
respectively.
So, should we run this test only when optimizing?
*** /dev/null Thu Aug 30 21:30:55 2001
--- gcc/testsuite/gcc.c-torture/execute/strncpy-1.c Thu Sep 25 17:56:49 2003
***************
*** 0 ****
--- 1,84 ----
+ /* Copyright (C) 2003 Free Software Foundation.
+
+ Test strncpy with various combinations of pointer alignments and lengths to
+ make sure any optimizations in the library are correct. */
+
+ #include <string.h>
+
+ #ifndef MAX_OFFSET
+ #define MAX_OFFSET (sizeof (long long))
+ #endif
+
+ #ifndef MAX_COPY
+ #define MAX_COPY (10 * sizeof (long long))
+ #endif
+
+ #ifndef MAX_EXTRA
+ #define MAX_EXTRA (sizeof (long long))
+ #endif
+
+ #define MAX_LENGTH (MAX_OFFSET + MAX_COPY + 1 + 2 * MAX_OFFSET + MAX_EXTRA)
+
+ /* Use a sequence length that is not divisible by two, to make it more
+ likely to detect when words are mixed up. */
+ #define SEQUENCE_LENGTH 31
+
+ static union {
+ char buf[MAX_LENGTH];
+ long long align_int;
+ long double align_fp;
+ } u1, u2;
+
+ main ()
+ {
+ int off1, off2, off3, len, max, capped_len, i;
+ char *p, *q, c;
+
+ for (off1 = 0; off1 < MAX_OFFSET; off1++)
+ for (off2 = 0; off2 < MAX_OFFSET; off2++)
+ for (off3 = 0; off3 < MAX_OFFSET; off3++)
+ for (len = 1; len < MAX_COPY; len++)
+ for (max = len - MAX_OFFSET; max <= len + 2 * (int) MAX_OFFSET; max++)
+ {
+ if (max < 0)
+ max = 0;
+ for (i = 0, c = 'A'; i < MAX_LENGTH; i++, c++)
+ {
+ u1.buf[i] = 'a';
+ if (c >= 'A' + SEQUENCE_LENGTH)
+ c = 'A';
+ u2.buf[i] = c;
+ }
+ u2.buf[off2 + len] = '\0';
+ u2.buf[off2 + len + off3] = '\0';
+
+ p = strncpy (u1.buf + off1, u2.buf + off2, max);
+ if (p != u1.buf + off1)
+ abort ();
+
+ q = u1.buf;
+ for (i = 0; i < off1; i++, q++)
+ if (*q != 'a')
+ abort ();
+ capped_len = len;
+ if (capped_len > max)
+ capped_len = max;
+
+ for (i = 0, c = 'A' + off2; i < capped_len; i++, q++, c++)
+ {
+ if (c >= 'A' + SEQUENCE_LENGTH)
+ c = 'A';
+ if (*q != c)
+ abort ();
+ }
+
+ for (i = len; i < max; i++)
+ if (*q++ != '\0')
+ abort ();
+ for (i = 0; i < MAX_EXTRA; i++, q++)
+ if (*q != 'a')
+ abort ();
+ }
+
+ exit (0);
+ }