diff --git a/Makefile.inc b/Makefile.inc index 9137e2b..f377844 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -1,5 +1,5 @@ -CFLAGS+=-g -pipe -std=c99 -D_POSIX_C_SOURCE=200809L -Wall -Wno-unused-function -Wno-missing-braces -LDFLAGS+=-g +CFLAGS+=-pipe -std=c99 -D_POSIX_C_SOURCE=200809L -Wall -Wno-unused-function -Wno-missing-braces +LDFLAGS+= SRC=$(sort $(wildcard *.c)) OBJ=$(SRC:.c=.o) @@ -8,8 +8,7 @@ BINOBJ=$(filter-out %_dso.o,$(OBJ)) DSO=$(DSOOBJ:.o=.so) BIN=$(BINOBJ:.o=) -ROOT=../.. -include $(ROOT)/config.mak +-include ../../config.mak all: $(BIN) $(DSO) run: all diff --git a/src/env/Makefile b/src/env/Makefile deleted file mode 100644 index ee4552b..0000000 --- a/src/env/Makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../Makefile.inc diff --git a/src/env/env.c b/src/env/env.c deleted file mode 100644 index 7962bf5..0000000 --- a/src/env/env.c +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE 1 -#endif -#include -#include -#include -#include -#include "test.h" - -extern char **environ; - -int main() -{ - char *s; - int r; - - if (clearenv() || (environ && *environ)) - error("clrearenv: %s\n", strerror(errno)); - if (putenv("TEST=1")) - error("putenv: %s\n", strerror(errno)); - if ((s=environ[1])) - error("environ[1]: %p, wanted 0\n", s); - if (!(s=getenv("TEST"))) - error("getenv(\"TEST\"): 0, wanted \"1\"\n"); - if (strcmp(s,"1") != 0) - error("getenv(\"TEST\"): \"%s\", wanted \"1\"\n", s); - if (unsetenv("TEST")) - error("unsetenv: %s\n", strerror(errno)); - if ((s=*environ)) - error("*environ: %p != 0\n", s); - if ((s=getenv("TEST"))) - error("getenv(\"TEST\"): %p, wanted 0\n", s); - if (setenv("TEST", "2", 0)) - error("setenv: %s\n", strerror(errno)); - if (strcmp(s=getenv("TEST"),"2") != 0) - error("getenv(\"TEST\"): \"%s\", wanted \"2\"\n", s); - if (setenv("TEST", "3", 0)) - error("setenv: %s\n", strerror(errno)); - if (strcmp(s=getenv("TEST"),"2") != 0) - error("getenv(\"TEST\"): \"%s\", wanted \"2\"\n", s); - if (setenv("TEST", "3", 1)) - error("setenv: %s\n", strerror(errno)); - if (strcmp(s=getenv("TEST"),"3") != 0) - error("getenv(\"TEST\"): \"%s\", wanted \"3\"\n", s); - /* test failures */ - if ((r=setenv("","",0)) != -1 || errno != EINVAL) - error("setenv(\"\",\"\",0): %d, errno: %d (%s), wanted -1, %d (EINVAL)\n", r, errno, strerror(errno), EINVAL); - if ((r=setenv(0,"",0)) != -1 || errno != EINVAL) - error("setenv(0,\"\",0): %d, errno: %d (%s), wanted -1, %d (EINVAL)\n", r, errno, strerror(errno), EINVAL); - return test_status; -} diff --git a/src/env/test.h b/src/env/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/env/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/general/Makefile b/src/general/Makefile new file mode 100644 index 0000000..c6bb180 --- /dev/null +++ b/src/general/Makefile @@ -0,0 +1,8 @@ +include ../../Makefile.inc + +dlopen: LDFLAGS+=-ldl -rdynamic +pthread: LDFLAGS+=-lpthread +sem: LDFLAGS+=-lpthread -lrt +strtod_simple: LDFLAGS+=-lm +tls_align: tls_align_dso.so +tls_align_dlopen: LDFLAGS+=-ldl diff --git a/src/general/basename.c b/src/general/basename.c new file mode 100644 index 0000000..a993587 --- /dev/null +++ b/src/general/basename.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include "test.h" + +#define T(path, want) \ +{ \ + char tmp[100]; \ + char *got = basename(strcpy(tmp, path)); \ + if (strcmp(want, got) != 0) \ + error("basename(\"%s\") got \"%s\" want \"%s\"\n", path, got, want); \ +} + +int main() +{ + if (strcmp(basename(0), ".") != 0) + error("basename(0) returned \"%s\"; expected \".\"\n", basename(0)); + T("", "."); + T("/usr/lib", "lib"); + T("/usr/", "usr"); + T("usr/", "usr"); + T("/", "/"); + T("///", "/"); + T("//usr//lib//", "lib"); + T(".", "."); + T("..", ".."); + return test_status; +} diff --git a/src/general/dirname.c b/src/general/dirname.c new file mode 100644 index 0000000..7a15a0a --- /dev/null +++ b/src/general/dirname.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include "test.h" + +#define T(path, want) \ +{ \ + char tmp[100]; \ + char *got = dirname(strcpy(tmp, path)); \ + if (strcmp(want, got) != 0) \ + error("dirname(\"%s\") got \"%s\" want \"%s\"\n", path, got, want); \ +} + +int main() +{ + if (strcmp(dirname(0), ".") != 0) + error("dirname(0) returned \"%s\"; expected \".\"\n", dirname(0)); + T("", "."); + T("/usr/lib", "/usr"); + T("/usr/", "/"); + T("usr", "."); + T("usr/", "."); + T("/", "/"); + T("///", "/"); + T(".", "."); + T("..", "."); + return test_status; +} diff --git a/src/general/dlopen.c b/src/general/dlopen.c new file mode 100644 index 0000000..38d08f1 --- /dev/null +++ b/src/general/dlopen.c @@ -0,0 +1,49 @@ +#include +#include "test.h" + +int foo; + +int main() +{ + void *h, *g; + int *i, *i2; + char *s; + void (*f)(void); + + h = dlopen("./dlopen_dso.so", RTLD_LAZY|RTLD_LOCAL); + if (!h) + error("dlopen ./dlopen_dso.so failed: %s\n", dlerror()); + i = dlsym(h, "i"); + if (!i) + error("dlsym i failed: %s\n", dlerror()); + if (*i != 1) + error("initialization failed: want i=1 got i=%d\n", *i); + f = (void (*)(void))dlsym(h, "f"); + if (!f) + error("dlsym f failed: %s\n", dlerror()); + f(); + if (*i != 2) + error("f call failed: want i=2 got i=%d\n", *i); + if (dlclose(h)) + error("dlclose failed: %s\n", dlerror()); + + g = dlopen(0, RTLD_LAZY|RTLD_LOCAL); + if (!g) + error("dlopen 0 failed: %s\n", dlerror()); + i2 = dlsym(g, "i"); + s = dlerror(); + if (i2 || s == 0) + error("dlsym i should have failed\n"); + if (dlsym(g, "main") == 0) + error("dlsym main failed: %s\n", dlerror()); + + h = dlopen("./dlopen_dso.so", RTLD_LAZY|RTLD_GLOBAL); + i2 = dlsym(g, "i"); + if (!i2) + error("dlsym i failed: %s\n", dlerror()); + if (*i2 != 2) + error("want i2=2, got i2=%d\n", *i2); + if (dlclose(g)) + error("dlclose failed: %s\n", dlerror()); + return test_status; +} diff --git a/src/general/dlopen_dso.c b/src/general/dlopen_dso.c new file mode 100644 index 0000000..cb3220a --- /dev/null +++ b/src/general/dlopen_dso.c @@ -0,0 +1,6 @@ +int i = 1; + +void f(void) +{ + i++; +} diff --git a/src/general/env.c b/src/general/env.c new file mode 100644 index 0000000..7962bf5 --- /dev/null +++ b/src/general/env.c @@ -0,0 +1,51 @@ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE 1 +#endif +#include +#include +#include +#include +#include "test.h" + +extern char **environ; + +int main() +{ + char *s; + int r; + + if (clearenv() || (environ && *environ)) + error("clrearenv: %s\n", strerror(errno)); + if (putenv("TEST=1")) + error("putenv: %s\n", strerror(errno)); + if ((s=environ[1])) + error("environ[1]: %p, wanted 0\n", s); + if (!(s=getenv("TEST"))) + error("getenv(\"TEST\"): 0, wanted \"1\"\n"); + if (strcmp(s,"1") != 0) + error("getenv(\"TEST\"): \"%s\", wanted \"1\"\n", s); + if (unsetenv("TEST")) + error("unsetenv: %s\n", strerror(errno)); + if ((s=*environ)) + error("*environ: %p != 0\n", s); + if ((s=getenv("TEST"))) + error("getenv(\"TEST\"): %p, wanted 0\n", s); + if (setenv("TEST", "2", 0)) + error("setenv: %s\n", strerror(errno)); + if (strcmp(s=getenv("TEST"),"2") != 0) + error("getenv(\"TEST\"): \"%s\", wanted \"2\"\n", s); + if (setenv("TEST", "3", 0)) + error("setenv: %s\n", strerror(errno)); + if (strcmp(s=getenv("TEST"),"2") != 0) + error("getenv(\"TEST\"): \"%s\", wanted \"2\"\n", s); + if (setenv("TEST", "3", 1)) + error("setenv: %s\n", strerror(errno)); + if (strcmp(s=getenv("TEST"),"3") != 0) + error("getenv(\"TEST\"): \"%s\", wanted \"3\"\n", s); + /* test failures */ + if ((r=setenv("","",0)) != -1 || errno != EINVAL) + error("setenv(\"\",\"\",0): %d, errno: %d (%s), wanted -1, %d (EINVAL)\n", r, errno, strerror(errno), EINVAL); + if ((r=setenv(0,"",0)) != -1 || errno != EINVAL) + error("setenv(0,\"\",0): %d, errno: %d (%s), wanted -1, %d (EINVAL)\n", r, errno, strerror(errno), EINVAL); + return test_status; +} diff --git a/src/general/fdopen.c b/src/general/fdopen.c new file mode 100644 index 0000000..1f4da03 --- /dev/null +++ b/src/general/fdopen.c @@ -0,0 +1,38 @@ +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 700 +#endif +#include +#include +#include +#include +#include +#include "test.h" + +#define TEST(c) do { \ + errno = 0; \ + if (!(c)) \ + error("%s failed (errno = %d)\n", #c, errno); \ +} while(0) + +int main(void) +{ + char tmp[] = "/tmp/testsuite-XXXXXX"; + char foo[6]; + int fd; + FILE *f; + + TEST((fd = mkstemp(tmp)) > 2); + TEST(write(fd, "hello", 6)==6); + TEST(f = fdopen(fd, "rb")); + if (f) { + TEST(ftello(f)==6); + TEST(fseeko(f, 0, SEEK_SET)==0); + TEST(fgets(foo, sizeof foo, f)); + if (strcmp(foo,"hello") != 0) + error("fgets read back: \"%s\"; wanted: \"hello\"\n", foo); + fclose(f); + } + if (fd > 2) + TEST(unlink(tmp) != -1); + return test_status; +} diff --git a/src/general/fnmatch.c b/src/general/fnmatch.c new file mode 100644 index 0000000..33f508f --- /dev/null +++ b/src/general/fnmatch.c @@ -0,0 +1,138 @@ +#include +#include +#include +#include +#include "test.h" + +/* adapted from dietlibc's test-newfnmatch.c */ + +/* xlat / printflags adapted from http://www.liacs.nl/~wichert/strace/ */ +#define FLAG(f) { f, #f } + +struct xlat { + int val; + char *str; +} fnmatch_flags[] = { + FLAG(FNM_NOESCAPE), + FLAG(FNM_PATHNAME), + FLAG(FNM_PERIOD), + {0, NULL}, +}; + +static char *flagstr(const struct xlat *map, int flags) +{ + static char buf[1000]; + char *sep; + + if (!flags) { + sprintf(buf, "0"); + return buf; + } + sep = ""; + for (; map->str; map++) { + if (map->val && (flags & map->val) == map->val) { + sprintf(buf, "%s%s", sep, map->str); + sep = "|"; + flags &= ~(map->val); + } + } + if (flags) + sprintf(buf, "%sunknown=%#x", sep, flags); + return buf; +} + +/* tests harness adapted from glibc testfnm.c */ +struct { + const char *pattern; + const char *string; + int flags; + int expected; +} tests[] = { + /* begin dietlibc tests */ + { "*.c", "foo.c", 0, 0 }, + { "*.c", ".c", 0, 0 }, + { "*.a", "foo.c", 0, FNM_NOMATCH }, + { "*.c", ".foo.c", 0, 0 }, + { "*.c", ".foo.c", FNM_PERIOD, FNM_NOMATCH }, + { "*.c", "foo.c", FNM_PERIOD, 0 }, + { "a\\*.c", "a*.c", FNM_NOESCAPE, FNM_NOMATCH }, + { "a\\*.c", "ax.c", 0, FNM_NOMATCH }, + { "a[xy].c", "ax.c", 0, 0 }, + { "a[!y].c", "ax.c", 0, 0 }, + { "a[a/z]*.c", "a/x.c", FNM_PATHNAME, FNM_NOMATCH }, + { "a/*.c", "a/x.c", FNM_PATHNAME, 0 }, + { "a*.c", "a/x.c", FNM_PATHNAME, FNM_NOMATCH }, + { "*/foo", "/foo", FNM_PATHNAME, 0 }, + { "-O[01]", "-O1", 0, 0 }, + { "[[?*\\]", "\\", 0, 0 }, + { "[]?*\\]", "]", 0, 0 }, + /* initial right-bracket tests */ + { "[!]a-]", "b", 0, 0 }, + { "[]-_]", "^", 0, 0 }, /* range: ']', '^', '_' */ + { "[!]-_]", "X", 0, 0 }, + { "??", "-", 0, FNM_NOMATCH }, + /* begin glibc tests */ + { "*LIB*", "lib", FNM_PERIOD, FNM_NOMATCH }, + { "a[/]b", "a/b", 0, 0 }, + { "a[/]b", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "[a-z]/[a-z]", "a/b", 0, 0 }, + { "*", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "*[/]b", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "*[b]", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "[*]/b", "a/b", 0, FNM_NOMATCH }, + { "[*]/b", "*/b", 0, 0 }, + { "[?]/b", "a/b", 0, FNM_NOMATCH }, + { "[?]/b", "?/b", 0, 0 }, + { "[[a]/b", "a/b", 0, 0 }, + { "[[a]/b", "[/b", 0, 0 }, + { "\\*/b", "a/b", 0, FNM_NOMATCH }, + { "\\*/b", "*/b", 0, 0 }, + { "\\?/b", "a/b", 0, FNM_NOMATCH }, + { "\\?/b", "?/b", 0, 0 }, + { "[/b", "[/b", 0, 0 }, + { "\\[/b", "[/b", 0, 0 }, + { "??""/b", "aa/b", 0, 0 }, + { "???b", "aa/b", 0, 0 }, + { "???b", "aa/b", FNM_PATHNAME, FNM_NOMATCH }, + { "?a/b", ".a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, + { "a/?b", "a/.b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, + { "*a/b", ".a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, + { "a/*b", "a/.b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, + { "[.]a/b", ".a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, + { "a/[.]b", "a/.b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, + { "*/?", "a/b", FNM_PATHNAME|FNM_PERIOD, 0 }, + { "?/*", "a/b", FNM_PATHNAME|FNM_PERIOD, 0 }, + { ".*/?", ".a/b", FNM_PATHNAME|FNM_PERIOD, 0 }, + { "*/.?", "a/.b", FNM_PATHNAME|FNM_PERIOD, 0 }, + { "*/*", "a/.b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, + { "*?*/*", "a/.b", FNM_PERIOD, 0 }, + { "*[.]/b", "a./b", FNM_PATHNAME|FNM_PERIOD, 0 }, + { "*[[:alpha:]]/*[[:alnum:]]", "a/b", FNM_PATHNAME, 0 }, + /* These three tests should result in error according to SUSv3. + * See XCU 2.13.1, XBD 9.3.5, & fnmatch() */ + { "*[![:digit:]]*/[![:d-d]", "a/b", FNM_PATHNAME, -FNM_NOMATCH }, + { "*[![:digit:]]*/[[:d-d]", "a/[", FNM_PATHNAME, -FNM_NOMATCH }, + { "*[![:digit:]]*/[![:d-d]", "a/[", FNM_PATHNAME, -FNM_NOMATCH }, + { "a?b", "a.b", FNM_PATHNAME|FNM_PERIOD, 0 }, + { "a*b", "a.b", FNM_PATHNAME|FNM_PERIOD, 0 }, + { "a[.]b", "a.b", FNM_PATHNAME|FNM_PERIOD, 0 }, +}; + +int main(void) +{ + int i; + + for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) { + int r, x; + + r = fnmatch(tests[i].pattern, tests[i].string, tests[i].flags); + x = tests[i].expected; + if (r != x && (r != FNM_NOMATCH || x != -FNM_NOMATCH)) { + error("fnmatch(\"%s\", \"%s\", %s) failed, got %d want %d\n", + tests[i].pattern, tests[i].string, + flagstr(fnmatch_flags, tests[i].flags), + r, x); + } + } + return test_status; +} diff --git a/src/general/fscanf.c b/src/general/fscanf.c new file mode 100644 index 0000000..62627b7 --- /dev/null +++ b/src/general/fscanf.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include "test.h" + +#define T(f, x, m) do { \ + r = (f); \ + if (r != (x)) \ + error("%s failed (got %d, expected %d, errno \"%s\") (%s)\n", \ + #f, r, x, errno ? strerror(errno) : "", m); \ + errno = 0; \ +} while (0) + +#define S(s, x, m) do { \ + if (strcmp(s, x) != 0) \ + error("got [%s] want [%s] (%s)\n", s, x, m); \ +} while(0) + +int main(void) +{ + int r, x, y; + char a[100], b[100]; + int p[2]; + FILE *f; + + T(pipe(p), 0, "open pipe"); + T(!(f = fdopen(p[0], "rb")), 0, "fdopen pipe"); + if (!f) { + close(p[0]); + close(p[1]); + return test_status; + } + + T(write(p[1], "hello, world\n", 13), 13, "write to pipe"); + T(fscanf(f, "%s %[own]", a, b), 2, ""); + S(a, "hello,", "wrong result for %s"); + S(b, "wo", "wrong result for %[own]"); + T(fgetc(f), 'r', "fgetc 'r'"); + + T(write(p[1], " 0x12 0x34", 10), 10, ""); + T(fscanf(f, "ld %5i%2i", &x, &y), 1, ""); + T(x, 0x12, ""); + T(fgetc(f), '3', "fgetc '3'"); + + fclose(f); + close(p[1]); + return test_status; +} diff --git a/src/general/mbc.c b/src/general/mbc.c new file mode 100644 index 0000000..56c546b --- /dev/null +++ b/src/general/mbc.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include "test.h" + +/* + * f = function call to test (or any expression) + * x = expected result + * m = message to print on failure + */ +#define T(f, x, m) (void)( \ + memset(&st, 0, sizeof st), \ + (i = (f)) == (x) || \ + error("%s failed (%s) got %d want %d\n", #f, m, i, x) ) +#define TCHAR(f, x, m) (void)( \ + memset(&st, 0, sizeof st), \ + (i = (f)) == (x) || \ + error("%s failed (%s) got 0x%04x want 0x%04x\n", #f, m, i, x) ) + +int main(void) +{ + const char *cs; + int i; + mbstate_t st, st2; + wchar_t wc, wcs[32]; + + (void)( + setlocale(LC_CTYPE, "en_US.UTF-8") || + setlocale(LC_CTYPE, "en_GB.UTF-8") || + setlocale(LC_CTYPE, "en.UTF-8") || + setlocale(LC_CTYPE, "POSIX.UTF-8") || + setlocale(LC_CTYPE, "C.UTF-8") || + setlocale(LC_CTYPE, "UTF-8") || + setlocale(LC_CTYPE, "") ); + + T(mbsrtowcs(wcs, (cs="abcdef",&cs), 3, &st), 3, "wrong semantics for wcs buf len"); + T(mbsrtowcs(wcs, (cs="abcdef",&cs), 8, &st), 6, "wrong semantics for wcs buf len"); + T(mbsrtowcs(NULL, (cs="abcdef",&cs), 2, &st), 6, "wrong semantics for NULL wcs"); + + if (strcmp(nl_langinfo(CODESET), "UTF-8")) + return error("cannot set UTF-8 locale for test (codeset=%s)\n", nl_langinfo(CODESET)); + + T(mbrtowc(&wc, "\x80", 1, &st), -1, "failed to catch error"); + T(mbrtowc(&wc, "\xc0", 1, &st), -1, "failed to catch illegal initial"); + + T(mbrtowc(&wc, "\xc0\x80", 2, &st), -1, "aliasing nul"); + T(mbrtowc(&wc, "\xc0\xaf", 2, &st), -1, "aliasing slash"); + T(mbrtowc(&wc, "\xe0\x80\xaf", 3, &st), -1, "aliasing slash"); + T(mbrtowc(&wc, "\xf0\x80\x80\xaf", 4, &st), -1, "aliasing slash"); + T(mbrtowc(&wc, "\xf8\x80\x80\x80\xaf", 5, &st), -1, "aliasing slash"); + T(mbrtowc(&wc, "\xfc\x80\x80\x80\x80\xaf", 6, &st), -1, "aliasing slash"); + T(mbrtowc(&wc, "\xe0\x82\x80", 3, &st), -1, "aliasing U+0080"); + T(mbrtowc(&wc, "\xe0\x9f\xbf", 3, &st), -1, "aliasing U+07FF"); + T(mbrtowc(&wc, "\xf0\x80\xa0\x80", 4, &st), -1, "aliasing U+0800"); + T(mbrtowc(&wc, "\xf0\x8f\xbf\xbd", 4, &st), -1, "aliasing U+FFFD"); + + T(mbrtowc(&wc, "\xed\xa0\x80", 3, &st), -1, "failed to catch surrogate"); + T(mbrtowc(&wc, "\xef\xbf\xbe", 3, &st), 3, "failed to accept U+FFFE"); + T(mbrtowc(&wc, "\xef\xbf\xbf", 3, &st), 3, "failed to accept U+FFFF"); + T(mbrtowc(&wc, "\xf4\x8f\xbf\xbe", 4, &st), 4, "failed to accept U+10FFFE"); + T(mbrtowc(&wc, "\xf4\x8f\xbf\xbf", 4, &st), 4, "failed to accept U+10FFFF"); + + T(mbrtowc(&wc, "\xc2\x80", 2, &st), 2, "wrong length"); + TCHAR((mbrtowc(&wc, "\xc2\x80", 2, &st),wc), 0x80, "wrong char"); + T(mbrtowc(&wc, "\xe0\xa0\x80", 3, &st), 3, "wrong length"); + TCHAR((mbrtowc(&wc, "\xe0\xa0\x80", 3, &st),wc), 0x800, "wrong char"); + T(mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st), 4, "wrong length"); + TCHAR((mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st),wc), 0x10000, "wrong char"); + + memset(&st2, 0, sizeof st2); + T(mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte"); + T(mbrtowc(&wc, "\x80", 1, &st2), 1, "failed to resume"); + TCHAR(wc, 0x80, "wrong char"); + + memset(&st2, 0, sizeof st2); + T(mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte"); + T(mbsrtowcs(wcs, (cs="\xa0""abc",&cs), 32, &st2), 4, "failed to resume"); + TCHAR(wcs[0], 0xa0, "wrong char"); + TCHAR(wcs[1], 'a', "wrong char"); + T(!cs, 1, "wrong final position"); + return test_status; +} diff --git a/src/general/memstream.c b/src/general/memstream.c new file mode 100644 index 0000000..749c664 --- /dev/null +++ b/src/general/memstream.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ +((r) = (f)) == (x) || \ +(error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_E(f) ( (errno = 0), (f) || \ +(error("%s failed (errno = %d)\n", #f, errno), 0) ) + +#define TEST_S(s, x, m) ( \ +!strcmp((s),(x)) || \ +(error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +#define TEST_M(s, x, n, m) ( \ +!memcmp((s),(x),(n)) || \ +(error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +int main(void) +{ + FILE *f; + char *s; + size_t l; + char buf[100]; + int i; + + s = 0; + TEST_E(f = open_memstream(&s, &l)); + TEST_E(putc('a', f) == 'a'); + TEST_E(putc('b', f) == 'b'); + TEST_E(putc('c', f) == 'c'); + TEST_E(!fflush(f)); + fclose(f); + if (s) TEST_S(s, "abc", "wrong output"); + free(s); + + s = 0; + TEST_E(f = open_memstream(&s, &l)); + TEST_E(fseek(f,1,SEEK_CUR)>=0); + TEST_E(putc('q', f) == 'q'); + TEST_E(!fflush(f)); + if (s) TEST_M(s, "\0q", 3, "wrong output"); + TEST(i, fseek(f,-3,SEEK_CUR), -1, "invalid seek allowed"); + TEST(i, errno, EINVAL, "%d != %d"); + TEST(i, ftell(f), 2, "%d != %d"); + TEST_E(fseek(f,-2,SEEK_CUR)>=0); + TEST_E(putc('e', f) == 'e'); + TEST_E(!fflush(f)); + if (s) TEST_S(s, "eq", "wrong output"); + fclose(f); + free(s); + + TEST_E(f = fmemopen(buf, 10, "r+")); + TEST_E(fputs("hello", f) >= 0); + TEST_E(fputc(0, f)==0); + TEST_E(fseek(f, 0, SEEK_SET)>=0); + i=0; + TEST_E(fscanf(f, "hello%n", &i)==0); + TEST(i, i, 5, "%d != %d"); + TEST(i, ftell(f), 5, "%d != %d"); + errno = 0; + TEST(i, fseek(f, 6, SEEK_CUR)<0, 1, ""); + TEST(i, errno!=0, 1, ""); + TEST(i, ftell(f), 5, "%d != %d"); + TEST_S(buf, "hello", ""); + fclose(f); + + TEST_E(f = fmemopen(buf, 10, "a+")); + TEST(i, ftell(f), 5, "%d != %d"); + TEST_E(fseek(f, 0, SEEK_SET)>=0); + TEST(i, getc(f), 'h', "%d != %d"); + TEST(i, getc(f), 'e', "%d != %d"); + TEST(i, getc(f), 'l', "%d != %d"); + TEST(i, getc(f), 'l', "%d != %d"); + TEST(i, getc(f), 'o', "%d != %d"); + TEST(i, getc(f), EOF, "%d != %d"); + TEST_E(fseek(f, 6, SEEK_SET)>=0); + TEST(i, ftell(f), 6, "%d != %d"); + TEST(i, getc(f), EOF, "%d != %d"); + TEST(i, ftell(f), 6, "%d != %d"); + TEST_E(fseek(f, 0, SEEK_SET)>=0); + TEST(i, getc(f), 'h', "%d != %d"); + TEST_E(fseek(f, 0, SEEK_CUR)>=0); + buf[7] = 'x'; + TEST_E(fprintf(f, "%d", i)==3); + TEST_E(fflush(f)==0); + TEST(i, ftell(f), 8, "%d != %d"); + TEST_S(buf, "hello104", ""); + fclose(f); + return test_status; +} diff --git a/src/general/popen.c b/src/general/popen.c new file mode 100644 index 0000000..f5cf05a --- /dev/null +++ b/src/general/popen.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || (error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_E(f) ( \ + (errno = 0), \ + (f) || (error("%s failed (errno = %d)\n", #f, errno), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +static sig_atomic_t got_sig; + +static void handler(int sig) { + got_sig = 1; +} + +int main(void) +{ + int i; + char foo[6]; + char cmd[64]; + FILE *f; + + TEST_E(f = popen("echo hello", "r")); + if (f) { + TEST_E(fgets(foo, sizeof foo, f)); + TEST_S(foo, "hello", "child process did not say hello"); + TEST(i, pclose(f), 0, "exit status %04x != %04x"); + } + + signal(SIGUSR1, handler); + snprintf(cmd, sizeof cmd, "read a ; test \"x$a\" = xhello && kill -USR1 %d", getpid()); + TEST_E(f = popen(cmd, "w")); + if (f) { + TEST_E(fputs("hello", f) >= 0); + TEST(i, pclose(f), 0, "exit status %04x != %04x"); + TEST(i, got_sig, 1, "child process did not send signal"); + } + signal(SIGUSR1, SIG_DFL); + return test_status; +} diff --git a/src/general/pthread.c b/src/general/pthread.c new file mode 100644 index 0000000..3c25c57 --- /dev/null +++ b/src/general/pthread.c @@ -0,0 +1,294 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ + msg = #f, ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +static volatile char *msg = ""; + +static void alarmhandler(int sig) { + error("timeout in %s\n", msg); + _Exit(1); +} + +static pthread_key_t k1, k2; + +static void dtor(void *p) +{ + *(int *)p = 1; +} + +static void *start1(void *arg) +{ + return arg; +} + +static void *start2(void *arg) +{ + int *p = arg; + if (pthread_setspecific(k1, p) || pthread_setspecific(k2, p+1)) + return arg; + return 0; +} + +static void *start3(void *arg) +{ + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0); + sem_post(arg); + for (;;); + return 0; +} + +static void cleanup4(void *arg) +{ + *(int *)arg = 1; +} + +static void *start4(void *arg) +{ + pthread_cleanup_push(cleanup4, arg); + sleep(3); + pthread_cleanup_pop(0); + return 0; +} + +static void cleanup4a2(void *arg) +{ + *(int *)arg += 2; +} + +static void cleanup4a3(void *arg) +{ + *(int *)arg += 3; +} + +static void cleanup4a4(void *arg) +{ + *(int *)arg += 4; +} + +static void *start4a(void *arg) +{ + int *foo = arg; + pthread_cleanup_push(cleanup4, foo); + pthread_cleanup_push(cleanup4a2, foo+1); + pthread_cleanup_push(cleanup4a3, foo+2); + pthread_cleanup_push(cleanup4a4, foo+3); + sleep(3); + pthread_cleanup_pop(0); + pthread_cleanup_pop(0); + pthread_cleanup_pop(0); + pthread_cleanup_pop(0); + return 0; +} + +static void *start5(void *arg) +{ + pthread_mutex_lock(arg); + return 0; +} + +static void *start6(void *arg) +{ + void **args = arg; + pthread_mutex_lock(args[1]); + pthread_barrier_wait(args[0]); + nanosleep(&(struct timespec){ .tv_nsec = 10000000 }, 0); + return 0; +} + +static void *start7(void *arg) +{ + void **args = arg; + pthread_mutex_lock(args[1]); + pthread_cond_signal(args[0]); + pthread_mutex_unlock(args[1]); + return 0; +} + +static void *start8(void *arg) +{ + void **args = arg; + pthread_mutex_t *m = args[1]; + pthread_cond_t *c = args[0]; + int *x = args[2]; + + pthread_mutex_lock(m); + while (*x) pthread_cond_wait(c, m); + pthread_mutex_unlock(m); + + return 0; +} + +int main(void) +{ + pthread_t td, td1, td2, td3; + int r; + void *res; + int foo[4], bar[2]; + pthread_barrier_t barrier2; + pthread_mutexattr_t mtx_a; + sem_t sem1; + pthread_mutex_t mtx; + pthread_cond_t cond; + + signal(SIGALRM, alarmhandler); + alarm(10); + + TEST(r, pthread_barrier_init(&barrier2, 0, 2), 0, "creating barrier"); + TEST(r, sem_init(&sem1, 0, 0), 0, "creating semaphore"); + + /* Test basic thread creation and joining */ + TEST(r, pthread_create(&td, 0, start1, &res), 0, "failed to create thread"); + res = 0; + TEST(r, pthread_join(td, &res), 0, "failed to join"); + TEST(r, (res==&res), 1, "wrong result from join"); + + /* Test POSIX thread-specific data */ + TEST(r, pthread_key_create(&k1, dtor), 0, "failed to create key"); + TEST(r, pthread_key_create(&k2, dtor), 0, "failed to create key"); + foo[0] = foo[1] = 0; + TEST(r, pthread_setspecific(k1, bar), 0, "failed to set tsd"); + TEST(r, pthread_setspecific(k2, bar+1), 0, "failed to set tsd"); + TEST(r, pthread_create(&td, 0, start2, foo), 0, "failed to create thread"); + TEST(r, pthread_join(td, &res), 0, "failed to join"); + TEST(res, res, 0, "pthread_setspecific failed in thread"); + TEST(r, foo[0], 1, "dtor failed to run"); + TEST(r, foo[1], 1, "dtor failed to run"); + TEST(res, pthread_getspecific(k1), bar, "tsd corrupted"); + TEST(res, pthread_getspecific(k2), bar+1, "tsd corrupted"); + TEST(r, pthread_setspecific(k1, 0), 0, "failed to clear tsd"); + TEST(r, pthread_setspecific(k2, 0), 0, "failed to clear tsd"); + TEST(r, pthread_key_delete(k1), 0, "failed to destroy key"); + TEST(r, pthread_key_delete(k2), 0, "failed to destroy key"); + + /* Asynchronous cancellation */ + TEST(r, pthread_create(&td, 0, start3, &sem1), 0, "failed to create thread"); + while (sem_wait(&sem1)); + TEST(r, pthread_cancel(td), 0, "canceling"); + TEST(r, pthread_join(td, &res), 0, "joining canceled thread"); + TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status"); + + /* Cancellation cleanup handlers */ + foo[0] = 0; + TEST(r, pthread_create(&td, 0, start4, foo), 0, "failed to create thread"); + TEST(r, pthread_cancel(td), 0, "cancelling"); + TEST(r, pthread_join(td, &res), 0, "joining canceled thread"); + TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status"); + TEST(r, foo[0], 1, "cleanup handler failed to run"); + + /* Nested cleanup handlers */ + memset(foo, 0, sizeof foo); + TEST(r, pthread_create(&td, 0, start4a, foo), 0, "failed to create thread"); + TEST(r, pthread_cancel(td), 0, "cancelling"); + TEST(r, pthread_join(td, &res), 0, "joining canceled thread"); + TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status"); + TEST(r, foo[0], 1, "cleanup handler failed to run"); + TEST(r, foo[1], 2, "cleanup handler failed to run"); + TEST(r, foo[2], 3, "cleanup handler failed to run"); + TEST(r, foo[3], 4, "cleanup handler failed to run"); + + /* Robust mutexes */ + TEST(r, pthread_mutexattr_init(&mtx_a), 0, "initializing mutex attr"); + TEST(r, pthread_mutexattr_setrobust(&mtx_a, PTHREAD_MUTEX_ROBUST), 0, "setting robust attribute"); + TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex"); + TEST(r, pthread_mutex_lock(&mtx), 0, "locking robust mutex"); + TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking robust mutex"); + TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread"); + TEST(r, pthread_join(td, &res), 0, "joining thread"); + TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d"); + TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d"); + TEST(r, pthread_mutex_lock(&mtx), ENOTRECOVERABLE, "re-locking orphaned robust mutex %d!=%d"); + TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying unrecoverable mutex %d!=%d"); + + TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex"); + TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread"); + TEST(r, pthread_join(td, &res), 0, "joining thread"); + TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d"); + TEST(r, pthread_mutex_consistent(&mtx), 0, "%d!=%d"); + TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d"); + TEST(r, pthread_mutex_lock(&mtx), 0, "re-locking orphaned robust mutex %d!=%d"); + TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying mutex %d!=%d"); + + TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "%d != %d"); + TEST(r, pthread_create(&td, 0, start6, (void *[]){ &barrier2, &mtx }), 0, "%d != %d"); + pthread_barrier_wait(&barrier2); + TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "%d != %d"); + TEST(r, pthread_join(td, &res), 0, "%d != %d"); + TEST(r, pthread_mutex_consistent(&mtx), 0, "%d != %d"); + TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); + TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d"); + + //TEST(r, (fd=open("/dev/zero", O_RDWR))>=0, 1, "opening zero page file"); + //TEST(r, + + /* Condition variables */ + TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d"); + TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d"); + TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); + TEST(r, pthread_create(&td, 0, start7, (void *[]){ &cond, &mtx }), 0, "%d != %d"); + TEST(r, pthread_cond_wait(&cond, &mtx), 0, "%d != %d"); + TEST(r, pthread_join(td, &res), 0, "%d != %d"); + TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); + TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d"); + TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d"); + + /* Condition variables with multiple waiters */ + TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d"); + TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d"); + TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); + foo[0] = 1; + TEST(r, pthread_create(&td1, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); + TEST(r, pthread_create(&td2, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); + TEST(r, pthread_create(&td3, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); + TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); + nanosleep(&(struct timespec){.tv_nsec=1000000}, 0); + foo[0] = 0; + TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); + TEST(r, pthread_cond_signal(&cond), 0, "%d != %d"); + TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); + TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); + TEST(r, pthread_cond_signal(&cond), 0, "%d != %d"); + TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); + TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); + TEST(r, pthread_cond_signal(&cond), 0, "%d != %d"); + TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); + TEST(r, pthread_join(td1, 0), 0, "%d != %d"); + TEST(r, pthread_join(td2, 0), 0, "%d != %d"); + TEST(r, pthread_join(td3, 0), 0, "%d != %d"); + TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d"); + TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d"); + + /* Condition variables with broadcast signals */ + TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d"); + TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d"); + TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); + foo[0] = 1; + TEST(r, pthread_create(&td1, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); + TEST(r, pthread_create(&td2, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); + TEST(r, pthread_create(&td3, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); + TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); + nanosleep(&(struct timespec){.tv_nsec=1000000}, 0); + TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); + foo[0] = 0; + TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); + TEST(r, pthread_cond_broadcast(&cond), 0, "%d != %d"); + TEST(r, pthread_join(td1, 0), 0, "%d != %d"); + TEST(r, pthread_join(td2, 0), 0, "%d != %d"); + TEST(r, pthread_join(td3, 0), 0, "%d != %d"); + TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d"); + TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d"); + return test_status; +} diff --git a/src/general/qsort.c b/src/general/qsort.c new file mode 100644 index 0000000..b45c080 --- /dev/null +++ b/src/general/qsort.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include "test.h" + +static int scmp(const void *a, const void *b) +{ + return strcmp(*(char **)a, *(char **)b); +} + +static int icmp(const void *a, const void *b) +{ + return *(int*)a - *(int*)b; +} + +/* 26 items -- even */ +static char *s[] = { + "Bob", "Alice", "John", "Ceres", + "Helga", "Drepper", "Emeralda", "Zoran", + "Momo", "Frank", "Pema", "Xavier", + "Yeva", "Gedun", "Irina", "Nono", + "Wiener", "Vincent", "Tsering", "Karnica", + "Lulu", "Quincy", "Osama", "Riley", + "Ursula", "Sam" +}; +/* 23 items -- odd, prime */ +static int n[] = { + 879045, 394, 99405644, 33434, 232323, 4334, 5454, + 343, 45545, 454, 324, 22, 34344, 233, 45345, 343, + 848405, 3434, 3434344, 3535, 93994, 2230404, 4334 +}; + +int main(void) +{ + int i; + + qsort(s, sizeof(s)/sizeof(char *), sizeof(char *), scmp); + for (i=0; i 0) { + error("string sort failed at index %d\n", i); + for (i=0; i n[i+1]) { + error("integer sort failed at index %d\n", i); + for (i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +int main(void) +{ + int r; + char buf[100]; + struct timespec ts; + sem_t *sem, *sem2; + int val; + + clock_gettime(CLOCK_REALTIME, &ts); + snprintf(buf, sizeof buf, "/testsuite-%d-%d", (int)getpid(), (int)ts.tv_nsec); + + TEST(r, !(sem=sem_open(buf, O_CREAT|O_EXCL, 0700, 1)), 0, "could not open sem"); + + TEST(r, sem_getvalue(sem, &val), 0, "failed to get sem value"); + TEST(r, val, 1, "wrong initial semaphore value"); + + TEST(r, !(sem2=sem_open(buf, 0)), 0, "could not reopen sem"); + TEST(r, sem!=sem2, 0, "reopened sem has different address"); + + TEST(r, sem_wait(sem), 0, "failed on sem wait"); + TEST(r, sem_getvalue(sem2, &val), 0, "failed to get sem value"); + TEST(r, val, 0, "wrong semaphore value on second handle"); + + TEST(r, sem_post(sem), 0, "failed on sem post"); + TEST(r, sem_getvalue(sem2, &val), 0, "failed to get sem value"); + TEST(r, val, 1, "wrong semaphore value on second handle"); + + TEST(r, sem_close(sem), 0, "failed to close sem"); + TEST(r, sem_close(sem), 0, "failed to close sem second time"); + TEST(r, sem_unlink(buf), 0, "failed to unlink sem"); + return test_status; +} diff --git a/src/general/snprintf.c b/src/general/snprintf.c new file mode 100644 index 0000000..1187fd7 --- /dev/null +++ b/src/general/snprintf.c @@ -0,0 +1,174 @@ +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 700 +#endif +#include +#include +#include +#include +#include +#include "test.h" + +#define DISABLE_SLOW_TESTS + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +static const struct { + const char *fmt; + int i; + const char *expect; +} int_tests[] = { + /* width, precision, alignment */ + { "%04d", 12, "0012" }, + { "%.3d", 12, "012" }, + { "%3d", 12, " 12" }, + { "%-3d", 12, "12 " }, + { "%+3d", 12, "+12" }, + { "%+-5d", 12, "+12 " }, + { "%+- 5d", 12, "+12 " }, + { "%- 5d", 12, " 12 " }, + { "% d", 12, " 12" }, + { "%0-5d", 12, "12 " }, + { "%-05d", 12, "12 " }, + + /* ...explicit precision of 0 shall be no characters. */ + { "%.0d", 0, "" }, + { "%.0o", 0, "" }, + { "%#.0d", 0, "" }, + { "%#.0o", 0, "" }, + { "%#.0x", 0, "" }, + + /* ...but it still has to honor width and flags. */ + { "%2.0u", 0, " " }, + { "%02.0u", 0, " " }, + { "%2.0d", 0, " " }, + { "%02.0d", 0, " " }, + { "% .0d", 0, " " }, + { "%+.0d", 0, "+" }, + + /* hex: test alt form and case */ + { "%x", 63, "3f" }, + { "%#x", 63, "0x3f" }, + { "%X", 63, "3F" }, + + /* octal: test alt form */ + { "%o", 15, "17" }, + { "%#o", 15, "017" }, + + { NULL, 0.0, NULL } +}; + +static const struct { + const char *fmt; + double f; + const char *expect; +} fp_tests[] = { + /* basic form, handling of exponent/precision for 0 */ + { "%a", 0.0, "0x0p+0" }, + { "%e", 0.0, "0.000000e+00" }, + { "%f", 0.0, "0.000000" }, + { "%g", 0.0, "0" }, + { "%#g", 0.0, "0.00000" }, + { "%la", 0.0, "0x0p+0" }, + { "%le", 0.0, "0.000000e+00" }, + { "%lf", 0.0, "0.000000" }, + { "%lg", 0.0, "0" }, + { "%#lg", 0.0, "0.00000" }, + + /* rounding */ + { "%f", 1.1, "1.100000" }, + { "%f", 1.2, "1.200000" }, + { "%f", 1.3, "1.300000" }, + { "%f", 1.4, "1.400000" }, + { "%f", 1.5, "1.500000" }, + { "%.4f", 1.06125, "1.0612" }, + { "%.2f", 1.375, "1.38" }, + { "%.1f", 1.375, "1.4" }, + { "%.1lf", 1.375, "1.4" }, + { "%.15f", 1.1, "1.100000000000000" }, + { "%.16f", 1.1, "1.1000000000000001" }, + { "%.17f", 1.1, "1.10000000000000009" }, + { "%.2e", 1500001.0, "1.50e+06" }, + { "%.2e", 1505000.0, "1.50e+06" }, + { "%.2e", 1505000.00000095367431640625, "1.51e+06" }, + { "%.2e", 1505001.0, "1.51e+06" }, + { "%.2e", 1506000.0, "1.51e+06" }, + + /* correctness in DBL_DIG places */ + { "%.15g", 1.23456789012345, "1.23456789012345" }, + + /* correct choice of notation for %g */ + { "%g", 0.0001, "0.0001" }, + { "%g", 0.00001, "1e-05" }, + { "%g", 123456, "123456" }, + { "%g", 1234567, "1.23457e+06" }, + { "%.7g", 1234567, "1234567" }, + { "%.7g", 12345678, "1.234568e+07" }, + { "%.8g", 0.1, "0.1" }, + { "%.9g", 0.1, "0.1" }, + { "%.10g", 0.1, "0.1" }, + { "%.11g", 0.1, "0.1" }, + + /* pi in double precision, printed to a few extra places */ + { "%.15f", M_PI, "3.141592653589793" }, + { "%.18f", M_PI, "3.141592653589793116" }, + + /* exact conversion of large integers */ + { "%.0f", 340282366920938463463374607431768211456.0, + "340282366920938463463374607431768211456" }, + + { NULL, 0.0, NULL } +}; + +int main(void) +{ + int i, j, k; + char b[2000]; + + TEST(i, snprintf(0, 0, "%d", 123456), 6, "length returned %d != %d"); + TEST(i, snprintf(0, 0, "%.4s", "hello"), 4, "length returned %d != %d"); + TEST(i, snprintf(b, 0, "%.0s", "goodbye"), 0, "length returned %d != %d"); + + strcpy(b, "xxxxxxxx"); + TEST(i, snprintf(b, 4, "%d", 123456), 6, "length returned %d != %d"); + TEST_S(b, "123", "incorrect output"); + TEST(i, b[5], 'x', "buffer overrun"); + + /* Perform ascii arithmetic to test printing tiny doubles */ + TEST(i, snprintf(b, sizeof b, "%.1022f", 0x1p-1021), 1024, "%d != %d"); + b[1] = '0'; + for (i=0; i<1021; i++) { + for (k=0, j=1023; j>0; j--) { + if (b[j]<'5') b[j]+=b[j]-'0'+k, k=0; + else b[j]+=b[j]-'0'-10+k, k=1; + } + } + TEST(i, b[1], '1', "'%c' != '%c'"); + for (j=2; b[j]=='0'; j++); + TEST(i, j, 1024, "%d != %d"); + + +#ifndef DISABLE_SLOW_TESTS + errno = 0; + TEST(i, snprintf(NULL, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d"); + TEST(i, snprintf(NULL, 0, "%.*u ", 2147483647, 0), -1, "integer overflow %d"); + TEST(i, errno, EOVERFLOW, "after overflow: %d != %d"); +#endif + for (j=0; int_tests[j].fmt; j++) { + TEST(i, snprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i), strlen(b), "%d != %d"); + TEST_S(b, int_tests[j].expect, "bad integer conversion"); + } + + for (j=0; fp_tests[j].fmt; j++) { + TEST(i, snprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f), strlen(b), "%d != %d"); + TEST_S(b, fp_tests[j].expect, "bad floating point conversion"); + } + + TEST(i, snprintf(0, 0, "%.4a", 1.0), 11, "%d != %d"); + return test_status; +} diff --git a/src/general/spawn.c b/src/general/spawn.c new file mode 100644 index 0000000..707b1a5 --- /dev/null +++ b/src/general/spawn.c @@ -0,0 +1,42 @@ +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 700 +#endif +#include +#include +#include +#include +#include +#include +#include +#include "test.h" + +#define TEST(f, x) (void)( \ + (r = (f)) == (x) || \ + error("%s failed, got %d want %d\n", #f, r, x) ) + +#define TEST_E(f) (void)( \ + (errno = 0), (f) || \ + error("%s failed (errno = %d \"%s\")\n", #f, errno, strerror(errno)) ) + +int main(void) +{ + int r; + char foo[10]; + int p[2]; + pid_t pid; + int status; + posix_spawn_file_actions_t fa; + + TEST_E(!pipe(p)); + TEST(posix_spawn_file_actions_init(&fa), 0); + TEST(posix_spawn_file_actions_addclose(&fa, p[0]), 0); + TEST(posix_spawn_file_actions_adddup2(&fa, p[1], 1), 0); + TEST(posix_spawn_file_actions_addclose(&fa, p[1]), 0); + TEST(posix_spawnp(&pid, "echo", &fa, 0, (char *[]){"echo","hello",0}, 0), 0); + close(p[1]); + TEST(waitpid(pid, &status, 0), pid); + TEST(read(p[0], foo, sizeof foo), 6); + close(p[0]); + TEST(posix_spawn_file_actions_destroy(&fa), 0); + return test_status; +} diff --git a/src/general/sscanf.c b/src/general/sscanf.c new file mode 100644 index 0000000..a087c21 --- /dev/null +++ b/src/general/sscanf.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +#define TEST_F(x) ( \ + TEST(i, sscanf(# x, "%lf", &d), 1, "got %d fields, expected %d"), \ + TEST(t, d, (double)x, "%g != %g") ) + +int main(void) +{ + int i; + char a[100], b[100]; + int x, y, z, u, v; + double d, t; + + TEST(i, sscanf("hello, world\n", "%s %s", a, b), 2, "only %d fields, expected %d"); + TEST_S(a, "hello,", ""); + TEST_S(b, "world", ""); + + TEST(i, sscanf("hello, world\n", "%[hel]%s", a, b), 2, "only %d fields, expected %d"); + TEST_S(a, "hell", ""); + TEST_S(b, "o,", ""); + + TEST(i, sscanf("hello, world\n", "%[hel] %s", a, b), 2, "only %d fields, expected %d"); + TEST_S(a, "hell", ""); + TEST_S(b, "o,", ""); + + a[8] = 'X'; + a[9] = 0; + TEST(i, sscanf("hello, world\n", "%8c%8c", a, b), 1, "%d fields, expected %d"); + TEST_S(a, "hello, wX", ""); + + TEST(i, sscanf("56789 0123 56a72", "%2d%d%*d %[0123456789]\n", &x, &y, a), 3, "only %d fields, expected %d"); + TEST(i, x, 56, "%d != %d"); + TEST(i, y, 789, "%d != %d"); + TEST_S(a, "56", ""); + + TEST(i, sscanf("011 0x100 11 0x100 100", "%i %i %o %x %x\n", &x, &y, &z, &u, &v), 5, "only %d fields, expected %d"); + TEST(i, x, 9, "%d != %d"); + TEST(i, y, 256, "%d != %d"); + TEST(i, z, 9, "%d != %d"); + TEST(i, u, 256, "%d != %d"); + TEST(i, v, 256, "%d != %d"); + + TEST(i, sscanf("20 xyz", "%d %d\n", &x, &y), 1, "only %d fields, expected %d"); + TEST(i, x, 20, "%d != %d"); + + TEST(i, sscanf("xyz", "%d\n", &x, &y), 0, "got %d fields, expected no match (%d)"); + + TEST(i, sscanf("", "%d\n", &x, &y), -1, "got %d fields, expected input failure (%d)"); + + TEST(i, sscanf(" 12345 6", "%2d%d%d", &x, &y, &z), 3, "only %d fields, expected %d"); + TEST(i, x, 12, "%d != %d"); + TEST(i, y, 345, "%d != %d"); + TEST(i, z, 6, "%d != %d"); + + TEST(i, sscanf(" 0x12 0x34", "%5i%2i", &x, &y), 1, "got %d fields, expected %d"); + TEST(i, x, 0x12, "%d != %d"); + + TEST_F(123); + TEST_F(123.0); + TEST_F(123.0e+0); + TEST_F(123.0e+4); + TEST_F(1.234e1234); + TEST_F(1.234e-1234); + TEST_F(1.234e56789); + TEST_F(1.234e-56789); + TEST_F(-0.5); + TEST_F(0.1); + TEST_F(0.2); + TEST_F(0.1e-10); + TEST_F(0x1234p56); + + TEST(i, sscanf("10e", "%lf", &d), 0, "got %d fields, expected no match (%d)"); + TEST(i, sscanf("", "%lf\n", &d), -1, "got %d fields, expected input failure (%d)"); + return test_status; +} diff --git a/src/general/sscanf_long.c b/src/general/sscanf_long.c new file mode 100644 index 0000000..a26999d --- /dev/null +++ b/src/general/sscanf_long.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include "test.h" + +static void setrl(int r, long lim) +{ + struct rlimit rl; + + if (getrlimit(r, &rl)) + error("getrlimit %d: %s\n", r, strerror(errno)); + rl.rlim_cur = lim; + if (setrlimit(r, &rl)) + error("setrlimit %d: %s\n", r, strerror(errno)); +} + +int main(void) +{ + enum {n = 8*1024*1024}; + char *s = malloc(n); + int i; + float f; + char c; + + if (!s) + return error("out of memory"); + setrl(RLIMIT_STACK, 128*1024); + + for (i = 0; i < n; i++) s[i] = '1'; + s[n-3] = ' '; + s[n-1] = 0; + + /* + * stack overflow if scanf copies s on the stack (glibc) + * same issue with %d except then storing the conversion + * result is undefined behaviour + */ + i = sscanf(s, "%f %c", &f, &c); + + if (i != 2) + error("sscanf returned %d, want 2\n", i); + if (f != INFINITY) + error("sscanf(longnum, \"%%f\") read %f, want inf\n", f); + if (c != '1') + error("sscanf(\"1\", %%c) read '%c', want '1'\n", c); + free(s); + return test_status; +} diff --git a/src/general/string.c b/src/general/string.c new file mode 100644 index 0000000..2e06eb9 --- /dev/null +++ b/src/general/string.c @@ -0,0 +1,116 @@ +#define _BSD_SOURCE +#include +#include +#include "test.h" + +/* r = place to store result + * f = function call to test (or any expression) + * x = expected result + * m = message to print on failure (with formats for r & x) +**/ + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +int main(void) +{ + char b[32]; + char *s; + int i; + + b[16]='a'; b[17]='b'; b[18]='c'; b[19]=0; + TEST(s, strcpy(b, b+16), b, "wrong return %p != %p"); + TEST_S(s, "abc", "strcpy gave incorrect string"); + TEST(s, strcpy(b+1, b+16), b+1, "wrong return %p != %p"); + TEST_S(s, "abc", "strcpy gave incorrect string"); + TEST(s, strcpy(b+2, b+16), b+2, "wrong return %p != %p"); + TEST_S(s, "abc", "strcpy gave incorrect string"); + TEST(s, strcpy(b+3, b+16), b+3, "wrong return %p != %p"); + TEST_S(s, "abc", "strcpy gave incorrect string"); + + TEST(s, strcpy(b+1, b+17), b+1, "wrong return %p != %p"); + TEST_S(s, "bc", "strcpy gave incorrect string"); + TEST(s, strcpy(b+2, b+18), b+2, "wrong return %p != %p"); + TEST_S(s, "c", "strcpy gave incorrect string"); + TEST(s, strcpy(b+3, b+19), b+3, "wrong return %p != %p"); + TEST_S(s, "", "strcpy gave incorrect string"); + + TEST(s, memset(b, 'x', sizeof b), b, "wrong return %p != %p"); + TEST(s, strncpy(b, "abc", sizeof b - 1), b, "wrong return %p != %p"); + TEST(i, memcmp(b, "abc\0\0\0\0", 8), 0, "strncpy fails to zero-pad dest"); + TEST(i, b[sizeof b - 1], 'x', "strncpy overruns buffer when n > strlen(src)"); + + b[3] = 'x'; b[4] = 0; + strncpy(b, "abc", 3); + TEST(i, b[2], 'c', "strncpy fails to copy last byte: %hhu != %hhu"); + TEST(i, b[3], 'x', "strncpy overruns buffer to null-terminate: %hhu != %hhu"); + + TEST(i, !strncmp("abcd", "abce", 3), 1, "strncmp compares past n"); + TEST(i, !!strncmp("abc", "abd", 3), 1, "strncmp fails to compare n-1st byte"); + + strcpy(b, "abc"); + TEST(s, strncat(b, "123456", 3), b, "%p != %p"); + TEST(i, b[6], 0, "strncat failed to null-terminate (%d)"); + TEST_S(s, "abc123", "strncat gave incorrect string"); + + strcpy(b, "aaababccdd0001122223"); + TEST(s, strchr(b, 'b'), b+3, "%p != %p"); + TEST(s, strrchr(b, 'b'), b+5, "%p != %p"); + TEST(i, strspn(b, "abcd"), 10, "%d != %d"); + TEST(i, strcspn(b, "0123"), 10, "%d != %d"); + TEST(s, strpbrk(b, "0123"), b+10, "%d != %d"); + + strcpy(b, "abc 123; xyz; foo"); + TEST(s, strtok(b, " "), b, "%p != %p"); + TEST_S(s, "abc", "strtok result"); + + TEST(s, strtok(NULL, ";"), b+4, "%p != %p"); + TEST_S(s, " 123", "strtok result"); + + TEST(s, strtok(NULL, " ;"), b+11, "%p != %p"); + TEST_S(s, "xyz", "strtok result"); + + TEST(s, strtok(NULL, " ;"), b+16, "%p != %p"); + TEST_S(s, "foo", "strtok result"); + +#ifdef HAVE_BSD_STRL + memset(b, 'x', sizeof b); + TEST(i, strlcpy(b, "abc", sizeof b - 1), 3, "length %d != %d"); + TEST(i, b[3], 0, "strlcpy did not null-terminate short string (%d)"); + TEST(i, b[4], 'x', "strlcpy wrote extra bytes (%d)"); + + memset(b, 'x', sizeof b); + TEST(i, strlcpy(b, "abc", 2), 3, "length %d != %d"); + TEST(i, b[0], 'a', "strlcpy did not copy character %d"); + TEST(i, b[1], 0, "strlcpy did not null-terminate long string (%d)"); + + memset(b, 'x', sizeof b); + TEST(i, strlcpy(b, "abc", 3), 3, "length %d != %d"); + TEST(i, b[2], 0, "strlcpy did not null-terminate l-length string (%d)"); + + TEST(i, strlcpy(NULL, "abc", 0), 3, "length %d != %d"); + + memcpy(b, "abc\0\0\0x\0", 8); + TEST(i, strlcat(b, "123", sizeof b), 6, "length %d != %d"); + TEST_S(b, "abc123", "strlcat result"); + + memcpy(b, "abc\0\0\0x\0", 8); + TEST(i, strlcat(b, "123", 6), 6, "length %d != %d"); + TEST_S(b, "abc12", "strlcat result"); + TEST(i, b[6], 'x', "strlcat wrote past string %d != %d"); + + memcpy(b, "abc\0\0\0x\0", 8); + TEST(i, strlcat(b, "123", 4), 6, "length %d != %d"); + TEST_S(b, "abc", "strlcat result"); + + memcpy(b, "abc\0\0\0x\0", 8); + TEST(i, strlcat(b, "123", 3), 6, "length %d != %d"); + TEST_S(b, "abc", "strlcat result"); +#endif + return test_status; +} diff --git a/src/general/string_strchr.c b/src/general/string_strchr.c new file mode 100644 index 0000000..9ee4ca8 --- /dev/null +++ b/src/general/string_strchr.c @@ -0,0 +1,52 @@ +#include +#include "test.h" + +#define N(s, c) { \ + char *p = s; \ + char *q = strchr(p, c); \ + if (q) \ + error("strchr(%s,%s) returned str+%d, wanted 0\n", #s, #c, q-p); \ +} + +#define T(s, c, n) { \ + char *p = s; \ + char *q = strchr(p, c); \ + if (q == 0) \ + error("strchr(%s,%s) returned 0, wanted str+%d\n", #s, #c, n); \ + else if (q - p != n) \ + error("strchr(%s,%s) returned str+%d, wanted str+%d\n", #s, #c, q-p, n); \ +} + +int main(void) +{ + int i; + char a[128]; + char s[256]; + + for (i = 0; i < 128; i++) + a[i] = (i+1) & 127; + for (i = 0; i < 256; i++) + *((unsigned char*)s+i) = i+1; + + N("", 'a') + N("a", 'b') + N("abc abc", 'x') + N(a, 128) + N(a, 255) + + T("", 0, 0) + T("a", 'a', 0) + T("a", 'a'+256, 0) + T("a", 0, 1) + T("abc abc", 'c', 2) + T(s, 1, 0) + T(s, 2, 1) + T(s, 10, 9) + T(s, 11, 10) + T(s, 127, 126) + T(s, 128, 127) + T(s, 255, 254) + T(s, 0, 255) + + return test_status; +} diff --git a/src/general/string_strcspn.c b/src/general/string_strcspn.c new file mode 100644 index 0000000..39b907e --- /dev/null +++ b/src/general/string_strcspn.c @@ -0,0 +1,33 @@ +#include +#include +#include "test.h" + +#define T(s, c, n) { \ + char *p = s; \ + char *q = c; \ + size_t r = strcspn(p, q); \ + if (r != n) \ + error("strcspn(%s,%s) returned %lu, wanted %lu\n", #s, #c, (unsigned long)r, (unsigned long)(n)); \ +} + +int main(void) +{ + int i; + char a[128]; + char s[256]; + + for (i = 0; i < 128; i++) + a[i] = (i+1) & 127; + for (i = 0; i < 256; i++) + *((unsigned char*)s+i) = i+1; + + T("", "", 0) + T("a", "", 1) + T("", "a", 0) + T("abc", "cde", 2) + T("abc", a, 0) + T("\xff\x80 abc", a, 2) + T(s, "\xff", 254) + + return test_status; +} diff --git a/src/general/strtod.c b/src/general/strtod.c new file mode 100644 index 0000000..c6967ac --- /dev/null +++ b/src/general/strtod.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include "test.h" + +#define length(x) (sizeof(x) / sizeof *(x)) + +static struct { + char *s; + double f; +} t[] = { + {"0", 0.0}, + {"00.00", 0.0}, + {"-.00000", -0.0}, + {"1e+1000000", INFINITY}, + {"1e-1000000", 0}, + // 2^-1074 * 0.5 - eps + {".2470328229206232720882843964341106861825299013071623822127928412503377536351043e-323", 0}, + // 2^-1074 * 0.5 + eps + {".2470328229206232720882843964341106861825299013071623822127928412503377536351044e-323", 0x1p-1074}, + // 2^-1074 * 1.5 - eps + {".7410984687618698162648531893023320585475897039214871466383785237510132609053131e-323", 0x1p-1074}, + // 2^-1074 * 1.5 + eps + {".7410984687618698162648531893023320585475897039214871466383785237510132609053132e-323", 0x1p-1073}, + // 2^-1022 + 2^-1075 - eps + {".2225073858507201630123055637955676152503612414573018013083228724049586647606759e-307", 0x1p-1022}, + // 2^-1022 + 2^-1075 + eps + {".2225073858507201630123055637955676152503612414573018013083228724049586647606760e-307", 0x1.0000000000001p-1022}, + // 2^1024 - 2^970 - eps + {"17976931348623158079372897140530341507993413271003782693617377898044" + "49682927647509466490179775872070963302864166928879109465555478519404" + "02630657488671505820681908902000708383676273854845817711531764475730" + "27006985557136695962284291481986083493647529271907416844436551070434" + "2711559699508093042880177904174497791.999999999999999999999999999999", 0x1.fffffffffffffp1023}, + // 2^1024 - 2^970 + {"17976931348623158079372897140530341507993413271003782693617377898044" + "49682927647509466490179775872070963302864166928879109465555478519404" + "02630657488671505820681908902000708383676273854845817711531764475730" + "27006985557136695962284291481986083493647529271907416844436551070434" + "2711559699508093042880177904174497792", INFINITY}, + // some random numbers + {".5961860348131807091861002266453941950428e00", 0.59618603481318067}, // 0x1.313f4bc3b584cp-1 + {"1.815013169218038729887460898733526957442e-1", 0.18150131692180388}, // 0x1.73b6f662e1712p-3 + {"42.07082357534453600681618685682257590772e-2", 0.42070823575344535}, // 0x1.aece23c6e028dp-2 + {"665.4686306516261456328973225579833470816e-3", 0.66546863065162609}, // 0x1.54b84dea53453p-1 + {"6101.852922970868621786690495485449831753e-4", 0.61018529229708685}, // 0x1.386a34e5d516bp-1 + {"76966.95208236968077849464348875471158549e-5", 0.76966952082369677}, // 0x1.8a121f9954dfap-1 + {"250506.5322228682496132604807222923702304e-6", 0.25050653222286823}, // 0x1.0084c8cd538c2p-2 + {"2740037.230228005325852424697698331177377e-7", 0.27400372302280052}, // 0x1.18946e9575ef4p-2 + {"20723093.50049742645941529268715428324490e-8", 0.20723093500497428}, // 0x1.a868b14486e4dp-3 + {"0.7900280238081604956226011047460238748912e1", 7.9002802380816046}, // 0x1.f99e3100f2eaep+2 + {"0.9822860653737296848190558448760465863597e2", 98.228606537372968}, // 0x1.88ea17d506accp+6 + {"0.7468949723190370809405570560160405324869e3", 746.89497231903704}, // 0x1.75728e73f48b7p+9 + {"0.1630268320282728475980459844271031751665e4", 1630.2683202827284}, // 0x1.97912c28d5cbp+10 + {"0.4637168629719170695109918769645492022088e5", 46371.686297191707}, // 0x1.6a475f6258737p+15 + {"0.6537805944497711554209461686415872067523e6", 653780.59444977110}, // 0x1.3f3a9305bb86cp+19 + {"0.2346324356502437045212230713960457676531e6", 234632.43565024371}, // 0x1.ca4437c3631eap+17 + {"0.9709481716420048341897258980454298205278e8", 97094817.164200485}, // 0x1.7263284a8242cp+26 + {"0.4996908522051874110779982354932499499602e9", 499690852.20518744}, // 0x1.dc8ad6434872ap+28 +}; + +int main(void) +{ + int i; + double x; + char *p; + + for (i = 0; i < length(t); i++) { + x = strtod(t[i].s, &p); + if (x != t[i].f) + error("strtod(\"%s\") want %a got %a\n", t[i].s, t[i].f, x); + } + return test_status; +} + diff --git a/src/general/strtod_long.c b/src/general/strtod_long.c new file mode 100644 index 0000000..d2db2e8 --- /dev/null +++ b/src/general/strtod_long.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include "test.h" + +int main(void) +{ + double x, want = .1111111111111111111111; + char buf[40000]; + + memset(buf, '1', sizeof buf); + buf[0] = '.'; + buf[sizeof buf - 1] = 0; + + if ((x=strtod(buf, 0)) != want) + error("strtod(.11[...]1) got %a want %a\n", x, want); + return test_status; +} + diff --git a/src/general/strtod_simple.c b/src/general/strtod_simple.c new file mode 100644 index 0000000..c95dc9e --- /dev/null +++ b/src/general/strtod_simple.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include "test.h" + +/* r = place to store result + * f = function call to test (or any expression) + * x = expected result + * m = message to print on failure (with formats for r & x) +**/ + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x, r-x), 0) ) + +int main(void) +{ + int i; + double d, d2; + char buf[1000]; + + for (i=0; i<100; i++) { + d = sin(i); + snprintf(buf, sizeof buf, "%.300f", d); + TEST(d2, strtod(buf, 0), d, "round trip fail %a != %a (%a)"); + } + + TEST(d, strtod("0x1p4", 0), 16.0, "hex float %a != %a"); + TEST(d, strtod("0x1.1p4", 0), 17.0, "hex float %a != %a"); + return test_status; +} + diff --git a/src/general/strtof.c b/src/general/strtof.c new file mode 100644 index 0000000..29876bc --- /dev/null +++ b/src/general/strtof.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include "test.h" + +#define length(x) (sizeof(x) / sizeof *(x)) + +static struct { + char *s; + float f; +} t[] = { + // 2^-149 * 0.5 - eps + {".7006492321624085354618647916449580656401309709382578858785341419448955413429303e-45", 0}, + // 2^-149 * 0.5 + eps + {".7006492321624085354618647916449580656401309709382578858785341419448955413429304e-45", 0x1p-149}, + // 2^-149 * 0.5 - eps + {".2101947696487225606385594374934874196920392912814773657635602425834686624028790e-44", 0x1p-149}, + // 2^-149 * 0.5 + eps + {".2101947696487225606385594374934874196920392912814773657635602425834686624028791e-44", 0x1p-148}, + // 2^-126 + 2^-150 - eps + {".1175494420887210724209590083408724842314472120785184615334540294131831453944281e-37", 0x1p-126}, + // 2^-126 + 2^-150 + eps + {".1175494420887210724209590083408724842314472120785184615334540294131831453944282e-37", 0x1.000002p-126}, + // 2^128 - 2^103 - eps + {"340282356779733661637539395458142568447.9999999999999999999", 0x1.fffffep127}, + // 2^128 - 2^103 + {"340282356779733661637539395458142568448", INFINITY}, +}; + +int main(void) +{ + int i; + float x; + char *p; + + for (i = 0; i < length(t); i++) { + x = strtof(t[i].s, &p); + if (x != t[i].f) + error("strtof(\"%s\") want %a got %a\n", t[i].s, t[i].f, x); + } + return test_status; +} diff --git a/src/general/strtol.c b/src/general/strtol.c new file mode 100644 index 0000000..81df887 --- /dev/null +++ b/src/general/strtol.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include "test.h" + +/* r = place to store result + * f = function call to test (or any expression) + * x = expected result + * m = message to print on failure (with formats for r & x) +**/ + +#define TEST(r, f, x, m) ( \ + errno = 0, msg = #f, ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST2(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", msg, r, x), 0) ) + +int main(void) +{ + int i; + long l; + unsigned long ul; + char *msg=""; + char *s, *c; + + TEST(l, atol("2147483647"), 2147483647L, "max 32bit signed %ld != %ld"); + TEST(l, strtol("2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld"); + TEST(ul, strtoul("4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu"); + + if (sizeof(long) == 4) { + TEST(l, strtol(s="2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld"); + TEST2(i, c-s, 10, "wrong final position %d != %d"); + TEST2(i, errno, ERANGE, "missing errno %d != %d"); + TEST(l, strtol(s="-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld"); + TEST2(i, c-s, 11, "wrong final position %d != %d"); + TEST2(i, errno, ERANGE, "missing errno %d != %d"); + TEST(ul, strtoul(s="4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu"); + TEST2(i, c-s, 10, "wrong final position %d != %d"); + TEST2(i, errno, ERANGE, "missing errno %d != %d"); + TEST(ul, strtoul(s="-1", &c, 0), -1UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 2, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + TEST(ul, strtoul(s="-2", &c, 0), -2UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 2, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + TEST(ul, strtoul(s="-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 11, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + TEST(ul, strtoul(s="-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 11, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + } else { + TEST(i, 0, 1, "64bit tests not implemented"); + } + + TEST(l, strtol("z", 0, 36), 35, "%ld != %ld"); + TEST(l, strtol("00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld"); + TEST(l, strtol(s="0F5F", &c, 16), 0x0f5f, "%ld != %ld"); + + TEST(l, strtol(s="0xz", &c, 16), 0, "%ld != %ld"); + TEST2(i, c-s, 1, "wrong final position %ld != %ld"); + + TEST(l, strtol(s="0x1234", &c, 16), 0x1234, "%ld != %ld"); + TEST2(i, c-s, 6, "wrong final position %ld != %ld"); + + c = NULL; + TEST(l, strtol(s="123", &c, 37), 0, "%ld != %ld"); + TEST2(i, c-s, 0, "wrong final position %d != %d"); + TEST2(i, errno, EINVAL, "%d != %d"); + return test_status; +} diff --git a/src/general/strtold.c b/src/general/strtold.c new file mode 100644 index 0000000..e37f7f0 --- /dev/null +++ b/src/general/strtold.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include "test.h" + +#define length(x) (sizeof(x) / sizeof *(x)) + +static struct { + char *s; + long double f; +} t[] = { + {"0", 0.0}, + {"12.345", 12.345L}, + {"1.2345e1", 12.345L}, + {"1e+1000000", INFINITY}, + {"1e-1000000", 0}, +#if LDBL_MANT_DIG == 53 + // 2^-1074 * 0.5 - eps + {".2470328229206232720882843964341106861825299013071623822127928412503377536351043e-323", 0}, + // 2^-1074 * 0.5 + eps + {".2470328229206232720882843964341106861825299013071623822127928412503377536351044e-323", 0x1p-1074}, + // 2^-1074 * 1.5 - eps + {".7410984687618698162648531893023320585475897039214871466383785237510132609053131e-323", 0x1p-1074}, + // 2^-1074 * 1.5 + eps + {".7410984687618698162648531893023320585475897039214871466383785237510132609053132e-323", 0x1p-1073}, + // 2^-1022 + 2^-1075 - eps + {".2225073858507201630123055637955676152503612414573018013083228724049586647606759e-307", 0x1p-1022}, + // 2^-1022 + 2^-1075 + eps + {".2225073858507201630123055637955676152503612414573018013083228724049586647606760e-307", 0x1.0000000000001p-1022}, + // 2^1024 - 2^970 - eps + {"17976931348623158079372897140530341507993413271003782693617377898044" + "49682927647509466490179775872070963302864166928879109465555478519404" + "02630657488671505820681908902000708383676273854845817711531764475730" + "27006985557136695962284291481986083493647529271907416844436551070434" + "2711559699508093042880177904174497791.999999999999999999999999999999", 0x1.fffffffffffffp1023}, + // 2^1024 - 2^970 + {"17976931348623158079372897140530341507993413271003782693617377898044" + "49682927647509466490179775872070963302864166928879109465555478519404" + "02630657488671505820681908902000708383676273854845817711531764475730" + "27006985557136695962284291481986083493647529271907416844436551070434" + "2711559699508093042880177904174497792", INFINITY}, + // some random numbers + {".5961860348131807091861002266453941950428e00", 0.59618603481318067}, // 0x1.313f4bc3b584cp-1 + {"1.815013169218038729887460898733526957442e-1", 0.18150131692180388}, // 0x1.73b6f662e1712p-3 + {"42.07082357534453600681618685682257590772e-2", 0.42070823575344535}, // 0x1.aece23c6e028dp-2 + {"665.4686306516261456328973225579833470816e-3", 0.66546863065162609}, // 0x1.54b84dea53453p-1 + {"6101.852922970868621786690495485449831753e-4", 0.61018529229708685}, // 0x1.386a34e5d516bp-1 + {"76966.95208236968077849464348875471158549e-5", 0.76966952082369677}, // 0x1.8a121f9954dfap-1 + {"250506.5322228682496132604807222923702304e-6", 0.25050653222286823}, // 0x1.0084c8cd538c2p-2 + {"2740037.230228005325852424697698331177377e-7", 0.27400372302280052}, // 0x1.18946e9575ef4p-2 + {"20723093.50049742645941529268715428324490e-8", 0.20723093500497428}, // 0x1.a868b14486e4dp-3 + {"0.7900280238081604956226011047460238748912e1", 7.9002802380816046}, // 0x1.f99e3100f2eaep+2 + {"0.9822860653737296848190558448760465863597e2", 98.228606537372968}, // 0x1.88ea17d506accp+6 + {"0.7468949723190370809405570560160405324869e3", 746.89497231903704}, // 0x1.75728e73f48b7p+9 + {"0.1630268320282728475980459844271031751665e4", 1630.2683202827284}, // 0x1.97912c28d5cbp+10 + {"0.4637168629719170695109918769645492022088e5", 46371.686297191707}, // 0x1.6a475f6258737p+15 + {"0.6537805944497711554209461686415872067523e6", 653780.59444977110}, // 0x1.3f3a9305bb86cp+19 + {"0.2346324356502437045212230713960457676531e6", 234632.43565024371}, // 0x1.ca4437c3631eap+17 + {"0.9709481716420048341897258980454298205278e8", 97094817.164200485}, // 0x1.7263284a8242cp+26 + {"0.4996908522051874110779982354932499499602e9", 499690852.20518744}, // 0x1.dc8ad6434872ap+28 +#elif LDBL_MANT_DIG == 64 + // 2^-16445 * 0.5 - eps + {".1822599765941237301264202966809709908199525407846781671860490243514185844316698e-4950", 0}, + // 2^-16445 * 0.5 + eps + {".1822599765941237301264202966809709908199525407846781671860490243514185844316699e-4950", 0x1p-16445L}, + // 2^-16445 * 1.5 - eps + {".5467799297823711903792608900429129724598576223540345015581470730542557532950096e-4950", 0x1p-16445L}, + // 2^-16445 * 1.5 + eps + {".5467799297823711903792608900429129724598576223540345015581470730542557532950097e-4950", 0x1p-16444L}, + // 2^-16382 + 2^-16446 - eps + {".3362103143112093506444937793915876332724499641527442230928779770593420866576777e-4931", 0x1p-16382L}, + // 2^-16382 + 2^-16446 + eps + {".3362103143112093506444937793915876332724499641527442230928779770593420866576778e-4931", 0x1.0000000000000002p-16382L}, + // 2^16384 - 2^16319 - eps + {"118973149535723176505351158982948.86679662540046955672e4900", 0x1.fffffffffffffffep16383L}, + // 2^16384 - 2^16319 + eps + {"118973149535723176505351158982948.86679662540046955673e4900", INFINITY}, +#endif +}; + +int main(void) +{ + int i; + long double x; + char *p; + + for (i = 0; i < length(t); i++) { + x = strtold(t[i].s, &p); + if (x != t[i].f) + error("strtold(\"%s\") want %La got %La\n", t[i].s, t[i].f, x); + } + return test_status; +} diff --git a/src/general/swprintf.c b/src/general/swprintf.c new file mode 100644 index 0000000..189de33 --- /dev/null +++ b/src/general/swprintf.c @@ -0,0 +1,141 @@ +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 700 +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_S(s, x, m) ( \ + !wcscmp((s),(x)) || \ + (error("[%ls] != [%ls] (%s)\n", s, x, m), 0) ) + +static const struct { + const wchar_t *fmt; + int i; + const wchar_t *expect; +} int_tests[] = { + /* width, precision, alignment */ + { L"%04d", 12, L"0012" }, + { L"%.3d", 12, L"012" }, + { L"%3d", 12, L" 12" }, + { L"%-3d", 12, L"12 " }, + { L"%+3d", 12, L"+12" }, + { L"%+-5d", 12, L"+12 " }, + { L"%+- 5d", 12, L"+12 " }, + { L"%- 5d", 12, L" 12 " }, + { L"% d", 12, L" 12" }, + { L"%0-5d", 12, L"12 " }, + { L"%-05d", 12, L"12 " }, + + /* ...explicit precision of 0 shall be no characters. */ + { L"%.0d", 0, L"" }, + { L"%.0o", 0, L"" }, + { L"%#.0d", 0, L"" }, + { L"%#.0o", 0, L"" }, + { L"%#.0x", 0, L"" }, + + /* hex: test alt form and case */ + { L"%x", 63, L"3f" }, + { L"%#x", 63, L"0x3f" }, + { L"%X", 63, L"3F" }, + + /* octal: test alt form */ + { L"%o", 15, L"17" }, + { L"%#o", 15, L"017" }, + + { NULL, 0.0, NULL } +}; + +static const struct { + const wchar_t *fmt; + double f; + const wchar_t *expect; +} fp_tests[] = { + /* basic form, handling of exponent/precision for 0 */ + { L"%e", 0.0, L"0.000000e+00" }, + { L"%f", 0.0, L"0.000000" }, + { L"%g", 0.0, L"0" }, + { L"%#g", 0.0, L"0.00000" }, + + /* rounding */ + { L"%f", 1.1, L"1.100000" }, + { L"%f", 1.2, L"1.200000" }, + { L"%f", 1.3, L"1.300000" }, + { L"%f", 1.4, L"1.400000" }, + { L"%f", 1.5, L"1.500000" }, + + /* correctness in DBL_DIG places */ + { L"%.15g", 1.23456789012345, L"1.23456789012345" }, + + /* correct choice of notation for %g */ + { L"%g", 0.0001, L"0.0001" }, + { L"%g", 0.00001, L"1e-05" }, + { L"%g", 123456, L"123456" }, + { L"%g", 1234567, L"1.23457e+06" }, + { L"%.7g", 1234567, L"1234567" }, + { L"%.7g", 12345678, L"1.234568e+07" }, + + /* pi in double precision, printed to a few extra places */ + { L"%.15f", M_PI, L"3.141592653589793" }, + { L"%.18f", M_PI, L"3.141592653589793116" }, + + /* exact conversion of large integers */ + { L"%.0f", 340282366920938463463374607431768211456.0, + L"340282366920938463463374607431768211456" }, + + { NULL, 0.0, NULL } +}; + +int main(void) +{ + int i, j; + wchar_t b[500]; + + (void)( + setlocale(LC_CTYPE, "en_US.UTF-8") || + setlocale(LC_CTYPE, "en_GB.UTF-8") || + setlocale(LC_CTYPE, "en.UTF-8") || + setlocale(LC_CTYPE, "POSIX.UTF-8") || + setlocale(LC_CTYPE, "C.UTF-8") || + setlocale(LC_CTYPE, "UTF-8") || + setlocale(LC_CTYPE, "") ); + + TEST(i, strcmp(nl_langinfo(CODESET), "UTF-8"), 0, "no UTF-8 locale; tests might fail"); + + TEST(i, swprintf(0, 0, L"%d", 123456)<0, 1, "%d != %d"); + + TEST(i, swprintf(b, 2, L"%lc", 0xc0), 1, "%d != %d"); + TEST(i, b[0], 0xc0, "wrong character %x != %x"); + TEST(i, swprintf(b, 2, L"%lc", 0x20ac), 1, "%d != %d"); + TEST(i, b[0], 0x20ac, "wrong character %x != %x"); + TEST(i, swprintf(b, 3, L"%s", "\xc3\x80!"), 2, "%d != %d"); + TEST(i, b[0], 0xc0, "wrong character %x != %x"); + TEST(i, swprintf(b, 2, L"%.1s", "\xc3\x80!"), 1, "%d != %d"); + TEST(i, b[0], 0xc0, "wrong character %x != %x"); + + wcscpy(b, L"xxxxxxxx"); + TEST(i, swprintf(b, 4, L"%d", 123456)<0, 1, "%d != %d"); + TEST_S(b, L"123", "incorrect output"); + TEST(i, b[5], 'x', "buffer overrun"); + + for (j=0; int_tests[j].fmt; j++) { + TEST(i, swprintf(b, sizeof b/sizeof *b, int_tests[j].fmt, int_tests[j].i), wcslen(b), "%d != %d"); + TEST_S(b, int_tests[j].expect, "bad integer conversion"); + } + + for (j=0; fp_tests[j].fmt; j++) { + TEST(i, swprintf(b, sizeof b/sizeof *b, fp_tests[j].fmt, fp_tests[j].f), wcslen(b), "%d != %d"); + TEST_S(b, fp_tests[j].expect, "bad floating point conversion"); + } + return test_status; +} diff --git a/src/general/test.h b/src/general/test.h new file mode 100644 index 0000000..a6a7706 --- /dev/null +++ b/src/general/test.h @@ -0,0 +1,20 @@ +#include +#include +#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) + +static int test_status; + +static int test_error(const char *n, int l, const char *s, ...) +{ + va_list ap; + + if (test_status == 0) + printf("FAIL\n"); + test_status = 1; + printf(" ERROR %s:%d: ", n, l); + va_start(ap, s); + vprintf(s, ap); + va_end(ap); + return -1; +} + diff --git a/src/general/time.c b/src/general/time.c new file mode 100644 index 0000000..27598b8 --- /dev/null +++ b/src/general/time.c @@ -0,0 +1,76 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include "test.h" + +/* We use this instead of memcmp because some broken C libraries + * add additional nonstandard fields to struct tm... */ + +int tm_cmp(struct tm tm1, struct tm tm2) +{ + return tm1.tm_sec != tm2.tm_sec || + tm1.tm_min != tm2.tm_min || + tm1.tm_hour != tm2.tm_hour || + tm1.tm_mday != tm2.tm_mday || + tm1.tm_mon != tm2.tm_mon || + tm1.tm_year != tm2.tm_year || + tm1.tm_wday != tm2.tm_wday || + tm1.tm_yday != tm2.tm_yday || + tm1.tm_isdst!= tm2.tm_isdst; +} + +char *tm_str(struct tm tm) +{ + static int i; + static char b[4][64]; + i = (i+1)%4; + snprintf(b[i], sizeof b[i], + "s=%02d m=%02d h=%02d mday=%02d mon=%02d year=%04d wday=%d yday=%d isdst=%d", + tm.tm_sec, tm.tm_min, tm.tm_hour, + tm.tm_mday, tm.tm_mon, tm.tm_year, + tm.tm_wday, tm.tm_yday, tm.tm_isdst); + return b[i]; +} + +#define TM(ss,mm,hh,md,mo,yr,wd,yd,dst) (struct tm){ \ + .tm_sec = ss, .tm_min = mm, .tm_hour = hh, \ + .tm_mday = md, .tm_mon = mo, .tm_year = yr, \ + .tm_wday = wd, .tm_yday = yd, .tm_isdst = dst } + +#define TM_EPOCH TM(0,0,0,1,0,70,4,0,0) +#define TM_Y2038_1S TM(7,14,3,19,0,138,2,18,0) +#define TM_Y2038 TM(8,14,3,19,0,138,2,18,0) + +#define TEST_TM(r,x,m) (!tm_cmp((r),(x)) || \ + (error("%s failed:\n\tresult: %s\n\texpect: %s\n", \ + m, tm_str(r), tm_str(x)), 0) ) + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x), 0) ) + +int main(void) +{ + struct tm tm, *tm_p; + time_t t; + + putenv("TZ=GMT"); + tzset(); + + t=0; tm_p = gmtime(&t); + TEST_TM(*tm_p, TM_EPOCH, "gmtime(0)"); + + tm = TM_Y2038_1S; + t = mktime(&tm); + tm = *(gmtime(&t)); + TEST_TM(*tm_p, TM_Y2038_1S, "mktime/gmtime(Y2038-1)"); + + tm = TM_Y2038; + t = mktime(&tm); + tm = *(gmtime(&t)); + TEST_TM(*tm_p, TM_Y2038, "mktime/gmtime(Y2038)"); + + /* FIXME: set a TZ var and check DST boundary conditions */ + return test_status; +} diff --git a/src/general/tls_align.c b/src/general/tls_align.c new file mode 100644 index 0000000..505abf2 --- /dev/null +++ b/src/general/tls_align.c @@ -0,0 +1,22 @@ +#include "test.h" + +extern struct { + char *name; + unsigned size; + unsigned align; + unsigned long addr; +} t[4]; + +int main() +{ + int i; + + for (i = 0; i < sizeof t/sizeof *t; i++) { + if (!t[i].name) + error("name is not set for t[%d]\n", i); + if (t[i].addr & (t[i].align-1)) + error("bad alignment: %s, size: %u, align: %u, addr: 0x%lx\n", + t[i].name, t[i].size, t[i].align, t[i].addr); + } + return test_status; +} diff --git a/src/general/tls_align_dlopen.c b/src/general/tls_align_dlopen.c new file mode 100644 index 0000000..64517e1 --- /dev/null +++ b/src/general/tls_align_dlopen.c @@ -0,0 +1,30 @@ +#include +#include "test.h" + +int main() +{ + int i; + void *h; + struct { + char *name; + unsigned size; + unsigned align; + unsigned long addr; + } *t; + + h = dlopen("./tls_align_dso.so", RTLD_LAZY); + if (!h) + error("dlopen failed\n"); + t = dlsym(h, "t"); + if (!t) + error("dlsym failed\n"); + + for (i = 0; i < 4; i++) { + if (!t[i].name) + error("name is not set for t[%d]\n", i); + if (t[i].addr & (t[i].align-1)) + error("bad alignment: %s, size: %u, align: %u, addr: 0x%lx\n", + t[i].name, t[i].size, t[i].align, t[i].addr); + } + return test_status; +} diff --git a/src/general/tls_align_dso.c b/src/general/tls_align_dso.c new file mode 100644 index 0000000..9ca759e --- /dev/null +++ b/src/general/tls_align_dso.c @@ -0,0 +1,30 @@ +__thread char c1 = 1; +__thread char xchar = 2; +__thread char c2 = 3; +__thread short xshort = 4; +__thread char c3 = 5; +__thread int xint = 6; +__thread char c4 = 7; +__thread long long xllong = 8; + +struct { + char *name; + unsigned size; + unsigned align; + unsigned long addr; +} t[4]; + +#define entry(i,x) \ + t[i].name = #x; \ + t[i].size = sizeof x; \ + t[i].align = __alignof__(x); \ + t[i].addr = (unsigned long)&x; + +__attribute__((constructor)) static void init(void) +{ + entry(0, xchar) + entry(1, xshort) + entry(2, xint) + entry(3, xllong) +} + diff --git a/src/general/tls_init.c b/src/general/tls_init.c new file mode 100644 index 0000000..8a43829 --- /dev/null +++ b/src/general/tls_init.c @@ -0,0 +1,42 @@ +#include +#include "test.h" + +__thread int tls_fix = 23; +__thread int tls_zero; + +static void *f(void *arg) +{ + if (tls_fix != 23) + error("fixed init failed: want 23 got %d\n", tls_fix); + if (tls_zero != 0) + error("zero init failed: want 0 got %d\n", tls_zero); + tls_fix++; + tls_zero++; + return 0; +} + +#define CHECK(f) do{ if(f) error("%s failed.\n", #f); }while(0) +#define length(a) (sizeof(a)/sizeof*(a)) + +int main() +{ + pthread_t t[5]; + int i, j; + + if (tls_fix != 23) + error("fixed init failed: want 23 got %d\n", tls_fix); + if (tls_zero != 0) + error("zero init failed: want 0 got %d\n", tls_zero); + + for (j = 0; j < 2; j++) { + for (i = 0; i < length(t); i++) { + CHECK(pthread_create(t+i, 0, f, 0)); + tls_fix++; + tls_zero++; + } + for (i = 0; i < length(t); i++) + CHECK(pthread_join(t[i], 0)); + } + + return test_status; +} diff --git a/src/general/ungetc.c b/src/general/ungetc.c new file mode 100644 index 0000000..9003a1c --- /dev/null +++ b/src/general/ungetc.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ + errno = 0, ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +int main(void) +{ + int i; + char a[100]; + FILE *f; + + TEST(i, !(f = tmpfile()), 0, "failed to create temp file %d!=%d (%s)"); + + if (!f) return test_status; + + TEST(i, fprintf(f, "hello, world\n"), 13, "%d != %d (%m)"); + TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d (%m)"); + + TEST(i, feof(f), 0, "%d != %d"); + TEST(i, fgetc(f), 'h', "'%c' != '%c'"); + TEST(i, ftell(f), 1, "%d != %d"); + TEST(i, ungetc('x', f), 'x', "%d != %d"); + TEST(i, ftell(f), 0, "%d != %d"); + TEST(i, fscanf(f, "%[h]", a), 0, "got %d fields, expected %d"); + TEST(i, ftell(f), 0, "%d != %d"); + TEST(i, fgetc(f), 'x', "'%c' != '%c'"); + TEST(i, ftell(f), 1, "%d != %d"); + + TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d"); + TEST(i, ungetc('x', f), 'x', "%d != %d"); + TEST(i, fread(a, 1, sizeof a, f), 14, "read %d, expected %d"); + a[14] = 0; + TEST_S(a, "xhello, world\n", "mismatch reading ungot character"); + + TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d"); + TEST(i, fscanf(f, "%[x]", a), 0, "got %d fields, expected %d"); + TEST(i, ungetc('x', f), 'x', "unget failed after fscanf: %d != %d"); + TEST(i, fgetc(f), 'x', "'%c' != '%c'"); + TEST(i, fgetc(f), 'h', "'%c' != '%c'"); + + fclose(f); + return test_status; +} diff --git a/src/general/wcstol.c b/src/general/wcstol.c new file mode 100644 index 0000000..abd76af --- /dev/null +++ b/src/general/wcstol.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ + errno = 0, msg = #f, ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST2(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (error("%s failed (" m ")\n", msg, r, x), 0) ) + +int main(void) +{ + int i; + long l; + unsigned long ul; + char *msg=""; + wchar_t *s, *c; + + TEST(l, wcstol(L"2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld"); + TEST(ul, wcstoul(L"4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu"); + + if (sizeof(long) == 4) { + TEST(l, wcstol(s=L"2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld"); + TEST2(i, c-s, 10, "wrong final position %d != %d"); + TEST2(i, errno, ERANGE, "missing errno %d != %d"); + TEST(l, wcstol(s=L"-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld"); + TEST2(i, c-s, 11, "wrong final position %d != %d"); + TEST2(i, errno, ERANGE, "missing errno %d != %d"); + TEST(ul, wcstoul(s=L"4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu"); + TEST2(i, c-s, 10, "wrong final position %d != %d"); + TEST2(i, errno, ERANGE, "missing errno %d != %d"); + TEST(ul, wcstoul(s=L"-1", &c, 0), -1UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 2, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + TEST(ul, wcstoul(s=L"-2", &c, 0), -2UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 2, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + TEST(ul, wcstoul(s=L"-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 11, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + TEST(ul, wcstoul(s=L"-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 11, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + } else { + TEST(i, 0, 1, "64bit tests not implemented"); + } + + TEST(l, wcstol(L"z", 0, 36), 35, "%ld != %ld"); + TEST(l, wcstol(L"00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld"); + + TEST(l, wcstol(s=L"0xz", &c, 16), 0, "%ld != %ld"); + TEST2(i, c-s, 1, "wrong final position %ld != %ld"); + + TEST(l, wcstol(s=L"0x1234", &c, 16), 0x1234, "%ld != %ld"); + TEST2(i, c-s, 6, "wrong final position %ld != %ld"); + + c = NULL; + TEST(l, wcstol(s=L"123", &c, 37), 0, "%ld != %ld"); + TEST2(i, c-s, 0, "wrong final position %d != %d"); + TEST2(i, errno, EINVAL, "%d != %d"); + return test_status; +} diff --git a/src/ldso/Makefile b/src/ldso/Makefile deleted file mode 100644 index ed7424c..0000000 --- a/src/ldso/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -include ../../Makefile.inc -LDFLAGS+=-ldl - diff --git a/src/ldso/dlopen.c b/src/ldso/dlopen.c deleted file mode 100644 index d3c46f0..0000000 --- a/src/ldso/dlopen.c +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include "test.h" - -int main() -{ - void *h, *g; - int *i, *i2; - char *s; - void (*f)(void); - - h = dlopen("./dlopen_dso.so", RTLD_LAZY|RTLD_LOCAL); - if (!h) - error("dlopen ./dlopen_dso.so failed: %s\n", dlerror()); - i = dlsym(h, "i"); - if (!i) - error("dlsym i failed: %s\n", dlerror()); - if (*i != 1) - error("initialization failed: want i=1 got i=%d\n", *i); - f = (void (*)(void))dlsym(h, "f"); - if (!f) - error("dlsym f failed: %s\n", dlerror()); - f(); - if (*i != 2) - error("f call failed: want i=2 got i=%d\n", *i); - g = dlopen(0, RTLD_LAZY|RTLD_LOCAL); - if (!g) - error("dlopen 0 failed: %s\n", dlerror()); - i2 = dlsym(g, "i"); - s = dlerror(); - if (i2 || s == 0) - error("dlsym i should have failed\n"); - h = dlopen("./dlopen_dso.so", RTLD_LAZY|RTLD_GLOBAL); - i2 = dlsym(g, "i"); - if (!i2) - error("dlsym i failed: %s\n", dlerror()); - if (*i2 != 2) - error("want i2=2, got i2=%d\n", *i2); - return test_status; -} diff --git a/src/ldso/dlopen_dso.c b/src/ldso/dlopen_dso.c deleted file mode 100644 index cb3220a..0000000 --- a/src/ldso/dlopen_dso.c +++ /dev/null @@ -1,6 +0,0 @@ -int i = 1; - -void f(void) -{ - i++; -} diff --git a/src/ldso/test.h b/src/ldso/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/ldso/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/misc/Makefile b/src/misc/Makefile deleted file mode 100644 index ee4552b..0000000 --- a/src/misc/Makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../Makefile.inc diff --git a/src/misc/basename.c b/src/misc/basename.c deleted file mode 100644 index a993587..0000000 --- a/src/misc/basename.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -#include "test.h" - -#define T(path, want) \ -{ \ - char tmp[100]; \ - char *got = basename(strcpy(tmp, path)); \ - if (strcmp(want, got) != 0) \ - error("basename(\"%s\") got \"%s\" want \"%s\"\n", path, got, want); \ -} - -int main() -{ - if (strcmp(basename(0), ".") != 0) - error("basename(0) returned \"%s\"; expected \".\"\n", basename(0)); - T("", "."); - T("/usr/lib", "lib"); - T("/usr/", "usr"); - T("usr/", "usr"); - T("/", "/"); - T("///", "/"); - T("//usr//lib//", "lib"); - T(".", "."); - T("..", ".."); - return test_status; -} diff --git a/src/misc/dirname.c b/src/misc/dirname.c deleted file mode 100644 index 7a15a0a..0000000 --- a/src/misc/dirname.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -#include "test.h" - -#define T(path, want) \ -{ \ - char tmp[100]; \ - char *got = dirname(strcpy(tmp, path)); \ - if (strcmp(want, got) != 0) \ - error("dirname(\"%s\") got \"%s\" want \"%s\"\n", path, got, want); \ -} - -int main() -{ - if (strcmp(dirname(0), ".") != 0) - error("dirname(0) returned \"%s\"; expected \".\"\n", dirname(0)); - T("", "."); - T("/usr/lib", "/usr"); - T("/usr/", "/"); - T("usr", "."); - T("usr/", "."); - T("/", "/"); - T("///", "/"); - T(".", "."); - T("..", "."); - return test_status; -} diff --git a/src/misc/test.h b/src/misc/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/misc/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/multibyte/Makefile b/src/multibyte/Makefile deleted file mode 100644 index ee4552b..0000000 --- a/src/multibyte/Makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../Makefile.inc diff --git a/src/multibyte/mbc.c b/src/multibyte/mbc.c deleted file mode 100644 index 56c546b..0000000 --- a/src/multibyte/mbc.c +++ /dev/null @@ -1,85 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "test.h" - -/* - * f = function call to test (or any expression) - * x = expected result - * m = message to print on failure - */ -#define T(f, x, m) (void)( \ - memset(&st, 0, sizeof st), \ - (i = (f)) == (x) || \ - error("%s failed (%s) got %d want %d\n", #f, m, i, x) ) -#define TCHAR(f, x, m) (void)( \ - memset(&st, 0, sizeof st), \ - (i = (f)) == (x) || \ - error("%s failed (%s) got 0x%04x want 0x%04x\n", #f, m, i, x) ) - -int main(void) -{ - const char *cs; - int i; - mbstate_t st, st2; - wchar_t wc, wcs[32]; - - (void)( - setlocale(LC_CTYPE, "en_US.UTF-8") || - setlocale(LC_CTYPE, "en_GB.UTF-8") || - setlocale(LC_CTYPE, "en.UTF-8") || - setlocale(LC_CTYPE, "POSIX.UTF-8") || - setlocale(LC_CTYPE, "C.UTF-8") || - setlocale(LC_CTYPE, "UTF-8") || - setlocale(LC_CTYPE, "") ); - - T(mbsrtowcs(wcs, (cs="abcdef",&cs), 3, &st), 3, "wrong semantics for wcs buf len"); - T(mbsrtowcs(wcs, (cs="abcdef",&cs), 8, &st), 6, "wrong semantics for wcs buf len"); - T(mbsrtowcs(NULL, (cs="abcdef",&cs), 2, &st), 6, "wrong semantics for NULL wcs"); - - if (strcmp(nl_langinfo(CODESET), "UTF-8")) - return error("cannot set UTF-8 locale for test (codeset=%s)\n", nl_langinfo(CODESET)); - - T(mbrtowc(&wc, "\x80", 1, &st), -1, "failed to catch error"); - T(mbrtowc(&wc, "\xc0", 1, &st), -1, "failed to catch illegal initial"); - - T(mbrtowc(&wc, "\xc0\x80", 2, &st), -1, "aliasing nul"); - T(mbrtowc(&wc, "\xc0\xaf", 2, &st), -1, "aliasing slash"); - T(mbrtowc(&wc, "\xe0\x80\xaf", 3, &st), -1, "aliasing slash"); - T(mbrtowc(&wc, "\xf0\x80\x80\xaf", 4, &st), -1, "aliasing slash"); - T(mbrtowc(&wc, "\xf8\x80\x80\x80\xaf", 5, &st), -1, "aliasing slash"); - T(mbrtowc(&wc, "\xfc\x80\x80\x80\x80\xaf", 6, &st), -1, "aliasing slash"); - T(mbrtowc(&wc, "\xe0\x82\x80", 3, &st), -1, "aliasing U+0080"); - T(mbrtowc(&wc, "\xe0\x9f\xbf", 3, &st), -1, "aliasing U+07FF"); - T(mbrtowc(&wc, "\xf0\x80\xa0\x80", 4, &st), -1, "aliasing U+0800"); - T(mbrtowc(&wc, "\xf0\x8f\xbf\xbd", 4, &st), -1, "aliasing U+FFFD"); - - T(mbrtowc(&wc, "\xed\xa0\x80", 3, &st), -1, "failed to catch surrogate"); - T(mbrtowc(&wc, "\xef\xbf\xbe", 3, &st), 3, "failed to accept U+FFFE"); - T(mbrtowc(&wc, "\xef\xbf\xbf", 3, &st), 3, "failed to accept U+FFFF"); - T(mbrtowc(&wc, "\xf4\x8f\xbf\xbe", 4, &st), 4, "failed to accept U+10FFFE"); - T(mbrtowc(&wc, "\xf4\x8f\xbf\xbf", 4, &st), 4, "failed to accept U+10FFFF"); - - T(mbrtowc(&wc, "\xc2\x80", 2, &st), 2, "wrong length"); - TCHAR((mbrtowc(&wc, "\xc2\x80", 2, &st),wc), 0x80, "wrong char"); - T(mbrtowc(&wc, "\xe0\xa0\x80", 3, &st), 3, "wrong length"); - TCHAR((mbrtowc(&wc, "\xe0\xa0\x80", 3, &st),wc), 0x800, "wrong char"); - T(mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st), 4, "wrong length"); - TCHAR((mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st),wc), 0x10000, "wrong char"); - - memset(&st2, 0, sizeof st2); - T(mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte"); - T(mbrtowc(&wc, "\x80", 1, &st2), 1, "failed to resume"); - TCHAR(wc, 0x80, "wrong char"); - - memset(&st2, 0, sizeof st2); - T(mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte"); - T(mbsrtowcs(wcs, (cs="\xa0""abc",&cs), 32, &st2), 4, "failed to resume"); - TCHAR(wcs[0], 0xa0, "wrong char"); - TCHAR(wcs[1], 'a', "wrong char"); - T(!cs, 1, "wrong final position"); - return test_status; -} diff --git a/src/multibyte/test.h b/src/multibyte/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/multibyte/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/process/Makefile b/src/process/Makefile deleted file mode 100644 index ee4552b..0000000 --- a/src/process/Makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../Makefile.inc diff --git a/src/process/spawn.c b/src/process/spawn.c deleted file mode 100644 index 707b1a5..0000000 --- a/src/process/spawn.c +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 700 -#endif -#include -#include -#include -#include -#include -#include -#include -#include "test.h" - -#define TEST(f, x) (void)( \ - (r = (f)) == (x) || \ - error("%s failed, got %d want %d\n", #f, r, x) ) - -#define TEST_E(f) (void)( \ - (errno = 0), (f) || \ - error("%s failed (errno = %d \"%s\")\n", #f, errno, strerror(errno)) ) - -int main(void) -{ - int r; - char foo[10]; - int p[2]; - pid_t pid; - int status; - posix_spawn_file_actions_t fa; - - TEST_E(!pipe(p)); - TEST(posix_spawn_file_actions_init(&fa), 0); - TEST(posix_spawn_file_actions_addclose(&fa, p[0]), 0); - TEST(posix_spawn_file_actions_adddup2(&fa, p[1], 1), 0); - TEST(posix_spawn_file_actions_addclose(&fa, p[1]), 0); - TEST(posix_spawnp(&pid, "echo", &fa, 0, (char *[]){"echo","hello",0}, 0), 0); - close(p[1]); - TEST(waitpid(pid, &status, 0), pid); - TEST(read(p[0], foo, sizeof foo), 6); - close(p[0]); - TEST(posix_spawn_file_actions_destroy(&fa), 0); - return test_status; -} diff --git a/src/process/test.h b/src/process/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/process/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/regex/Makefile b/src/regex/Makefile deleted file mode 100644 index ee4552b..0000000 --- a/src/regex/Makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../Makefile.inc diff --git a/src/regex/fnmatch.c b/src/regex/fnmatch.c deleted file mode 100644 index 33f508f..0000000 --- a/src/regex/fnmatch.c +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include -#include -#include -#include "test.h" - -/* adapted from dietlibc's test-newfnmatch.c */ - -/* xlat / printflags adapted from http://www.liacs.nl/~wichert/strace/ */ -#define FLAG(f) { f, #f } - -struct xlat { - int val; - char *str; -} fnmatch_flags[] = { - FLAG(FNM_NOESCAPE), - FLAG(FNM_PATHNAME), - FLAG(FNM_PERIOD), - {0, NULL}, -}; - -static char *flagstr(const struct xlat *map, int flags) -{ - static char buf[1000]; - char *sep; - - if (!flags) { - sprintf(buf, "0"); - return buf; - } - sep = ""; - for (; map->str; map++) { - if (map->val && (flags & map->val) == map->val) { - sprintf(buf, "%s%s", sep, map->str); - sep = "|"; - flags &= ~(map->val); - } - } - if (flags) - sprintf(buf, "%sunknown=%#x", sep, flags); - return buf; -} - -/* tests harness adapted from glibc testfnm.c */ -struct { - const char *pattern; - const char *string; - int flags; - int expected; -} tests[] = { - /* begin dietlibc tests */ - { "*.c", "foo.c", 0, 0 }, - { "*.c", ".c", 0, 0 }, - { "*.a", "foo.c", 0, FNM_NOMATCH }, - { "*.c", ".foo.c", 0, 0 }, - { "*.c", ".foo.c", FNM_PERIOD, FNM_NOMATCH }, - { "*.c", "foo.c", FNM_PERIOD, 0 }, - { "a\\*.c", "a*.c", FNM_NOESCAPE, FNM_NOMATCH }, - { "a\\*.c", "ax.c", 0, FNM_NOMATCH }, - { "a[xy].c", "ax.c", 0, 0 }, - { "a[!y].c", "ax.c", 0, 0 }, - { "a[a/z]*.c", "a/x.c", FNM_PATHNAME, FNM_NOMATCH }, - { "a/*.c", "a/x.c", FNM_PATHNAME, 0 }, - { "a*.c", "a/x.c", FNM_PATHNAME, FNM_NOMATCH }, - { "*/foo", "/foo", FNM_PATHNAME, 0 }, - { "-O[01]", "-O1", 0, 0 }, - { "[[?*\\]", "\\", 0, 0 }, - { "[]?*\\]", "]", 0, 0 }, - /* initial right-bracket tests */ - { "[!]a-]", "b", 0, 0 }, - { "[]-_]", "^", 0, 0 }, /* range: ']', '^', '_' */ - { "[!]-_]", "X", 0, 0 }, - { "??", "-", 0, FNM_NOMATCH }, - /* begin glibc tests */ - { "*LIB*", "lib", FNM_PERIOD, FNM_NOMATCH }, - { "a[/]b", "a/b", 0, 0 }, - { "a[/]b", "a/b", FNM_PATHNAME, FNM_NOMATCH }, - { "[a-z]/[a-z]", "a/b", 0, 0 }, - { "*", "a/b", FNM_PATHNAME, FNM_NOMATCH }, - { "*[/]b", "a/b", FNM_PATHNAME, FNM_NOMATCH }, - { "*[b]", "a/b", FNM_PATHNAME, FNM_NOMATCH }, - { "[*]/b", "a/b", 0, FNM_NOMATCH }, - { "[*]/b", "*/b", 0, 0 }, - { "[?]/b", "a/b", 0, FNM_NOMATCH }, - { "[?]/b", "?/b", 0, 0 }, - { "[[a]/b", "a/b", 0, 0 }, - { "[[a]/b", "[/b", 0, 0 }, - { "\\*/b", "a/b", 0, FNM_NOMATCH }, - { "\\*/b", "*/b", 0, 0 }, - { "\\?/b", "a/b", 0, FNM_NOMATCH }, - { "\\?/b", "?/b", 0, 0 }, - { "[/b", "[/b", 0, 0 }, - { "\\[/b", "[/b", 0, 0 }, - { "??""/b", "aa/b", 0, 0 }, - { "???b", "aa/b", 0, 0 }, - { "???b", "aa/b", FNM_PATHNAME, FNM_NOMATCH }, - { "?a/b", ".a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, - { "a/?b", "a/.b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, - { "*a/b", ".a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, - { "a/*b", "a/.b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, - { "[.]a/b", ".a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, - { "a/[.]b", "a/.b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, - { "*/?", "a/b", FNM_PATHNAME|FNM_PERIOD, 0 }, - { "?/*", "a/b", FNM_PATHNAME|FNM_PERIOD, 0 }, - { ".*/?", ".a/b", FNM_PATHNAME|FNM_PERIOD, 0 }, - { "*/.?", "a/.b", FNM_PATHNAME|FNM_PERIOD, 0 }, - { "*/*", "a/.b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, - { "*?*/*", "a/.b", FNM_PERIOD, 0 }, - { "*[.]/b", "a./b", FNM_PATHNAME|FNM_PERIOD, 0 }, - { "*[[:alpha:]]/*[[:alnum:]]", "a/b", FNM_PATHNAME, 0 }, - /* These three tests should result in error according to SUSv3. - * See XCU 2.13.1, XBD 9.3.5, & fnmatch() */ - { "*[![:digit:]]*/[![:d-d]", "a/b", FNM_PATHNAME, -FNM_NOMATCH }, - { "*[![:digit:]]*/[[:d-d]", "a/[", FNM_PATHNAME, -FNM_NOMATCH }, - { "*[![:digit:]]*/[![:d-d]", "a/[", FNM_PATHNAME, -FNM_NOMATCH }, - { "a?b", "a.b", FNM_PATHNAME|FNM_PERIOD, 0 }, - { "a*b", "a.b", FNM_PATHNAME|FNM_PERIOD, 0 }, - { "a[.]b", "a.b", FNM_PATHNAME|FNM_PERIOD, 0 }, -}; - -int main(void) -{ - int i; - - for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) { - int r, x; - - r = fnmatch(tests[i].pattern, tests[i].string, tests[i].flags); - x = tests[i].expected; - if (r != x && (r != FNM_NOMATCH || x != -FNM_NOMATCH)) { - error("fnmatch(\"%s\", \"%s\", %s) failed, got %d want %d\n", - tests[i].pattern, tests[i].string, - flagstr(fnmatch_flags, tests[i].flags), - r, x); - } - } - return test_status; -} diff --git a/src/regex/test.h b/src/regex/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/regex/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/stdio/Makefile b/src/stdio/Makefile deleted file mode 100644 index ee4552b..0000000 --- a/src/stdio/Makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../Makefile.inc diff --git a/src/stdio/fdopen.c b/src/stdio/fdopen.c deleted file mode 100644 index 98a849f..0000000 --- a/src/stdio/fdopen.c +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 700 -#endif -#include -#include -#include -#include -#include -#include "test.h" - -#define TEST(c) do { \ - errno = 0; \ - if (!(c)) \ - error("%s failed (errno = %d)\n", #c, errno); \ -} while(0) - -int main(void) -{ - char tmp[] = "/tmp/testsuite-XXXXXX"; - char foo[6]; - int fd; - FILE *f; - - TEST((fd = mkstemp(tmp)) > 2); - TEST(write(fd, "hello", 6)==6); - TEST(f = fdopen(fd, "rb")); - if (f) { - TEST(ftello(f)==6); - TEST(fseeko(f, 0, SEEK_SET)==0); - TEST(fgets(foo, sizeof foo, f)); - if (strcmp(foo,"hello") != 0) - error("fgets read back wrong message: \"%s\" wanted: \"hello\"\n", foo); - fclose(f); - } - if (fd > 2) - TEST(unlink(tmp) != -1); - return test_status; -} diff --git a/src/stdio/fscanf.c b/src/stdio/fscanf.c deleted file mode 100644 index 62627b7..0000000 --- a/src/stdio/fscanf.c +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "test.h" - -#define T(f, x, m) do { \ - r = (f); \ - if (r != (x)) \ - error("%s failed (got %d, expected %d, errno \"%s\") (%s)\n", \ - #f, r, x, errno ? strerror(errno) : "", m); \ - errno = 0; \ -} while (0) - -#define S(s, x, m) do { \ - if (strcmp(s, x) != 0) \ - error("got [%s] want [%s] (%s)\n", s, x, m); \ -} while(0) - -int main(void) -{ - int r, x, y; - char a[100], b[100]; - int p[2]; - FILE *f; - - T(pipe(p), 0, "open pipe"); - T(!(f = fdopen(p[0], "rb")), 0, "fdopen pipe"); - if (!f) { - close(p[0]); - close(p[1]); - return test_status; - } - - T(write(p[1], "hello, world\n", 13), 13, "write to pipe"); - T(fscanf(f, "%s %[own]", a, b), 2, ""); - S(a, "hello,", "wrong result for %s"); - S(b, "wo", "wrong result for %[own]"); - T(fgetc(f), 'r', "fgetc 'r'"); - - T(write(p[1], " 0x12 0x34", 10), 10, ""); - T(fscanf(f, "ld %5i%2i", &x, &y), 1, ""); - T(x, 0x12, ""); - T(fgetc(f), '3', "fgetc '3'"); - - fclose(f); - close(p[1]); - return test_status; -} diff --git a/src/stdio/memstream.c b/src/stdio/memstream.c deleted file mode 100644 index 749c664..0000000 --- a/src/stdio/memstream.c +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include -#include -#include -#include -#include "test.h" - -#define TEST(r, f, x, m) ( \ -((r) = (f)) == (x) || \ -(error("%s failed (" m ")\n", #f, r, x), 0) ) - -#define TEST_E(f) ( (errno = 0), (f) || \ -(error("%s failed (errno = %d)\n", #f, errno), 0) ) - -#define TEST_S(s, x, m) ( \ -!strcmp((s),(x)) || \ -(error("[%s] != [%s] (%s)\n", s, x, m), 0) ) - -#define TEST_M(s, x, n, m) ( \ -!memcmp((s),(x),(n)) || \ -(error("[%s] != [%s] (%s)\n", s, x, m), 0) ) - -int main(void) -{ - FILE *f; - char *s; - size_t l; - char buf[100]; - int i; - - s = 0; - TEST_E(f = open_memstream(&s, &l)); - TEST_E(putc('a', f) == 'a'); - TEST_E(putc('b', f) == 'b'); - TEST_E(putc('c', f) == 'c'); - TEST_E(!fflush(f)); - fclose(f); - if (s) TEST_S(s, "abc", "wrong output"); - free(s); - - s = 0; - TEST_E(f = open_memstream(&s, &l)); - TEST_E(fseek(f,1,SEEK_CUR)>=0); - TEST_E(putc('q', f) == 'q'); - TEST_E(!fflush(f)); - if (s) TEST_M(s, "\0q", 3, "wrong output"); - TEST(i, fseek(f,-3,SEEK_CUR), -1, "invalid seek allowed"); - TEST(i, errno, EINVAL, "%d != %d"); - TEST(i, ftell(f), 2, "%d != %d"); - TEST_E(fseek(f,-2,SEEK_CUR)>=0); - TEST_E(putc('e', f) == 'e'); - TEST_E(!fflush(f)); - if (s) TEST_S(s, "eq", "wrong output"); - fclose(f); - free(s); - - TEST_E(f = fmemopen(buf, 10, "r+")); - TEST_E(fputs("hello", f) >= 0); - TEST_E(fputc(0, f)==0); - TEST_E(fseek(f, 0, SEEK_SET)>=0); - i=0; - TEST_E(fscanf(f, "hello%n", &i)==0); - TEST(i, i, 5, "%d != %d"); - TEST(i, ftell(f), 5, "%d != %d"); - errno = 0; - TEST(i, fseek(f, 6, SEEK_CUR)<0, 1, ""); - TEST(i, errno!=0, 1, ""); - TEST(i, ftell(f), 5, "%d != %d"); - TEST_S(buf, "hello", ""); - fclose(f); - - TEST_E(f = fmemopen(buf, 10, "a+")); - TEST(i, ftell(f), 5, "%d != %d"); - TEST_E(fseek(f, 0, SEEK_SET)>=0); - TEST(i, getc(f), 'h', "%d != %d"); - TEST(i, getc(f), 'e', "%d != %d"); - TEST(i, getc(f), 'l', "%d != %d"); - TEST(i, getc(f), 'l', "%d != %d"); - TEST(i, getc(f), 'o', "%d != %d"); - TEST(i, getc(f), EOF, "%d != %d"); - TEST_E(fseek(f, 6, SEEK_SET)>=0); - TEST(i, ftell(f), 6, "%d != %d"); - TEST(i, getc(f), EOF, "%d != %d"); - TEST(i, ftell(f), 6, "%d != %d"); - TEST_E(fseek(f, 0, SEEK_SET)>=0); - TEST(i, getc(f), 'h', "%d != %d"); - TEST_E(fseek(f, 0, SEEK_CUR)>=0); - buf[7] = 'x'; - TEST_E(fprintf(f, "%d", i)==3); - TEST_E(fflush(f)==0); - TEST(i, ftell(f), 8, "%d != %d"); - TEST_S(buf, "hello104", ""); - fclose(f); - return test_status; -} diff --git a/src/stdio/popen.c b/src/stdio/popen.c deleted file mode 100644 index f5cf05a..0000000 --- a/src/stdio/popen.c +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -#include -#include -#include -#include "test.h" - -#define TEST(r, f, x, m) ( \ - ((r) = (f)) == (x) || (error("%s failed (" m ")\n", #f, r, x), 0) ) - -#define TEST_E(f) ( \ - (errno = 0), \ - (f) || (error("%s failed (errno = %d)\n", #f, errno), 0) ) - -#define TEST_S(s, x, m) ( \ - !strcmp((s),(x)) || \ - (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) - -static sig_atomic_t got_sig; - -static void handler(int sig) { - got_sig = 1; -} - -int main(void) -{ - int i; - char foo[6]; - char cmd[64]; - FILE *f; - - TEST_E(f = popen("echo hello", "r")); - if (f) { - TEST_E(fgets(foo, sizeof foo, f)); - TEST_S(foo, "hello", "child process did not say hello"); - TEST(i, pclose(f), 0, "exit status %04x != %04x"); - } - - signal(SIGUSR1, handler); - snprintf(cmd, sizeof cmd, "read a ; test \"x$a\" = xhello && kill -USR1 %d", getpid()); - TEST_E(f = popen(cmd, "w")); - if (f) { - TEST_E(fputs("hello", f) >= 0); - TEST(i, pclose(f), 0, "exit status %04x != %04x"); - TEST(i, got_sig, 1, "child process did not send signal"); - } - signal(SIGUSR1, SIG_DFL); - return test_status; -} diff --git a/src/stdio/snprintf.c b/src/stdio/snprintf.c deleted file mode 100644 index 1187fd7..0000000 --- a/src/stdio/snprintf.c +++ /dev/null @@ -1,174 +0,0 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 700 -#endif -#include -#include -#include -#include -#include -#include "test.h" - -#define DISABLE_SLOW_TESTS - -#define TEST(r, f, x, m) ( \ - ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x), 0) ) - -#define TEST_S(s, x, m) ( \ - !strcmp((s),(x)) || \ - (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) - -static const struct { - const char *fmt; - int i; - const char *expect; -} int_tests[] = { - /* width, precision, alignment */ - { "%04d", 12, "0012" }, - { "%.3d", 12, "012" }, - { "%3d", 12, " 12" }, - { "%-3d", 12, "12 " }, - { "%+3d", 12, "+12" }, - { "%+-5d", 12, "+12 " }, - { "%+- 5d", 12, "+12 " }, - { "%- 5d", 12, " 12 " }, - { "% d", 12, " 12" }, - { "%0-5d", 12, "12 " }, - { "%-05d", 12, "12 " }, - - /* ...explicit precision of 0 shall be no characters. */ - { "%.0d", 0, "" }, - { "%.0o", 0, "" }, - { "%#.0d", 0, "" }, - { "%#.0o", 0, "" }, - { "%#.0x", 0, "" }, - - /* ...but it still has to honor width and flags. */ - { "%2.0u", 0, " " }, - { "%02.0u", 0, " " }, - { "%2.0d", 0, " " }, - { "%02.0d", 0, " " }, - { "% .0d", 0, " " }, - { "%+.0d", 0, "+" }, - - /* hex: test alt form and case */ - { "%x", 63, "3f" }, - { "%#x", 63, "0x3f" }, - { "%X", 63, "3F" }, - - /* octal: test alt form */ - { "%o", 15, "17" }, - { "%#o", 15, "017" }, - - { NULL, 0.0, NULL } -}; - -static const struct { - const char *fmt; - double f; - const char *expect; -} fp_tests[] = { - /* basic form, handling of exponent/precision for 0 */ - { "%a", 0.0, "0x0p+0" }, - { "%e", 0.0, "0.000000e+00" }, - { "%f", 0.0, "0.000000" }, - { "%g", 0.0, "0" }, - { "%#g", 0.0, "0.00000" }, - { "%la", 0.0, "0x0p+0" }, - { "%le", 0.0, "0.000000e+00" }, - { "%lf", 0.0, "0.000000" }, - { "%lg", 0.0, "0" }, - { "%#lg", 0.0, "0.00000" }, - - /* rounding */ - { "%f", 1.1, "1.100000" }, - { "%f", 1.2, "1.200000" }, - { "%f", 1.3, "1.300000" }, - { "%f", 1.4, "1.400000" }, - { "%f", 1.5, "1.500000" }, - { "%.4f", 1.06125, "1.0612" }, - { "%.2f", 1.375, "1.38" }, - { "%.1f", 1.375, "1.4" }, - { "%.1lf", 1.375, "1.4" }, - { "%.15f", 1.1, "1.100000000000000" }, - { "%.16f", 1.1, "1.1000000000000001" }, - { "%.17f", 1.1, "1.10000000000000009" }, - { "%.2e", 1500001.0, "1.50e+06" }, - { "%.2e", 1505000.0, "1.50e+06" }, - { "%.2e", 1505000.00000095367431640625, "1.51e+06" }, - { "%.2e", 1505001.0, "1.51e+06" }, - { "%.2e", 1506000.0, "1.51e+06" }, - - /* correctness in DBL_DIG places */ - { "%.15g", 1.23456789012345, "1.23456789012345" }, - - /* correct choice of notation for %g */ - { "%g", 0.0001, "0.0001" }, - { "%g", 0.00001, "1e-05" }, - { "%g", 123456, "123456" }, - { "%g", 1234567, "1.23457e+06" }, - { "%.7g", 1234567, "1234567" }, - { "%.7g", 12345678, "1.234568e+07" }, - { "%.8g", 0.1, "0.1" }, - { "%.9g", 0.1, "0.1" }, - { "%.10g", 0.1, "0.1" }, - { "%.11g", 0.1, "0.1" }, - - /* pi in double precision, printed to a few extra places */ - { "%.15f", M_PI, "3.141592653589793" }, - { "%.18f", M_PI, "3.141592653589793116" }, - - /* exact conversion of large integers */ - { "%.0f", 340282366920938463463374607431768211456.0, - "340282366920938463463374607431768211456" }, - - { NULL, 0.0, NULL } -}; - -int main(void) -{ - int i, j, k; - char b[2000]; - - TEST(i, snprintf(0, 0, "%d", 123456), 6, "length returned %d != %d"); - TEST(i, snprintf(0, 0, "%.4s", "hello"), 4, "length returned %d != %d"); - TEST(i, snprintf(b, 0, "%.0s", "goodbye"), 0, "length returned %d != %d"); - - strcpy(b, "xxxxxxxx"); - TEST(i, snprintf(b, 4, "%d", 123456), 6, "length returned %d != %d"); - TEST_S(b, "123", "incorrect output"); - TEST(i, b[5], 'x', "buffer overrun"); - - /* Perform ascii arithmetic to test printing tiny doubles */ - TEST(i, snprintf(b, sizeof b, "%.1022f", 0x1p-1021), 1024, "%d != %d"); - b[1] = '0'; - for (i=0; i<1021; i++) { - for (k=0, j=1023; j>0; j--) { - if (b[j]<'5') b[j]+=b[j]-'0'+k, k=0; - else b[j]+=b[j]-'0'-10+k, k=1; - } - } - TEST(i, b[1], '1', "'%c' != '%c'"); - for (j=2; b[j]=='0'; j++); - TEST(i, j, 1024, "%d != %d"); - - -#ifndef DISABLE_SLOW_TESTS - errno = 0; - TEST(i, snprintf(NULL, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d"); - TEST(i, snprintf(NULL, 0, "%.*u ", 2147483647, 0), -1, "integer overflow %d"); - TEST(i, errno, EOVERFLOW, "after overflow: %d != %d"); -#endif - for (j=0; int_tests[j].fmt; j++) { - TEST(i, snprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i), strlen(b), "%d != %d"); - TEST_S(b, int_tests[j].expect, "bad integer conversion"); - } - - for (j=0; fp_tests[j].fmt; j++) { - TEST(i, snprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f), strlen(b), "%d != %d"); - TEST_S(b, fp_tests[j].expect, "bad floating point conversion"); - } - - TEST(i, snprintf(0, 0, "%.4a", 1.0), 11, "%d != %d"); - return test_status; -} diff --git a/src/stdio/sscanf.c b/src/stdio/sscanf.c deleted file mode 100644 index a087c21..0000000 --- a/src/stdio/sscanf.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include -#include "test.h" - -#define TEST(r, f, x, m) ( \ - ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x), 0) ) - -#define TEST_S(s, x, m) ( \ - !strcmp((s),(x)) || \ - (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) - -#define TEST_F(x) ( \ - TEST(i, sscanf(# x, "%lf", &d), 1, "got %d fields, expected %d"), \ - TEST(t, d, (double)x, "%g != %g") ) - -int main(void) -{ - int i; - char a[100], b[100]; - int x, y, z, u, v; - double d, t; - - TEST(i, sscanf("hello, world\n", "%s %s", a, b), 2, "only %d fields, expected %d"); - TEST_S(a, "hello,", ""); - TEST_S(b, "world", ""); - - TEST(i, sscanf("hello, world\n", "%[hel]%s", a, b), 2, "only %d fields, expected %d"); - TEST_S(a, "hell", ""); - TEST_S(b, "o,", ""); - - TEST(i, sscanf("hello, world\n", "%[hel] %s", a, b), 2, "only %d fields, expected %d"); - TEST_S(a, "hell", ""); - TEST_S(b, "o,", ""); - - a[8] = 'X'; - a[9] = 0; - TEST(i, sscanf("hello, world\n", "%8c%8c", a, b), 1, "%d fields, expected %d"); - TEST_S(a, "hello, wX", ""); - - TEST(i, sscanf("56789 0123 56a72", "%2d%d%*d %[0123456789]\n", &x, &y, a), 3, "only %d fields, expected %d"); - TEST(i, x, 56, "%d != %d"); - TEST(i, y, 789, "%d != %d"); - TEST_S(a, "56", ""); - - TEST(i, sscanf("011 0x100 11 0x100 100", "%i %i %o %x %x\n", &x, &y, &z, &u, &v), 5, "only %d fields, expected %d"); - TEST(i, x, 9, "%d != %d"); - TEST(i, y, 256, "%d != %d"); - TEST(i, z, 9, "%d != %d"); - TEST(i, u, 256, "%d != %d"); - TEST(i, v, 256, "%d != %d"); - - TEST(i, sscanf("20 xyz", "%d %d\n", &x, &y), 1, "only %d fields, expected %d"); - TEST(i, x, 20, "%d != %d"); - - TEST(i, sscanf("xyz", "%d\n", &x, &y), 0, "got %d fields, expected no match (%d)"); - - TEST(i, sscanf("", "%d\n", &x, &y), -1, "got %d fields, expected input failure (%d)"); - - TEST(i, sscanf(" 12345 6", "%2d%d%d", &x, &y, &z), 3, "only %d fields, expected %d"); - TEST(i, x, 12, "%d != %d"); - TEST(i, y, 345, "%d != %d"); - TEST(i, z, 6, "%d != %d"); - - TEST(i, sscanf(" 0x12 0x34", "%5i%2i", &x, &y), 1, "got %d fields, expected %d"); - TEST(i, x, 0x12, "%d != %d"); - - TEST_F(123); - TEST_F(123.0); - TEST_F(123.0e+0); - TEST_F(123.0e+4); - TEST_F(1.234e1234); - TEST_F(1.234e-1234); - TEST_F(1.234e56789); - TEST_F(1.234e-56789); - TEST_F(-0.5); - TEST_F(0.1); - TEST_F(0.2); - TEST_F(0.1e-10); - TEST_F(0x1234p56); - - TEST(i, sscanf("10e", "%lf", &d), 0, "got %d fields, expected no match (%d)"); - TEST(i, sscanf("", "%lf\n", &d), -1, "got %d fields, expected input failure (%d)"); - return test_status; -} diff --git a/src/stdio/sscanf_long.c b/src/stdio/sscanf_long.c deleted file mode 100644 index a26999d..0000000 --- a/src/stdio/sscanf_long.c +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "test.h" - -static void setrl(int r, long lim) -{ - struct rlimit rl; - - if (getrlimit(r, &rl)) - error("getrlimit %d: %s\n", r, strerror(errno)); - rl.rlim_cur = lim; - if (setrlimit(r, &rl)) - error("setrlimit %d: %s\n", r, strerror(errno)); -} - -int main(void) -{ - enum {n = 8*1024*1024}; - char *s = malloc(n); - int i; - float f; - char c; - - if (!s) - return error("out of memory"); - setrl(RLIMIT_STACK, 128*1024); - - for (i = 0; i < n; i++) s[i] = '1'; - s[n-3] = ' '; - s[n-1] = 0; - - /* - * stack overflow if scanf copies s on the stack (glibc) - * same issue with %d except then storing the conversion - * result is undefined behaviour - */ - i = sscanf(s, "%f %c", &f, &c); - - if (i != 2) - error("sscanf returned %d, want 2\n", i); - if (f != INFINITY) - error("sscanf(longnum, \"%%f\") read %f, want inf\n", f); - if (c != '1') - error("sscanf(\"1\", %%c) read '%c', want '1'\n", c); - free(s); - return test_status; -} diff --git a/src/stdio/swprintf.c b/src/stdio/swprintf.c deleted file mode 100644 index 189de33..0000000 --- a/src/stdio/swprintf.c +++ /dev/null @@ -1,141 +0,0 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 700 -#endif -#include -#include -#include -#include -#include -#include -#include -#include -#include "test.h" - -#define TEST(r, f, x, m) ( \ - ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x), 0) ) - -#define TEST_S(s, x, m) ( \ - !wcscmp((s),(x)) || \ - (error("[%ls] != [%ls] (%s)\n", s, x, m), 0) ) - -static const struct { - const wchar_t *fmt; - int i; - const wchar_t *expect; -} int_tests[] = { - /* width, precision, alignment */ - { L"%04d", 12, L"0012" }, - { L"%.3d", 12, L"012" }, - { L"%3d", 12, L" 12" }, - { L"%-3d", 12, L"12 " }, - { L"%+3d", 12, L"+12" }, - { L"%+-5d", 12, L"+12 " }, - { L"%+- 5d", 12, L"+12 " }, - { L"%- 5d", 12, L" 12 " }, - { L"% d", 12, L" 12" }, - { L"%0-5d", 12, L"12 " }, - { L"%-05d", 12, L"12 " }, - - /* ...explicit precision of 0 shall be no characters. */ - { L"%.0d", 0, L"" }, - { L"%.0o", 0, L"" }, - { L"%#.0d", 0, L"" }, - { L"%#.0o", 0, L"" }, - { L"%#.0x", 0, L"" }, - - /* hex: test alt form and case */ - { L"%x", 63, L"3f" }, - { L"%#x", 63, L"0x3f" }, - { L"%X", 63, L"3F" }, - - /* octal: test alt form */ - { L"%o", 15, L"17" }, - { L"%#o", 15, L"017" }, - - { NULL, 0.0, NULL } -}; - -static const struct { - const wchar_t *fmt; - double f; - const wchar_t *expect; -} fp_tests[] = { - /* basic form, handling of exponent/precision for 0 */ - { L"%e", 0.0, L"0.000000e+00" }, - { L"%f", 0.0, L"0.000000" }, - { L"%g", 0.0, L"0" }, - { L"%#g", 0.0, L"0.00000" }, - - /* rounding */ - { L"%f", 1.1, L"1.100000" }, - { L"%f", 1.2, L"1.200000" }, - { L"%f", 1.3, L"1.300000" }, - { L"%f", 1.4, L"1.400000" }, - { L"%f", 1.5, L"1.500000" }, - - /* correctness in DBL_DIG places */ - { L"%.15g", 1.23456789012345, L"1.23456789012345" }, - - /* correct choice of notation for %g */ - { L"%g", 0.0001, L"0.0001" }, - { L"%g", 0.00001, L"1e-05" }, - { L"%g", 123456, L"123456" }, - { L"%g", 1234567, L"1.23457e+06" }, - { L"%.7g", 1234567, L"1234567" }, - { L"%.7g", 12345678, L"1.234568e+07" }, - - /* pi in double precision, printed to a few extra places */ - { L"%.15f", M_PI, L"3.141592653589793" }, - { L"%.18f", M_PI, L"3.141592653589793116" }, - - /* exact conversion of large integers */ - { L"%.0f", 340282366920938463463374607431768211456.0, - L"340282366920938463463374607431768211456" }, - - { NULL, 0.0, NULL } -}; - -int main(void) -{ - int i, j; - wchar_t b[500]; - - (void)( - setlocale(LC_CTYPE, "en_US.UTF-8") || - setlocale(LC_CTYPE, "en_GB.UTF-8") || - setlocale(LC_CTYPE, "en.UTF-8") || - setlocale(LC_CTYPE, "POSIX.UTF-8") || - setlocale(LC_CTYPE, "C.UTF-8") || - setlocale(LC_CTYPE, "UTF-8") || - setlocale(LC_CTYPE, "") ); - - TEST(i, strcmp(nl_langinfo(CODESET), "UTF-8"), 0, "no UTF-8 locale; tests might fail"); - - TEST(i, swprintf(0, 0, L"%d", 123456)<0, 1, "%d != %d"); - - TEST(i, swprintf(b, 2, L"%lc", 0xc0), 1, "%d != %d"); - TEST(i, b[0], 0xc0, "wrong character %x != %x"); - TEST(i, swprintf(b, 2, L"%lc", 0x20ac), 1, "%d != %d"); - TEST(i, b[0], 0x20ac, "wrong character %x != %x"); - TEST(i, swprintf(b, 3, L"%s", "\xc3\x80!"), 2, "%d != %d"); - TEST(i, b[0], 0xc0, "wrong character %x != %x"); - TEST(i, swprintf(b, 2, L"%.1s", "\xc3\x80!"), 1, "%d != %d"); - TEST(i, b[0], 0xc0, "wrong character %x != %x"); - - wcscpy(b, L"xxxxxxxx"); - TEST(i, swprintf(b, 4, L"%d", 123456)<0, 1, "%d != %d"); - TEST_S(b, L"123", "incorrect output"); - TEST(i, b[5], 'x', "buffer overrun"); - - for (j=0; int_tests[j].fmt; j++) { - TEST(i, swprintf(b, sizeof b/sizeof *b, int_tests[j].fmt, int_tests[j].i), wcslen(b), "%d != %d"); - TEST_S(b, int_tests[j].expect, "bad integer conversion"); - } - - for (j=0; fp_tests[j].fmt; j++) { - TEST(i, swprintf(b, sizeof b/sizeof *b, fp_tests[j].fmt, fp_tests[j].f), wcslen(b), "%d != %d"); - TEST_S(b, fp_tests[j].expect, "bad floating point conversion"); - } - return test_status; -} diff --git a/src/stdio/test.h b/src/stdio/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/stdio/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/stdio/ungetc.c b/src/stdio/ungetc.c deleted file mode 100644 index 9003a1c..0000000 --- a/src/stdio/ungetc.c +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include -#include -#include -#include "test.h" - -#define TEST(r, f, x, m) ( \ - errno = 0, ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) ) - -#define TEST_S(s, x, m) ( \ - !strcmp((s),(x)) || \ - (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) - -int main(void) -{ - int i; - char a[100]; - FILE *f; - - TEST(i, !(f = tmpfile()), 0, "failed to create temp file %d!=%d (%s)"); - - if (!f) return test_status; - - TEST(i, fprintf(f, "hello, world\n"), 13, "%d != %d (%m)"); - TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d (%m)"); - - TEST(i, feof(f), 0, "%d != %d"); - TEST(i, fgetc(f), 'h', "'%c' != '%c'"); - TEST(i, ftell(f), 1, "%d != %d"); - TEST(i, ungetc('x', f), 'x', "%d != %d"); - TEST(i, ftell(f), 0, "%d != %d"); - TEST(i, fscanf(f, "%[h]", a), 0, "got %d fields, expected %d"); - TEST(i, ftell(f), 0, "%d != %d"); - TEST(i, fgetc(f), 'x', "'%c' != '%c'"); - TEST(i, ftell(f), 1, "%d != %d"); - - TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d"); - TEST(i, ungetc('x', f), 'x', "%d != %d"); - TEST(i, fread(a, 1, sizeof a, f), 14, "read %d, expected %d"); - a[14] = 0; - TEST_S(a, "xhello, world\n", "mismatch reading ungot character"); - - TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d"); - TEST(i, fscanf(f, "%[x]", a), 0, "got %d fields, expected %d"); - TEST(i, ungetc('x', f), 'x', "unget failed after fscanf: %d != %d"); - TEST(i, fgetc(f), 'x', "'%c' != '%c'"); - TEST(i, fgetc(f), 'h', "'%c' != '%c'"); - - fclose(f); - return test_status; -} diff --git a/src/stdlib/Makefile b/src/stdlib/Makefile deleted file mode 100644 index e8f62fa..0000000 --- a/src/stdlib/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -include ../../Makefile.inc -strtod_simple: LDFLAGS+=-lm - diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c deleted file mode 100644 index b45c080..0000000 --- a/src/stdlib/qsort.c +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include -#include -#include "test.h" - -static int scmp(const void *a, const void *b) -{ - return strcmp(*(char **)a, *(char **)b); -} - -static int icmp(const void *a, const void *b) -{ - return *(int*)a - *(int*)b; -} - -/* 26 items -- even */ -static char *s[] = { - "Bob", "Alice", "John", "Ceres", - "Helga", "Drepper", "Emeralda", "Zoran", - "Momo", "Frank", "Pema", "Xavier", - "Yeva", "Gedun", "Irina", "Nono", - "Wiener", "Vincent", "Tsering", "Karnica", - "Lulu", "Quincy", "Osama", "Riley", - "Ursula", "Sam" -}; -/* 23 items -- odd, prime */ -static int n[] = { - 879045, 394, 99405644, 33434, 232323, 4334, 5454, - 343, 45545, 454, 324, 22, 34344, 233, 45345, 343, - 848405, 3434, 3434344, 3535, 93994, 2230404, 4334 -}; - -int main(void) -{ - int i; - - qsort(s, sizeof(s)/sizeof(char *), sizeof(char *), scmp); - for (i=0; i 0) { - error("string sort failed at index %d\n", i); - for (i=0; i n[i+1]) { - error("integer sort failed at index %d\n", i); - for (i=0; i -#include -#include -#include "test.h" - -#define length(x) (sizeof(x) / sizeof *(x)) - -static struct { - char *s; - double f; -} t[] = { - {"0", 0.0}, - {"00.00", 0.0}, - {"-.00000", -0.0}, - {"1e+1000000", INFINITY}, - {"1e-1000000", 0}, - // 2^-1074 * 0.5 - eps - {".2470328229206232720882843964341106861825299013071623822127928412503377536351043e-323", 0}, - // 2^-1074 * 0.5 + eps - {".2470328229206232720882843964341106861825299013071623822127928412503377536351044e-323", 0x1p-1074}, - // 2^-1074 * 1.5 - eps - {".7410984687618698162648531893023320585475897039214871466383785237510132609053131e-323", 0x1p-1074}, - // 2^-1074 * 1.5 + eps - {".7410984687618698162648531893023320585475897039214871466383785237510132609053132e-323", 0x1p-1073}, - // 2^-1022 + 2^-1075 - eps - {".2225073858507201630123055637955676152503612414573018013083228724049586647606759e-307", 0x1p-1022}, - // 2^-1022 + 2^-1075 + eps - {".2225073858507201630123055637955676152503612414573018013083228724049586647606760e-307", 0x1.0000000000001p-1022}, - // 2^1024 - 2^970 - eps - {"17976931348623158079372897140530341507993413271003782693617377898044" - "49682927647509466490179775872070963302864166928879109465555478519404" - "02630657488671505820681908902000708383676273854845817711531764475730" - "27006985557136695962284291481986083493647529271907416844436551070434" - "2711559699508093042880177904174497791.999999999999999999999999999999", 0x1.fffffffffffffp1023}, - // 2^1024 - 2^970 - {"17976931348623158079372897140530341507993413271003782693617377898044" - "49682927647509466490179775872070963302864166928879109465555478519404" - "02630657488671505820681908902000708383676273854845817711531764475730" - "27006985557136695962284291481986083493647529271907416844436551070434" - "2711559699508093042880177904174497792", INFINITY}, - // some random numbers - {".5961860348131807091861002266453941950428e00", 0.59618603481318067}, // 0x1.313f4bc3b584cp-1 - {"1.815013169218038729887460898733526957442e-1", 0.18150131692180388}, // 0x1.73b6f662e1712p-3 - {"42.07082357534453600681618685682257590772e-2", 0.42070823575344535}, // 0x1.aece23c6e028dp-2 - {"665.4686306516261456328973225579833470816e-3", 0.66546863065162609}, // 0x1.54b84dea53453p-1 - {"6101.852922970868621786690495485449831753e-4", 0.61018529229708685}, // 0x1.386a34e5d516bp-1 - {"76966.95208236968077849464348875471158549e-5", 0.76966952082369677}, // 0x1.8a121f9954dfap-1 - {"250506.5322228682496132604807222923702304e-6", 0.25050653222286823}, // 0x1.0084c8cd538c2p-2 - {"2740037.230228005325852424697698331177377e-7", 0.27400372302280052}, // 0x1.18946e9575ef4p-2 - {"20723093.50049742645941529268715428324490e-8", 0.20723093500497428}, // 0x1.a868b14486e4dp-3 - {"0.7900280238081604956226011047460238748912e1", 7.9002802380816046}, // 0x1.f99e3100f2eaep+2 - {"0.9822860653737296848190558448760465863597e2", 98.228606537372968}, // 0x1.88ea17d506accp+6 - {"0.7468949723190370809405570560160405324869e3", 746.89497231903704}, // 0x1.75728e73f48b7p+9 - {"0.1630268320282728475980459844271031751665e4", 1630.2683202827284}, // 0x1.97912c28d5cbp+10 - {"0.4637168629719170695109918769645492022088e5", 46371.686297191707}, // 0x1.6a475f6258737p+15 - {"0.6537805944497711554209461686415872067523e6", 653780.59444977110}, // 0x1.3f3a9305bb86cp+19 - {"0.2346324356502437045212230713960457676531e6", 234632.43565024371}, // 0x1.ca4437c3631eap+17 - {"0.9709481716420048341897258980454298205278e8", 97094817.164200485}, // 0x1.7263284a8242cp+26 - {"0.4996908522051874110779982354932499499602e9", 499690852.20518744}, // 0x1.dc8ad6434872ap+28 -}; - -int main(void) -{ - int i; - double x; - char *p; - - for (i = 0; i < length(t); i++) { - x = strtod(t[i].s, &p); - if (x != t[i].f) - error("strtod(\"%s\") want %a got %a\n", t[i].s, t[i].f, x); - } - return test_status; -} - diff --git a/src/stdlib/strtod_long.c b/src/stdlib/strtod_long.c deleted file mode 100644 index d2db2e8..0000000 --- a/src/stdlib/strtod_long.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -#include -#include "test.h" - -int main(void) -{ - double x, want = .1111111111111111111111; - char buf[40000]; - - memset(buf, '1', sizeof buf); - buf[0] = '.'; - buf[sizeof buf - 1] = 0; - - if ((x=strtod(buf, 0)) != want) - error("strtod(.11[...]1) got %a want %a\n", x, want); - return test_status; -} - diff --git a/src/stdlib/strtod_simple.c b/src/stdlib/strtod_simple.c deleted file mode 100644 index c95dc9e..0000000 --- a/src/stdlib/strtod_simple.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include -#include "test.h" - -/* r = place to store result - * f = function call to test (or any expression) - * x = expected result - * m = message to print on failure (with formats for r & x) -**/ - -#define TEST(r, f, x, m) ( \ - ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x, r-x), 0) ) - -int main(void) -{ - int i; - double d, d2; - char buf[1000]; - - for (i=0; i<100; i++) { - d = sin(i); - snprintf(buf, sizeof buf, "%.300f", d); - TEST(d2, strtod(buf, 0), d, "round trip fail %a != %a (%a)"); - } - - TEST(d, strtod("0x1p4", 0), 16.0, "hex float %a != %a"); - TEST(d, strtod("0x1.1p4", 0), 17.0, "hex float %a != %a"); - return test_status; -} - diff --git a/src/stdlib/strtof.c b/src/stdlib/strtof.c deleted file mode 100644 index 29876bc..0000000 --- a/src/stdlib/strtof.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include -#include "test.h" - -#define length(x) (sizeof(x) / sizeof *(x)) - -static struct { - char *s; - float f; -} t[] = { - // 2^-149 * 0.5 - eps - {".7006492321624085354618647916449580656401309709382578858785341419448955413429303e-45", 0}, - // 2^-149 * 0.5 + eps - {".7006492321624085354618647916449580656401309709382578858785341419448955413429304e-45", 0x1p-149}, - // 2^-149 * 0.5 - eps - {".2101947696487225606385594374934874196920392912814773657635602425834686624028790e-44", 0x1p-149}, - // 2^-149 * 0.5 + eps - {".2101947696487225606385594374934874196920392912814773657635602425834686624028791e-44", 0x1p-148}, - // 2^-126 + 2^-150 - eps - {".1175494420887210724209590083408724842314472120785184615334540294131831453944281e-37", 0x1p-126}, - // 2^-126 + 2^-150 + eps - {".1175494420887210724209590083408724842314472120785184615334540294131831453944282e-37", 0x1.000002p-126}, - // 2^128 - 2^103 - eps - {"340282356779733661637539395458142568447.9999999999999999999", 0x1.fffffep127}, - // 2^128 - 2^103 - {"340282356779733661637539395458142568448", INFINITY}, -}; - -int main(void) -{ - int i; - float x; - char *p; - - for (i = 0; i < length(t); i++) { - x = strtof(t[i].s, &p); - if (x != t[i].f) - error("strtof(\"%s\") want %a got %a\n", t[i].s, t[i].f, x); - } - return test_status; -} diff --git a/src/stdlib/strtol.c b/src/stdlib/strtol.c deleted file mode 100644 index 81df887..0000000 --- a/src/stdlib/strtol.c +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include -#include -#include -#include "test.h" - -/* r = place to store result - * f = function call to test (or any expression) - * x = expected result - * m = message to print on failure (with formats for r & x) -**/ - -#define TEST(r, f, x, m) ( \ - errno = 0, msg = #f, ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x), 0) ) - -#define TEST2(r, f, x, m) ( \ - ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", msg, r, x), 0) ) - -int main(void) -{ - int i; - long l; - unsigned long ul; - char *msg=""; - char *s, *c; - - TEST(l, atol("2147483647"), 2147483647L, "max 32bit signed %ld != %ld"); - TEST(l, strtol("2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld"); - TEST(ul, strtoul("4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu"); - - if (sizeof(long) == 4) { - TEST(l, strtol(s="2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld"); - TEST2(i, c-s, 10, "wrong final position %d != %d"); - TEST2(i, errno, ERANGE, "missing errno %d != %d"); - TEST(l, strtol(s="-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld"); - TEST2(i, c-s, 11, "wrong final position %d != %d"); - TEST2(i, errno, ERANGE, "missing errno %d != %d"); - TEST(ul, strtoul(s="4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu"); - TEST2(i, c-s, 10, "wrong final position %d != %d"); - TEST2(i, errno, ERANGE, "missing errno %d != %d"); - TEST(ul, strtoul(s="-1", &c, 0), -1UL, "rejected negative %lu != %lu"); - TEST2(i, c-s, 2, "wrong final position %d != %d"); - TEST2(i, errno, 0, "spurious errno %d != %d"); - TEST(ul, strtoul(s="-2", &c, 0), -2UL, "rejected negative %lu != %lu"); - TEST2(i, c-s, 2, "wrong final position %d != %d"); - TEST2(i, errno, 0, "spurious errno %d != %d"); - TEST(ul, strtoul(s="-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu"); - TEST2(i, c-s, 11, "wrong final position %d != %d"); - TEST2(i, errno, 0, "spurious errno %d != %d"); - TEST(ul, strtoul(s="-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu"); - TEST2(i, c-s, 11, "wrong final position %d != %d"); - TEST2(i, errno, 0, "spurious errno %d != %d"); - } else { - TEST(i, 0, 1, "64bit tests not implemented"); - } - - TEST(l, strtol("z", 0, 36), 35, "%ld != %ld"); - TEST(l, strtol("00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld"); - TEST(l, strtol(s="0F5F", &c, 16), 0x0f5f, "%ld != %ld"); - - TEST(l, strtol(s="0xz", &c, 16), 0, "%ld != %ld"); - TEST2(i, c-s, 1, "wrong final position %ld != %ld"); - - TEST(l, strtol(s="0x1234", &c, 16), 0x1234, "%ld != %ld"); - TEST2(i, c-s, 6, "wrong final position %ld != %ld"); - - c = NULL; - TEST(l, strtol(s="123", &c, 37), 0, "%ld != %ld"); - TEST2(i, c-s, 0, "wrong final position %d != %d"); - TEST2(i, errno, EINVAL, "%d != %d"); - return test_status; -} diff --git a/src/stdlib/strtold.c b/src/stdlib/strtold.c deleted file mode 100644 index e37f7f0..0000000 --- a/src/stdlib/strtold.c +++ /dev/null @@ -1,94 +0,0 @@ -#include -#include -#include -#include -#include "test.h" - -#define length(x) (sizeof(x) / sizeof *(x)) - -static struct { - char *s; - long double f; -} t[] = { - {"0", 0.0}, - {"12.345", 12.345L}, - {"1.2345e1", 12.345L}, - {"1e+1000000", INFINITY}, - {"1e-1000000", 0}, -#if LDBL_MANT_DIG == 53 - // 2^-1074 * 0.5 - eps - {".2470328229206232720882843964341106861825299013071623822127928412503377536351043e-323", 0}, - // 2^-1074 * 0.5 + eps - {".2470328229206232720882843964341106861825299013071623822127928412503377536351044e-323", 0x1p-1074}, - // 2^-1074 * 1.5 - eps - {".7410984687618698162648531893023320585475897039214871466383785237510132609053131e-323", 0x1p-1074}, - // 2^-1074 * 1.5 + eps - {".7410984687618698162648531893023320585475897039214871466383785237510132609053132e-323", 0x1p-1073}, - // 2^-1022 + 2^-1075 - eps - {".2225073858507201630123055637955676152503612414573018013083228724049586647606759e-307", 0x1p-1022}, - // 2^-1022 + 2^-1075 + eps - {".2225073858507201630123055637955676152503612414573018013083228724049586647606760e-307", 0x1.0000000000001p-1022}, - // 2^1024 - 2^970 - eps - {"17976931348623158079372897140530341507993413271003782693617377898044" - "49682927647509466490179775872070963302864166928879109465555478519404" - "02630657488671505820681908902000708383676273854845817711531764475730" - "27006985557136695962284291481986083493647529271907416844436551070434" - "2711559699508093042880177904174497791.999999999999999999999999999999", 0x1.fffffffffffffp1023}, - // 2^1024 - 2^970 - {"17976931348623158079372897140530341507993413271003782693617377898044" - "49682927647509466490179775872070963302864166928879109465555478519404" - "02630657488671505820681908902000708383676273854845817711531764475730" - "27006985557136695962284291481986083493647529271907416844436551070434" - "2711559699508093042880177904174497792", INFINITY}, - // some random numbers - {".5961860348131807091861002266453941950428e00", 0.59618603481318067}, // 0x1.313f4bc3b584cp-1 - {"1.815013169218038729887460898733526957442e-1", 0.18150131692180388}, // 0x1.73b6f662e1712p-3 - {"42.07082357534453600681618685682257590772e-2", 0.42070823575344535}, // 0x1.aece23c6e028dp-2 - {"665.4686306516261456328973225579833470816e-3", 0.66546863065162609}, // 0x1.54b84dea53453p-1 - {"6101.852922970868621786690495485449831753e-4", 0.61018529229708685}, // 0x1.386a34e5d516bp-1 - {"76966.95208236968077849464348875471158549e-5", 0.76966952082369677}, // 0x1.8a121f9954dfap-1 - {"250506.5322228682496132604807222923702304e-6", 0.25050653222286823}, // 0x1.0084c8cd538c2p-2 - {"2740037.230228005325852424697698331177377e-7", 0.27400372302280052}, // 0x1.18946e9575ef4p-2 - {"20723093.50049742645941529268715428324490e-8", 0.20723093500497428}, // 0x1.a868b14486e4dp-3 - {"0.7900280238081604956226011047460238748912e1", 7.9002802380816046}, // 0x1.f99e3100f2eaep+2 - {"0.9822860653737296848190558448760465863597e2", 98.228606537372968}, // 0x1.88ea17d506accp+6 - {"0.7468949723190370809405570560160405324869e3", 746.89497231903704}, // 0x1.75728e73f48b7p+9 - {"0.1630268320282728475980459844271031751665e4", 1630.2683202827284}, // 0x1.97912c28d5cbp+10 - {"0.4637168629719170695109918769645492022088e5", 46371.686297191707}, // 0x1.6a475f6258737p+15 - {"0.6537805944497711554209461686415872067523e6", 653780.59444977110}, // 0x1.3f3a9305bb86cp+19 - {"0.2346324356502437045212230713960457676531e6", 234632.43565024371}, // 0x1.ca4437c3631eap+17 - {"0.9709481716420048341897258980454298205278e8", 97094817.164200485}, // 0x1.7263284a8242cp+26 - {"0.4996908522051874110779982354932499499602e9", 499690852.20518744}, // 0x1.dc8ad6434872ap+28 -#elif LDBL_MANT_DIG == 64 - // 2^-16445 * 0.5 - eps - {".1822599765941237301264202966809709908199525407846781671860490243514185844316698e-4950", 0}, - // 2^-16445 * 0.5 + eps - {".1822599765941237301264202966809709908199525407846781671860490243514185844316699e-4950", 0x1p-16445L}, - // 2^-16445 * 1.5 - eps - {".5467799297823711903792608900429129724598576223540345015581470730542557532950096e-4950", 0x1p-16445L}, - // 2^-16445 * 1.5 + eps - {".5467799297823711903792608900429129724598576223540345015581470730542557532950097e-4950", 0x1p-16444L}, - // 2^-16382 + 2^-16446 - eps - {".3362103143112093506444937793915876332724499641527442230928779770593420866576777e-4931", 0x1p-16382L}, - // 2^-16382 + 2^-16446 + eps - {".3362103143112093506444937793915876332724499641527442230928779770593420866576778e-4931", 0x1.0000000000000002p-16382L}, - // 2^16384 - 2^16319 - eps - {"118973149535723176505351158982948.86679662540046955672e4900", 0x1.fffffffffffffffep16383L}, - // 2^16384 - 2^16319 + eps - {"118973149535723176505351158982948.86679662540046955673e4900", INFINITY}, -#endif -}; - -int main(void) -{ - int i; - long double x; - char *p; - - for (i = 0; i < length(t); i++) { - x = strtold(t[i].s, &p); - if (x != t[i].f) - error("strtold(\"%s\") want %La got %La\n", t[i].s, t[i].f, x); - } - return test_status; -} diff --git a/src/stdlib/test.h b/src/stdlib/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/stdlib/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/stdlib/wcstol.c b/src/stdlib/wcstol.c deleted file mode 100644 index abd76af..0000000 --- a/src/stdlib/wcstol.c +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include -#include -#include "test.h" - -#define TEST(r, f, x, m) ( \ - errno = 0, msg = #f, ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x), 0) ) - -#define TEST2(r, f, x, m) ( \ - ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", msg, r, x), 0) ) - -int main(void) -{ - int i; - long l; - unsigned long ul; - char *msg=""; - wchar_t *s, *c; - - TEST(l, wcstol(L"2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld"); - TEST(ul, wcstoul(L"4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu"); - - if (sizeof(long) == 4) { - TEST(l, wcstol(s=L"2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld"); - TEST2(i, c-s, 10, "wrong final position %d != %d"); - TEST2(i, errno, ERANGE, "missing errno %d != %d"); - TEST(l, wcstol(s=L"-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld"); - TEST2(i, c-s, 11, "wrong final position %d != %d"); - TEST2(i, errno, ERANGE, "missing errno %d != %d"); - TEST(ul, wcstoul(s=L"4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu"); - TEST2(i, c-s, 10, "wrong final position %d != %d"); - TEST2(i, errno, ERANGE, "missing errno %d != %d"); - TEST(ul, wcstoul(s=L"-1", &c, 0), -1UL, "rejected negative %lu != %lu"); - TEST2(i, c-s, 2, "wrong final position %d != %d"); - TEST2(i, errno, 0, "spurious errno %d != %d"); - TEST(ul, wcstoul(s=L"-2", &c, 0), -2UL, "rejected negative %lu != %lu"); - TEST2(i, c-s, 2, "wrong final position %d != %d"); - TEST2(i, errno, 0, "spurious errno %d != %d"); - TEST(ul, wcstoul(s=L"-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu"); - TEST2(i, c-s, 11, "wrong final position %d != %d"); - TEST2(i, errno, 0, "spurious errno %d != %d"); - TEST(ul, wcstoul(s=L"-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu"); - TEST2(i, c-s, 11, "wrong final position %d != %d"); - TEST2(i, errno, 0, "spurious errno %d != %d"); - } else { - TEST(i, 0, 1, "64bit tests not implemented"); - } - - TEST(l, wcstol(L"z", 0, 36), 35, "%ld != %ld"); - TEST(l, wcstol(L"00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld"); - - TEST(l, wcstol(s=L"0xz", &c, 16), 0, "%ld != %ld"); - TEST2(i, c-s, 1, "wrong final position %ld != %ld"); - - TEST(l, wcstol(s=L"0x1234", &c, 16), 0x1234, "%ld != %ld"); - TEST2(i, c-s, 6, "wrong final position %ld != %ld"); - - c = NULL; - TEST(l, wcstol(s=L"123", &c, 37), 0, "%ld != %ld"); - TEST2(i, c-s, 0, "wrong final position %d != %d"); - TEST2(i, errno, EINVAL, "%d != %d"); - return test_status; -} diff --git a/src/string/Makefile b/src/string/Makefile deleted file mode 100644 index ee4552b..0000000 --- a/src/string/Makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../Makefile.inc diff --git a/src/string/string.c b/src/string/string.c deleted file mode 100644 index 2e06eb9..0000000 --- a/src/string/string.c +++ /dev/null @@ -1,116 +0,0 @@ -#define _BSD_SOURCE -#include -#include -#include "test.h" - -/* r = place to store result - * f = function call to test (or any expression) - * x = expected result - * m = message to print on failure (with formats for r & x) -**/ - -#define TEST(r, f, x, m) ( \ - ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x), 0) ) - -#define TEST_S(s, x, m) ( \ - !strcmp((s),(x)) || \ - (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) - -int main(void) -{ - char b[32]; - char *s; - int i; - - b[16]='a'; b[17]='b'; b[18]='c'; b[19]=0; - TEST(s, strcpy(b, b+16), b, "wrong return %p != %p"); - TEST_S(s, "abc", "strcpy gave incorrect string"); - TEST(s, strcpy(b+1, b+16), b+1, "wrong return %p != %p"); - TEST_S(s, "abc", "strcpy gave incorrect string"); - TEST(s, strcpy(b+2, b+16), b+2, "wrong return %p != %p"); - TEST_S(s, "abc", "strcpy gave incorrect string"); - TEST(s, strcpy(b+3, b+16), b+3, "wrong return %p != %p"); - TEST_S(s, "abc", "strcpy gave incorrect string"); - - TEST(s, strcpy(b+1, b+17), b+1, "wrong return %p != %p"); - TEST_S(s, "bc", "strcpy gave incorrect string"); - TEST(s, strcpy(b+2, b+18), b+2, "wrong return %p != %p"); - TEST_S(s, "c", "strcpy gave incorrect string"); - TEST(s, strcpy(b+3, b+19), b+3, "wrong return %p != %p"); - TEST_S(s, "", "strcpy gave incorrect string"); - - TEST(s, memset(b, 'x', sizeof b), b, "wrong return %p != %p"); - TEST(s, strncpy(b, "abc", sizeof b - 1), b, "wrong return %p != %p"); - TEST(i, memcmp(b, "abc\0\0\0\0", 8), 0, "strncpy fails to zero-pad dest"); - TEST(i, b[sizeof b - 1], 'x', "strncpy overruns buffer when n > strlen(src)"); - - b[3] = 'x'; b[4] = 0; - strncpy(b, "abc", 3); - TEST(i, b[2], 'c', "strncpy fails to copy last byte: %hhu != %hhu"); - TEST(i, b[3], 'x', "strncpy overruns buffer to null-terminate: %hhu != %hhu"); - - TEST(i, !strncmp("abcd", "abce", 3), 1, "strncmp compares past n"); - TEST(i, !!strncmp("abc", "abd", 3), 1, "strncmp fails to compare n-1st byte"); - - strcpy(b, "abc"); - TEST(s, strncat(b, "123456", 3), b, "%p != %p"); - TEST(i, b[6], 0, "strncat failed to null-terminate (%d)"); - TEST_S(s, "abc123", "strncat gave incorrect string"); - - strcpy(b, "aaababccdd0001122223"); - TEST(s, strchr(b, 'b'), b+3, "%p != %p"); - TEST(s, strrchr(b, 'b'), b+5, "%p != %p"); - TEST(i, strspn(b, "abcd"), 10, "%d != %d"); - TEST(i, strcspn(b, "0123"), 10, "%d != %d"); - TEST(s, strpbrk(b, "0123"), b+10, "%d != %d"); - - strcpy(b, "abc 123; xyz; foo"); - TEST(s, strtok(b, " "), b, "%p != %p"); - TEST_S(s, "abc", "strtok result"); - - TEST(s, strtok(NULL, ";"), b+4, "%p != %p"); - TEST_S(s, " 123", "strtok result"); - - TEST(s, strtok(NULL, " ;"), b+11, "%p != %p"); - TEST_S(s, "xyz", "strtok result"); - - TEST(s, strtok(NULL, " ;"), b+16, "%p != %p"); - TEST_S(s, "foo", "strtok result"); - -#ifdef HAVE_BSD_STRL - memset(b, 'x', sizeof b); - TEST(i, strlcpy(b, "abc", sizeof b - 1), 3, "length %d != %d"); - TEST(i, b[3], 0, "strlcpy did not null-terminate short string (%d)"); - TEST(i, b[4], 'x', "strlcpy wrote extra bytes (%d)"); - - memset(b, 'x', sizeof b); - TEST(i, strlcpy(b, "abc", 2), 3, "length %d != %d"); - TEST(i, b[0], 'a', "strlcpy did not copy character %d"); - TEST(i, b[1], 0, "strlcpy did not null-terminate long string (%d)"); - - memset(b, 'x', sizeof b); - TEST(i, strlcpy(b, "abc", 3), 3, "length %d != %d"); - TEST(i, b[2], 0, "strlcpy did not null-terminate l-length string (%d)"); - - TEST(i, strlcpy(NULL, "abc", 0), 3, "length %d != %d"); - - memcpy(b, "abc\0\0\0x\0", 8); - TEST(i, strlcat(b, "123", sizeof b), 6, "length %d != %d"); - TEST_S(b, "abc123", "strlcat result"); - - memcpy(b, "abc\0\0\0x\0", 8); - TEST(i, strlcat(b, "123", 6), 6, "length %d != %d"); - TEST_S(b, "abc12", "strlcat result"); - TEST(i, b[6], 'x', "strlcat wrote past string %d != %d"); - - memcpy(b, "abc\0\0\0x\0", 8); - TEST(i, strlcat(b, "123", 4), 6, "length %d != %d"); - TEST_S(b, "abc", "strlcat result"); - - memcpy(b, "abc\0\0\0x\0", 8); - TEST(i, strlcat(b, "123", 3), 6, "length %d != %d"); - TEST_S(b, "abc", "strlcat result"); -#endif - return test_status; -} diff --git a/src/string/test.h b/src/string/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/string/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/thread/Makefile b/src/thread/Makefile deleted file mode 100644 index c799ad6..0000000 --- a/src/thread/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -include ../../Makefile.inc -LDFLAGS+=-lpthread - -sem: LDFLAGS += -lrt -tls_align_dlopen: LDFLAGS += -ldl - -tls_align: tls_align_dso.so - diff --git a/src/thread/pthread.c b/src/thread/pthread.c deleted file mode 100644 index 3c25c57..0000000 --- a/src/thread/pthread.c +++ /dev/null @@ -1,294 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "test.h" - -#define TEST(r, f, x, m) ( \ - msg = #f, ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x), 0) ) - -#define TEST_S(s, x, m) ( \ - !strcmp((s),(x)) || \ - (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) - -static volatile char *msg = ""; - -static void alarmhandler(int sig) { - error("timeout in %s\n", msg); - _Exit(1); -} - -static pthread_key_t k1, k2; - -static void dtor(void *p) -{ - *(int *)p = 1; -} - -static void *start1(void *arg) -{ - return arg; -} - -static void *start2(void *arg) -{ - int *p = arg; - if (pthread_setspecific(k1, p) || pthread_setspecific(k2, p+1)) - return arg; - return 0; -} - -static void *start3(void *arg) -{ - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0); - sem_post(arg); - for (;;); - return 0; -} - -static void cleanup4(void *arg) -{ - *(int *)arg = 1; -} - -static void *start4(void *arg) -{ - pthread_cleanup_push(cleanup4, arg); - sleep(3); - pthread_cleanup_pop(0); - return 0; -} - -static void cleanup4a2(void *arg) -{ - *(int *)arg += 2; -} - -static void cleanup4a3(void *arg) -{ - *(int *)arg += 3; -} - -static void cleanup4a4(void *arg) -{ - *(int *)arg += 4; -} - -static void *start4a(void *arg) -{ - int *foo = arg; - pthread_cleanup_push(cleanup4, foo); - pthread_cleanup_push(cleanup4a2, foo+1); - pthread_cleanup_push(cleanup4a3, foo+2); - pthread_cleanup_push(cleanup4a4, foo+3); - sleep(3); - pthread_cleanup_pop(0); - pthread_cleanup_pop(0); - pthread_cleanup_pop(0); - pthread_cleanup_pop(0); - return 0; -} - -static void *start5(void *arg) -{ - pthread_mutex_lock(arg); - return 0; -} - -static void *start6(void *arg) -{ - void **args = arg; - pthread_mutex_lock(args[1]); - pthread_barrier_wait(args[0]); - nanosleep(&(struct timespec){ .tv_nsec = 10000000 }, 0); - return 0; -} - -static void *start7(void *arg) -{ - void **args = arg; - pthread_mutex_lock(args[1]); - pthread_cond_signal(args[0]); - pthread_mutex_unlock(args[1]); - return 0; -} - -static void *start8(void *arg) -{ - void **args = arg; - pthread_mutex_t *m = args[1]; - pthread_cond_t *c = args[0]; - int *x = args[2]; - - pthread_mutex_lock(m); - while (*x) pthread_cond_wait(c, m); - pthread_mutex_unlock(m); - - return 0; -} - -int main(void) -{ - pthread_t td, td1, td2, td3; - int r; - void *res; - int foo[4], bar[2]; - pthread_barrier_t barrier2; - pthread_mutexattr_t mtx_a; - sem_t sem1; - pthread_mutex_t mtx; - pthread_cond_t cond; - - signal(SIGALRM, alarmhandler); - alarm(10); - - TEST(r, pthread_barrier_init(&barrier2, 0, 2), 0, "creating barrier"); - TEST(r, sem_init(&sem1, 0, 0), 0, "creating semaphore"); - - /* Test basic thread creation and joining */ - TEST(r, pthread_create(&td, 0, start1, &res), 0, "failed to create thread"); - res = 0; - TEST(r, pthread_join(td, &res), 0, "failed to join"); - TEST(r, (res==&res), 1, "wrong result from join"); - - /* Test POSIX thread-specific data */ - TEST(r, pthread_key_create(&k1, dtor), 0, "failed to create key"); - TEST(r, pthread_key_create(&k2, dtor), 0, "failed to create key"); - foo[0] = foo[1] = 0; - TEST(r, pthread_setspecific(k1, bar), 0, "failed to set tsd"); - TEST(r, pthread_setspecific(k2, bar+1), 0, "failed to set tsd"); - TEST(r, pthread_create(&td, 0, start2, foo), 0, "failed to create thread"); - TEST(r, pthread_join(td, &res), 0, "failed to join"); - TEST(res, res, 0, "pthread_setspecific failed in thread"); - TEST(r, foo[0], 1, "dtor failed to run"); - TEST(r, foo[1], 1, "dtor failed to run"); - TEST(res, pthread_getspecific(k1), bar, "tsd corrupted"); - TEST(res, pthread_getspecific(k2), bar+1, "tsd corrupted"); - TEST(r, pthread_setspecific(k1, 0), 0, "failed to clear tsd"); - TEST(r, pthread_setspecific(k2, 0), 0, "failed to clear tsd"); - TEST(r, pthread_key_delete(k1), 0, "failed to destroy key"); - TEST(r, pthread_key_delete(k2), 0, "failed to destroy key"); - - /* Asynchronous cancellation */ - TEST(r, pthread_create(&td, 0, start3, &sem1), 0, "failed to create thread"); - while (sem_wait(&sem1)); - TEST(r, pthread_cancel(td), 0, "canceling"); - TEST(r, pthread_join(td, &res), 0, "joining canceled thread"); - TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status"); - - /* Cancellation cleanup handlers */ - foo[0] = 0; - TEST(r, pthread_create(&td, 0, start4, foo), 0, "failed to create thread"); - TEST(r, pthread_cancel(td), 0, "cancelling"); - TEST(r, pthread_join(td, &res), 0, "joining canceled thread"); - TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status"); - TEST(r, foo[0], 1, "cleanup handler failed to run"); - - /* Nested cleanup handlers */ - memset(foo, 0, sizeof foo); - TEST(r, pthread_create(&td, 0, start4a, foo), 0, "failed to create thread"); - TEST(r, pthread_cancel(td), 0, "cancelling"); - TEST(r, pthread_join(td, &res), 0, "joining canceled thread"); - TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status"); - TEST(r, foo[0], 1, "cleanup handler failed to run"); - TEST(r, foo[1], 2, "cleanup handler failed to run"); - TEST(r, foo[2], 3, "cleanup handler failed to run"); - TEST(r, foo[3], 4, "cleanup handler failed to run"); - - /* Robust mutexes */ - TEST(r, pthread_mutexattr_init(&mtx_a), 0, "initializing mutex attr"); - TEST(r, pthread_mutexattr_setrobust(&mtx_a, PTHREAD_MUTEX_ROBUST), 0, "setting robust attribute"); - TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex"); - TEST(r, pthread_mutex_lock(&mtx), 0, "locking robust mutex"); - TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking robust mutex"); - TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread"); - TEST(r, pthread_join(td, &res), 0, "joining thread"); - TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d"); - TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d"); - TEST(r, pthread_mutex_lock(&mtx), ENOTRECOVERABLE, "re-locking orphaned robust mutex %d!=%d"); - TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying unrecoverable mutex %d!=%d"); - - TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex"); - TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread"); - TEST(r, pthread_join(td, &res), 0, "joining thread"); - TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d"); - TEST(r, pthread_mutex_consistent(&mtx), 0, "%d!=%d"); - TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d"); - TEST(r, pthread_mutex_lock(&mtx), 0, "re-locking orphaned robust mutex %d!=%d"); - TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying mutex %d!=%d"); - - TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "%d != %d"); - TEST(r, pthread_create(&td, 0, start6, (void *[]){ &barrier2, &mtx }), 0, "%d != %d"); - pthread_barrier_wait(&barrier2); - TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "%d != %d"); - TEST(r, pthread_join(td, &res), 0, "%d != %d"); - TEST(r, pthread_mutex_consistent(&mtx), 0, "%d != %d"); - TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); - TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d"); - - //TEST(r, (fd=open("/dev/zero", O_RDWR))>=0, 1, "opening zero page file"); - //TEST(r, - - /* Condition variables */ - TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d"); - TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d"); - TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); - TEST(r, pthread_create(&td, 0, start7, (void *[]){ &cond, &mtx }), 0, "%d != %d"); - TEST(r, pthread_cond_wait(&cond, &mtx), 0, "%d != %d"); - TEST(r, pthread_join(td, &res), 0, "%d != %d"); - TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); - TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d"); - TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d"); - - /* Condition variables with multiple waiters */ - TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d"); - TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d"); - TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); - foo[0] = 1; - TEST(r, pthread_create(&td1, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); - TEST(r, pthread_create(&td2, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); - TEST(r, pthread_create(&td3, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); - TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); - nanosleep(&(struct timespec){.tv_nsec=1000000}, 0); - foo[0] = 0; - TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); - TEST(r, pthread_cond_signal(&cond), 0, "%d != %d"); - TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); - TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); - TEST(r, pthread_cond_signal(&cond), 0, "%d != %d"); - TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); - TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); - TEST(r, pthread_cond_signal(&cond), 0, "%d != %d"); - TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); - TEST(r, pthread_join(td1, 0), 0, "%d != %d"); - TEST(r, pthread_join(td2, 0), 0, "%d != %d"); - TEST(r, pthread_join(td3, 0), 0, "%d != %d"); - TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d"); - TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d"); - - /* Condition variables with broadcast signals */ - TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d"); - TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d"); - TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); - foo[0] = 1; - TEST(r, pthread_create(&td1, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); - TEST(r, pthread_create(&td2, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); - TEST(r, pthread_create(&td3, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d"); - TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); - nanosleep(&(struct timespec){.tv_nsec=1000000}, 0); - TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d"); - foo[0] = 0; - TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d"); - TEST(r, pthread_cond_broadcast(&cond), 0, "%d != %d"); - TEST(r, pthread_join(td1, 0), 0, "%d != %d"); - TEST(r, pthread_join(td2, 0), 0, "%d != %d"); - TEST(r, pthread_join(td3, 0), 0, "%d != %d"); - TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d"); - TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d"); - return test_status; -} diff --git a/src/thread/sem.c b/src/thread/sem.c deleted file mode 100644 index 77d6796..0000000 --- a/src/thread/sem.c +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "test.h" - -#define TEST(r, f, x, m) ( \ - ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x), 0) ) - -#define TEST_S(s, x, m) ( \ - !strcmp((s),(x)) || \ - (error("[%s] != [%s] (%s)\n", s, x, m), 0) ) - -int main(void) -{ - int r; - char buf[100]; - struct timespec ts; - sem_t *sem, *sem2; - int val; - - clock_gettime(CLOCK_REALTIME, &ts); - snprintf(buf, sizeof buf, "/testsuite-%d-%d", (int)getpid(), (int)ts.tv_nsec); - - TEST(r, !(sem=sem_open(buf, O_CREAT|O_EXCL, 0700, 1)), 0, "could not open sem"); - - TEST(r, sem_getvalue(sem, &val), 0, "failed to get sem value"); - TEST(r, val, 1, "wrong initial semaphore value"); - - TEST(r, !(sem2=sem_open(buf, 0)), 0, "could not reopen sem"); - TEST(r, sem!=sem2, 0, "reopened sem has different address"); - - TEST(r, sem_wait(sem), 0, "failed on sem wait"); - TEST(r, sem_getvalue(sem2, &val), 0, "failed to get sem value"); - TEST(r, val, 0, "wrong semaphore value on second handle"); - - TEST(r, sem_post(sem), 0, "failed on sem post"); - TEST(r, sem_getvalue(sem2, &val), 0, "failed to get sem value"); - TEST(r, val, 1, "wrong semaphore value on second handle"); - - TEST(r, sem_close(sem), 0, "failed to close sem"); - TEST(r, sem_close(sem), 0, "failed to close sem second time"); - TEST(r, sem_unlink(buf), 0, "failed to unlink sem"); - return test_status; -} diff --git a/src/thread/test.h b/src/thread/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/thread/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/thread/tls_align.c b/src/thread/tls_align.c deleted file mode 100644 index 505abf2..0000000 --- a/src/thread/tls_align.c +++ /dev/null @@ -1,22 +0,0 @@ -#include "test.h" - -extern struct { - char *name; - unsigned size; - unsigned align; - unsigned long addr; -} t[4]; - -int main() -{ - int i; - - for (i = 0; i < sizeof t/sizeof *t; i++) { - if (!t[i].name) - error("name is not set for t[%d]\n", i); - if (t[i].addr & (t[i].align-1)) - error("bad alignment: %s, size: %u, align: %u, addr: 0x%lx\n", - t[i].name, t[i].size, t[i].align, t[i].addr); - } - return test_status; -} diff --git a/src/thread/tls_align_dlopen.c b/src/thread/tls_align_dlopen.c deleted file mode 100644 index 64517e1..0000000 --- a/src/thread/tls_align_dlopen.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include "test.h" - -int main() -{ - int i; - void *h; - struct { - char *name; - unsigned size; - unsigned align; - unsigned long addr; - } *t; - - h = dlopen("./tls_align_dso.so", RTLD_LAZY); - if (!h) - error("dlopen failed\n"); - t = dlsym(h, "t"); - if (!t) - error("dlsym failed\n"); - - for (i = 0; i < 4; i++) { - if (!t[i].name) - error("name is not set for t[%d]\n", i); - if (t[i].addr & (t[i].align-1)) - error("bad alignment: %s, size: %u, align: %u, addr: 0x%lx\n", - t[i].name, t[i].size, t[i].align, t[i].addr); - } - return test_status; -} diff --git a/src/thread/tls_align_dso.c b/src/thread/tls_align_dso.c deleted file mode 100644 index 9ca759e..0000000 --- a/src/thread/tls_align_dso.c +++ /dev/null @@ -1,30 +0,0 @@ -__thread char c1 = 1; -__thread char xchar = 2; -__thread char c2 = 3; -__thread short xshort = 4; -__thread char c3 = 5; -__thread int xint = 6; -__thread char c4 = 7; -__thread long long xllong = 8; - -struct { - char *name; - unsigned size; - unsigned align; - unsigned long addr; -} t[4]; - -#define entry(i,x) \ - t[i].name = #x; \ - t[i].size = sizeof x; \ - t[i].align = __alignof__(x); \ - t[i].addr = (unsigned long)&x; - -__attribute__((constructor)) static void init(void) -{ - entry(0, xchar) - entry(1, xshort) - entry(2, xint) - entry(3, xllong) -} - diff --git a/src/thread/tls_init.c b/src/thread/tls_init.c deleted file mode 100644 index 8a43829..0000000 --- a/src/thread/tls_init.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include "test.h" - -__thread int tls_fix = 23; -__thread int tls_zero; - -static void *f(void *arg) -{ - if (tls_fix != 23) - error("fixed init failed: want 23 got %d\n", tls_fix); - if (tls_zero != 0) - error("zero init failed: want 0 got %d\n", tls_zero); - tls_fix++; - tls_zero++; - return 0; -} - -#define CHECK(f) do{ if(f) error("%s failed.\n", #f); }while(0) -#define length(a) (sizeof(a)/sizeof*(a)) - -int main() -{ - pthread_t t[5]; - int i, j; - - if (tls_fix != 23) - error("fixed init failed: want 23 got %d\n", tls_fix); - if (tls_zero != 0) - error("zero init failed: want 0 got %d\n", tls_zero); - - for (j = 0; j < 2; j++) { - for (i = 0; i < length(t); i++) { - CHECK(pthread_create(t+i, 0, f, 0)); - tls_fix++; - tls_zero++; - } - for (i = 0; i < length(t); i++) - CHECK(pthread_join(t[i], 0)); - } - - return test_status; -} diff --git a/src/time/Makefile b/src/time/Makefile deleted file mode 100644 index ee4552b..0000000 --- a/src/time/Makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../Makefile.inc diff --git a/src/time/test.h b/src/time/test.h deleted file mode 100644 index a6a7706..0000000 --- a/src/time/test.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) - -static int test_status; - -static int test_error(const char *n, int l, const char *s, ...) -{ - va_list ap; - - if (test_status == 0) - printf("FAIL\n"); - test_status = 1; - printf(" ERROR %s:%d: ", n, l); - va_start(ap, s); - vprintf(s, ap); - va_end(ap); - return -1; -} - diff --git a/src/time/time.c b/src/time/time.c deleted file mode 100644 index 27598b8..0000000 --- a/src/time/time.c +++ /dev/null @@ -1,76 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include -#include -#include -#include "test.h" - -/* We use this instead of memcmp because some broken C libraries - * add additional nonstandard fields to struct tm... */ - -int tm_cmp(struct tm tm1, struct tm tm2) -{ - return tm1.tm_sec != tm2.tm_sec || - tm1.tm_min != tm2.tm_min || - tm1.tm_hour != tm2.tm_hour || - tm1.tm_mday != tm2.tm_mday || - tm1.tm_mon != tm2.tm_mon || - tm1.tm_year != tm2.tm_year || - tm1.tm_wday != tm2.tm_wday || - tm1.tm_yday != tm2.tm_yday || - tm1.tm_isdst!= tm2.tm_isdst; -} - -char *tm_str(struct tm tm) -{ - static int i; - static char b[4][64]; - i = (i+1)%4; - snprintf(b[i], sizeof b[i], - "s=%02d m=%02d h=%02d mday=%02d mon=%02d year=%04d wday=%d yday=%d isdst=%d", - tm.tm_sec, tm.tm_min, tm.tm_hour, - tm.tm_mday, tm.tm_mon, tm.tm_year, - tm.tm_wday, tm.tm_yday, tm.tm_isdst); - return b[i]; -} - -#define TM(ss,mm,hh,md,mo,yr,wd,yd,dst) (struct tm){ \ - .tm_sec = ss, .tm_min = mm, .tm_hour = hh, \ - .tm_mday = md, .tm_mon = mo, .tm_year = yr, \ - .tm_wday = wd, .tm_yday = yd, .tm_isdst = dst } - -#define TM_EPOCH TM(0,0,0,1,0,70,4,0,0) -#define TM_Y2038_1S TM(7,14,3,19,0,138,2,18,0) -#define TM_Y2038 TM(8,14,3,19,0,138,2,18,0) - -#define TEST_TM(r,x,m) (!tm_cmp((r),(x)) || \ - (error("%s failed:\n\tresult: %s\n\texpect: %s\n", \ - m, tm_str(r), tm_str(x)), 0) ) - -#define TEST(r, f, x, m) ( \ - ((r) = (f)) == (x) || \ - (error("%s failed (" m ")\n", #f, r, x), 0) ) - -int main(void) -{ - struct tm tm, *tm_p; - time_t t; - - putenv("TZ=GMT"); - tzset(); - - t=0; tm_p = gmtime(&t); - TEST_TM(*tm_p, TM_EPOCH, "gmtime(0)"); - - tm = TM_Y2038_1S; - t = mktime(&tm); - tm = *(gmtime(&t)); - TEST_TM(*tm_p, TM_Y2038_1S, "mktime/gmtime(Y2038-1)"); - - tm = TM_Y2038; - t = mktime(&tm); - tm = *(gmtime(&t)); - TEST_TM(*tm_p, TM_Y2038, "mktime/gmtime(Y2038)"); - - /* FIXME: set a TZ var and check DST boundary conditions */ - return test_status; -}