1 /* $OpenBSD: strtonumtest.c,v 1.1 2004/08/03 20:38:36 otto Exp $ */ 2 /* 3 * Copyright (c) 2004 Otto Moerbeek <otto@drijf.net> 4 * 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. 8 * 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. 16 */ 17 18 /* OPENBSD ORIGINAL: regress/lib/libc/strtonum/strtonumtest.c */ 19 20 #include <limits.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 24 int fail; 25 26 void 27 test(const char *p, long long lb, long long ub, int ok) 28 { 29 long long val; 30 const char *q; 31 32 val = strtonum(p, lb, ub, &q); 33 if (ok && q != NULL) { 34 fprintf(stderr, "%s [%lld-%lld] ", p, lb, ub); 35 fprintf(stderr, "NUMBER NOT ACCEPTED %s\n", q); 36 fail = 1; 37 } else if (!ok && q == NULL) { 38 fprintf(stderr, "%s [%lld-%lld] %lld ", p, lb, ub, val); 39 fprintf(stderr, "NUMBER ACCEPTED\n"); 40 fail = 1; 41 } 42 } 43 44 int main(int argc, char *argv[]) 45 { 46 test("1", 0, 10, 1); 47 test("0", -2, 5, 1); 48 test("0", 2, 5, 0); 49 test("0", 2, LLONG_MAX, 0); 50 test("-2", 0, LLONG_MAX, 0); 51 test("0", -5, LLONG_MAX, 1); 52 test("-3", -3, LLONG_MAX, 1); 53 test("-9223372036854775808", LLONG_MIN, LLONG_MAX, 1); 54 test("9223372036854775807", LLONG_MIN, LLONG_MAX, 1); 55 test("-9223372036854775809", LLONG_MIN, LLONG_MAX, 0); 56 test("9223372036854775808", LLONG_MIN, LLONG_MAX, 0); 57 test("1000000000000000000000000", LLONG_MIN, LLONG_MAX, 0); 58 test("-1000000000000000000000000", LLONG_MIN, LLONG_MAX, 0); 59 test("-2", 10, -1, 0); 60 test("-2", -10, -1, 1); 61 test("-20", -10, -1, 0); 62 test("20", -10, -1, 0); 63 64 return (fail); 65 } 66 67