ffi-hydrogen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +30 -0
  5. data/.travis.yml +10 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +72 -0
  9. data/Rakefile +46 -0
  10. data/bench/both.rb +86 -0
  11. data/bench/encode.rb +57 -0
  12. data/bench/encrypt.rb +80 -0
  13. data/bench/init.rb +5 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/ffi-hydrogen.gemspec +31 -0
  17. data/lib/ffi/hydrogen.rb +216 -0
  18. data/vendor/.clang-format +2 -0
  19. data/vendor/.gitignore +3 -0
  20. data/vendor/README.md +2 -0
  21. data/vendor/libhydrogen/.clang-format +95 -0
  22. data/vendor/libhydrogen/.gitignore +32 -0
  23. data/vendor/libhydrogen/.travis.yml +22 -0
  24. data/vendor/libhydrogen/LICENSE +18 -0
  25. data/vendor/libhydrogen/Makefile +61 -0
  26. data/vendor/libhydrogen/Makefile.arduino +51 -0
  27. data/vendor/libhydrogen/README.md +29 -0
  28. data/vendor/libhydrogen/hydrogen.c +18 -0
  29. data/vendor/libhydrogen/hydrogen.h +317 -0
  30. data/vendor/libhydrogen/impl/common.h +316 -0
  31. data/vendor/libhydrogen/impl/core.h +220 -0
  32. data/vendor/libhydrogen/impl/gimli-core/portable.h +39 -0
  33. data/vendor/libhydrogen/impl/gimli-core/sse2.h +97 -0
  34. data/vendor/libhydrogen/impl/gimli-core.h +25 -0
  35. data/vendor/libhydrogen/impl/hash.h +138 -0
  36. data/vendor/libhydrogen/impl/hydrogen_p.h +83 -0
  37. data/vendor/libhydrogen/impl/kdf.h +20 -0
  38. data/vendor/libhydrogen/impl/kx.h +441 -0
  39. data/vendor/libhydrogen/impl/pwhash.h +281 -0
  40. data/vendor/libhydrogen/impl/random.h +376 -0
  41. data/vendor/libhydrogen/impl/secretbox.h +236 -0
  42. data/vendor/libhydrogen/impl/sign.h +207 -0
  43. data/vendor/libhydrogen/impl/x25519.h +383 -0
  44. data/vendor/libhydrogen/library.properties +10 -0
  45. data/vendor/libhydrogen/logo.png +0 -0
  46. data/vendor/libhydrogen/tests/tests.c +431 -0
  47. data/vendor/main.c +140 -0
  48. data/vendor/stringencoders/.gitignore +25 -0
  49. data/vendor/stringencoders/.travis.yml +13 -0
  50. data/vendor/stringencoders/AUTHORS +1 -0
  51. data/vendor/stringencoders/COPYING +2 -0
  52. data/vendor/stringencoders/ChangeLog +170 -0
  53. data/vendor/stringencoders/Doxyfile +276 -0
  54. data/vendor/stringencoders/INSTALL +119 -0
  55. data/vendor/stringencoders/LICENSE +22 -0
  56. data/vendor/stringencoders/Makefile.am +3 -0
  57. data/vendor/stringencoders/NEWS +3 -0
  58. data/vendor/stringencoders/README +2 -0
  59. data/vendor/stringencoders/README.md +32 -0
  60. data/vendor/stringencoders/bootstrap.sh +3 -0
  61. data/vendor/stringencoders/configure-gcc-hardened.sh +16 -0
  62. data/vendor/stringencoders/configure.ac +44 -0
  63. data/vendor/stringencoders/doxy/footer.html +34 -0
  64. data/vendor/stringencoders/doxy/header.html +85 -0
  65. data/vendor/stringencoders/indent.sh +9 -0
  66. data/vendor/stringencoders/javascript/base64-speed.html +43 -0
  67. data/vendor/stringencoders/javascript/base64-test.html +209 -0
  68. data/vendor/stringencoders/javascript/base64.html +18 -0
  69. data/vendor/stringencoders/javascript/base64.js +176 -0
  70. data/vendor/stringencoders/javascript/qunit.css +119 -0
  71. data/vendor/stringencoders/javascript/qunit.js +1062 -0
  72. data/vendor/stringencoders/javascript/urlparse-test.html +367 -0
  73. data/vendor/stringencoders/javascript/urlparse.js +328 -0
  74. data/vendor/stringencoders/make-ci.sh +13 -0
  75. data/vendor/stringencoders/makerelease.sh +16 -0
  76. data/vendor/stringencoders/python/b85.py +176 -0
  77. data/vendor/stringencoders/src/Makefile.am +134 -0
  78. data/vendor/stringencoders/src/arraytoc.c +85 -0
  79. data/vendor/stringencoders/src/arraytoc.h +43 -0
  80. data/vendor/stringencoders/src/extern_c_begin.h +3 -0
  81. data/vendor/stringencoders/src/extern_c_end.h +3 -0
  82. data/vendor/stringencoders/src/html_named_entities_generator.py +203 -0
  83. data/vendor/stringencoders/src/modp_ascii.c +159 -0
  84. data/vendor/stringencoders/src/modp_ascii.h +162 -0
  85. data/vendor/stringencoders/src/modp_ascii_data.h +84 -0
  86. data/vendor/stringencoders/src/modp_ascii_gen.c +55 -0
  87. data/vendor/stringencoders/src/modp_b16.c +125 -0
  88. data/vendor/stringencoders/src/modp_b16.h +148 -0
  89. data/vendor/stringencoders/src/modp_b16_data.h +104 -0
  90. data/vendor/stringencoders/src/modp_b16_gen.c +65 -0
  91. data/vendor/stringencoders/src/modp_b2.c +69 -0
  92. data/vendor/stringencoders/src/modp_b2.h +130 -0
  93. data/vendor/stringencoders/src/modp_b2_data.h +44 -0
  94. data/vendor/stringencoders/src/modp_b2_gen.c +36 -0
  95. data/vendor/stringencoders/src/modp_b36.c +108 -0
  96. data/vendor/stringencoders/src/modp_b36.h +170 -0
  97. data/vendor/stringencoders/src/modp_b64.c +254 -0
  98. data/vendor/stringencoders/src/modp_b64.h +236 -0
  99. data/vendor/stringencoders/src/modp_b64_data.h +477 -0
  100. data/vendor/stringencoders/src/modp_b64_gen.c +168 -0
  101. data/vendor/stringencoders/src/modp_b64r.c +254 -0
  102. data/vendor/stringencoders/src/modp_b64r.h +242 -0
  103. data/vendor/stringencoders/src/modp_b64r_data.h +477 -0
  104. data/vendor/stringencoders/src/modp_b64w.c +254 -0
  105. data/vendor/stringencoders/src/modp_b64w.h +231 -0
  106. data/vendor/stringencoders/src/modp_b64w_data.h +477 -0
  107. data/vendor/stringencoders/src/modp_b85.c +109 -0
  108. data/vendor/stringencoders/src/modp_b85.h +171 -0
  109. data/vendor/stringencoders/src/modp_b85_data.h +36 -0
  110. data/vendor/stringencoders/src/modp_b85_gen.c +65 -0
  111. data/vendor/stringencoders/src/modp_bjavascript.c +65 -0
  112. data/vendor/stringencoders/src/modp_bjavascript.h +105 -0
  113. data/vendor/stringencoders/src/modp_bjavascript_data.h +84 -0
  114. data/vendor/stringencoders/src/modp_bjavascript_gen.c +58 -0
  115. data/vendor/stringencoders/src/modp_burl.c +228 -0
  116. data/vendor/stringencoders/src/modp_burl.h +259 -0
  117. data/vendor/stringencoders/src/modp_burl_data.h +136 -0
  118. data/vendor/stringencoders/src/modp_burl_gen.c +121 -0
  119. data/vendor/stringencoders/src/modp_html.c +128 -0
  120. data/vendor/stringencoders/src/modp_html.h +53 -0
  121. data/vendor/stringencoders/src/modp_html_named_entities.h +9910 -0
  122. data/vendor/stringencoders/src/modp_json.c +315 -0
  123. data/vendor/stringencoders/src/modp_json.h +103 -0
  124. data/vendor/stringencoders/src/modp_json_data.h +57 -0
  125. data/vendor/stringencoders/src/modp_json_gen.py +60 -0
  126. data/vendor/stringencoders/src/modp_mainpage.h +120 -0
  127. data/vendor/stringencoders/src/modp_numtoa.c +350 -0
  128. data/vendor/stringencoders/src/modp_numtoa.h +100 -0
  129. data/vendor/stringencoders/src/modp_qsiter.c +76 -0
  130. data/vendor/stringencoders/src/modp_qsiter.h +71 -0
  131. data/vendor/stringencoders/src/modp_stdint.h +43 -0
  132. data/vendor/stringencoders/src/modp_utf8.c +88 -0
  133. data/vendor/stringencoders/src/modp_utf8.h +38 -0
  134. data/vendor/stringencoders/src/modp_xml.c +311 -0
  135. data/vendor/stringencoders/src/modp_xml.h +166 -0
  136. data/vendor/stringencoders/src/stringencoders.pc +10 -0
  137. data/vendor/stringencoders/src/stringencoders.pc.in +10 -0
  138. data/vendor/stringencoders/test/Makefile.am +113 -0
  139. data/vendor/stringencoders/test/apr_base64.c +262 -0
  140. data/vendor/stringencoders/test/apr_base64.h +120 -0
  141. data/vendor/stringencoders/test/cxx_test.cc +482 -0
  142. data/vendor/stringencoders/test/minunit.h +82 -0
  143. data/vendor/stringencoders/test/modp_ascii_test.c +281 -0
  144. data/vendor/stringencoders/test/modp_b16_test.c +288 -0
  145. data/vendor/stringencoders/test/modp_b2_test.c +250 -0
  146. data/vendor/stringencoders/test/modp_b64_test.c +266 -0
  147. data/vendor/stringencoders/test/modp_b85_test.c +130 -0
  148. data/vendor/stringencoders/test/modp_bjavascript_test.c +137 -0
  149. data/vendor/stringencoders/test/modp_burl_test.c +423 -0
  150. data/vendor/stringencoders/test/modp_html_test.c +296 -0
  151. data/vendor/stringencoders/test/modp_json_test.c +336 -0
  152. data/vendor/stringencoders/test/modp_numtoa_test.c +545 -0
  153. data/vendor/stringencoders/test/modp_qsiter_test.c +280 -0
  154. data/vendor/stringencoders/test/modp_utf8_test.c +188 -0
  155. data/vendor/stringencoders/test/modp_xml_test.c +339 -0
  156. data/vendor/stringencoders/test/speedtest.c +241 -0
  157. data/vendor/stringencoders/test/speedtest_ascii.c +345 -0
  158. data/vendor/stringencoders/test/speedtest_msg.c +78 -0
  159. data/vendor/stringencoders/test/speedtest_numtoa.c +276 -0
  160. metadata +314 -0
