Advertisement
uaa

[wip:20221230] my_fcvt(), my_gcvt() test

uaa
Dec 30th, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /* $OpenBSD: gcvt_test.c,v 1.5 2019/01/25 00:19:26 millert Exp $ */
  2.  
  3. /*
  4. * Public domain, 2010, Todd C. Miller <[email protected]>
  5. *
  6. * Modified for my_fcvt() and my_gcvt()
  7. * by SASANO Takayoshi <[email protected]>
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. static struct test_vector {
  15. double d;
  16. int ndig;
  17. } test_vectors[] = {
  18. /* adapted from perl's Configure test */
  19. { 0.1, 8, },
  20. { 0.01, 8, },
  21. { 0.001, 8, },
  22. { 0.0001, 8, },
  23. { 0.00009, 8, },
  24. { 1.0, 8, },
  25. { 1.1, 8, },
  26. { 1.01, 8, },
  27. { 1.001, 8, },
  28. { 1.0001, 8, },
  29. { 1.00001, 8, },
  30. { 1.0, 8, },
  31. { 0.0, 8, },
  32. { -1.0, 8, },
  33. { 100000, 8, },
  34. { -100000, 8, },
  35. { 123.456, 8, },
  36. { 1e34, 8, },
  37. /* adapted from emx */
  38. { 0.0, -1, },
  39. { 0.0, 0, },
  40. { 0.0, 1, },
  41. { 0.0, 2, },
  42. { 1.0, -1, },
  43. { 1.0, 0, },
  44. { 1.0, 2, },
  45. { 1.0, 10, },
  46. { 1.236, -1, },
  47. { 1.236, 0, },
  48. { 1.236, 1, },
  49. { 1.236, 2, },
  50. { 1.236, 3, },
  51. { 1.236, 4, },
  52. { 1.236, 5, },
  53. { 1.236, 6, },
  54. { 12.36, -1, },
  55. { 12.36, 0, },
  56. { 12.36, 1, },
  57. { 12.36, 2, },
  58. { 12.36, 3, },
  59. { 12.36, 4, },
  60. { 12.36, 5, },
  61. { 12.36, 6, },
  62. { 123.6, -1, },
  63. { 123.6, 0, },
  64. { 123.6, 1, },
  65. { 123.6, 2, },
  66. { 123.6, 3, },
  67. { 123.6, 4, },
  68. { 123.6, 5, },
  69. { 123.6, 6, },
  70. { 1236, -1, },
  71. { 1236.0, 0, },
  72. { 1236.0, 1, },
  73. { 1236.0, 2, },
  74. { 1236.0, 3, },
  75. { 1236.0, 4, },
  76. { 1236.0, 5, },
  77. { 1236.0, 6, },
  78. { 1e100, 10, },
  79. { 1e100, 20, },
  80. { 0.01236, -1, },
  81. { 0.01236, 0, },
  82. { 0.01236, 1, },
  83. { 0.01236, 2, },
  84. { 0.01236, 3, },
  85. { 0.01236, 4, },
  86. { 1e-100, 20, },
  87. { 1e-100, -1, },
  88. { -1.2, 5, },
  89. { -0.03, 5, },
  90. { 0.1, 1, },
  91. { 0.1, 0, },
  92. { 0.099999, 10, },
  93. { 0.99999, 10, },
  94. };
  95.  
  96. #define NTESTVEC (sizeof(test_vectors) / sizeof(test_vectors[0]))
  97.  
  98. char *my_gcvt(double value, int ndigit, char *buf)
  99. {
  100. sprintf(buf, "%.*g", ndigit, value);
  101. return buf;
  102. }
  103.  
  104. char *my_fcvt(double value, int ndigit, int *decpt, int *sign)
  105. {
  106. #define FCVT_BUFFER_SIZE 512
  107.  
  108. int n;
  109. static char buf[FCVT_BUFFER_SIZE];
  110. char tmp[FCVT_BUFFER_SIZE], *p = tmp, *q;
  111.  
  112. if (ndigit < 0) {
  113. fprintf(stderr, "my_fcvt: ndigit %d\n", ndigit);
  114. return NULL;
  115. }
  116.  
  117. snprintf(tmp, sizeof(tmp), "%#+.*f", ndigit, value);
  118. if (sign != NULL) *sign = (*p == '-');
  119. p++;
  120.  
  121. if (*p == '0') {
  122. p += 2;
  123. for (n = 0; *p == '0'; p++, n--);
  124. } else {
  125. q = strchr(p, '.');
  126. n = q - p;
  127. memcpy(buf, p, n);
  128.  
  129. p += n + 1;
  130. }
  131.  
  132. strcpy(buf + ((n < 0) ? 0 : n), p);
  133. if (decpt != NULL) *decpt = n;
  134. return buf;
  135. }
  136.  
  137. static int
  138. dotest_gcvt(struct test_vector *tv)
  139. {
  140. char buf0[256], buf1[256];
  141.  
  142. gcvt(tv->d, tv->ndig, buf0);
  143. my_gcvt(tv->d, tv->ndig, buf1);
  144. if (strcmp(buf0, buf1)) {
  145. fprintf(stderr, "%g @ %d: expected %s got %s\n",
  146. tv->d, tv->ndig, buf0, buf1);
  147. return 1;
  148. }
  149. return 0;
  150. }
  151.  
  152. static int
  153. dotest_fcvt(struct test_vector *tv)
  154. {
  155. char *p, buf0[256], buf1[256];
  156. int decpt0, decpt1, sign0, sign1;
  157.  
  158. decpt0 = sign0 = 0;
  159. memset(buf0, 0, sizeof(buf0));
  160. p = fcvt(tv->d, tv->ndig, &decpt0, &sign0);
  161. if (p != NULL)
  162. strcpy(buf0, p);
  163.  
  164. decpt1 = sign1 = 0;
  165. memset(buf1, 0, sizeof(buf1));
  166. p = my_fcvt(tv->d, tv->ndig, &decpt1, &sign1);
  167. if (p != NULL)
  168. strcpy(buf1, p);
  169.  
  170. if (strcmp(buf0, buf1) || decpt0 != decpt1 || sign0 != sign1) {
  171. fprintf(stderr, "%g @ %d: expected %s %d %d got %s %d %d\n",
  172. tv->d, tv->ndig,
  173. buf0, decpt0, sign0, buf1, decpt1, sign1);
  174. return 1;
  175. }
  176.  
  177. return 0;
  178. }
  179.  
  180. int
  181. main(int argc, char *argv[])
  182. {
  183. int i, failures = 0;
  184.  
  185. for (i = 0; i < NTESTVEC; i++) {
  186. failures += dotest_gcvt(&test_vectors[i]);
  187. }
  188.  
  189. for (i = 0; i < NTESTVEC; i++) {
  190. failures += dotest_fcvt(&test_vectors[i]);
  191. }
  192.  
  193. return failures;
  194. }
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement