1 /* $OpenBSD: strtonumtest.c,v 1.1 2004/08/03 20:38:36 otto Exp $ */
3 * Copyright (c) 2004 Otto Moerbeek <otto@drijf.net>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* OPENBSD ORIGINAL: regress/lib/libc/strtonum/strtonumtest.c */
24 /* LLONG_MAX is known as LONGLONG_MAX on AIX */
25 #if defined(LONGLONG_MAX) && !defined(LLONG_MAX)
26 # define LLONG_MAX LONGLONG_MAX
27 # define LLONG_MIN LONGLONG_MIN
30 /* LLONG_MAX is known as LONG_LONG_MAX on HP-UX */
31 #if defined(LONG_LONG_MAX) && !defined(LLONG_MAX)
32 # define LLONG_MAX LONG_LONG_MAX
33 # define LLONG_MIN LONG_LONG_MIN
36 long long strtonum(const char *, long long, long long, const char **);
41 test(const char *p
, long long lb
, long long ub
, int ok
)
46 val
= strtonum(p
, lb
, ub
, &q
);
47 if (ok
&& q
!= NULL
) {
48 fprintf(stderr
, "%s [%lld-%lld] ", p
, lb
, ub
);
49 fprintf(stderr
, "NUMBER NOT ACCEPTED %s\n", q
);
51 } else if (!ok
&& q
== NULL
) {
52 fprintf(stderr
, "%s [%lld-%lld] %lld ", p
, lb
, ub
, val
);
53 fprintf(stderr
, "NUMBER ACCEPTED\n");
58 int main(int argc
, char *argv
[])
63 test("0", 2, LLONG_MAX
, 0);
64 test("-2", 0, LLONG_MAX
, 0);
65 test("0", -5, LLONG_MAX
, 1);
66 test("-3", -3, LLONG_MAX
, 1);
67 test("-9223372036854775808", LLONG_MIN
, LLONG_MAX
, 1);
68 test("9223372036854775807", LLONG_MIN
, LLONG_MAX
, 1);
69 test("-9223372036854775809", LLONG_MIN
, LLONG_MAX
, 0);
70 test("9223372036854775808", LLONG_MIN
, LLONG_MAX
, 0);
71 test("1000000000000000000000000", LLONG_MIN
, LLONG_MAX
, 0);
72 test("-1000000000000000000000000", LLONG_MIN
, LLONG_MAX
, 0);
73 test("-2", 10, -1, 0);
74 test("-2", -10, -1, 1);
75 test("-20", -10, -1, 0);
76 test("20", -10, -1, 0);