aboutsummaryrefslogtreecommitdiffstats
path: root/roms/skiboot/libc/test
diff options
context:
space:
mode:
authorAngelos Mouzakitis <a.mouzakitis@virtualopensystems.com>2023-10-10 14:33:42 +0000
committerAngelos Mouzakitis <a.mouzakitis@virtualopensystems.com>2023-10-10 14:33:42 +0000
commitaf1a266670d040d2f4083ff309d732d648afba2a (patch)
tree2fc46203448ddcc6f81546d379abfaeb323575e9 /roms/skiboot/libc/test
parente02cda008591317b1625707ff8e115a4841aa889 (diff)
Add submodule dependency filesHEADmaster
Change-Id: Iaf8d18082d3991dec7c0ebbea540f092188eb4ec
Diffstat (limited to 'roms/skiboot/libc/test')
-rw-r--r--roms/skiboot/libc/test/.gitignore10
-rw-r--r--roms/skiboot/libc/test/Makefile.check74
-rw-r--r--roms/skiboot/libc/test/run-ctype-test.c56
-rw-r--r--roms/skiboot/libc/test/run-ctype.c95
-rw-r--r--roms/skiboot/libc/test/run-memops-test.c97
-rw-r--r--roms/skiboot/libc/test/run-memops.c98
-rw-r--r--roms/skiboot/libc/test/run-snprintf-test.c42
-rw-r--r--roms/skiboot/libc/test/run-snprintf.c246
-rw-r--r--roms/skiboot/libc/test/run-stdlib-test.c19
-rw-r--r--roms/skiboot/libc/test/run-stdlib.c85
-rw-r--r--roms/skiboot/libc/test/run-time.c86
11 files changed, 908 insertions, 0 deletions
diff --git a/roms/skiboot/libc/test/.gitignore b/roms/skiboot/libc/test/.gitignore
new file mode 100644
index 000000000..c1002af5b
--- /dev/null
+++ b/roms/skiboot/libc/test/.gitignore
@@ -0,0 +1,10 @@
+gmon.out
+run-ctype
+run-ctype-gcov
+run-memops
+run-memops-gcov
+run-snprintf
+run-snprintf-gcov
+run-stdlib
+run-stdlib-gcov
+
diff --git a/roms/skiboot/libc/test/Makefile.check b/roms/skiboot/libc/test/Makefile.check
new file mode 100644
index 000000000..a668334a1
--- /dev/null
+++ b/roms/skiboot/libc/test/Makefile.check
@@ -0,0 +1,74 @@
+# -*-Makefile-*-
+LIBC_TEST := libc/test/run-time
+
+# We have some tricky tests that have system libc and skiboot libc,
+# so that we can test parts of skiboot libc but in userspace.
+
+LIBC_DUALLIB_TEST := libc/test/run-snprintf \
+ libc/test/run-memops \
+ libc/test/run-stdlib \
+ libc/test/run-ctype
+
+LCOV_EXCLUDE += $(LIBC_TEST:%=%.c) $(LIBC_DUALLIB_TEST:%=%.c) $(LIBC_DUALLIB_TEST:%=%-test.c)
+
+.PHONY : libc-check libc-coverage
+libc-check: $(LIBC_TEST:%=%-check) $(LIBC_DUALLIB_TEST:%=%-check)
+libc-coverage: $(LIBC_TEST:%=%-gcov-run) $(LIBC_DUALLIB_TEST:%=%-gcov-run)
+
+check: libc-check
+coverage: libc-coverage
+
+$(LIBC_TEST:%=%-gcov-run) : %-run: %
+ $(call Q, TEST-COVERAGE ,$< , $<)
+
+$(LIBC_DUALLIB_TEST:%=%-gcov-run) : %-run: %
+ $(eval LCOV_DIRS += -d $(dir $<) )
+ $(call Q, TEST-COVERAGE , (cd $(dir $<); GCOV_PREFIX_STRIP=`(c=0; while [ "\`pwd\`" != '/' ]; do cd ..; c=\`expr 1 + $$c\`; done; echo $$c)` ./$(notdir $<) ), $< )
+
+$(LIBC_TEST:%=%-check) : %-check: %
+ $(call Q, RUN-TEST ,$(VALGRIND) $<, $<)
+
+$(LIBC_DUALLIB_TEST:%=%-check) : %-check: %
+ $(call Q, RUN-TEST ,$(VALGRIND) $<, $<)
+
+$(LIBC_TEST) : % : %.c
+ $(call Q, HOSTCC ,$(HOSTCC) $(HOSTCFLAGS) -O0 -g -I include -I . -I libfdt -I libc/include -o $@ $<, $<)
+
+$(LIBC_TEST:%=%-gcov): %-gcov : %.c %
+ $(call Q, HOSTCC ,(cd $(dir $<); $(HOSTCC) $(HOSTCFLAGS) $(HOSTGCOVCFLAGS) -I$(shell pwd)/include -I$(shell pwd) -I$(shell pwd)/libfdt -I$(shell pwd)/libc/include -o $(notdir $@) $(notdir $<)), $<)
+
+$(LIBC_DUALLIB_TEST) : % : %.o %-test.o
+ $(call Q, HOSTCC ,$(HOSTCC) $(HOSTCFLAGS) -O0 -g -I include -I . -I libfdt -o $@ $@-test.o $<, $<)
+
+$(LIBC_DUALLIB_TEST:%=%-gcov) : %-gcov : %-gcov.o %-gcov-test.o
+ $(call Q, HOSTCC ,(cd $(dir $<); $(HOSTCC) $(HOSTCFLAGS) $(HOSTGCOVCFLAGS) -I$(shell pwd)/include -I$(shell pwd)/. -I$(shell pwd)/libfdt -o $(notdir $@) $(notdir $@)-test.o $(notdir $<)), $<)
+
+$(LIBC_DUALLIB_TEST:%=%-test.o): %-test.o : %-test.c
+ $(call Q, HOSTCC ,$(HOSTCC) $(HOSTCFLAGS) -O0 -g -I include -I . -I libfdt -I libc/include -ffreestanding -o $@ -c $<, $<)
+
+$(LIBC_DUALLIB_TEST:%=%.o): %.o : %.c
+ $(call Q, HOSTCC ,$(HOSTCC) $(HOSTCFLAGS) -O0 -g -o $@ -c $<, $<)
+
+$(LIBC_DUALLIB_TEST:%=%-gcov.o): %-gcov.o : %.c
+ $(call Q, HOSTCC ,(cd $(dir $<); $(HOSTCC) $(HOSTCFLAGS) -fprofile-arcs -ftest-coverage -lgcov -pg -O0 -g -o $(notdir $@) -c $(notdir $<)), $<)
+
+$(LIBC_DUALLIB_TEST:%=%-gcov-test.o): %-gcov-test.o : %-test.c %
+ $(call Q, HOSTCC ,(cd $(dir $<); $(HOSTCC) $(HOSTCFLAGS) -fprofile-arcs -ftest-coverage -lgcov -pg -O0 -g -I$(shell pwd)/include -I$(shell pwd)/. -I$(shell pwd)/libfdt -I$(shell pwd)/libc/include -ffreestanding -o $(notdir $@) -c $(notdir $<) ), $<)
+
+-include $(wildcard libc/test/*.d)
+
+clean: libc-test-clean
+
+libc-test-clean:
+ $(RM) -f libc/test/*.[od]
+ $(RM) -f $(LIBC_TEST) \
+ $(LIBC_TEST:%=%-gcov) \
+ $(LIBC_TEST:%=%.gcda) \
+ $(LIBC_TEST:%=%.gcno)
+ $(RM) -f $(LIBC_DUALLIB_TEST) \
+ $(LIBC_DUALLIB_TEST:%=%-gcov) \
+ $(LIBC_DUALLIB_TEST:%=%-gcov.gcda) \
+ $(LIBC_DUALLIB_TEST:%=%-gcov.gcno) \
+ $(LIBC_DUALLIB_TEST:%=%-gcov-test.gcda) \
+ $(LIBC_DUALLIB_TEST:%=%-gcov-test.gcno) \
+ $(LIBC_DUALLIB_TEST:%=%-test.o)
diff --git a/roms/skiboot/libc/test/run-ctype-test.c b/roms/skiboot/libc/test/run-ctype-test.c
new file mode 100644
index 000000000..d491b4942
--- /dev/null
+++ b/roms/skiboot/libc/test/run-ctype-test.c
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+/*
+ * Copyright 2013-2015 IBM Corp.
+ *
+ * This file is run with the skiboot libc files rather than system libc.
+ * This means we have a bit of "fun" with actually executing the tests on
+ * the host.
+ * Patches to make this less ugly are very welcome.
+ */
+
+#include <config.h>
+#include <stdarg.h>
+
+#include "../ctype/isdigit.c"
+#include "../ctype/isprint.c"
+#include "../ctype/isspace.c"
+#include "../ctype/isxdigit.c"
+#include "../ctype/tolower.c"
+#include "../ctype/toupper.c"
+
+int skiboot_isdigit(int ch);
+int skiboot_isprint(int ch);
+int skiboot_isspace(int ch);
+int skiboot_isxdigit(int ch);
+int skiboot_tolower(int ch);
+int skiboot_toupper(int ch);
+
+int skiboot_isdigit(int ch)
+{
+ return isdigit(ch);
+}
+
+int skiboot_isprint(int ch)
+{
+ return isprint(ch);
+}
+
+int skiboot_isspace(int ch)
+{
+ return isspace(ch);
+}
+
+int skiboot_isxdigit(int ch)
+{
+ return isxdigit(ch);
+}
+
+int skiboot_tolower(int ch)
+{
+ return tolower(ch);
+}
+
+int skiboot_toupper(int ch)
+{
+ return toupper(ch);
+}
diff --git a/roms/skiboot/libc/test/run-ctype.c b/roms/skiboot/libc/test/run-ctype.c
new file mode 100644
index 000000000..a43d37123
--- /dev/null
+++ b/roms/skiboot/libc/test/run-ctype.c
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+/*
+ * Copyright 2013-2015 IBM Corp.
+ */
+
+#define BUFSZ 50
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <ctype.h>
+
+int skiboot_isdigit(int ch);
+int skiboot_isprint(int ch);
+int skiboot_isspace(int ch);
+int skiboot_isxdigit(int ch);
+int skiboot_tolower(int ch);
+int skiboot_toupper(int ch);
+
+int main(void)
+{
+ int i;
+ int r1, r2;
+
+ for(i = '0'; i <= '9'; i++)
+ assert(skiboot_isdigit(i));
+ assert(skiboot_isdigit('a') == 0);
+ assert(skiboot_isdigit('Z') == 0);
+
+ for (i = 0; i < 257; i++) {
+ r1 = skiboot_isdigit(i);
+ r2 = isdigit(i);
+ if (r1)
+ assert(r2);
+ if (!r1)
+ assert(!r2);
+ }
+
+ for(i = '0'; i <= '9'; i++)
+ assert(skiboot_isprint(i));
+ assert(skiboot_isprint('\0') == 0);
+ assert(skiboot_isprint(4) == 0);
+
+ for (i = 0; i < 257; i++) {
+ r1 = skiboot_isprint(i);
+ r2 = isprint(i);
+ if (r1)
+ assert(r2);
+ if (!r1)
+ assert(!r2);
+ }
+
+ for(i = '0'; i <= '9'; i++)
+ assert(skiboot_isspace(i) == 0);
+ assert(skiboot_isspace('\f'));
+ assert(skiboot_isspace('\n'));
+ assert(skiboot_isspace(' '));
+
+ for (i = 0; i < 257; i++) {
+ r1 = skiboot_isspace(i);
+ r2 = isspace(i);
+ if (r1)
+ assert(r2);
+ if (!r1)
+ assert(!r2);
+ }
+
+ for(i = '0'; i <= '9'; i++)
+ assert(skiboot_isxdigit(i));
+ assert(skiboot_isxdigit('a'));
+ assert(skiboot_isxdigit('A'));
+ assert(skiboot_isxdigit('F'));
+ assert(skiboot_isxdigit('Z') == 0);
+
+ for (i = 0; i < 257; i++) {
+ r1 = skiboot_isxdigit(i);
+ r2 = isxdigit(i);
+ if (r1)
+ assert(r2);
+ if (!r1)
+ assert(!r2);
+ }
+
+ for (i = 0; i < 257; i++) {
+ assert(skiboot_tolower(i) == tolower(i));
+ }
+
+ for (i = 0; i < 257; i++) {
+ assert(skiboot_toupper(i) == toupper(i));
+ }
+
+ return 0;
+}
diff --git a/roms/skiboot/libc/test/run-memops-test.c b/roms/skiboot/libc/test/run-memops-test.c
new file mode 100644
index 000000000..5be0d9ac1
--- /dev/null
+++ b/roms/skiboot/libc/test/run-memops-test.c
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+/*
+ * Copyright 2013-2015 IBM Corp.
+ *
+ * This file is run with the skiboot libc files rather than system libc.
+ * This means we have a bit of "fun" with actually executing the tests on
+ * the host.
+ * Patches to make this less ugly are very welcome.
+ */
+
+#include <config.h>
+#include <stdarg.h>
+
+#include "../string/memchr.c"
+#include "../string/memcmp.c"
+#include "../string/memcpy.c"
+#include "../string/memmove.c"
+#include "../string/memset.c"
+#include "../string/strcasecmp.c"
+#include "../string/strcat.c"
+#include "../string/strchr.c"
+#include "../string/strrchr.c"
+#include "../string/strcmp.c"
+#include "../string/strcpy.c"
+/* #include "../string/strdup.c" */
+#include "../string/strlen.c"
+#include "../string/strncasecmp.c"
+#include "../string/strncmp.c"
+#include "../string/strncpy.c"
+#include "../string/strstr.c"
+#include "../string/strtok.c"
+#include <stdlib.h>
+
+int test_memset(char* buf, int c, size_t s);
+int test_memchr(const void *ptr, int c, size_t n, void* expected);
+int test_memcmp(const void *ptr1, const void *ptr2, size_t n, int expected);
+int test_strcmp(const void *ptr1, const void *ptr2, int expected);
+int test_strchr(const char *s, int c, char *expected);
+int test_strrchr(const char *s, int c, char *expected);
+int test_strcasecmp(const char *s1, const char *s2, int expected);
+int test_strncasecmp(const char *s1, const char *s2, size_t n, int expected);
+int test_memmove(void *dest, const void *src, size_t n, const void *r, const void *expected, size_t expected_n);
+
+int test_memset(char* buf, int c, size_t s)
+{
+ int i;
+ int r= 0;
+
+ memset(buf, c, s);
+ for(i=0; i<s; i++)
+ if (buf[i] != c)
+ r = -1;
+
+ return r;
+}
+
+int test_memchr(const void *ptr, int c, size_t n, void* expected)
+{
+ return(expected == memchr(ptr, c, n));
+}
+
+int test_memcmp(const void *ptr1, const void *ptr2, size_t n, int expected)
+{
+ return(expected == memcmp(ptr1, ptr2, n));
+}
+
+int test_strcmp(const void *ptr1, const void *ptr2, int expected)
+{
+ return(expected == strcmp(ptr1, ptr2));
+}
+
+int test_strchr(const char *s, int c, char *expected)
+{
+ return(expected == strchr(s, c));
+}
+
+int test_strrchr(const char *s, int c, char *expected)
+{
+ return(expected == strrchr(s, c));
+}
+
+int test_strcasecmp(const char *s1, const char *s2, int expected)
+{
+ return(expected == strcasecmp(s1, s2));
+}
+
+int test_strncasecmp(const char *s1, const char *s2, size_t n, int expected)
+{
+ return(expected == strncasecmp(s1, s2, n));
+}
+
+int test_memmove(void *dest, const void *src, size_t n, const void *r, const void *expected, size_t expected_n)
+{
+ if (memmove(dest, src, n) != dest)
+ return -1;
+ return(memcmp(r, expected, expected_n) == 0);
+}
diff --git a/roms/skiboot/libc/test/run-memops.c b/roms/skiboot/libc/test/run-memops.c
new file mode 100644
index 000000000..a00c2fb36
--- /dev/null
+++ b/roms/skiboot/libc/test/run-memops.c
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+/*
+ * Copyright 2013-2015 IBM Corp.
+ */
+
+#define BUFSZ 50
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+
+int test_memset(char* buf, int c, size_t s);
+int test_memchr(const void *ptr, int c, size_t n, void* expected);
+int test_memcmp(const void *ptr1, const void *ptr2, size_t n, int expected);
+int test_strcmp(const void *ptr1, const void *ptr2, int expected);
+int test_strchr(const char *s, int c, char *expected);
+int test_strrchr(const char *s, int c, char *expected);
+int test_strcasecmp(const char *s1, const char *s2, int expected);
+int test_strncasecmp(const char *s1, const char *s2, size_t n, int expected);
+int test_memmove(void *dest, const void *src, size_t n, const void *r, const void *expected, size_t expected_n);
+
+int main(void)
+{
+ char *buf;
+ char *buf2;
+
+ buf = malloc(100);
+ assert(test_memset(buf, 0x42, 100) == 0);
+ free(buf);
+
+ buf = malloc(128);
+ assert(test_memset(buf, 0, 128) == 0);
+ assert(test_memset(buf+1, 0, 127) == 0);
+ free(buf);
+
+ buf = malloc(1024);
+ assert(test_memset(buf, 0, 1024) == 0);
+ free(buf);
+
+ buf = malloc(20);
+ strncpy(buf, "Hello World!", 20);
+ assert(test_memchr(buf, 'o', strlen(buf), buf+4));
+ assert(test_memchr(buf, 'a', strlen(buf), NULL));
+
+ assert(test_memcmp(buf, "Hello World!", strlen(buf), 0));
+ assert(test_memcmp(buf, "Hfllow World", strlen(buf), -1));
+
+ assert(test_strcmp(buf, "Hello World!", 0));
+ assert(test_strcmp(buf, "Hfllow World", -1));
+
+ assert(test_strchr(buf, 'H', buf));
+ assert(test_strchr(buf, 'e', buf+1));
+ assert(test_strchr(buf, 'a', NULL));
+ assert(test_strchr(buf, '!', buf+11));
+
+ assert(test_strrchr(buf, 'H', buf));
+ assert(test_strrchr(buf, 'o', buf+7));
+ assert(test_strrchr(buf, 'a', NULL));
+ assert(test_strrchr(buf, 'l', buf+9));
+ assert(test_strrchr(buf, '!', buf+11));
+
+ assert(test_strcasecmp(buf, "Hello World!", 0));
+ assert(test_strcasecmp(buf, "HELLO WORLD!", 0));
+ assert(test_strcasecmp(buf, "IELLO world!", -1));
+ assert(test_strcasecmp(buf, "HeLLo WOrlc!", 1));
+
+ assert(test_strncasecmp(buf, "Hello World!", strlen(buf), 0));
+ assert(test_strncasecmp(buf, "HELLO WORLD!", strlen(buf), 0));
+ assert(test_strncasecmp(buf, "IELLO world!", strlen(buf), -1));
+ assert(test_strncasecmp(buf, "HeLLo WOrlc!", strlen(buf), 1));
+
+ assert(test_strncasecmp(buf, "HeLLo WOrlc!", 0, 0));
+ assert(test_strncasecmp(buf, "HeLLo WOrlc!", 1, 0));
+ assert(test_strncasecmp(buf, "HeLLo WOrlc!", 2, 0));
+ assert(test_strncasecmp(buf, "HeLLp WOrlc!", 5, -1));
+
+ free(buf);
+
+ buf = malloc(20);
+ buf2 = malloc(20);
+ strncpy(buf, "Hello", 20);
+ strncpy(buf2, " World!", 20);
+
+ assert(test_memmove(buf + 5, buf2, strlen(buf2), buf,
+ "Hello World!", strlen("Hello World!")));
+
+ strncpy(buf, "HHello World!", 20);
+ assert(test_memmove(buf, buf+1, strlen("Hello World!"), buf, "Hello World!", strlen("Hello World!")));
+
+ strncpy(buf, "0123456789", 20);
+ assert(test_memmove(buf+1, buf , strlen("0123456789"), buf, "00123456789", strlen("00123456789")));
+
+ free(buf);
+ free(buf2);
+
+ return 0;
+}
diff --git a/roms/skiboot/libc/test/run-snprintf-test.c b/roms/skiboot/libc/test/run-snprintf-test.c
new file mode 100644
index 000000000..a6ad02373
--- /dev/null
+++ b/roms/skiboot/libc/test/run-snprintf-test.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+/*
+ * Copyright 2013-2015 IBM Corp.
+ *
+ * This file is run with the skiboot libc files rather than system libc.
+ * This means we have a bit of "fun" with actually executing the tests on
+ * the host.
+ * Patches to make this less ugly are very welcome.
+ */
+
+#include <config.h>
+#include <stdarg.h>
+
+#include "../stdio/snprintf.c"
+#include "../stdio/vsnprintf.c"
+
+int test1(void);
+
+int test1(void)
+{
+ return snprintf(NULL, 1, "Hello");
+}
+
+int skiboot_snprintf(char *buf, size_t bufsz, size_t l, const char* format, ...);
+
+int skiboot_snprintf(char *buf, size_t bufsz, size_t l, const char* format, ...)
+{
+ va_list ar;
+ int count;
+
+ if (buf)
+ memset(buf, 0, bufsz);
+
+ if ((buf==NULL) || (format==NULL))
+ return(-1);
+
+ va_start(ar, format);
+ count = vsnprintf(buf, l, format, ar);
+ va_end(ar);
+
+ return(count);
+}
diff --git a/roms/skiboot/libc/test/run-snprintf.c b/roms/skiboot/libc/test/run-snprintf.c
new file mode 100644
index 000000000..946d446fa
--- /dev/null
+++ b/roms/skiboot/libc/test/run-snprintf.c
@@ -0,0 +1,246 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+/*
+ * Copyright 2013-2015 IBM Corp.
+ */
+
+#define BUFSZ 50
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+
+int test1(void);
+int skiboot_snprintf(char *buf, size_t bufsz, size_t l, const char* format, ...);
+
+static void test_printf_0u(int n)
+{
+ char *buf, *buf2;
+ int blen;
+ unsigned int i;
+
+ for(i=1; i<10; i++)
+ {
+ blen = i+1;
+ if (n<0)
+ blen++;
+
+ buf = (char*)malloc(blen);
+ buf2 = (char*)malloc(blen);
+ skiboot_snprintf(buf, blen, blen, "%08u", n);
+ snprintf(buf2, blen, "%08u", n);
+ n = n * 10;
+ assert(0 == strncmp(buf, buf2, blen));
+ free(buf);
+ free(buf2);
+ }
+}
+
+static void test_printf_u(int n)
+{
+ char *buf, *buf2;
+ int blen;
+ unsigned int r;
+ unsigned int i;
+
+ for(i=1; i<10; i++)
+ {
+ blen = i+1;
+ if (n<0)
+ blen++;
+
+ buf = (char*)malloc(blen);
+ buf2 = (char*)malloc(blen);
+ r = skiboot_snprintf(buf, blen, blen, "%u", n);
+ snprintf(buf2, blen, "%u", n);
+ n = n * 10;
+ if (n<0)
+ assert(i+1 == r);
+ else
+ assert(i == r);
+ assert(0 == strncmp(buf, buf2, blen));
+ free(buf);
+ free(buf2);
+ }
+}
+
+static void test_printf_d(int n)
+{
+ char *buf, *buf2;
+ int blen;
+ int r;
+ int i;
+
+ for(i=1; i<10; i++)
+ {
+ blen = i+1;
+ if (n<0)
+ blen++;
+
+ buf = (char*)malloc(blen);
+ buf2 = (char*)malloc(blen);
+ r = skiboot_snprintf(buf, blen, blen, "%d", n);
+ snprintf(buf2, blen, "%d", n);
+ n = n * 10;
+ if (n<0)
+ assert(i+1 == r);
+ else
+ assert(i == r);
+ assert(0 == strncmp(buf, buf2, blen));
+ free(buf);
+ free(buf2);
+ }
+}
+
+static void test_printf_x(const char* f)
+{
+ char *buf, *buf2;
+ int blen;
+ int i, r;
+ unsigned int n=0x1;
+
+ for (i=0; i<8; i++)
+ {
+ blen = i+2;
+ buf = (char*)malloc(blen);
+ buf2 = (char*)malloc(blen);
+ r = skiboot_snprintf(buf, blen, blen, f, n);
+ snprintf(buf2, blen, f, n);
+ assert(i+1 == r);
+ assert(0 == strncmp(buf, buf2, blen));
+ free(buf);
+ free(buf2);
+ n = n << 4;
+ }
+}
+
+static void test_printf_c(void)
+{
+ char *buf= (char*)malloc(2);
+ char buf2[2];
+ unsigned char i= 0xff;
+ int r;
+ while(i)
+ {
+ r = skiboot_snprintf(buf, 2, 2, "%c", i);
+ snprintf(buf2, 2, "%c", i);
+ assert(r==1);
+ assert(0 == strncmp(buf, buf2, 2));
+ i--;
+ }
+ free(buf);
+}
+
+static void test_printf_p(void)
+{
+ char *buf= (char*)malloc(32);
+ char buf2[32];
+ skiboot_snprintf(buf, 32, 32, "%p", buf);
+ snprintf(buf2, 32, "%p", buf);
+ assert(0 == strncmp(buf, buf2, 32));
+ free(buf);
+}
+
+static void test_printf_o(void)
+{
+ char *buf= (char*)malloc(32);
+ char buf2[32];
+ skiboot_snprintf(buf, 32, 32, "%o", 0x12345678);
+ snprintf(buf2, 32, "%o", 0x12345678);
+ assert(0 == strncmp(buf, buf2, 32));
+ free(buf);
+}
+
+static void test_printf_h(short i)
+{
+ char *buf= (char*)malloc(32);
+ char buf2[32];
+ skiboot_snprintf(buf, 32, 32, "%hd", i);
+ snprintf(buf2, 32, "%hd", i);
+ assert(0 == strncmp(buf, buf2, 32));
+ free(buf);
+}
+
+static void test_printf_z(size_t i)
+{
+ char *buf= (char*)malloc(32);
+ char buf2[32];
+ skiboot_snprintf(buf, 32, 32, "%zu", i);
+ snprintf(buf2, 32, "%zu", i);
+ assert(0 == strncmp(buf, buf2, 32));
+ free(buf);
+}
+
+int main(void)
+{
+ char *buf;
+ int r;
+
+ buf = (char*)malloc(BUFSZ);
+ memset(buf, 0, BUFSZ);
+
+ assert(-1 == test1());
+
+ r = skiboot_snprintf(buf, BUFSZ, 2, "%%");
+ assert(r==1);
+ assert(buf[0] == '%' && buf[1] == 0);
+
+ r = skiboot_snprintf(buf, BUFSZ, 2, "%d", 137);
+ /* BUG/FIXME:
+ * skiboot libc does NOT return the length of the buffer you'd need
+ * Instead, it'll return something random, possibly zero (as here)
+ * but as you'll see in test_in_buf_len2, sometimes not.
+ *
+ * Basically, we're not POSIX printf and this is some day going to
+ * cause things to be awful.
+ */
+ assert(0 == r); // BUG, should be 3
+ assert(0 == strncmp(buf, "", 3));
+
+ r = skiboot_snprintf(buf, BUFSZ, 4, "%d", 137);
+ assert(3 == r);
+ assert(0 == strncmp(buf, "137", 3));
+ assert(buf[3] == 0);
+
+ /* Now we test the strange behaviour of our printf.
+ * For strings, we get partial prints going, but if we whack an
+ * integer on the end, we may or may not get that integer, depending
+ * on if we have enough size. We should test that though */
+
+ r = skiboot_snprintf(buf, BUFSZ, 4, "Hello %d", 137);
+ assert(3 == r);
+ assert(0 == strncmp(buf, "Hel", 3));
+ assert(buf[3] == 0);
+ r = skiboot_snprintf(buf, BUFSZ, 7, "Hello %d", 137);
+ assert(6 == r);
+ assert(0 == strncmp(buf, "Hello ", 6));
+ assert(buf[6] == 0);
+ r = skiboot_snprintf(buf, BUFSZ, 10, "Hello %d", 137);
+ assert(9 == r);
+ assert(0 == strncmp(buf, "Hello 137", 10));
+ assert(buf[9] == 0);
+ free(buf);
+
+ test_printf_u(1);
+ test_printf_0u(1);
+ test_printf_d(1);
+ test_printf_d(-1);
+ test_printf_x("%x");
+ test_printf_x("%X");
+ test_printf_c();
+ test_printf_p();
+ test_printf_o();
+ test_printf_h(0);
+ test_printf_h(128);
+ test_printf_h(256);
+ test_printf_h(-1);
+ test_printf_h(32767);
+ test_printf_h(32768);
+ test_printf_h(65535);
+ test_printf_z(0);
+ test_printf_z(-1);
+ test_printf_z(12345);
+ test_printf_z(128000000);
+
+ return 0;
+}
diff --git a/roms/skiboot/libc/test/run-stdlib-test.c b/roms/skiboot/libc/test/run-stdlib-test.c
new file mode 100644
index 000000000..f7d7137c9
--- /dev/null
+++ b/roms/skiboot/libc/test/run-stdlib-test.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+/*
+ * Copyright 2013-2015 IBM Corp.
+ *
+ * This file is run with the skiboot libc files rather than system libc.
+ * This means we have a bit of "fun" with actually executing the tests on
+ * the host.
+ * Patches to make this less ugly are very welcome.
+ */
+
+#include <config.h>
+#include <stdarg.h>
+
+#include "../stdlib/atoi.c"
+#include "../stdlib/atol.c"
+#include "../stdlib/error.c"
+#include "../stdlib/rand.c"
+#include "../stdlib/strtol.c"
+#include "../stdlib/strtoul.c"
diff --git a/roms/skiboot/libc/test/run-stdlib.c b/roms/skiboot/libc/test/run-stdlib.c
new file mode 100644
index 000000000..174920897
--- /dev/null
+++ b/roms/skiboot/libc/test/run-stdlib.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+/*
+ * Copyright 2013-2015 IBM Corp.
+ */
+
+#define BUFSZ 50
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+
+int main(void)
+{
+ char buf[] = "42, and stuff.";
+ char *ptr;
+
+ /* atoi/strtol - general correct behavior */
+ assert(atoi("0") == 0);
+ assert(atoi("1") == 1);
+ assert(atoi(" 123456") == 123456);
+ assert(atoi("-72") == -72);
+ assert(atoi(" -84") == -84);
+ assert(atoi("2147483647") == 2147483647);
+
+ /* atoi/strtol - numbers before and after strings */
+ assert(atoi("hello!123") == 0);
+ assert(atoi(buf) == 42);
+ assert(atoi("42isthemagicnumber") == 42);
+
+ /* atoi is base 10 only */
+ assert(atoi("0x800") == 0);
+
+ /* atol - ensure it recognises longs */
+ assert(atol("2147483648") == 2147483648);
+ assert(atol("-2147483649") == -2147483649);
+
+ /* strtol detects hex */
+ assert(strtol("0x800", NULL, 0) == 0x800);
+ /* But not with a duplicate prefix */
+ assert(strtol("0x0x800", NULL, 0) == 0);
+
+ /* strtol - invalid/weird bases */
+ assert(strtol("z", NULL, -1) == 0);
+ assert(strtol("11111", NULL, 1) == 0);
+ assert(strtol("z", NULL, 37) == 0);
+ assert(strtol("z", NULL, 36) == 35);
+ assert(strtol("-Y", NULL, 36) == -34);
+
+ /* strtol - ptr advanced correctly */
+ ptr = buf;
+ assert(strtol(buf, &ptr, 10) == 42);
+ assert(ptr == buf + 2);
+
+ /* strtoul - base 10 */
+ assert(strtoul("0", NULL, 10) == 0);
+ assert(strtoul("1", NULL, 10) == 1);
+ assert(strtoul(" 123456", NULL, 10) == 123456);
+ assert(strtoul("-72", NULL, 10) == 0);
+ assert(strtoul("9999999999", NULL, 10) == 9999999999);
+ assert(strtoul("hello!123", NULL, 10) == 0);
+ assert(strtoul(buf, NULL, 10) == 42);
+ assert(strtoul("42isthemagicnumber", NULL, 10) == 42);
+
+ /* strtoul - autodetection of base */
+ assert(strtoul(" 123456", NULL, 0) == 123456);
+ assert(strtoul("0x800", NULL, 0) == 0x800);
+ assert(strtoul("0x0x800", NULL, 0) == 0);
+
+ /* strtoul - weird/invalid bases */
+ assert(strtoul("z", NULL, -1) == 0);
+ assert(strtoul("11111", NULL, 1) == 0);
+ assert(strtoul("z", NULL, 37) == 0);
+ assert(strtoul("z", NULL, 36) == 35);
+ assert(strtoul("Y", NULL, 36) == 34);
+
+ /* labs - ensure it returns absolute value */
+ assert(labs(0) == 0);
+ assert(labs(2147483647) == 2147483647);
+ assert(labs(-2147483647) == 2147483647);
+ assert(labs(9223372036854775807) == 9223372036854775807);
+ assert(labs(-9223372036854775807) == 9223372036854775807);
+
+ return 0;
+}
diff --git a/roms/skiboot/libc/test/run-time.c b/roms/skiboot/libc/test/run-time.c
new file mode 100644
index 000000000..95e83e5a9
--- /dev/null
+++ b/roms/skiboot/libc/test/run-time.c
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+/*
+ * Copyright 2014-2015 IBM Corp.
+ */
+#include "/usr/include/assert.h"
+#include <stdio.h>
+#include <libc/include/time.h>
+#include <stdint.h>
+
+#include "../time.c"
+
+#define MKTIME_TEST(Y,M,D,h,m,s,t) \
+ tm.tm_year = Y; \
+ tm.tm_mon = M; \
+ tm.tm_mday = D; \
+ tm.tm_hour = h; \
+ tm.tm_min = m; \
+ tm.tm_sec = s; \
+ assert(mktime(&tm) == t); \
+ assert(tm.tm_year == Y); \
+ assert(tm.tm_mon == M); \
+ assert(tm.tm_mday == D); \
+ assert(tm.tm_hour == h); \
+ assert(tm.tm_min == m); \
+ assert(tm.tm_sec == s)
+
+#define GMTIME_TEST(Y,M,D,h,m,s,tv) \
+ t = tv; \
+ gmtime_r(&t, &tm); \
+ assert(tm.tm_year == Y); \
+ assert(tm.tm_mon == M); \
+ assert(tm.tm_mday == D); \
+ assert(tm.tm_hour == h); \
+ assert(tm.tm_min == m); \
+ assert(tm.tm_sec == s)
+
+#define TIME_TEST(Y,M,D,h,m,s,tv) \
+ MKTIME_TEST(Y,M,D,h,m,s,tv); \
+ GMTIME_TEST(Y,M,D,h,m,s,tv)
+
+int main(void)
+{
+ struct tm tm;
+ time_t t = 0;
+
+ TIME_TEST(1970, 0, 1, 0, 0, 0, 0);
+ TIME_TEST(1971, 0, 1, 0, 0, 0, 365*SECS_PER_DAY);
+ TIME_TEST(1972, 0, 1, 0, 0, 0, 2*365*SECS_PER_DAY);
+ TIME_TEST(1972, 11, 31, 0, 0, 0, 3*365*SECS_PER_DAY);
+ TIME_TEST(1973, 0, 1, 0, 0, 0, (3*365+1)*SECS_PER_DAY);
+ TIME_TEST(2000, 11, 31, 0, 0, 0, 978220800);
+ TIME_TEST(2001, 0, 1, 0, 0, 0, 978307200);
+ TIME_TEST(2003, 11, 31, 0, 0, 0, 1072828800);
+ TIME_TEST(2004, 0, 1, 0, 0, 0, 1072828800+SECS_PER_DAY);
+ TIME_TEST(2004, 11, 29, 0, 0, 0, 1072828800+364*SECS_PER_DAY);
+ TIME_TEST(2004, 11, 30, 0, 0, 0, 1072828800+365*SECS_PER_DAY);
+ TIME_TEST(2004, 11, 31, 0, 0, 0, 1072828800+366*SECS_PER_DAY);
+ TIME_TEST(2004, 11, 31, 23, 59, 59, 1072828800+367*SECS_PER_DAY-1);
+ TIME_TEST(2100, 11, 31, 0, 0, 0, 4133894400);
+ TIME_TEST(2101, 0, 1, 0, 0, 0, 4133980800);
+
+ /* Test the normalisation functionality of mktime */
+ tm.tm_year = 2000;
+ tm.tm_mon = 1;
+ tm.tm_mday = 10;
+ tm.tm_hour = 5;
+ tm.tm_min = 32;
+ tm.tm_sec = 105;
+ mktime(&tm);
+ assert(tm.tm_year == 2000);
+ assert(tm.tm_mon == 1);
+ assert(tm.tm_mday == 10);
+ assert(tm.tm_hour == 5);
+ assert(tm.tm_min == 33);
+ assert(tm.tm_sec == 45);
+ tm.tm_sec += 366*24*60*60;
+ mktime(&tm);
+ assert(tm.tm_year == 2001);
+ assert(tm.tm_mon == 1);
+ assert(tm.tm_mday == 10);
+ assert(tm.tm_hour == 5);
+ assert(tm.tm_min == 33);
+ assert(tm.tm_sec == 45);
+
+ return 0;
+}