parse_number_test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @file parse_number_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <inttypes.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <errno.h>
  34. #include <time.h>
  35. #include <misc/parse_number.h>
  36. #include <misc/debug.h>
  37. static void test_random (int num_digits, int digit_modulo)
  38. {
  39. ASSERT(num_digits > 0);
  40. ASSERT(digit_modulo > 0);
  41. uint8_t digits[40];
  42. for (int i = 0; i < num_digits; i++) {
  43. digits[i] = '0' + (rand() % digit_modulo);
  44. }
  45. digits[num_digits] = '\0';
  46. char *endptr;
  47. uintmax_t std_num = strtoumax((const char *)digits, &endptr, 10);
  48. int std_res = !*endptr && !(std_num == UINTMAX_MAX && errno == ERANGE);
  49. uintmax_t num = 0;
  50. int res = parse_unsigned_integer_bin((const char *)digits, num_digits, &num);
  51. if (res != std_res) {
  52. printf("fail1 %s\n", (const char *)digits);
  53. ASSERT_FORCE(0);
  54. }
  55. if (res && num != std_num) {
  56. printf("fail2 %s\n", (const char *)digits);
  57. ASSERT_FORCE(0);
  58. }
  59. }
  60. static void test_value (uintmax_t x)
  61. {
  62. char str[40];
  63. sprintf(str, "%" PRIuMAX, x);
  64. uintmax_t y;
  65. int res = parse_unsigned_integer_bin(str, strlen(str), &y);
  66. ASSERT_FORCE(res);
  67. ASSERT_FORCE(y == x);
  68. }
  69. static void test_value_range (uintmax_t start, uintmax_t count)
  70. {
  71. uintmax_t i = start;
  72. do {
  73. test_value(i);
  74. i++;
  75. } while (i != start + count);
  76. }
  77. int main ()
  78. {
  79. srand(time(NULL));
  80. for (int num_digits = 1; num_digits <= 22; num_digits++) {
  81. for (int i = 0; i < 1000000; i++) {
  82. test_random(num_digits, 10);
  83. }
  84. for (int i = 0; i < 1000000; i++) {
  85. test_random(num_digits, 11);
  86. }
  87. }
  88. test_value_range(UINTMAX_C(0), 5000000);
  89. test_value_range(UINTMAX_C(100000000), 5000000);
  90. test_value_range(UINTMAX_C(258239003), 5000000);
  91. test_value_range(UINTMAX_C(8241096180752634), 5000000);
  92. test_value_range(UINTMAX_C(9127982390882308083), 5000000);
  93. test_value_range(UINTMAX_C(18446744073700000000), 20000000);
  94. return 0;
  95. }