aboutsummaryrefslogtreecommitdiff
path: root/string
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>2002-11-08 22:10:01 +0000
committerRoland McGrath <roland@gnu.org>2002-11-08 22:10:01 +0000
commite8c1660f7ba480c5a17103b557941fc6db7a5146 (patch)
tree71673df7864e35860bd8c7e8d2f3424fdb1dffc9 /string
parente0bc9a8d13290de5328ae49311493246f406e0bd (diff)
downloadglibc-e8c1660f7ba480c5a17103b557941fc6db7a5146.zip
glibc-e8c1660f7ba480c5a17103b557941fc6db7a5146.tar.gz
glibc-e8c1660f7ba480c5a17103b557941fc6db7a5146.tar.bz2
* string/test-strchr.c (stupid_strchr): New function.
(do_random_tests): Make sure the string is zero terminated. * string/test-strpbrk.c (stupid_strpbrk): New function. (do_random_tests): Make sure the string is zero terminated. * string/test-strcmp.c (stupid_strcmp): New function. (do_random_tests): Make sure the strings are zero terminated. * string/test-strspn.c (stupid_strspn): New function. (simple_strspn): Rename rej argument to acc. (do_random_tests): Make sure the string is zero terminated. * string/test-strcspn.c (stupid_strcspn): New function. * string/test-strncpy.c (stupid_strncpy): New function. * string/test-stpncpy.c (stupid_stpncpy): New function. * string/test-strncmp.c (stupid_strncmp): New function. (do_random_tests): Make sure the strings are zero terminated. * string/test-string.h (impl_t): Change test into long. (IMPL): Add __attribute__((aligned (sizeof (void *)))).
Diffstat (limited to 'string')
-rw-r--r--string/test-stpncpy.c15
-rw-r--r--string/test-strchr.c24
-rw-r--r--string/test-strcmp.c57
-rw-r--r--string/test-strcspn.c17
-rw-r--r--string/test-string.h6
-rw-r--r--string/test-strncmp.c56
-rw-r--r--string/test-strncpy.c15
-rw-r--r--string/test-strpbrk.c44
-rw-r--r--string/test-strspn.c63
9 files changed, 214 insertions, 83 deletions
diff --git a/string/test-stpncpy.c b/string/test-stpncpy.c
index 5892c68..5bcb1dd 100644
--- a/string/test-stpncpy.c
+++ b/string/test-stpncpy.c
@@ -23,7 +23,9 @@
#include "test-string.h"
char *simple_stpncpy (char *, const char *, size_t);
+char *stupid_stpncpy (char *, const char *, size_t);
+IMPL (stupid_stpncpy, 0)
IMPL (simple_stpncpy, 0)
IMPL (stpncpy, 1)
@@ -42,4 +44,17 @@ simple_stpncpy (char *dst, const char *src, size_t n)
return dst;
}
+char *
+stupid_stpncpy (char *dst, const char *src, size_t n)
+{
+ size_t ns = strlen (src);
+ size_t i, nc = n < ns ? n : ns;
+
+ for (i = 0; i < nc; ++i)
+ dst[i] = src[i];
+ for (; i < n; ++i)
+ dst[i] = '\0';
+ return dst + nc;
+}
+
#include "test-strncpy.c"
diff --git a/string/test-strchr.c b/string/test-strchr.c
index 72f7ec9..9483e71 100644
--- a/string/test-strchr.c
+++ b/string/test-strchr.c
@@ -23,7 +23,9 @@
typedef char *(*proto_t) (const char *, int);
char *simple_strchr (const char *, int);
+char *stupid_strchr (const char *, int);
+IMPL (stupid_strchr, 0)
IMPL (simple_strchr, 0)
IMPL (strchr, 1)
@@ -36,6 +38,17 @@ simple_strchr (const char *s, int c)
return (char *) s;
}
+char *
+stupid_strchr (const char *s, int c)
+{
+ size_t n = strlen (s) + 1;
+
+ while (n--)
+ if (*s++ == (char) c)
+ return (char *) s - 1;
+ return NULL;
+}
+
static void
do_one_test (impl_t *impl, const char *s, int c, char *exp_res)
{
@@ -115,15 +128,18 @@ do_random_tests (void)
{
align = random () & 15;
pos = random () & 511;
+ seek_char = random () & 255;
if (pos + align >= 511)
pos = 510 - align - (random () & 7);
len = random () & 511;
- if (pos >= len)
- len = pos + (random () & 7);
+ if ((pos == len && seek_char)
+ || (pos > len && (random () & 1)))
+ len = pos + 1 + (random () & 7);
if (len + align >= 512)
len = 511 - align - (random () & 7);
- seek_char = random () & 255;
- j = len + align + 64;
+ if (pos == len && seek_char)
+ len = pos + 1;
+ j = (pos > len ? pos : len) + align + 64;
if (j > 512)
j = 512;
diff --git a/string/test-strcmp.c b/string/test-strcmp.c
index bfc147b..dbbe3a4 100644
--- a/string/test-strcmp.c
+++ b/string/test-strcmp.c
@@ -23,7 +23,9 @@
typedef int (*proto_t) (const char *, const char *);
int simple_strcmp (const char *, const char *);
+int stupid_strcmp (const char *, const char *);
+IMPL (stupid_strcmp, 0)
IMPL (simple_strcmp, 0)
IMPL (strcmp, 1)
@@ -37,6 +39,19 @@ simple_strcmp (const char *s1, const char *s2)
return ret;
}
+int
+stupid_strcmp (const char *s1, const char *s2)
+{
+ size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
+ size_t n = ns1 < ns2 ? ns1 : ns2;
+ int ret = 0;
+
+ while (n--)
+ if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0)
+ break;
+ return ret;
+}
+
static void
do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
{
@@ -110,7 +125,7 @@ do_test (size_t align1, size_t align2, size_t len, int max_char,
static void
do_random_tests (void)
{
- size_t i, j, n, align1, align2, pos, len;
+ size_t i, j, n, align1, align2, pos, len1, len2;
int result, r;
unsigned char *p1 = buf1 + page_size - 512;
unsigned char *p2 = buf2 + page_size - 512;
@@ -123,22 +138,25 @@ do_random_tests (void)
else
align2 = align1 + (random () & 24);
pos = random () & 511;
- j = align1;
- if (align2 > j)
- j = align2;
+ j = align1 > align2 ? align1 : align2;
if (pos + j >= 511)
pos = 510 - j - (random () & 7);
- len = random () & 511;
- if (pos >= len)
- len = pos + (random () & 7);
- if (len + j >= 512)
- len = 511 - j - (random () & 7);
- j = len + align1 + 64;
- if (j > 512) j = 512;
+ len1 = random () & 511;
+ if (pos >= len1 && (random () & 1))
+ len1 = pos + (random () & 7);
+ if (len1 + j >= 512)
+ len1 = 511 - j - (random () & 7);
+ if (pos >= len1)
+ len2 = len1;
+ else
+ len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
+ j = (pos > len2 ? pos : len2) + align1 + 64;
+ if (j > 512)
+ j = 512;
for (i = 0; i < j; ++i)
{
p1[i] = random () & 255;
- if (i < len + align1 && !p1[i])
+ if (i < len1 + align1 && !p1[i])
{
p1[i] = random () & 255;
if (!p1[i])
@@ -148,7 +166,7 @@ do_random_tests (void)
for (i = 0; i < j; ++i)
{
p2[i] = random () & 255;
- if (i < len + align2 && !p2[i])
+ if (i < len2 + align2 && !p2[i])
{
p2[i] = random () & 255;
if (!p2[i])
@@ -158,12 +176,7 @@ do_random_tests (void)
result = 0;
memcpy (p2 + align2, p1 + align1, pos);
- if (pos >= len)
- {
- p1[len + align1] = 0;
- p2[len + align2] = 0;
- }
- else
+ if (pos < len1)
{
if (p2[align2 + pos] == p1[align1 + pos])
{
@@ -177,6 +190,8 @@ do_random_tests (void)
else
result = 1;
}
+ p1[len1 + align1] = 0;
+ p2[len2 + align2] = 0;
FOR_EACH_IMPL (impl, 1)
{
@@ -185,8 +200,8 @@ do_random_tests (void)
|| (r < 0 && result >= 0)
|| (r > 0 && result <= 0))
{
- error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p",
- n, impl->name, align1, align2, len, pos, r, result, p1, p2);
+ error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p",
+ n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
ret = 1;
}
}
diff --git a/string/test-strcspn.c b/string/test-strcspn.c
index 0fd65cd..b563179 100644
--- a/string/test-strcspn.c
+++ b/string/test-strcspn.c
@@ -25,7 +25,9 @@
typedef size_t (*proto_t) (const char *, const char *);
size_t simple_strcspn (const char *, const char *);
+size_t stupid_strcspn (const char *, const char *);
+IMPL (stupid_strcspn, 0)
IMPL (simple_strcspn, 0)
IMPL (strcspn, 1)
@@ -34,7 +36,7 @@ simple_strcspn (const char *s, const char *rej)
{
const char *r, *str = s;
char c;
-
+
while ((c = *s++) != '\0')
for (r = rej; *r != '\0'; ++r)
if (*r == c)
@@ -42,4 +44,17 @@ simple_strcspn (const char *s, const char *rej)
return s - str - 1;
}
+size_t
+stupid_strcspn (const char *s, const char *rej)
+{
+ size_t ns = strlen (s), nrej = strlen (rej);
+ size_t i, j;
+
+ for (i = 0; i < ns; ++i)
+ for (j = 0; j < nrej; ++j)
+ if (s[i] == rej[j])
+ return i;
+ return i;
+}
+
#include "test-strpbrk.c"
diff --git a/string/test-string.h b/string/test-string.h
index 896b713..ae7fc34 100644
--- a/string/test-string.h
+++ b/string/test-string.h
@@ -22,13 +22,13 @@ typedef struct
{
const char *name;
void (*fn) (void);
- int test;
+ long test;
} impl_t;
extern impl_t __start_impls[], __stop_impls[];
#define IMPL(name, test) \
- impl_t tst_ ## name \
- __attribute__ ((section ("impls"))) \
+ impl_t tst_ ## name \
+ __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
= { #name, (void (*) (void))name, test };
#ifdef TEST_MAIN
diff --git a/string/test-strncmp.c b/string/test-strncmp.c
index b02b987..8881909 100644
--- a/string/test-strncmp.c
+++ b/string/test-strncmp.c
@@ -23,7 +23,9 @@
typedef int (*proto_t) (const char *, const char *, size_t);
int simple_strncmp (const char *, const char *, size_t);
+int stupid_strncmp (const char *, const char *, size_t);
+IMPL (stupid_strncmp, 0)
IMPL (simple_strncmp, 0)
IMPL (strncmp, 1)
@@ -37,6 +39,18 @@ simple_strncmp (const char *s1, const char *s2, size_t n)
return ret;
}
+int
+stupid_strncmp (const char *s1, const char *s2, size_t n)
+{
+ size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
+ int ret = 0;
+
+ n = ns1 < n ? ns1 : n;
+ n = ns2 < n ? ns2 : n;
+ while (n-- && (ret = *(unsigned char *) s1++ - * (unsigned char *) s2++) == 0);
+ return ret;
+}
+
static void
do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
int exp_result)
@@ -117,7 +131,7 @@ do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char,
static void
do_random_tests (void)
{
- size_t i, j, n, align1, align2, pos, len, size;
+ size_t i, j, n, align1, align2, pos, len1, len2, size;
int result, r;
unsigned char *p1 = buf1 + page_size - 512;
unsigned char *p2 = buf2 + page_size - 512;
@@ -131,22 +145,25 @@ do_random_tests (void)
align2 = align1 + (random () & 24);
pos = random () & 511;
size = random () & 511;
- j = align1;
- if (align2 > j)
- j = align2;
+ j = align1 > align2 ? align1 : align2;
if (pos + j >= 511)
pos = 510 - j - (random () & 7);
- len = random () & 511;
- if (pos >= len)
- len = pos + (random () & 7);
- if (len + j >= 512)
- len = 511 - j - (random () & 7);
- j = len + align1 + 64;
- if (j > 512) j = 512;
+ len1 = random () & 511;
+ if (pos >= len1 && (random () & 1))
+ len1 = pos + (random () & 7);
+ if (len1 + j >= 512)
+ len1 = 511 - j - (random () & 7);
+ if (pos >= len1)
+ len2 = len1;
+ else
+ len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
+ j = (pos > len2 ? pos : len2) + align1 + 64;
+ if (j > 512)
+ j = 512;
for (i = 0; i < j; ++i)
{
p1[i] = random () & 255;
- if (i < len + align1 && !p1[i])
+ if (i < len1 + align1 && !p1[i])
{
p1[i] = random () & 255;
if (!p1[i])
@@ -156,7 +173,7 @@ do_random_tests (void)
for (i = 0; i < j; ++i)
{
p2[i] = random () & 255;
- if (i < len + align2 && !p2[i])
+ if (i < len2 + align2 && !p2[i])
{
p2[i] = random () & 255;
if (!p2[i])
@@ -166,12 +183,7 @@ do_random_tests (void)
result = 0;
memcpy (p2 + align2, p1 + align1, pos);
- if (pos >= len)
- {
- p1[len + align1] = 0;
- p2[len + align2] = 0;
- }
- else
+ if (pos < len1)
{
if (p2[align2 + pos] == p1[align1 + pos])
{
@@ -188,6 +200,8 @@ do_random_tests (void)
result = 1;
}
}
+ p1[len1 + align1] = 0;
+ p2[len2 + align2] = 0;
FOR_EACH_IMPL (impl, 1)
{
@@ -196,8 +210,8 @@ do_random_tests (void)
|| (r < 0 && result >= 0)
|| (r > 0 && result <= 0))
{
- error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p",
- n, impl->name, align1, align2, len, pos, size, r, result, p1, p2);
+ error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p",
+ n, impl->name, align1, align2, len1, len2, pos, size, r, result, p1, p2);
ret = 1;
}
}
diff --git a/string/test-strncpy.c b/string/test-strncpy.c
index 5aa5169..11ff30e 100644
--- a/string/test-strncpy.c
+++ b/string/test-strncpy.c
@@ -24,7 +24,9 @@
# include "test-string.h"
char *simple_strncpy (char *, const char *, size_t);
+char *stupid_strncpy (char *, const char *, size_t);
+IMPL (stupid_strncpy, 0)
IMPL (simple_strncpy, 0)
IMPL (strncpy, 1)
@@ -41,6 +43,19 @@ simple_strncpy (char *dst, const char *src, size_t n)
}
return ret;
}
+
+char *
+stupid_strncpy (char *dst, const char *src, size_t n)
+{
+ size_t ns = strlen (src);
+ size_t i, nc = n < ns ? n : ns;
+
+ for (i = 0; i < nc; ++i)
+ dst[i] = src[i];
+ for (; i < n; ++i)
+ dst[i] = '\0';
+ return dst;
+}
#endif
typedef char *(*proto_t) (char *, const char *, size_t);
diff --git a/string/test-strpbrk.c b/string/test-strpbrk.c
index 06832556..77cf56d 100644
--- a/string/test-strpbrk.c
+++ b/string/test-strpbrk.c
@@ -26,7 +26,9 @@
typedef char *(*proto_t) (const char *, const char *);
char *simple_strpbrk (const char *, const char *);
+char *stupid_strpbrk (const char *, const char *);
+IMPL (stupid_strpbrk, 0)
IMPL (simple_strpbrk, 0)
IMPL (strpbrk, 1)
@@ -42,6 +44,19 @@ simple_strpbrk (const char *s, const char *rej)
return (char *) s - 1;
return NULL;
}
+
+char *
+stupid_strpbrk (const char *s, const char *rej)
+{
+ size_t ns = strlen (s), nrej = strlen (rej);
+ size_t i, j;
+
+ for (i = 0; i < ns; ++i)
+ for (j = 0; j < nrej; ++j)
+ if (s[i] == rej[j])
+ return (char *) s + i;
+ return NULL;
+}
#endif
static void
@@ -127,7 +142,7 @@ do_test (size_t align, size_t pos, size_t len)
static void
do_random_tests (void)
{
- size_t i, j, n, align, pos, len;
+ size_t i, j, n, align, pos, len, rlen;
RES_TYPE result;
int c;
unsigned char *p = buf1 + page_size - 512;
@@ -139,12 +154,17 @@ do_random_tests (void)
pos = random () & 511;
if (pos + align >= 511)
pos = 510 - align - (random () & 7);
+ len = random () & 511;
+ if (pos >= len && (random () & 1))
+ len = pos + 1 + (random () & 7);
+ if (len + align >= 512)
+ len = 511 - align - (random () & 7);
if (random () & 1)
- len = random () & 63;
+ rlen = random () & 63;
else
- len = random () & 15;
- rej = buf2 + page_size - len - 1 - (random () & 7);
- for (i = 0; i < len; ++i)
+ rlen = random () & 15;
+ rej = buf2 + page_size - rlen - 1 - (random () & 7);
+ for (i = 0; i < rlen; ++i)
{
rej[i] = random () & 255;
if (!rej[i])
@@ -156,14 +176,16 @@ do_random_tests (void)
for (c = 1; c <= 255; ++c)
if (strchr (rej, c) == NULL)
break;
- j = pos + align + 64;
+ j = (pos > len ? pos : len) + align + 64;
if (j > 512)
j = 512;
for (i = 0; i < j; i++)
{
- if (i == pos + align)
- p[i] = rej[random () % (len + 1)];
+ if (i == len + align)
+ p[i] = '\0';
+ else if (i == pos + align)
+ p[i] = rej[random () % (rlen + 1)];
else if (i < align || i > pos + align)
p[i] = random () & 255;
else
@@ -178,13 +200,13 @@ do_random_tests (void)
}
}
- result = STRPBRK_RESULT (p + align, pos);
+ result = STRPBRK_RESULT (p + align, pos < len ? pos : len);
FOR_EACH_IMPL (impl, 1)
if (CALL (impl, p + align, rej) != result)
{
- error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd) %p != %p",
- n, impl->name, align, rej, len, pos,
+ error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %p != %p",
+ n, impl->name, align, rej, rlen, pos, len,
(void *) CALL (impl, p + align, rej), (void *) result);
ret = 1;
}
diff --git a/string/test-strspn.c b/string/test-strspn.c
index 00e30b4..4a64752 100644
--- a/string/test-strspn.c
+++ b/string/test-strspn.c
@@ -23,19 +23,21 @@
typedef size_t (*proto_t) (const char *, const char *);
size_t simple_strspn (const char *, const char *);
+size_t stupid_strspn (const char *, const char *);
+IMPL (stupid_strspn, 0)
IMPL (simple_strspn, 0)
IMPL (strspn, 1)
size_t
-simple_strspn (const char *s, const char *rej)
+simple_strspn (const char *s, const char *acc)
{
const char *r, *str = s;
char c;
while ((c = *s++) != '\0')
{
- for (r = rej; *r != '\0'; ++r)
+ for (r = acc; *r != '\0'; ++r)
if (*r == c)
break;
if (*r == '\0')
@@ -44,6 +46,23 @@ simple_strspn (const char *s, const char *rej)
return s - str - 1;
}
+size_t
+stupid_strspn (const char *s, const char *acc)
+{
+ size_t ns = strlen (s), nacc = strlen (acc);
+ size_t i, j;
+
+ for (i = 0; i < ns; ++i)
+ {
+ for (j = 0; j < nacc; ++j)
+ if (s[i] == acc[j])
+ break;
+ if (j == nacc)
+ return i;
+ }
+ return i;
+}
+
static void
do_one_test (impl_t *impl, const char *s, const char *acc, size_t exp_res)
{
@@ -115,7 +134,7 @@ do_test (size_t align, size_t pos, size_t len)
static void
do_random_tests (void)
{
- size_t i, j, n, align, pos, len;
+ size_t i, j, n, align, pos, alen, len;
unsigned char *p = buf1 + page_size - 512;
unsigned char *acc;
@@ -123,17 +142,20 @@ do_random_tests (void)
{
align = random () & 15;
if (random () & 1)
- len = random () & 63;
+ alen = random () & 63;
else
- len = random () & 15;
- if (!len)
+ alen = random () & 15;
+ if (!alen)
pos = 0;
else
pos = random () & 511;
if (pos + align >= 511)
pos = 510 - align - (random () & 7);
- acc = buf2 + page_size - len - 1 - (random () & 7);
- for (i = 0; i < len; ++i)
+ len = random () & 511;
+ if (len + align >= 512)
+ len = 511 - align - (random () & 7);
+ acc = buf2 + page_size - alen - 1 - (random () & 7);
+ for (i = 0; i < alen; ++i)
{
acc[i] = random () & 255;
if (!acc[i])
@@ -142,35 +164,32 @@ do_random_tests (void)
acc[i] = 1 + (random () & 127);
}
acc[i] = '\0';
- j = pos + align + 64;
+ j = (pos > len ? pos : len) + align + 64;
if (j > 512)
j = 512;
for (i = 0; i < j; i++)
{
- if (i == pos + align)
+ if (i == len + align)
+ p[i] = '\0';
+ else if (i == pos + align)
{
- if ((random () & 7) == 0)
+ p[i] = random () & 255;
+ if (strchr (acc, p[i]))
p[i] = '\0';
- else
- {
- p[i] = random () & 255;
- if (strchr (acc, p[i]))
- p[i] = '\0';
- }
}
else if (i < align || i > pos + align)
p[i] = random () & 255;
else
- p[i] = acc [random () % len];
+ p[i] = acc [random () % alen];
}
FOR_EACH_IMPL (impl, 1)
- if (CALL (impl, p + align, acc) != pos)
+ if (CALL (impl, p + align, acc) != (pos < len ? pos : len))
{
- error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd) %zd != %zd",
- n, impl->name, align, acc, len,
- CALL (impl, p + align, acc), pos);
+ error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %zd != %zd",
+ n, impl->name, align, acc, alen, pos, len,
+ CALL (impl, p + align, acc), (pos < len ? pos : len));
ret = 1;
}
}