[Skiboot] [PATCH 1/3] Test libc stdlib functions (atoi/strtol and friends)

Daniel Axtens dja at axtens.net
Fri Feb 20 13:45:05 AEDT 2015


This increases coverage of atoi, atol, strtol and strtoul to 100%.

Signed-off-by: Daniel Axtens <dja at axtens.net>
---
 libc/test/run-stdlib.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/libc/test/run-stdlib.c b/libc/test/run-stdlib.c
index 15ec9bd..bb64790 100644
--- a/libc/test/run-stdlib.c
+++ b/libc/test/run-stdlib.c
@@ -23,5 +23,65 @@
 
 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);
+
+	/* our atoi recognises hex!  */
+	assert(atoi("0x800") == 0x800);
+	/* Really weird hex! */
+	assert(atoi("0x0x800") == 0x800);
+
+	/* atol - ensure it recognises longs */
+	assert(atol("2147483648") == 2147483648);
+	assert(atol("-2147483649") == -2147483649);
+
+	/* 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);
+	/* Again, really weird hex */
+	assert(strtoul("0x0x800", NULL, 0) == 0x800);
+
+	/* 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);
+
 	return 0;
 }
-- 
2.1.4



More information about the Skiboot mailing list