scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit a68625773cbd82ba0f38d563aba336d5668b2e43
parent fad3fe30d4c05fd3307e3b216897635abbad173a
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Thu, 30 Apr 2026 22:37:58 +0200

tests/libc: Add 0086-strtol

Diffstat:
Mtests/libc/execute/.gitignore | 1+
Atests/libc/execute/0086-strtol.c | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/libc/execute/libc-tests.lst | 1+
3 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/tests/libc/execute/.gitignore b/tests/libc/execute/.gitignore @@ -84,3 +84,4 @@ test.log 0083-strtod 0084-strtof 0085-strtold +0086-strtol diff --git a/tests/libc/execute/0086-strtol.c b/tests/libc/execute/0086-strtol.c @@ -0,0 +1,76 @@ +#include <assert.h> +#include <errno.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> + +/* +output: +testing +done +end: +*/ + + +int +test(void) +{ + long n; + char buf[64], *endp; + + errno = 0; + n = strtol("1024", NULL, 10); + assert(n == 1024 && errno == 0); + + n = strtol("1024", &endp, 10); + assert(n == 1024 && errno == 0 && *endp == '\0'); + + n = strtol("1000", &endp, 10); + assert(n == 1000 && errno == 0 && *endp == '\0'); + + n = strtol("1000", &endp, 8); + assert(n == 512 && errno == 0 && *endp == '\0'); + + n = strtol("1000", &endp, 16); + assert(n == 4096 && errno == 0 && *endp == '\0'); + + n = strtol("1000", &endp, 0); + assert(n == 1000 && errno == 0 && *endp == '\0'); + + n = strtol("0x1000", &endp, 0); + assert(n == 4096 && errno == 0 && *endp == '\0'); + + n = strtol("01000", &endp, 0); + assert(n == 512 && errno == 0 && *endp == '\0'); + + n = strtol(" +1a", &endp, 0); + assert(n == 1 && errno == 0 && *endp == 'a'); + + n = strtol(" -1a", &endp, 0); + assert(n == -1 && errno == 0 && *endp == 'a'); + + sprintf(buf, "%ld", LONG_MAX); + n = strtol(buf, NULL, 10); + assert(n == LONG_MAX && errno == 0); + + sprintf(buf, "%ld", LONG_MIN); + n = strtol(buf, NULL, 10); + assert(n == LONG_MIN && errno == 0); + + n = strtol("9999999999999999999999999", NULL, 10); + assert(n == LONG_MAX && errno == ERANGE); + + errno = 0; + n = strtol("-999999999999999999999999", NULL, 10); + assert(n == LONG_MIN && errno == ERANGE); +} + +int +main(void) +{ + puts("testing"); + test(); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst @@ -82,3 +82,4 @@ 0083-strtod [TODO] 0084-strtof [TODO] 0085-strtold [TODO] +0086-strtol