summaryrefslogtreecommitdiffstats
path: root/libc
diff options
context:
space:
mode:
authorDaniel Axtens <dja@axtens.net>2015-02-20 13:45:05 +1100
committerStewart Smith <stewart@linux.vnet.ibm.com>2015-02-23 13:35:49 +1100
commite4ce915ea0631f5b17082aa8c70079b0e9dc33af (patch)
tree65aaaf46d386ad1194d3f3de397911683b833d38 /libc
parent952377e097ea328e224a38953ce8ddd2cdda5fa9 (diff)
downloadtalos-skiboot-e4ce915ea0631f5b17082aa8c70079b0e9dc33af.tar.gz
talos-skiboot-e4ce915ea0631f5b17082aa8c70079b0e9dc33af.zip
Test libc stdlib functions (atoi/strtol and friends)
This increases coverage of atoi, atol, strtol and strtoul to 100%. Signed-off-by: Daniel Axtens <dja@axtens.net> Reviewed-by: Cyril Bur <cyrilbur@gmail.com> Signed-off-by: Stewart Smith <stewart@linux.vnet.ibm.com>
Diffstat (limited to 'libc')
-rw-r--r--libc/test/run-stdlib.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/libc/test/run-stdlib.c b/libc/test/run-stdlib.c
index 15ec9bd5..bb647904 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;
}
OpenPOWER on IntegriCloud