1- // Copyright 2020 Junekey Jeon
1+ // Copyright 2020-2024 Junekey Jeon
22//
33// The contents of this file may be used under the terms of
44// the Apache License v2.0 with LLVM Exceptions.
1616// KIND, either express or implied.
1717
1818#include " dragonbox/dragonbox_to_chars.h"
19+ #include " simple_dragonbox.h"
1920#include " random_float.h"
2021#include " ryu/ryu.h"
2122
2627static void reference_implementation (float x, char * buffer) { f2s_buffered (x, buffer); }
2728static void reference_implementation (double x, char * buffer) { d2s_buffered (x, buffer); }
2829
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) {
3232 char buffer1[64 ];
3333 char buffer2[64 ];
3434 auto rg = generate_correctly_seeded_mt19937_64 ();
@@ -37,7 +37,7 @@ static bool uniform_random_test(std::size_t number_of_tests, TypenameString&& ty
3737 auto x = uniformly_randomly_generate_general_float<Float>(rg);
3838
3939 // 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);
4141 reference_implementation (x, buffer2);
4242
4343 std::string_view view1 (buffer1);
@@ -51,48 +51,92 @@ static bool uniform_random_test(std::size_t number_of_tests, TypenameString&& ty
5151 }
5252
5353 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 " ;
5658 }
5759
5860 return success;
5961}
6062
6163int main () {
62- constexpr bool run_float = true ;
6364 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 ;
6469
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 ;
6970 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 ;
7375
7476 bool success = true ;
7577
7678 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+ });
7984 std::cout << " Done.\n\n\n " ;
8085 }
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+ });
85109 std::cout << " Done.\n\n\n " ;
86110 }
87111 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); });
90131 std::cout << " Done.\n\n\n " ;
91132 }
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+ });
96140 std::cout << " Done.\n\n\n " ;
97141 }
98142
0 commit comments