|
1 | 1 | #include <fmt/format.h> |
2 | 2 |
|
3 | | -#include <array> |
4 | | -#include <bit> |
5 | 3 | #include <cctype> |
6 | | -#include <cmath> |
7 | | -#include <cstring> |
8 | | -#include <iostream> |
9 | 4 | #include <string> |
10 | | -#include <string_view> |
11 | | -#include <charconv> |
12 | 5 | #include <fstream> |
13 | 6 | #include <vector> |
14 | 7 |
|
|
17 | 10 | #include "floatutils.h" |
18 | 11 | #include "benchutil.h" |
19 | 12 |
|
20 | | -struct test_case { |
21 | | - double value; |
22 | | - std::string str_value; |
23 | | -}; |
24 | | - |
25 | 13 | // Helper function to load doubles from a file |
26 | | -std::vector<test_case> load_doubles_from_file(const std::string& filename) { |
27 | | - std::vector<test_case> numbers; |
| 14 | +std::vector<TestCase<double>> load_doubles_from_file(const std::string& filename) { |
| 15 | + std::vector<TestCase<double>> numbers; |
28 | 16 | std::ifstream file(filename); |
29 | 17 |
|
30 | 18 | if (!file.is_open()) { |
31 | | - fmt::print("Error: Could not open file {}\n", filename); |
| 19 | + fmt::println("Error: Could not open file {}", filename); |
32 | 20 | return numbers; |
33 | 21 | } |
34 | 22 |
|
35 | 23 | for (std::string line; std::getline(file, line);) { |
36 | 24 | if (auto num = parse_float<double>(line)) |
37 | | - numbers.emplace_back(*num,line); |
| 25 | + numbers.emplace_back(*num, line); |
38 | 26 | else |
39 | | - fmt::print("Warning: Could not parse '{}' as double, skipping\n", line); |
| 27 | + fmt::println("Warning: Could not parse '{}' as double, skipping", line); |
40 | 28 | } |
41 | 29 |
|
42 | 30 | file.close(); |
43 | 31 | return numbers; |
44 | 32 | } |
45 | 33 |
|
46 | 34 | void run_file_test(const std::string& filename, bool errol, const std::vector<std::string>& algo_filter = {}) { |
47 | | - fmt::println("{:20} {:20}", "Algorithm", "Valid shortest serialization"); |
48 | | - |
49 | | - std::array<Benchmarks::BenchArgs<double>, Benchmarks::COUNT> args; |
50 | | - args = Benchmarks::initArgs<double>(errol); |
51 | | - |
52 | | - // Load the doubles from file |
53 | | - auto test_values = load_doubles_from_file(filename); |
| 35 | + const auto test_values = load_doubles_from_file(filename); |
54 | 36 | if (test_values.empty()) { |
55 | | - fmt::print("No valid numbers to test\n"); |
| 37 | + fmt::println("No valid numbers to test"); |
56 | 38 | return; |
57 | 39 | } |
58 | 40 |
|
59 | | - for (const auto &algo : args) { |
60 | | - if (!algo.used) { |
61 | | - fmt::println("# skipping {}", algo.name); |
62 | | - continue; |
63 | | - } |
64 | | - if (algo.func == Benchmarks::dragonbox<double>) { |
65 | | - fmt::println("# skipping {} because it is the reference.", algo.name); |
66 | | - continue; |
67 | | - } |
68 | | - if (algo_filtered_out(algo.name, algo_filter)) { |
69 | | - fmt::println("# filtered out {}", algo.name); |
70 | | - continue; |
71 | | - } |
72 | | - |
73 | | - bool incorrect = false; |
74 | | - char buf1[100], buf2[100]; |
75 | | - std::span<char> bufRef(buf1, sizeof(buf1)), bufAlgo(buf2, sizeof(buf2)); |
76 | | - fmt::print("# processing {}", algo.name); |
77 | | - fflush(stdout); |
78 | | - |
79 | | - size_t total = test_values.size(); |
80 | | - for (size_t i = 0; i < total; ++i) { |
81 | | - if (i % (total/10) == 0 && total > 10) { |
82 | | - printf("."); |
83 | | - fflush(stdout); |
84 | | - } |
85 | | - double d = test_values[i].value; |
86 | | - const std::string& str_value = test_values[i].str_value; |
87 | | - if (std::isnan(d) || std::isinf(d)) |
88 | | - continue; |
89 | | - |
90 | | - const size_t vRef = Benchmarks::dragonbox(d, bufRef); |
91 | | - const size_t vAlgo = algo.func(d, bufAlgo); |
92 | | - |
93 | | - std::string_view svRef{bufRef.data(), vRef}; |
94 | | - std::string_view svAlgo{bufAlgo.data(), vAlgo}; |
95 | | - //fmt::print(" RESULT {}: {} ", algo.name, svAlgo); |
96 | | - |
97 | | - auto countRef = count_significant_digits(svRef); |
98 | | - auto countAlgo = count_significant_digits(svAlgo); |
99 | | - auto backRef = parse_float<double>(svRef); |
100 | | - auto backAlgo = parse_float<double>(svAlgo); |
101 | | - |
102 | | - if(!backRef || !backAlgo) { |
103 | | - incorrect = true; |
104 | | - fmt::print(" parse error: case: {}; d = {}, bufRef = {}, bufAlgo = {}", |
105 | | - str_value, float_to_hex<double>(d), svRef, svAlgo); |
106 | | - fflush(stdout); |
107 | | - break; |
108 | | - } |
109 | | - if(*backRef != d || *backAlgo != d) { |
110 | | - fmt::println("\n# Error: parsing the output with std::from_chars does not bring back the input."); |
111 | | - } |
112 | | - if(*backRef != d) { |
113 | | - incorrect = true; |
114 | | - fmt::print(" ref mismatch:case: {}; d = {}, backRef = {}; svRef = {}, svAlgo = {}", |
115 | | - str_value, float_to_hex<double>(d), *backRef, svRef, svAlgo); |
116 | | - fflush(stdout); |
117 | | - break; |
118 | | - } |
119 | | - if(*backAlgo != d) { |
120 | | - incorrect = true; |
121 | | - fmt::print(" algo mismatch: case: {}; d = {}, backAlgo = {}; svRef = {}, svAlgo = {}, " |
122 | | - "parsing the output with std::from_chars does not recover the original", |
123 | | - str_value, float_to_hex<double>(d), *backAlgo, svRef, svAlgo); |
124 | | - fflush(stdout); |
125 | | - break; |
126 | | - } |
127 | | - if (countRef != countAlgo) { |
128 | | - incorrect = true; |
129 | | - fmt::print(" mismatch: case: {}; d = {}, bufRef = {}, bufAlgo = {}", |
130 | | - str_value, float_to_hex<double>(d), svRef, svAlgo); |
131 | | - fflush(stdout); |
132 | | - break; |
133 | | - } |
134 | | - } |
135 | | - fmt::print("\n"); |
136 | | - fmt::println("{:20} {:20}", algo.name, incorrect == 0 ? "yes" : "no"); |
137 | | - } |
| 41 | + evaluate_properties_helper<double>(errol, algo_filter, test_values); |
138 | 42 | } |
139 | 43 |
|
140 | 44 | cxxopts::Options |
|
0 commit comments