Skip to content

Commit ece4e5f

Browse files
committed
Move simple_dragonbox.h into include directory and merge its tests into the tests of the main implementation
1 parent 90568ea commit ece4e5f

7 files changed

+129
-146
lines changed

CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,4 @@ if (DRAGONBOX_ENABLE_SUBPROJECT)
106106
add_subdirectory("subproject/benchmark")
107107
add_subdirectory("subproject/meta")
108108
add_subdirectory("subproject/test")
109-
add_subdirectory("subproject/simple")
110109
endif()

subproject/simple/CMakeLists.txt

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
add_library(simple_dragonbox INTERFACE simple_dragonbox.h)
2-
target_compile_features(simple_dragonbox INTERFACE cxx_std_17)
3-
4-
add_executable(simple_dragonbox_test simple_dragonbox_test.cpp)
5-
target_link_libraries(simple_dragonbox_test PRIVATE
6-
simple_dragonbox
7-
dragonbox::common
8-
ryu::ryu
9-
)
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
2+
3+
project(dragonbox_simple LANGUAGES CXX)
4+
5+
add_library(dragonbox_simple INTERFACE include/simple_dragonbox.h)
6+
add_library(dragonbox::simple ALIAS dragonbox_simple)
7+
8+
target_include_directories(dragonbox_simple
9+
INTERFACE
10+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
11+
12+
target_compile_features(dragonbox_simple INTERFACE cxx_std_17)

subproject/simple/simple_dragonbox_test.cpp

-98
This file was deleted.

subproject/test/CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ if (NOT TARGET common)
1616
FetchContent_Declare(common SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../common")
1717
FetchContent_MakeAvailable(common)
1818
endif()
19+
if (NOT TARGET simple)
20+
FetchContent_Declare(simple SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../simple")
21+
FetchContent_MakeAvailable(simple)
22+
endif()
1923
if (NOT TARGET ryu)
2024
FetchContent_Declare(ryu SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../3rdparty/ryu")
2125
FetchContent_MakeAvailable(ryu)
@@ -35,7 +39,7 @@ function(add_test NAME)
3539

3640
add_executable(${NAME} source/${NAME}.cpp)
3741

38-
target_link_libraries(${NAME} PRIVATE ${dragonbox} dragonbox::common)
42+
target_link_libraries(${NAME} PRIVATE ${dragonbox} dragonbox::common dragonbox::simple)
3943

4044
if(TEST_RYU)
4145
target_link_libraries(${NAME} PRIVATE ryu::ryu)

subproject/test/source/test_all_shorter_interval_cases.cpp

+41-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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.
@@ -16,6 +16,7 @@
1616
// KIND, either express or implied.
1717

1818
#include "dragonbox/dragonbox_to_chars.h"
19+
#include "simple_dragonbox.h"
1920
#include "ryu/ryu.h"
2021

2122
#include <iostream>
@@ -26,8 +27,8 @@
2627
static void reference_implementation(float x, char* buffer) { f2s_buffered(x, buffer); }
2728
static void reference_implementation(double x, char* buffer) { d2s_buffered(x, buffer); }
2829

29-
template <class Float, class... Args>
30-
static bool test_all_shorter_interval_cases_impl(Args&&... args) {
30+
template <class Float, class TestTarget>
31+
static bool test_all_shorter_interval_cases_impl(TestTarget&& test_target) {
3132
using conversion_traits = jkj::dragonbox::default_float_bit_carrier_conversion_traits<Float>;
3233
using ieee754_format_info = typename conversion_traits::format;
3334
using carrier_uint = typename conversion_traits::carrier_uint;
@@ -42,7 +43,7 @@ static bool test_all_shorter_interval_cases_impl(Args&&... args) {
4243
<< ieee754_format_info::significand_bits;
4344
auto x = conversion_traits::carrier_to_float(br);
4445

45-
jkj::dragonbox::to_chars(x, buffer1, std::forward<Args>(args)...);
46+
test_target(x, buffer1);
4647
reference_implementation(x, buffer2);
4748

4849
std::string_view view1(buffer1);
@@ -68,19 +69,49 @@ int main() {
6869
bool success = true;
6970

7071
std::cout << "[Testing all shorter interval cases for binary32...]\n";
71-
success &= test_all_shorter_interval_cases_impl<float>();
72+
success &= test_all_shorter_interval_cases_impl<float>(
73+
[](auto x, char* buffer) { jkj::dragonbox::to_chars(x, buffer); });
7274
std::cout << "Done.\n\n\n";
7375

74-
std::cout << "[Testing all shorter interval cases for binary32 with compressed cache...]\n";
75-
success &= test_all_shorter_interval_cases_impl<float>(jkj::dragonbox::policy::cache::compact);
76+
std::cout << "[Testing all shorter interval cases for binary32 (compact cache)...]\n";
77+
success &= test_all_shorter_interval_cases_impl<float>([](auto x, char* buffer) {
78+
jkj::dragonbox::to_chars(x, buffer, jkj::dragonbox::policy::cache::compact);
79+
});
80+
std::cout << "Done.\n\n\n";
81+
82+
std::cout << "[Testing all shorter interval cases for binary32 (simplified impl)...]\n";
83+
success &= test_all_shorter_interval_cases_impl<float>(
84+
[](auto x, char* buffer) { jkj::simple_dragonbox::to_chars(x, buffer); });
85+
std::cout << "Done.\n\n\n";
86+
87+
std::cout
88+
<< "[Testing all shorter interval cases for binary32 (simplified impl, compact cache)...]\n";
89+
success &= test_all_shorter_interval_cases_impl<float>([](auto x, char* buffer) {
90+
jkj::simple_dragonbox::to_chars(x, buffer, jkj::simple_dragonbox::policy::cache::compact);
91+
});
7692
std::cout << "Done.\n\n\n";
7793

7894
std::cout << "[Testing all shorter interval cases for binary64...]\n";
79-
success &= test_all_shorter_interval_cases_impl<double>();
95+
success &= test_all_shorter_interval_cases_impl<double>(
96+
[](auto x, char* buffer) { jkj::dragonbox::to_chars(x, buffer); });
97+
std::cout << "Done.\n\n\n";
98+
99+
std::cout << "[Testing all shorter interval cases for binary64 (compact cache)...]\n";
100+
success &= test_all_shorter_interval_cases_impl<double>([](auto x, char* buffer) {
101+
jkj::dragonbox::to_chars(x, buffer, jkj::dragonbox::policy::cache::compact);
102+
});
103+
std::cout << "Done.\n\n\n";
104+
105+
std::cout << "[Testing all shorter interval cases for binary64 (simplified impl)...]\n";
106+
success &= test_all_shorter_interval_cases_impl<double>(
107+
[](auto x, char* buffer) { jkj::simple_dragonbox::to_chars(x, buffer); });
80108
std::cout << "Done.\n\n\n";
81109

82-
std::cout << "[Testing all shorter interval cases for binary64 with compressed cache...]\n";
83-
success &= test_all_shorter_interval_cases_impl<double>(jkj::dragonbox::policy::cache::compact);
110+
std::cout
111+
<< "[Testing all shorter interval cases for binary64 (simplified impl, compact cache)...]\n";
112+
success &= test_all_shorter_interval_cases_impl<double>([](auto x, char* buffer) {
113+
jkj::simple_dragonbox::to_chars(x, buffer, jkj::simple_dragonbox::policy::cache::compact);
114+
});
84115
std::cout << "Done.\n\n\n";
85116

86117
if (!success) {

subproject/test/source/uniform_random_test.cpp

+71-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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.
@@ -16,6 +16,7 @@
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

@@ -26,9 +27,8 @@
2627
static void reference_implementation(float x, char* buffer) { f2s_buffered(x, buffer); }
2728
static 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

6163
int 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

Comments
 (0)