@@ -0,0 +1,545 @@
1
+ /* we compile as C90 but use snprintf */
2
+ #define _ISOC99_SOURCE
3
+
4
+ #include "minunit.h"
5
+ #include <limits.h>
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+
10
+ #include "modp_numtoa.h"
11
+
12
+ /* Need only for INFINITY and optionally NAN macros */
13
+ /* We do not link with -lm */
14
+ #include <math.h>
15
+
16
+ static char* testITOA(void)
17
+ {
18
+ char buf1[100];
19
+ char buf2[100];
20
+ int i;
21
+ size_t len;
22
+ for (i = 0; i < 100000; ++i) {
23
+ sprintf(buf1, "%d", i);
24
+ len = modp_itoa10(i, buf2);
25
+ mu_assert_int_equals(len, strlen(buf1));
26
+ mu_assert_str_equals(buf1, buf2);
27
+
28
+ sprintf(buf1, "%d", -i);
29
+ len = modp_itoa10(-i, buf2);
30
+ mu_assert_int_equals(len, strlen(buf1));
31
+ mu_assert_str_equals(buf1, buf2);
32
+
33
+ sprintf(buf1, "%d", INT_MAX - i);
34
+ len = modp_itoa10(INT_MAX - i, buf2);
35
+ mu_assert_int_equals(len, strlen(buf1));
36
+ mu_assert_str_equals(buf1, buf2);
37
+
38
+ sprintf(buf1, "%d", -(INT_MAX - i));
39
+ len = modp_itoa10(-(INT_MAX - i), buf2);
40
+ mu_assert_int_equals(len, strlen(buf1));
41
+ mu_assert_str_equals(buf1, buf2);
42
+ }
43
+ return 0;
44
+ }
45
+
46
+ static char* testUITOA(void)
47
+ {
48
+ char buf1[100];
49
+ char buf2[100];
50
+ uint32_t i;
51
+ size_t len;
52
+ for (i = 0; i < 1000000; ++i) {
53
+ sprintf(buf1, "%u", i);
54
+ len = modp_uitoa10(i, buf2);
55
+ mu_assert_int_equals(len, strlen(buf1));
56
+ mu_assert_str_equals(buf1, buf2);
57
+ }
58
+
59
+ for (i = 0; i < 1000000; ++i) {
60
+ sprintf(buf1, "%u", 0xFFFFFFFFu - i);
61
+ len = modp_uitoa10(0xFFFFFFFFu - i, buf2);
62
+ mu_assert_int_equals(len, strlen(buf1));
63
+ mu_assert_str_equals(buf1, buf2);
64
+ }
65
+ return 0;
66
+ }
67
+
68
+ static char* testLITOA(void)
69
+ {
70
+ char buf1[100];
71
+ char buf2[100];
72
+ long int i;
73
+ size_t len;
74
+ for (i = 0; i < 100000; ++i) {
75
+ sprintf(buf1, "%ld", i);
76
+ len = modp_litoa10(i, buf2);
77
+ mu_assert_int_equals(len, strlen(buf1));
78
+ mu_assert_str_equals(buf1, buf2);
79
+
80
+ sprintf(buf1, "%ld", -i);
81
+ len = modp_litoa10(-i, buf2);
82
+ mu_assert_int_equals(len, strlen(buf1));
83
+ mu_assert_str_equals(buf1, buf2);
84
+
85
+ sprintf(buf1, "%ld", LONG_MAX - i);
86
+ len = modp_litoa10(LONG_MAX - i, buf2);
87
+ mu_assert_int_equals(len, strlen(buf1));
88
+ mu_assert_str_equals(buf1, buf2);
89
+
90
+ sprintf(buf1, "%ld", -(LONG_MAX - i));
91
+ len = modp_litoa10(-(LONG_MAX - i), buf2);
92
+ mu_assert_int_equals(len, strlen(buf1));
93
+ mu_assert_str_equals(buf1, buf2);
94
+ }
95
+ return 0;
96
+ }
97
+
98
+ static char* testULITOA(void)
99
+ {
100
+ char buf1[100];
101
+ char buf2[100];
102
+ size_t len;
103
+ long long unsigned int i;
104
+ for (i = 0; i < 1000000; ++i) {
105
+ sprintf(buf1, "%llu", i);
106
+ len = modp_ulitoa10(i, buf2);
107
+ mu_assert_int_equals(len, strlen(buf1));
108
+ mu_assert_str_equals(buf1, buf2);
109
+ }
110
+
111
+ for (i = 0; i < 1000000; ++i) {
112
+ sprintf(buf1, "%llu", 0xFFFFFFFFFFFFFFFFllu - i);
113
+ len = modp_ulitoa10(0xFFFFFFFFFFFFFFFFull - i, buf2);
114
+ mu_assert_int_equals(len, strlen(buf1));
115
+ mu_assert_str_equals(buf1, buf2);
116
+ }
117
+ return 0;
118
+ }
119
+
120
+ static char* testDoubleToA(void)
121
+ {
122
+ char buf1[100];
123
+ char buf2[100];
124
+ char msg[200];
125
+ double d;
126
+ size_t len;
127
+ char* tmp;
128
+ size_t tmplen;
129
+
130
+ /* test each combination of whole number + fraction,
131
+ at every precision */
132
+ /* and test negative version */
133
+ double wholes[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0,
134
+ 67.0, 101.0, 10000, 99999 };
135
+ double frac[] = { 0.0, 0.1, 0.2, 0.3, 0.4, 0.49, 0.5, 0.51, 0.6, 0.7,
136
+ 0.9, 0.01, 0.25, 0.125, 0.03, 0.0625, 0.0078125,
137
+ 0.001, 0.00001, 0.99, 0.999, 0.9999, 0.99999, 0.999999,
138
+ 0.875, 0.9375, 0.96875, 0.9921875,
139
+ // 0.95, 0.995, 0.9995, 0.99995, 0.999995, 0.9999995,
140
+ 0.09, 0.099, 0.0999, 0.09999, 0.099999, 0.0999999,
141
+ 0.09999999 };
142
+
143
+ /* TBD
144
+ * 0.95, 0.995, 0.9995, 0.99995, 0.999995, 0.9999995
145
+ * since not exactly represented by floating point
146
+ * printf uses some tricks that we do not use
147
+ * causing test issues
148
+ */
149
+
150
+ const char* formats[] = { "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f",
151
+ "%.6f", "%.7f", "%.8f", "%.9f" };
152
+
153
+ size_t imax = sizeof(wholes) / sizeof(double);
154
+ size_t jmax = sizeof(frac) / sizeof(double);
155
+ size_t kmax = sizeof(formats) / sizeof(const char*);
156
+
157
+ size_t i, j, k;
158
+ for (i = 0; i < imax; ++i) {
159
+ for (j = 0; j < jmax; ++j) {
160
+ for (k = 0; k < kmax; ++k) {
161
+ d = wholes[i] + frac[j];
162
+
163
+ sprintf(buf1, formats[k], d);
164
+ sprintf(msg, "orig=%f whole=%f, frac=%f, prec=%d -- want %s",
165
+ wholes[i] + frac[j], wholes[i], frac[j], (int)k, buf1);
166
+ len = modp_dtoa(d, buf2, (int)k);
167
+ mu_assert_str_equals_msg(msg, buf1, buf2);
168
+ mu_assert_int_equals(len, strlen(buf1));
169
+
170
+ if ((int)wholes[i] != 0 && (int)(frac[j] * 10000000) != 0) {
171
+ sprintf(msg, "whole=%f, frac=%f, prec=%d -- ",
172
+ -wholes[i], frac[j], (int)k);
173
+ /* not dealing with "-0" issues */
174
+ d = -d;
175
+ sprintf(buf1, formats[k], d);
176
+ len = modp_dtoa(d, buf2, (int)k);
177
+ mu_assert_int_equals(len, strlen(buf1));
178
+ mu_assert_str_equals_msg(msg, buf1, buf2);
179
+
180
+ /* find the '.', and see how many chars are after it */
181
+ tmp = buf2;
182
+ while (*tmp != '.' && *tmp != '\0') {
183
+ ++tmp;
184
+ }
185
+ if (*tmp == '\0') {
186
+ tmplen = 0;
187
+ } else {
188
+ tmplen = strlen(++tmp);
189
+ }
190
+
191
+ sprintf(msg, "whole=%f, frac=%f, prec=%d, got=%d %s-- ",
192
+ wholes[i], frac[j], (int)k, (int)tmplen, buf2);
193
+ mu_assert_msg(msg, k >= tmplen);
194
+ }
195
+ }
196
+ }
197
+ }
198
+
199
+ /* test very large positive number */
200
+ d = 1.0e200;
201
+ len = modp_dtoa(d, buf2, 6);
202
+ mu_assert_int_equals(len, strlen(buf2));
203
+ mu_assert_str_equals("1.000000e+200", buf2);
204
+
205
+ /* test very large negative number */
206
+ d = -1.0e200;
207
+ len = modp_dtoa(d, buf2, 6);
208
+ mu_assert_int_equals(len, strlen(buf2));
209
+ mu_assert_str_equals("-1.000000e+200", buf2);
210
+
211
+ /* test very small positive number */
212
+ d = 1e-10;
213
+ sprintf(buf1, "%.6f", d);
214
+ len = modp_dtoa(d, buf2, 6);
215
+ mu_assert_int_equals(len, strlen(buf1));
216
+ mu_assert_str_equals(buf1, buf2);
217
+
218
+ /* test very small negative number */
219
+ d = -1e-10;
220
+ sprintf(buf1, "%.6f", d);
221
+ len = modp_dtoa(d, buf2, 6);
222
+ mu_assert_int_equals(len, strlen(buf1));
223
+ mu_assert_str_equals(buf1, buf2);
224
+
225
+ return 0;
226
+ }
227
+
228
+ /* Helper function
229
+ * Removes trailing zeros
230
+ * this is horible but it's just for testing.
231
+ */
232
+ static void stripTrailingZeros(char* buf)
233
+ {
234
+ size_t i;
235
+ int hasdot = 0;
236
+ for (i = 0; i < strlen(buf); ++i) {
237
+ if (buf[i] == '.') {
238
+ hasdot = 1;
239
+ break;
240
+ }
241
+ }
242
+
243
+ /* it's just an integer */
244
+ if (!hasdot) {
245
+ return;
246
+ }
247
+
248
+ i = strlen(buf);
249
+ if (i == 0) {
250
+ return;
251
+ }
252
+ --i;
253
+
254
+ while (i > 0 && (buf[i] == '0' || buf[i] == '.')) {
255
+ if (buf[i] == '.') {
256
+ buf[i] = '\0';
257
+ break;
258
+ } else {
259
+ buf[i] = '\0';
260
+ --i;
261
+ }
262
+ }
263
+ }
264
+
265
+ static char* testDoubleToA2(void)
266
+ {
267
+ char buf1[100];
268
+ char buf2[100];
269
+ char msg[200];
270
+ double d;
271
+
272
+ char* tmp;
273
+ size_t len;
274
+ size_t tmplen;
275
+
276
+ /* test each combination of whole number + fraction,
277
+ at every precision */
278
+ /* and test negative version */
279
+ double wholes[] = { 0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0,
280
+ 67.0, 101.0, 10000, 99999 };
281
+ double frac[] = { 0.0, 0.1, 0.2, 0.3, 0.4, 0.49, 0.5, 0.51, 0.6, 0.7,
282
+ 0.9, 0.01, 0.25, 0.125, 0.03, 0.0625, 0.0078125,
283
+ 0.001, 0.00001, 0.99, 0.999, 0.9999, 0.99999, 0.999999,
284
+ 0.875, 0.9375, 0.96875, 0.9921875,
285
+ 0.09, 0.099, 0.0999, 0.09999, 0.099999, 0.0999999,
286
+ 0.09999999 };
287
+ const char* formats[] = { "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f",
288
+ "%.6f", "%.7f", "%.8f", "%.9f" };
289
+
290
+ int imax = sizeof(wholes) / sizeof(double);
291
+ int jmax = sizeof(frac) / sizeof(double);
292
+ int kmax = sizeof(formats) / sizeof(const char*);
293
+
294
+ int i, j, k;
295
+ for (i = 0; i < imax; ++i) {
296
+ for (j = 0; j < jmax; ++j) {
297
+ for (k = 0; k < kmax; ++k) {
298
+ d = wholes[i] + frac[j];
299
+
300
+ sprintf(msg, "whole=%f, frac=%f, prec=%d -- ",
301
+ wholes[i], frac[j], k);
302
+
303
+ sprintf(buf1, formats[k], d);
304
+ stripTrailingZeros(buf1);
305
+ len = modp_dtoa2(d, buf2, k);
306
+
307
+ if ((int)wholes[i] != 0 && (int)(frac[j] * 10000000) != 0) {
308
+
309
+ /* find the '.', and see how many chars are after it */
310
+ tmp = buf2;
311
+ while (*tmp != '.' && *tmp != '\0') {
312
+ ++tmp;
313
+ }
314
+ if (*tmp == '\0') {
315
+ tmplen = 0;
316
+ } else {
317
+ tmplen = strlen(++tmp);
318
+ }
319
+
320
+ sprintf(msg, "orig=%f whole=%f, frac=%f, prec=%d -- want %s",
321
+ wholes[i] + frac[j], wholes[i], frac[j], (int)k, buf1);
322
+ mu_assert_str_equals_msg(msg, buf1, buf2);
323
+ mu_assert_msg(msg, (size_t)k >= tmplen);
324
+
325
+ /* not dealing with "-0" issues */
326
+ d = -d;
327
+ sprintf(buf1, formats[k], d);
328
+ stripTrailingZeros(buf1);
329
+
330
+ len = modp_dtoa2(d, buf2, k);
331
+ mu_assert_int_equals(len, strlen(buf2));
332
+ mu_assert_str_equals_msg(msg, buf1, buf2);
333
+ }
334
+ }
335
+ }
336
+ }
337
+
338
+ /* test very large positive number */
339
+ d = 1.0e200;
340
+ len = modp_dtoa2(d, buf2, 6);
341
+ mu_assert_int_equals(len, strlen(buf2));
342
+ mu_assert_str_equals("1.000000e+200", buf2);
343
+
344
+ /* test very large negative number */
345
+ d = -1.0e200;
346
+ len = modp_dtoa2(d, buf2, 6);
347
+ mu_assert_int_equals(len, strlen(buf2));
348
+ mu_assert_str_equals("-1.000000e+200", buf2);
349
+
350
+ /* test very small positive number */
351
+ d = 1e-10;
352
+ sprintf(buf1, "%.6f", d);
353
+ stripTrailingZeros(buf1);
354
+
355
+ len = modp_dtoa2(d, buf2, 6);
356
+ mu_assert_int_equals(len, strlen(buf2));
357
+ mu_assert_str_equals(buf1, buf2);
358
+
359
+ /* test very small negative number */
360
+ d = -1e-10;
361
+ sprintf(buf1, "%.6f", d);
362
+ stripTrailingZeros(buf1);
363
+
364
+ len = modp_dtoa2(d, buf2, 6);
365
+ mu_assert_int_equals(len, strlen(buf2));
366
+ mu_assert_str_equals(buf1, buf2);
367
+
368
+ /* test bad precision values */
369
+ d = 1.1;
370
+ len = modp_dtoa(d, buf2, -1);
371
+ mu_assert_int_equals(len, strlen(buf2));
372
+ mu_assert_str_equals("1", buf2);
373
+ len = modp_dtoa2(d, buf2, 10);
374
+ mu_assert_int_equals(len, strlen(buf2));
375
+ mu_assert_str_equals("1.1", buf2);
376
+ return 0;
377
+ }
378
+
379
+ /* From Issue 7 -- http://code.google.com/p/stringencoders/issues/detail?id=7
380
+ * thanks to http://code.google.com/u/simhasana/
381
+ */
382
+ static char* testOverflowLITOA(void)
383
+ {
384
+ char buf1[100];
385
+ char buf2[100];
386
+
387
+ long long int longmin = LONG_MIN;
388
+ sprintf(buf1, "%lld", longmin);
389
+ modp_litoa10(longmin, buf2);
390
+ mu_assert_str_equals(buf1, buf2);
391
+
392
+ long long int longmax = LONG_MAX;
393
+ sprintf(buf1, "%lld", longmax);
394
+ modp_litoa10(longmax, buf2);
395
+ mu_assert_str_equals(buf1, buf2);
396
+
397
+ return 0;
398
+ }
399
+
400
+ static char* testOverflowITOA(void)
401
+ {
402
+ char buf1[100];
403
+ char buf2[100];
404
+
405
+ int32_t intmin = INT_MIN;
406
+ sprintf(buf1, "%d", intmin);
407
+ modp_itoa10(intmin, buf2);
408
+ mu_assert_str_equals(buf1, buf2);
409
+
410
+ int32_t intmax = INT_MAX;
411
+ sprintf(buf1, "%d", intmax);
412
+ modp_itoa10(intmax, buf2);
413
+ mu_assert_str_equals(buf1, buf2);
414
+
415
+ return 0;
416
+ }
417
+
418
+ /* Test NaN and Infinity behavior */
419
+ static char* testDTOANonFinite(void)
420
+ {
421
+ char buf2[100];
422
+ double d;
423
+
424
+ /* Test for inf */
425
+ d = 1e200 * 1e200;
426
+ /* NOTE!!! next line will core dump!
427
+ * sprintf(buf1, "%.6f", d);
428
+ */
429
+ buf2[0] = '\0';
430
+ modp_dtoa2(d, buf2, 6);
431
+ mu_assert_str_equals("inf", buf2);
432
+ return 0;
433
+ }
434
+
435
+ static char* testDTOAInfinity(void)
436
+ {
437
+
438
+ /* INFINITY should be standard. Defined in <math.h> */
439
+ /* http://www.gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html */
440
+ #ifdef INFINITY
441
+ char buf1[100];
442
+ char buf2[100];
443
+ double d = INFINITY;
444
+
445
+ /* test libc support */
446
+ sprintf(buf1, "%f", d);
447
+ mu_assert_str_equals("inf", buf1);
448
+
449
+ buf2[0] = '\0';
450
+ modp_dtoa(d, buf2, 6);
451
+ mu_assert_str_equals("inf", buf2);
452
+
453
+ buf2[0] = '\0';
454
+ modp_dtoa2(d, buf2, 6);
455
+ mu_assert_str_equals("inf", buf2);
456
+ #endif
457
+
458
+ return 0;
459
+ }
460
+
461
+ static char* testDTOAandNAN(void)
462
+ {
463
+ /* NAN is a GNU extension, defined in <math.h> */
464
+ /* http://www.gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html */
465
+
466
+ #ifdef NAN
467
+ char buf1[100];
468
+ char buf2[100];
469
+ double d;
470
+
471
+ d = NAN;
472
+
473
+ /* test libc support */
474
+ sprintf(buf1, "%f", d);
475
+ mu_assert_str_equals("nan", buf1);
476
+
477
+ /* now test ours */
478
+ buf2[0] = '\0';
479
+ modp_dtoa(d, buf2, 6);
480
+ mu_assert_str_equals("nan", buf2);
481
+ buf2[0] = '\0';
482
+ modp_dtoa2(d, buf2, 6);
483
+ mu_assert_str_equals("nan", buf2);
484
+ #endif
485
+
486
+ return 0;
487
+ }
488
+
489
+ static char* testUITOA16(void)
490
+ {
491
+ char buf1[100];
492
+ char buf2[100];
493
+
494
+ modp_uitoa16(1, buf1, 1);
495
+ mu_assert_str_equals(buf1, "00000001");
496
+
497
+ modp_uitoa16(0, buf1, 1);
498
+ mu_assert_str_equals(buf1, "00000000");
499
+
500
+ modp_uitoa16(0xFFFFFFFF, buf1, 1);
501
+ mu_assert_str_equals(buf1, "FFFFFFFF");
502
+
503
+ unsigned int i;
504
+ for (i = 1; i < 1000000; ++i) {
505
+ sprintf(buf1, "%08X", i);
506
+ modp_uitoa16(i, buf2, 1);
507
+ mu_assert_str_equals(buf1, buf2);
508
+ }
509
+ return 0;
510
+ }
511
+
512
+ /**
513
+ * Attempt to replicate issue
514
+ * http://code.google.com/p/stringencoders/issues/detail?id=15
515
+ */
516
+ static char* testRoundingPrecisionOverflow(void)
517
+ {
518
+ char buf1[100];
519
+
520
+ modp_dtoa(0.09999999, buf1, 6);
521
+ mu_assert_str_equals(buf1, "0.100000");
522
+ modp_dtoa2(0.09999999, buf1, 6);
523
+ mu_assert_str_equals(buf1, "0.1");
524
+ return 0;
525
+ }
526
+
527
+ static char* all_tests(void)
528
+ {
529
+ mu_run_test(testITOA);
530
+ mu_run_test(testUITOA);
531
+ mu_run_test(testLITOA);
532
+ mu_run_test(testULITOA);
533
+ mu_run_test(testDoubleToA);
534
+ mu_run_test(testDoubleToA2);
535
+ mu_run_test(testOverflowLITOA);
536
+ mu_run_test(testOverflowITOA);
537
+ mu_run_test(testDTOANonFinite);
538
+ mu_run_test(testDTOAInfinity);
539
+ mu_run_test(testDTOAandNAN);
540
+ mu_run_test(testUITOA16);
541
+ mu_run_test(testRoundingPrecisionOverflow);
542
+ return 0;
543
+ }
544
+
545
+ UNITTESTS