1
- // Copyright 2020 Junekey Jeon
1
+ // Copyright 2020-2024 Junekey Jeon
2
2
//
3
3
// The contents of this file may be used under the terms of
4
4
// the Apache License v2.0 with LLVM Exceptions.
16
16
// KIND, either express or implied.
17
17
18
18
#include " dragonbox/dragonbox_to_chars.h"
19
+ #include " simple_dragonbox.h"
19
20
#include " random_float.h"
20
21
#include " ryu/ryu.h"
21
22
26
27
static void reference_implementation (float x, char * buffer) { f2s_buffered (x, buffer); }
27
28
static void reference_implementation (double x, char * buffer) { d2s_buffered (x, buffer); }
28
29
29
- template <class Float , class TypenameString , class ... Args>
30
- static bool uniform_random_test (std::size_t number_of_tests, TypenameString&& type_name_string,
31
- Args&&... args) {
30
+ template <class Float , class TestTarget >
31
+ static bool uniform_random_test (std::size_t number_of_tests, TestTarget&& test_target) {
32
32
char buffer1[64 ];
33
33
char buffer2[64 ];
34
34
auto rg = generate_correctly_seeded_mt19937_64 ();
@@ -37,7 +37,7 @@ static bool uniform_random_test(std::size_t number_of_tests, TypenameString&& ty
37
37
auto x = uniformly_randomly_generate_general_float<Float>(rg);
38
38
39
39
// Check if the output is identical to the reference implementation (Ryu).
40
- jkj::dragonbox::to_chars (x, buffer1, std::forward<Args>(args)... );
40
+ test_target (x, buffer1);
41
41
reference_implementation (x, buffer2);
42
42
43
43
std::string_view view1 (buffer1);
@@ -51,48 +51,92 @@ static bool uniform_random_test(std::size_t number_of_tests, TypenameString&& ty
51
51
}
52
52
53
53
if (success) {
54
- std::cout << " Uniform random test for " << type_name_string << " with " << number_of_tests
55
- << " examples succeeded.\n " ;
54
+ std::cout << " Uniform random test with " << number_of_tests << " examples succeeded.\n " ;
55
+ }
56
+ else {
57
+ std::cout << " Error detected.\n " ;
56
58
}
57
59
58
60
return success;
59
61
}
60
62
61
63
int main () {
62
- constexpr bool run_float = true ;
63
64
constexpr std::size_t number_of_uniform_random_tests_float = 10000000 ;
65
+ constexpr bool run_float = true ;
66
+ constexpr bool run_float_with_compact_cache = true ;
67
+ constexpr bool run_simple_float = true ;
68
+ constexpr bool run_simpl_float_with_compact_cache = true ;
64
69
65
- constexpr bool run_float_with_compressed_cache = true ;
66
- constexpr std::size_t number_of_uniform_random_tests_float_compressed = 10000000 ;
67
-
68
- constexpr bool run_double = true ;
69
70
constexpr std::size_t number_of_uniform_random_tests_double = 10000000 ;
70
-
71
- constexpr bool run_double_with_compressed_cache = true ;
72
- constexpr std::size_t number_of_uniform_random_tests_double_compressed = 10000000 ;
71
+ constexpr bool run_double = true ;
72
+ constexpr bool run_double_with_compact_cache = true ;
73
+ constexpr bool run_simple_double = true ;
74
+ constexpr bool run_simple_double_with_compact_cache = true ;
73
75
74
76
bool success = true ;
75
77
76
78
if (run_float) {
77
- std::cout << " [Testing uniformly randomly generated float inputs...]\n " ;
78
- success &= uniform_random_test<float >(number_of_uniform_random_tests_float, " float" );
79
+ std::cout << " [Testing uniformly randomly generated binary32 inputs...]\n " ;
80
+ success &=
81
+ uniform_random_test<float >(number_of_uniform_random_tests_float, [](auto x, char * buffer) {
82
+ jkj::dragonbox::to_chars (x, buffer);
83
+ });
79
84
std::cout << " Done.\n\n\n " ;
80
85
}
81
- if (run_float_with_compressed_cache) {
82
- std::cout << " [Testing uniformly randomly generated float inputs with compressed cache...]\n " ;
83
- success &= uniform_random_test<float >(number_of_uniform_random_tests_float_compressed, " float" ,
84
- jkj::dragonbox::policy::cache::compact);
86
+ if (run_float_with_compact_cache) {
87
+ std::cout << " [Testing uniformly randomly generated binary32 inputs (compact cache)...]\n " ;
88
+ success &=
89
+ uniform_random_test<float >(number_of_uniform_random_tests_float, [](auto x, char * buffer) {
90
+ jkj::dragonbox::to_chars (x, buffer, jkj::dragonbox::policy::cache::compact);
91
+ });
92
+ std::cout << " Done.\n\n\n " ;
93
+ }
94
+ if (run_simple_float) {
95
+ std::cout << " [Testing uniformly randomly generated binary32 inputs (simplified impl)...]\n " ;
96
+ success &=
97
+ uniform_random_test<float >(number_of_uniform_random_tests_float, [](auto x, char * buffer) {
98
+ jkj::simple_dragonbox::to_chars (x, buffer);
99
+ });
100
+ std::cout << " Done.\n\n\n " ;
101
+ }
102
+ if (run_simpl_float_with_compact_cache) {
103
+ std::cout << " [Testing uniformly randomly generated binary32 inputs (simplified impl, compact "
104
+ " cache)...]\n " ;
105
+ success &= uniform_random_test<float >(number_of_uniform_random_tests_float, [](auto x,
106
+ char * buffer) {
107
+ jkj::simple_dragonbox::to_chars (x, buffer, jkj::simple_dragonbox::policy::cache::compact);
108
+ });
85
109
std::cout << " Done.\n\n\n " ;
86
110
}
87
111
if (run_double) {
88
- std::cout << " [Testing uniformly randomly generated double inputs...]\n " ;
89
- success &= uniform_random_test<double >(number_of_uniform_random_tests_double, " double" );
112
+ std::cout << " [Testing uniformly randomly generated binary64 inputs...]\n " ;
113
+ success &= uniform_random_test<double >(
114
+ number_of_uniform_random_tests_double,
115
+ [](auto x, char * buffer) { jkj::dragonbox::to_chars (x, buffer); });
116
+ std::cout << " Done.\n\n\n " ;
117
+ }
118
+ if (run_double_with_compact_cache) {
119
+ std::cout << " [Testing uniformly randomly generated binary64 inputs (compact cache)...]\n " ;
120
+ success &= uniform_random_test<double >(
121
+ number_of_uniform_random_tests_double, [](auto x, char * buffer) {
122
+ jkj::dragonbox::to_chars (x, buffer, jkj::dragonbox::policy::cache::compact);
123
+ });
124
+ std::cout << " Done.\n\n\n " ;
125
+ }
126
+ if (run_simple_double) {
127
+ std::cout << " [Testing uniformly randomly generated binary64 inputs (simplified impl)...]\n " ;
128
+ success &= uniform_random_test<double >(
129
+ number_of_uniform_random_tests_double,
130
+ [](auto x, char * buffer) { jkj::simple_dragonbox::to_chars (x, buffer); });
90
131
std::cout << " Done.\n\n\n " ;
91
132
}
92
- if (run_double_with_compressed_cache) {
93
- std::cout << " [Testing uniformly randomly generated double inputs with compressed cache...]\n " ;
94
- success &= uniform_random_test<double >(number_of_uniform_random_tests_double_compressed,
95
- " double" , jkj::dragonbox::policy::cache::compact);
133
+ if (run_simple_double_with_compact_cache) {
134
+ std::cout << " [Testing uniformly randomly generated binary64 inputs with (simplified impl, "
135
+ " compact cache)...]\n " ;
136
+ success &= uniform_random_test<double >(number_of_uniform_random_tests_double, [](auto x,
137
+ char * buffer) {
138
+ jkj::simple_dragonbox::to_chars (x, buffer, jkj::simple_dragonbox::policy::cache::compact);
139
+ });
96
140
std::cout << " Done.\n\n\n " ;
97
141
}
98
142
0 commit comments