Skip to content

Commit

Permalink
Move simple_dragonbox.h into include directory and merge its tests in…
Browse files Browse the repository at this point in the history
…to the tests of the main implementation
  • Loading branch information
jk-jeon committed Aug 28, 2024
1 parent 90568ea commit ece4e5f
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 146 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,4 @@ if (DRAGONBOX_ENABLE_SUBPROJECT)
add_subdirectory("subproject/benchmark")
add_subdirectory("subproject/meta")
add_subdirectory("subproject/test")
add_subdirectory("subproject/simple")
endif()
21 changes: 12 additions & 9 deletions subproject/simple/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
add_library(simple_dragonbox INTERFACE simple_dragonbox.h)
target_compile_features(simple_dragonbox INTERFACE cxx_std_17)

add_executable(simple_dragonbox_test simple_dragonbox_test.cpp)
target_link_libraries(simple_dragonbox_test PRIVATE
simple_dragonbox
dragonbox::common
ryu::ryu
)
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(dragonbox_simple LANGUAGES CXX)

add_library(dragonbox_simple INTERFACE include/simple_dragonbox.h)
add_library(dragonbox::simple ALIAS dragonbox_simple)

target_include_directories(dragonbox_simple
INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)

target_compile_features(dragonbox_simple INTERFACE cxx_std_17)
File renamed without changes.
98 changes: 0 additions & 98 deletions subproject/simple/simple_dragonbox_test.cpp

This file was deleted.

