test-strtol.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Test program for strtol family of funtions,
  2. Copyright (C) 2014 Free Software Foundation, Inc.
  3. Written by Yury Gribov <y.gribov@samsung.com>
  4. This file is part of the libiberty library, which is part of GCC.
  5. This file is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. In addition to the permissions in the GNU General Public License, the
  10. Free Software Foundation gives you unlimited permission to link the
  11. compiled version of this file into combinations with other programs,
  12. and to distribute those combinations without any restriction coming
  13. from the use of this file. (The General Public License restrictions
  14. do apply in other respects; for example, they cover modification of
  15. the file, and distribution when not linked into a combined
  16. executable.)
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include "libiberty.h"
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #ifndef EXIT_SUCCESS
  41. #define EXIT_SUCCESS 0
  42. #endif
  43. #ifndef EXIT_FAILURE
  44. #define EXIT_FAILURE 1
  45. #endif
  46. /* Test input data. */
  47. enum conversion_fun
  48. {
  49. STRTOL,
  50. STRTOLL,
  51. STRTOUL,
  52. STRTOULL,
  53. };
  54. #ifdef HAVE_LONG_LONG
  55. typedef unsigned long long integer_type;
  56. #else
  57. typedef unsigned long integer_type;
  58. #endif
  59. struct test_data_t
  60. {
  61. enum conversion_fun fun;
  62. const char *nptr;
  63. int base;
  64. integer_type res;
  65. int errnum;
  66. };
  67. const struct test_data_t test_data[] = {
  68. { STRTOL, "0x123", 0, 0x123L, 0 },
  69. { STRTOL, "123", 0, 123L, 0 },
  70. { STRTOL, "0123", 0, 0123L, 0 },
  71. { STRTOL, "0x7FFFFFFF", 0, 0x7fffffffL, 0 },
  72. { STRTOL, "-0x80000000", 0, -0x80000000L, 0 },
  73. { STRTOUL, "0x123", 0, 0x123UL, 0 },
  74. { STRTOUL, "123", 0, 123UL, 0 },
  75. { STRTOUL, "0123", 0, 0123UL, 0 },
  76. { STRTOUL, "0xFFFFFFFF", 0, 0xffffffffUL, 0 },
  77. #if SIZEOF_LONG == 4
  78. { STRTOL, "0x80000000", 0, 0x7fffffffL, ERANGE },
  79. { STRTOL, "-0x80000001", 0, -0x80000000L, ERANGE },
  80. { STRTOUL, "0x100000000", 0, 0xffffffffUL, ERANGE },
  81. #endif
  82. #ifdef HAVE_LONG_LONG
  83. { STRTOLL, "0x123", 0, 0x123LL, 0 },
  84. { STRTOLL, "123", 0, 123LL, 0 },
  85. { STRTOLL, "0123", 0, 0123LL, 0 },
  86. { STRTOLL, "0x7FFFFFFFFFFFFFFF", 0, 0x7fffffffffffffffLL, 0 },
  87. { STRTOLL, "-0x8000000000000000", 0, -0x8000000000000000LL, 0 },
  88. { STRTOULL, "0x123", 0, 0x123ULL, 0 },
  89. { STRTOULL, "123", 0, 123ULL, 0 },
  90. { STRTOULL, "0123", 0, 0123ULL, 0 },
  91. { STRTOULL, "0xFFFFFFFFFFFFFFFF", 0, 0xffffffffffffffffULL, 0 },
  92. #if SIZEOF_LONG_LONG == 8
  93. { STRTOLL, "0x8000000000000000", 0, 0x7fffffffffffffffLL, ERANGE },
  94. { STRTOLL, "-0x8000000000000001", 0, -0x8000000000000000LL, ERANGE },
  95. { STRTOULL, "0x10000000000000000", 0, 0xffffffffffffffffULL, ERANGE },
  96. #endif
  97. #endif
  98. };
  99. /* run_tests:
  100. Run conversion function
  101. Compare results
  102. Return number of fails */
  103. int
  104. run_tests (const struct test_data_t *test_data, size_t ntests)
  105. {
  106. int fails = 0, failed;
  107. size_t i;
  108. for (i = 0; i < ntests; ++i)
  109. {
  110. integer_type res;
  111. int saved_errno;
  112. errno = 0;
  113. switch (test_data[i].fun)
  114. {
  115. case STRTOL:
  116. res = (unsigned long) strtol (test_data[i].nptr,
  117. 0, test_data[i].base);
  118. break;
  119. case STRTOUL:
  120. res = strtoul (test_data[i].nptr, 0, test_data[i].base);
  121. break;
  122. #ifdef HAVE_LONG_LONG
  123. case STRTOLL:
  124. res = strtoll (test_data[i].nptr, 0, test_data[i].base);
  125. break;
  126. case STRTOULL:
  127. res = strtoull (test_data[i].nptr, 0, test_data[i].base);
  128. break;
  129. #endif
  130. }
  131. saved_errno = errno;
  132. failed = 0;
  133. /* Compare result */
  134. if (res != test_data[i].res)
  135. {
  136. printf ("FAIL: test-strtol-%zd. Results don't match.\n", i);
  137. failed++;
  138. }
  139. /* Compare errno */
  140. if (saved_errno != test_data[i].errnum)
  141. {
  142. printf ("FAIL: test-strtol-%zd. Errnos don't match.\n", i);
  143. failed++;
  144. }
  145. if (!failed)
  146. printf ("PASS: test-strtol-%zd.\n", i);
  147. else
  148. fails++;
  149. }
  150. return fails;
  151. }
  152. int
  153. main(int argc, char **argv)
  154. {
  155. int fails;
  156. fails = run_tests (test_data, sizeof (test_data) / sizeof (test_data[0]));
  157. exit (fails ? EXIT_FAILURE : EXIT_SUCCESS);
  158. }