1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/* Copyright 2013-2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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;
}
|