blob: a36cdd75ea8c6fb29517fe7af84fb898c0483e33 [file] [log] [blame]
Nigel Tao12103402020-09-07 23:11:07 +10001// Copyright 2020 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// ----------------
16
17// manual-test-parse-number-f64.c tests Wuffs' parse_number_f64 function. The
Nigel Tao93f95502020-10-23 18:46:12 +110018// https://github.com/nigeltao/parse-number-fxx-test-data repository contains
Nigel Tao12103402020-09-07 23:11:07 +100019// the data files, containing one test case per line, like:
20//
Nigel Tao93f95502020-10-23 18:46:12 +110021// 3C00 3F800000 3FF0000000000000 1
22// 3D00 3FA00000 3FF4000000000000 1.25
23// 3D9A 3FB33333 3FF6666666666666 1.4
24// 57B7 42F6E979 405EDD2F1A9FBE77 123.456
25// 622A 44454000 4088A80000000000 789
26// 7C00 7F800000 7FF0000000000000 123.456e789
Nigel Tao12103402020-09-07 23:11:07 +100027
28#include <inttypes.h>
29#include <stdio.h>
30#include <stdlib.h>
31
32// Wuffs ships as a "single file C library" or "header file library" as per
33// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
34//
35// To use that single file as a "foo.c"-like implementation, instead of a
36// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
37// compiling it.
38#define WUFFS_IMPLEMENTATION
39
40// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
Nigel Tao2f788042021-01-23 19:29:19 +110041// release/c/etc.c choose which parts of Wuffs to build. That file contains the
42// entire Wuffs standard library, implementing a variety of codecs and file
Nigel Tao12103402020-09-07 23:11:07 +100043// formats. Without this macro definition, an optimizing compiler or linker may
44// very well discard Wuffs code for unused codecs, but listing the Wuffs
45// modules we use makes that process explicit. Preprocessing means that such
46// code simply isn't compiled.
47#define WUFFS_CONFIG__MODULES
48#define WUFFS_CONFIG__MODULE__BASE
49
50// If building this program in an environment that doesn't easily accommodate
51// relative includes, you can use the script/inline-c-relative-includes.go
52// program to generate a stand-alone C++ file.
53#include "../release/c/wuffs-unsupported-snapshot.c"
54
Nigel Tao6775c052020-10-24 17:44:06 +110055// Uncomment one or all of these to use the
56// - github.com/lemire/fast_double_parser
57// - github.com/lemire/fast_float
58// libraries. These header-only libraries are C++, not C.
59//
Nigel Tao12103402020-09-07 23:11:07 +100060// #define USE_LEMIRE_FAST_DOUBLE_PARSER
Nigel Tao6775c052020-10-24 17:44:06 +110061// #define USE_LEMIRE_FAST_FLOAT
Nigel Tao12103402020-09-07 23:11:07 +100062
63#ifdef USE_LEMIRE_FAST_DOUBLE_PARSER
64#include "/the/path/to/fast_double_parser/include/fast_double_parser.h"
65#endif
Nigel Tao6775c052020-10-24 17:44:06 +110066#ifdef USE_LEMIRE_FAST_FLOAT
67#include "/the/path/to/fast_float/include/fast_float/fast_float.h"
68#endif
Nigel Tao12103402020-09-07 23:11:07 +100069
70const uint8_t hex[256] = {
71 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x00-0x07
72 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x08-0x0F
73 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x10-0x17
74 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x18-0x1F
75 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x20-0x27
76 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x28-0x2F
77 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, // 0x30-0x37 0-7
78 0x8, 0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x38-0x3F 8-9
79
80 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0x0, // 0x40-0x47 A-F
81 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x48-0x4F
82 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x50-0x57
83 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x58-0x5F
Nigel Tao93f95502020-10-23 18:46:12 +110084 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0x0, // 0x60-0x67 a-f
Nigel Tao12103402020-09-07 23:11:07 +100085 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x68-0x6F
86 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x70-0x77
87 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x78-0x7F
88
89 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x80-0x87
90 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x88-0x8F
91 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x90-0x97
92 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x98-0x9F
93 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xA0-0xA7
94 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xA8-0xAF
95 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xB0-0xB7
96 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xB8-0xBF
97
98 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xC0-0xC7
99 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xC8-0xCF
100 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xD0-0xD7
101 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xD8-0xDF
102 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xE0-0xE7
103 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xE8-0xEF
104 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xF0-0xF7
105 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0xF8-0xFF
106};
107
108#ifndef SRC_BUFFER_ARRAY_SIZE
109#define SRC_BUFFER_ARRAY_SIZE (64 * 1024 * 1024)
110#endif
111
112uint8_t g_src_buffer_array[SRC_BUFFER_ARRAY_SIZE];
113
114wuffs_base__io_buffer g_src;
115
116const char* g_filename;
117FILE* g_file;
118uint64_t g_line;
119
120const char* //
121read_src() {
122 if (g_src.meta.closed) {
123 return "main: internal error: read requested on a closed source";
124 }
125 wuffs_base__io_buffer__compact(&g_src);
126 if (g_src.meta.wi >= g_src.data.len) {
127 return "main: g_src buffer is full";
128 }
129 size_t n = fread(g_src.data.ptr + g_src.meta.wi, sizeof(uint8_t),
130 g_src.data.len - g_src.meta.wi, g_file);
131 g_src.meta.wi += n;
132 g_src.meta.closed = feof(g_file);
133 if ((n == 0) && !g_src.meta.closed) {
134 return "main: read error";
135 }
136 return NULL;
137}
138
139void //
140fail_parse(const char* impl, const char* z) {
141 fprintf(stderr, "main: %s could not parse \"%s\" at %s:%" PRIu64 "\n", impl,
142 z, g_filename, g_line);
143}
144
145void //
146fail(const char* impl, const char* z, uint64_t have, uint64_t want) {
147 fprintf(stderr, "main: %s mismatch at %s:%" PRIu64 "\n", impl, g_filename,
148 g_line);
149 fprintf(stderr, "src: %s\n", z);
Nigel Tao6775c052020-10-24 17:44:06 +1100150 fprintf(stderr, "have: 0x%016" PRIX64 " %f\n", have,
151 wuffs_base__ieee_754_bit_representation__from_u64_to_f64(have));
152 fprintf(stderr, "want: 0x%016" PRIX64 " %f\n", want,
153 wuffs_base__ieee_754_bit_representation__from_u64_to_f64(want));
Nigel Tao12103402020-09-07 23:11:07 +1000154}
155
156bool //
157process_line(wuffs_base__slice_u8 s) {
Nigel Tao93f95502020-10-23 18:46:12 +1100158 if (s.len < 32) {
Nigel Tao12103402020-09-07 23:11:07 +1000159 fprintf(stderr, "main: short input at %s:%" PRIu64 "\n", g_filename,
160 g_line);
161 return false;
162 } else if (s.len > 2048) {
163 fprintf(stderr, "main: long input at %s:%" PRIu64 "\n", g_filename, g_line);
164 return false;
165 }
166 uint64_t want = 0;
Nigel Tao93f95502020-10-23 18:46:12 +1100167 for (int i = 14; i < 30; i++) {
Nigel Tao12103402020-09-07 23:11:07 +1000168 want = (want << 4) | hex[s.ptr[i]];
169 }
Nigel Tao93f95502020-10-23 18:46:12 +1100170 s.ptr += 31;
171 s.len -= 31;
Nigel Tao12103402020-09-07 23:11:07 +1000172
173 // Convert ".123" to "0.123". Not all parsers like a leading dot.
174 if (s.ptr[0] == '.') {
175 s.ptr--;
176 s.len++;
177 s.ptr[0] = '0';
178 }
179
180 char z[2049];
181 memcpy(&z[0], s.ptr, s.len);
182 z[s.len] = 0;
183
184 // Check libc's strtod.
185 {
186 double have_f64 = strtod(&z[0], NULL);
187 uint64_t have =
188 wuffs_base__ieee_754_bit_representation__from_f64_to_u64(have_f64);
189 if (have != want) {
190 fail("strtod", &z[0], have, want);
191 return false;
192 }
193 }
194
195#ifdef USE_LEMIRE_FAST_DOUBLE_PARSER
196 // Check lemire/fast_double_parser's parse_number.
197 //
198 // https://github.com/lemire/fast_double_parser/blob/master/README.md says
199 // "the parser will reject overly large values that would not fit in
200 // binary64. It will not produce NaN or infinite values".
201 if (want < 0x7FF0000000000000ul) {
202 double have_f64;
203 if (!fast_double_parser::decimal_separator_dot::parse_number(&z[0],
204 &have_f64)) {
Nigel Tao6775c052020-10-24 17:44:06 +1100205 fail_parse("lemire/fast_double_parser", &z[0]);
Nigel Tao12103402020-09-07 23:11:07 +1000206 return false;
207 }
208 uint64_t have =
209 wuffs_base__ieee_754_bit_representation__from_f64_to_u64(have_f64);
210 if (have != want) {
Nigel Tao6775c052020-10-24 17:44:06 +1100211 fail("lemire/fast_double_parser", &z[0], have, want);
212 return false;
213 }
214 }
215#endif
216
217#ifdef USE_LEMIRE_FAST_FLOAT
218 // Check lemire/fast_float's from_chars.
219 {
220 double have_f64;
221 fast_float::from_chars_result result = fast_float::from_chars(
222 static_cast<char*>(static_cast<void*>(s.ptr)),
223 static_cast<char*>(static_cast<void*>(s.ptr + s.len)), have_f64,
224 fast_float::chars_format::general);
225 if (result.ec != std::errc()) {
226 fail_parse("lemire/fast_float", &z[0]);
227 return false;
228 }
229 uint64_t have =
230 wuffs_base__ieee_754_bit_representation__from_f64_to_u64(have_f64);
231 if (have != want) {
232 fail("lemire/fast_float", &z[0], have, want);
Nigel Tao12103402020-09-07 23:11:07 +1000233 return false;
234 }
235 }
236#endif
237
238 // Check Wuffs' wuffs_base__parse_number_f64.
239 {
240 wuffs_base__result_f64 res = wuffs_base__parse_number_f64(
241 s, WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS);
242 if (res.status.repr) {
Nigel Tao6775c052020-10-24 17:44:06 +1100243 fail_parse("wuffs", &z[0]);
Nigel Tao12103402020-09-07 23:11:07 +1000244 return false;
245 }
246 uint64_t have =
247 wuffs_base__ieee_754_bit_representation__from_f64_to_u64(res.value);
248 if (have != want) {
249 fail("wuffs", &z[0], have, want);
250 return false;
251 }
252 }
253
254 return true;
255}
256
257bool //
258process_file(char* filename) {
259 if (g_file) {
260 fclose(g_file);
261 g_file = NULL;
262 }
263 g_filename = filename;
Nigel Tao816475b2021-07-07 20:28:35 +1000264 g_file = fopen(g_filename, "rb");
Nigel Tao12103402020-09-07 23:11:07 +1000265 if (!g_file) {
266 fprintf(stderr, "main: could not open %s\n", g_filename);
267 return false;
268 }
269 g_line = 0;
270 g_src = wuffs_base__slice_u8__writer(
271 wuffs_base__make_slice_u8(&g_src_buffer_array[0], SRC_BUFFER_ARRAY_SIZE));
272
273 while (true) {
274 for (size_t i = g_src.meta.ri; i < g_src.meta.wi; i++) {
275 if (g_src.data.ptr[i] == '\n') {
276 g_line++;
277 if (!process_line(wuffs_base__make_slice_u8(
278 &g_src.data.ptr[g_src.meta.ri], i - g_src.meta.ri))) {
279 return false;
280 }
281 g_src.meta.ri = i + 1;
282 continue;
283 }
284 }
285
286 if (g_src.meta.closed) {
287 if (g_src.meta.ri != g_src.meta.wi) {
288 fprintf(stderr, "main: unexpected end-of-file\n");
289 return false;
290 }
291 break;
292 }
293
294 const char* error_msg = read_src();
295 if (error_msg) {
296 fprintf(stderr, "%s\n", error_msg);
297 return false;
298 }
299 }
300
301 printf("%8" PRIu64 " OK in %s\n", g_line, g_filename);
302 return true;
303}
304
305int //
306main(int argc, char** argv) {
307 g_file = NULL;
308 for (int argi = 1; argi < argc; argi++) {
309 if (!process_file(argv[argi])) {
310 return 1;
311 }
312 }
313 return 0;
314}