strtonumtest.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /* OPENBSD ORIGINAL: regress/lib/libc/strtonum/strtonumtest.c */
  18. #include <limits.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. /* LLONG_MAX is known as LONGLONG_MAX on AIX */
  22. #if defined(LONGLONG_MAX) && !defined(LLONG_MAX)
  23. # define LLONG_MAX LONGLONG_MAX
  24. # define LLONG_MIN LONGLONG_MIN
  25. #endif
  26. /* LLONG_MAX is known as LONG_LONG_MAX on HP-UX */
  27. #if defined(LONG_LONG_MAX) && !defined(LLONG_MAX)
  28. # define LLONG_MAX LONG_LONG_MAX
  29. # define LLONG_MIN LONG_LONG_MIN
  30. #endif
  31. long long strtonum(const char *, long long, long long, const char **);
  32. int fail;
  33. void
  34. test(const char *p, long long lb, long long ub, int ok)
  35. {
  36. long long val;
  37. const char *q;
  38. val = strtonum(p, lb, ub, &q);
  39. if (ok && q != NULL) {
  40. fprintf(stderr, "%s [%lld-%lld] ", p, lb, ub);
  41. fprintf(stderr, "NUMBER NOT ACCEPTED %s\n", q);
  42. fail = 1;
  43. } else if (!ok && q == NULL) {
  44. fprintf(stderr, "%s [%lld-%lld] %lld ", p, lb, ub, val);
  45. fprintf(stderr, "NUMBER ACCEPTED\n");
  46. fail = 1;
  47. }
  48. }
  49. int main(int argc, char *argv[])
  50. {
  51. test("1", 0, 10, 1);
  52. test("0", -2, 5, 1);
  53. test("0", 2, 5, 0);
  54. test("0", 2, LLONG_MAX, 0);
  55. test("-2", 0, LLONG_MAX, 0);
  56. test("0", -5, LLONG_MAX, 1);
  57. test("-3", -3, LLONG_MAX, 1);
  58. test("-9223372036854775808", LLONG_MIN, LLONG_MAX, 1);
  59. test("9223372036854775807", LLONG_MIN, LLONG_MAX, 1);
  60. test("-9223372036854775809", LLONG_MIN, LLONG_MAX, 0);
  61. test("9223372036854775808", LLONG_MIN, LLONG_MAX, 0);
  62. test("1000000000000000000000000", LLONG_MIN, LLONG_MAX, 0);
  63. test("-1000000000000000000000000", LLONG_MIN, LLONG_MAX, 0);
  64. test("-2", 10, -1, 0);
  65. test("-2", -10, -1, 1);
  66. test("-20", -10, -1, 0);
  67. test("20", -10, -1, 0);
  68. return (fail);
  69. }