6 changes: 5 additions & 1 deletion subproject/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ if (NOT TARGET common)
FetchContent_Declare(common SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../common")
FetchContent_MakeAvailable(common)
endif()
if (NOT TARGET simple)
FetchContent_Declare(simple SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../simple")
FetchContent_MakeAvailable(simple)
endif()
if (NOT TARGET ryu)
FetchContent_Declare(ryu SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../3rdparty/ryu")
FetchContent_MakeAvailable(ryu)
Expand All @@ -35,7 +39,7 @@ function(add_test NAME)

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

target_link_libraries(${NAME} PRIVATE ${dragonbox} dragonbox::common)
target_link_libraries(${NAME} PRIVATE ${dragonbox} dragonbox::common dragonbox::simple)

if(TEST_RYU)
target_link_libraries(${NAME} PRIVATE ryu::ryu)
Expand Down
51 changes: 41 additions & 10 deletions subproject/test/source/test_all_shorter_interval_cases.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Junekey Jeon
// Copyright 2020-2024 Junekey Jeon
//
// The contents of this file may be used under the terms of
// the Apache License v2.0 with LLVM Exceptions.
Expand All @@ -16,6 +16,7 @@
// KIND, either express or implied.

#include "dragonbox/dragonbox_to_chars.h"
#include "simple_dragonbox.h"
#include "ryu/ryu.h"

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

template <class Float, class... Args>
static bool test_all_shorter_interval_cases_impl(Args&&... args) {
template <class Float, class TestTarget>
static bool test_all_shorter_interval_cases_impl(TestTarget&& test_target) {
using conversion_traits = jkj::dragonbox::default_float_bit_carrier_conversion_traits<Float>;
using ieee754_format_info = typename conversion_traits::format;
using carrier_uint = typename conversion_traits::carrier_uint;
Expand All @@ -42,7 +43,7 @@ static bool test_all_shorter_interval_cases_impl(Args&&... args) {
<< ieee754_format_info::significand_bits;
auto x = conversion_traits::carrier_to_float(br);

jkj::dragonbox::to_chars(x, buffer1, std::forward<Args>(args)...);
test_target(x, buffer1);
reference_implementation(x, buffer2);

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

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

std::cout << "[Testing all shorter interval cases for binary32 with compressed cache...]\n";
success &= test_all_shorter_interval_cases_impl<float>(jkj::dragonbox::policy::cache::compact);
std::cout << "[Testing all shorter interval cases for binary32 (compact cache)...]\n";
success &= test_all_shorter_interval_cases_impl<float>([](auto x, char* buffer) {
jkj::dragonbox::to_chars(x, buffer, jkj::dragonbox::policy::cache::compact);
});
std::cout << "Done.\n\n\n";

std::cout << "[Testing all shorter interval cases for binary32 (simplified impl)...]\n";
success &= test_all_shorter_interval_cases_impl<float>(
[](auto x, char* buffer) { jkj::simple_dragonbox::to_chars(x, buffer); });
std::cout << "Done.\n\n\n";

std::cout
<< "[Testing all shorter interval cases for binary32 (simplified impl, compact cache)...]\n";
success &= test_all_shorter_interval_cases_impl<float>([](auto x, char* buffer) {
jkj::simple_dragonbox::to_chars(x, buffer, jkj::simple_dragonbox::policy::cache::compact);
});
std::cout << "Done.\n\n\n";

std::cout << "[Testing all shorter interval cases for binary64...]\n";
success &= test_all_shorter_interval_cases_impl<double>();
success &= test_all_shorter_interval_cases_impl<double>(
[](auto x, char* buffer) { jkj::dragonbox::to_chars(x, buffer); });
std::cout << "Done.\n\n\n";

std::cout << "[Testing all shorter interval cases for binary64 (compact cache)...]\n";
success &= test_all_shorter_interval_cases_impl<double>([](auto x, char* buffer) {
jkj::dragonbox::to_chars(x, buffer, jkj::dragonbox::policy::cache::compact);
});
std::cout << "Done.\n\n\n";

std::cout << "[Testing all shorter interval cases for binary64 (simplified impl)...]\n";
success &= test_all_shorter_interval_cases_impl<double>(
[](auto x, char* buffer) { jkj::simple_dragonbox::to_chars(x, buffer); });
std::cout << "Done.\n\n\n";

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

if (!success) {
Expand Down
98 changes: 71 additions & 27 deletions subproject/test/source/uniform_random_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Junekey Jeon
// Copyright 2020-2024 Junekey Jeon
//
// The contents of this file may be used under the terms of
// the Apache License v2.0 with LLVM Exceptions.
Expand All @@ -16,6 +16,7 @@
// KIND, either express or implied.

#include "dragonbox/dragonbox_to_chars.h"
#include "simple_dragonbox.h"
#include "random_float.h"
#include "ryu/ryu.h"

Expand All @@ -26,9 +27,8 @@
static void reference_implementation(float x, char* buffer) { f2s_buffered(x, buffer); }
static void reference_implementation(double x, char* buffer) { d2s_buffered(x, buffer); }

template <class Float, class TypenameString, class... Args>
static bool uniform_random_test(std::size_t number_of_tests, TypenameString&& type_name_string,
Args&&... args) {
template <class Float, class TestTarget>
static bool uniform_random_test(std::size_t number_of_tests, TestTarget&& test_target) {
char buffer1[64];
char buffer2[64];
auto rg = generate_correctly_seeded_mt19937_64();
Expand All @@ -37,7 +37,7 @@ static bool uniform_random_test(std::size_t number_of_tests, TypenameString&& ty
auto x = uniformly_randomly_generate_general_float<Float>(rg);

// Check if the output is identical to the reference implementation (Ryu).
jkj::dragonbox::to_chars(x, buffer1, std::forward<Args>(args)...);
test_target(x, buffer1);
reference_implementation(x, buffer2);

std::string_view view1(buffer1);
Expand All @@ -51,48 +51,92 @@ static bool uniform_random_test(std::size_t number_of_tests, TypenameString&& ty
}

if (success) {
std::cout << "Uniform random test for " << type_name_string << " with " << number_of_tests
<< " examples succeeded.\n";
std::cout << "Uniform random test with " << number_of_tests << " examples succeeded.\n";
}
else {
std::cout << "Error detected.\n";
}

return success;
}

int main() {
constexpr bool run_float = true;
constexpr std::size_t number_of_uniform_random_tests_float = 10000000;
constexpr bool run_float = true;
constexpr bool run_float_with_compact_cache = true;
constexpr bool run_simple_float = true;
constexpr bool run_simpl_float_with_compact_cache = true;

constexpr bool run_float_with_compressed_cache = true;
constexpr std::size_t number_of_uniform_random_tests_float_compressed = 10000000;

constexpr bool run_double = true;
constexpr std::size_t number_of_uniform_random_tests_double = 10000000;

constexpr bool run_double_with_compressed_cache = true;
constexpr std::size_t number_of_uniform_random_tests_double_compressed = 10000000;
constexpr bool run_double = true;
constexpr bool run_double_with_compact_cache = true;
constexpr bool run_simple_double = true;
constexpr bool run_simple_double_with_compact_cache = true;

bool success = true;

if (run_float) {
std::cout << "[Testing uniformly randomly generated float inputs...]\n";
success &= uniform_random_test<float>(number_of_uniform_random_tests_float, "float");
std::cout << "[Testing uniformly randomly generated binary32 inputs...]\n";
success &=
uniform_random_test<float>(number_of_uniform_random_tests_float, [](auto x, char* buffer) {
jkj::dragonbox::to_chars(x, buffer);
});
std::cout << "Done.\n\n\n";
}
if (run_float_with_compressed_cache) {
std::cout << "[Testing uniformly randomly generated float inputs with compressed cache...]\n";
success &= uniform_random_test<float>(number_of_uniform_random_tests_float_compressed, "float",
jkj::dragonbox::policy::cache::compact);
if (run_float_with_compact_cache) {
std::cout << "[Testing uniformly randomly generated binary32 inputs (compact cache)...]\n";
success &=
uniform_random_test<float>(number_of_uniform_random_tests_float, [](auto x, char* buffer) {
jkj::dragonbox::to_chars(x, buffer, jkj::dragonbox::policy::cache::compact);
});
std::cout << "Done.\n\n\n";
}
if (run_simple_float) {
std::cout << "[Testing uniformly randomly generated binary32 inputs (simplified impl)...]\n";
success &=
uniform_random_test<float>(number_of_uniform_random_tests_float, [](auto x, char* buffer) {
jkj::simple_dragonbox::to_chars(x, buffer);
});
std::cout << "Done.\n\n\n";
}
if (run_simpl_float_with_compact_cache) {
std::cout << "[Testing uniformly randomly generated binary32 inputs (simplified impl, compact "
"cache)...]\n";
success &= uniform_random_test<float>(number_of_uniform_random_tests_float, [](auto x,
char* buffer) {
jkj::simple_dragonbox::to_chars(x, buffer, jkj::simple_dragonbox::policy::cache::compact);
});
std::cout << "Done.\n\n\n";
}
if (run_double) {
std::cout << "[Testing uniformly randomly generated double inputs...]\n";
success &= uniform_random_test<double>(number_of_uniform_random_tests_double, "double");
std::cout << "[Testing uniformly randomly generated binary64 inputs...]\n";
success &= uniform_random_test<double>(
number_of_uniform_random_tests_double,
[](auto x, char* buffer) { jkj::dragonbox::to_chars(x, buffer); });
std::cout << "Done.\n\n\n";
}
if (run_double_with_compact_cache) {
std::cout << "[Testing uniformly randomly generated binary64 inputs (compact cache)...]\n";
success &= uniform_random_test<double>(
number_of_uniform_random_tests_double, [](auto x, char* buffer) {
jkj::dragonbox::to_chars(x, buffer, jkj::dragonbox::policy::cache::compact);
});
std::cout << "Done.\n\n\n";
}
if (run_simple_double) {
std::cout << "[Testing uniformly randomly generated binary64 inputs (simplified impl)...]\n";
success &= uniform_random_test<double>(
number_of_uniform_random_tests_double,
[](auto x, char* buffer) { jkj::simple_dragonbox::to_chars(x, buffer); });
std::cout << "Done.\n\n\n";
}
if (run_double_with_compressed_cache) {
std::cout << "[Testing uniformly randomly generated double inputs with compressed cache...]\n";
success &= uniform_random_test<double>(number_of_uniform_random_tests_double_compressed,
"double", jkj::dragonbox::policy::cache::compact);
if (run_simple_double_with_compact_cache) {
std::cout << "[Testing uniformly randomly generated binary64 inputs with (simplified impl, "
"compact cache)...]\n";
success &= uniform_random_test<double>(number_of_uniform_random_tests_double, [](auto x,
char* buffer) {
jkj::simple_dragonbox::to_chars(x, buffer, jkj::simple_dragonbox::policy::cache::compact);
});
std::cout << "Done.\n\n\n";
}

Expand Down

0 comments on commit ece4e5f

Please sign in to comment.