Skip to content

Commit

Permalink
Replace Catch2 with doctest (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmiddleditch authored Jan 15, 2024
1 parent 9f9616e commit 7b20684
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 93 deletions.
15 changes: 6 additions & 9 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,21 @@ IncludeCategories:
- Regex: '^"[^/]+"'
Priority: 0
SortPriority: 10
- Regex: '^["<]potato/spud/'
- Regex: '^["<]nanofmt/'
Priority: 50
SortPriority: 70
- Regex: '^["<]potato/runtime/'
Priority: 50
SortPriority: 60
- Regex: '^["<]potato/'
Priority: 50
SortPriority: 30
- Regex: '^".*"'
Priority: 0
SortPriority: 20
- Regex: '/'
Priority: 100
Priority: 90
SortPriority: 80
- Regex: '[.]'
Priority: 90
SortPriority: 90
- Regex: '.*'
Priority: 100
SortPriority: 90
SortPriority: 100
IncludeIsMainRegex: '([.][a-z]+)?$'
IndentCaseBlocks: true
IndentCaseLabels: true
Expand Down
12 changes: 0 additions & 12 deletions cmake/fetch_catch2.cmake

This file was deleted.

10 changes: 10 additions & 0 deletions cmake/fetch_doctest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
include(FetchContent)

FetchContent_Declare(
doctest
SYSTEM
GIT_REPOSITORY https://github.com/doctest/doctest.git
GIT_TAG v2.4.11
)

FetchContent_MakeAvailable(doctest)
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include(fetch_catch2)
include(fetch_doctest)

add_executable(nanofmt_test)
target_sources(nanofmt_test PRIVATE
Expand All @@ -10,7 +10,7 @@ target_sources(nanofmt_test PRIVATE
)
target_link_libraries(nanofmt_test PRIVATE
nanofmt
Catch2::Catch2WithMain
doctest::doctest_with_main
)

add_test(NAME nanofmt_test COMMAND nanofmt_test)
48 changes: 25 additions & 23 deletions tests/test_charconv.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
// Copyright (c) Sean Middleditch and contributors. See accompanying LICENSE.md for copyright details.

#include "test_utils.h"

#include "nanofmt/charconv.h"

#include <catch2/catch_test_macros.hpp>
#include <doctest/doctest.h>

#include <cstdint>
#include <limits>

TEST_CASE("nanofmt.to_chars.integers", "[nanofmt][to_chars][integers]") {
TEST_CASE("nanofmt.to_chars.integers") {
using namespace NANOFMT_NS::test;
using namespace NANOFMT_NS;

SECTION("decimal") {
SUBCASE("decimal") {
CHECK(to_string(0) == "0");
CHECK(to_string(1) == "1");
CHECK(to_string(10) == "10");
CHECK(to_string(100) == "100");
CHECK(to_string(1'000) == "1000");
}

SECTION("hex") {
SUBCASE("hex") {
CHECK(to_string(0x0, int_format::hex) == "0");
CHECK(to_string(0x1, int_format::hex) == "1");

CHECK(to_string(0xdeadc0de, int_format::hex) == "deadc0de");
CHECK(to_string(0xdeadc0de, int_format::hex_upper) == "DEADC0DE");
}

SECTION("binary") {
SUBCASE("binary") {
CHECK(to_string(0b0, int_format::binary) == "0");
CHECK(to_string(0b1, int_format::binary) == "1");

Expand All @@ -36,15 +38,15 @@ TEST_CASE("nanofmt.to_chars.integers", "[nanofmt][to_chars][integers]") {
CHECK(to_string(-0b01011010, int_format::binary) == "-1011010");
}

SECTION("negatives") {
SUBCASE("negatives") {
CHECK(to_string(-0) == "0");
CHECK(to_string(-1) == "-1");
CHECK(to_string(-10) == "-10");
CHECK(to_string(-100) == "-100");
CHECK(to_string(-1'000) == "-1000");
}

SECTION("bounds") {
SUBCASE("bounds") {
CHECK(to_string(std::numeric_limits<std::int8_t>::max()) == "127");
CHECK(to_string(std::numeric_limits<std::int16_t>::max()) == "32767");
CHECK(to_string(std::numeric_limits<std::int32_t>::max()) == "2147483647");
Expand All @@ -62,11 +64,11 @@ TEST_CASE("nanofmt.to_chars.integers", "[nanofmt][to_chars][integers]") {
}
}

TEST_CASE("nanofmt.to_chars.fixed", "[nanofmt][to_chars][float][fixed]") {
TEST_CASE("nanofmt.to_chars.fixed") {
using namespace NANOFMT_NS::test;
using namespace NANOFMT_NS;

SECTION("whole numbers") {
SUBCASE("whole numbers") {
CHECK(to_string(0e0f, float_format::fixed) == "0");
CHECK(to_string(1e0f, float_format::fixed) == "1");
CHECK(to_string(1e1f, float_format::fixed) == "10");
Expand All @@ -75,14 +77,14 @@ TEST_CASE("nanofmt.to_chars.fixed", "[nanofmt][to_chars][float][fixed]") {
CHECK(to_string(1e4f, float_format::fixed) == "10000");
}

SECTION("fractional numbers") {
SUBCASE("fractional numbers") {
CHECK(to_string(1e-1f, float_format::fixed) == "0.1");
CHECK(to_string(1e-2f, float_format::fixed) == "0.01");
CHECK(to_string(1e-3f, float_format::fixed) == "0.001");
CHECK(to_string(1e-4f, float_format::fixed) == "0.0001");
}

SECTION("bounds") {
SUBCASE("bounds") {
CHECK(
to_string(std::numeric_limits<float>::max(), float_format::fixed) ==
"340282350000000000000000000000000000000");
Expand All @@ -106,7 +108,7 @@ TEST_CASE("nanofmt.to_chars.fixed", "[nanofmt][to_chars][float][fixed]") {
"00022250738585072014");
}

SECTION("rounding") {
SUBCASE("rounding") {
CHECK(to_string(.001, float_format::fixed, 0) == "0");
CHECK(to_string(1.55, float_format::fixed, 0) == "2");
CHECK(to_string(1.55, float_format::fixed, 1) == "1.6");
Expand All @@ -116,19 +118,19 @@ TEST_CASE("nanofmt.to_chars.fixed", "[nanofmt][to_chars][float][fixed]") {
CHECK(to_string(1.251, float_format::fixed, 1) == "1.3");
}

SECTION("nonfinite") {
SUBCASE("nonfinite") {
CHECK(to_string(std::numeric_limits<float>::infinity(), float_format::fixed) == "inf");
CHECK(to_string(-std::numeric_limits<float>::infinity(), float_format::fixed) == "-inf");
CHECK(to_string(std::numeric_limits<float>::quiet_NaN(), float_format::fixed) == "nan");
CHECK(to_string(-std::numeric_limits<float>::quiet_NaN(), float_format::fixed) == "-nan");
}
}

TEST_CASE("nanofmt.to_chars.scientifix", "[nanofmt][to_chars][float][scientific]") {
TEST_CASE("nanofmt.to_chars.scientifix") {
using namespace NANOFMT_NS::test;
using namespace NANOFMT_NS;

SECTION("whole numbers") {
SUBCASE("whole numbers") {
CHECK(to_string(0e0f, float_format::scientific) == "0e+00");
CHECK(to_string(1e0f, float_format::scientific) == "1e+00");
CHECK(to_string(1e1f, float_format::scientific) == "1e+01");
Expand All @@ -137,57 +139,57 @@ TEST_CASE("nanofmt.to_chars.scientifix", "[nanofmt][to_chars][float][scientific]
CHECK(to_string(1e4f, float_format::scientific) == "1e+04");
}

SECTION("fractional numbers") {
SUBCASE("fractional numbers") {
CHECK(to_string(1e-1f, float_format::scientific) == "1e-01");
CHECK(to_string(1e-2f, float_format::scientific) == "1e-02");
CHECK(to_string(1e-3f, float_format::scientific) == "1e-03");
CHECK(to_string(1e-4f, float_format::scientific) == "1e-04");
}

SECTION("mixed numbers") {
SUBCASE("mixed numbers") {
CHECK(to_string(1.55e-1f, float_format::scientific) == "1.55e-01");
CHECK(to_string(12.55e-2f, float_format::scientific) == "1.255e-01");
CHECK(to_string(12'000.55'001f, float_format::scientific) == "1.200055e+04");
CHECK(to_string(1.55e-4f, float_format::scientific) == "1.55e-04");
}

SECTION("bounds") {
SUBCASE("bounds") {
CHECK(to_string(std::numeric_limits<float>::max(), float_format::scientific) == "3.4028235e+38");
CHECK(to_string(std::numeric_limits<double>::max(), float_format::scientific) == "1.7976931348623157e+308");

CHECK(to_string(std::numeric_limits<float>::min(), float_format::scientific) == "1.1754944e-38");
CHECK(to_string(std::numeric_limits<double>::min(), float_format::scientific) == "2.2250738585072014e-308");
}

SECTION("rounding") {
SUBCASE("rounding") {
CHECK(to_string(1.55e-4f, float_format::scientific, 0) == "2e-04");
CHECK(to_string(1.35e-4f, float_format::scientific, 1) == "1.4e-04");
CHECK(to_string(1.25e-4f, float_format::scientific, 1) == "1.2e-04");
CHECK(to_string(1.351e-4f, float_format::scientific, 1) == "1.4e-04");
CHECK(to_string(1.251e-4f, float_format::scientific, 1) == "1.3e-04");
}

SECTION("nonfinite") {
SUBCASE("nonfinite") {
CHECK(to_string(std::numeric_limits<float>::infinity(), float_format::scientific) == "inf");
CHECK(to_string(-std::numeric_limits<float>::infinity(), float_format::scientific) == "-inf");
CHECK(to_string(std::numeric_limits<float>::quiet_NaN(), float_format::scientific) == "nan");
CHECK(to_string(-std::numeric_limits<float>::quiet_NaN(), float_format::scientific) == "-nan");
}
}

TEST_CASE("nanofmt.to_chars.general", "[nanofmt][to_chars][float][general]") {
TEST_CASE("nanofmt.to_chars.general") {
using namespace NANOFMT_NS::test;
using namespace NANOFMT_NS;

SECTION("bounds") {
SUBCASE("bounds") {
CHECK(to_string(std::numeric_limits<float>::max(), float_format::general) == "3.40282e+38");
CHECK(to_string(std::numeric_limits<double>::max(), float_format::general) == "1.79769e+308");

CHECK(to_string(std::numeric_limits<float>::min(), float_format::general) == "1.17549e-38");
CHECK(to_string(std::numeric_limits<double>::min(), float_format::general) == "2.22507e-308");
}

SECTION("nonfinite") {
SUBCASE("nonfinite") {
CHECK(to_string(std::numeric_limits<float>::infinity(), float_format::general) == "inf");
CHECK(to_string(-std::numeric_limits<float>::infinity(), float_format::general) == "-inf");
CHECK(to_string(std::numeric_limits<float>::quiet_NaN(), float_format::general) == "nan");
Expand Down
Loading

0 comments on commit 7b20684

Please sign in to comment.