parse_number_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if (res) {
  60. uint8_t *nozero_digits = digits;
  61. while (*nozero_digits == '0' && nozero_digits != &digits[num_digits - 1]) {
  62. nozero_digits++;
  63. }
  64. char buf[40];
  65. int size = compute_decimal_repr_size(num);
  66. generate_decimal_repr(num, buf, size);
  67. buf[size] = '\0';
  68. ASSERT_FORCE(!strcmp(buf, (const char *)nozero_digits));
  69. }
  70. }
  71. static void test_value (uintmax_t x)
  72. {
  73. char str[40];
  74. sprintf(str, "%" PRIuMAX, x);
  75. uintmax_t y;
  76. int res = parse_unsigned_integer_bin(str, strlen(str), &y);
  77. ASSERT_FORCE(res);
  78. ASSERT_FORCE(y == x);
  79. char str2[40];
  80. int size = compute_decimal_repr_size(x);
  81. generate_decimal_repr(x, str2, size);
  82. str2[size] = '\0';
  83. ASSERT_FORCE(!strcmp(str2, str));
  84. }
  85. static void test_value_range (uintmax_t start, uintmax_t count)
  86. {
  87. uintmax_t i = start;
  88. do {
  89. test_value(i);
  90. i++;
  91. } while (i != start + count);
  92. }
  93. int main ()
  94. {
  95. srand(time(NULL));
  96. for (int num_digits = 1; num_digits <= 22; num_digits++) {
  97. for (int i = 0; i < 1000000; i++) {
  98. test_random(num_digits, 10);
  99. }
  100. for (int i = 0; i < 1000000; i++) {
  101. test_random(num_digits, 11);
  102. }
  103. }
  104. test_value_range(UINTMAX_C(0), 5000000);
  105. test_value_range(UINTMAX_C(100000000), 5000000);
  106. test_value_range(UINTMAX_C(258239003), 5000000);
  107. test_value_range(UINTMAX_C(8241096180752634), 5000000);
  108. test_value_range(UINTMAX_C(9127982390882308083), 5000000);
  109. test_value_range(UINTMAX_C(18446744073700000000), 20000000);
  110. return 0;
  111. }