Celestia

Форк
0
/
formatnum_test.cpp 
158 строк · 4.5 Кб
1
#include <array>
2
#include <locale>
3
#include <string>
4
#include <string_view>
5
#include <tuple>
6

7
#include <fmt/format.h>
8

9
#include <doctest.h>
10

11
#include <celutil/formatnum.h>
12

13
using namespace std::string_view_literals;
14
using celestia::util::NumberFormat;
15
using celestia::util::NumberFormatter;
16

17
namespace
18
{
19

20
class TestNumpunct : public std::numpunct<wchar_t>
21
{
22
protected:
23
    // Custom separators
24
    wchar_t do_decimal_point() const override { return L'x'; }
25
    wchar_t do_thousands_sep() const override { return L'_'; }
26
    // Simulate Indian grouping rules
27
    std::string do_grouping() const override { return "\003\002"; }
28
};
29

30
using TestCase = std::tuple<double, unsigned int, std::string_view>;
31

32
} // end unnamed namespace
33

34
TEST_SUITE_BEGIN("Formatnum");
35

36
TEST_CASE("NumberFormat None")
37
{
38
    constexpr auto format = NumberFormat::None;
39

40
    std::locale testloc{ std::locale("C"), new TestNumpunct() };
41

42
    NumberFormatter formatter(testloc);
43

44
    constexpr std::array testCases
45
    {
46
        TestCase{ 0.0, 0, "0"sv },
47
        TestCase{ 0.0, 2, "0x00"sv },
48
        TestCase{ -0.0, 0, "-0"sv },
49
        TestCase{ -0.0, 3, "-0x000"sv },
50
        TestCase{ 12.345, 1, "12x3"sv },
51
        TestCase{ -12.346, 2, "-12x35"sv },
52
    };
53

54
    for (const auto& [value, precision, expected] : testCases)
55
    {
56
        auto actual = fmt::format("{}", formatter.format(value, precision, format));
57
        REQUIRE(expected == actual);
58
    }
59
}
60

61
TEST_CASE("NumberFormat GroupThousands")
62
{
63
    constexpr auto format = NumberFormat::GroupThousands;
64

65
    std::locale testloc{ std::locale("C"), new TestNumpunct() };
66

67
    NumberFormatter formatter(testloc);
68

69
    constexpr std::array testCases
70
    {
71
        TestCase{ 0.0, 0, "0"sv },
72
        TestCase{ 0.0, 2, "0x00"sv },
73
        TestCase{ -0.0, 0, "-0"sv },
74
        TestCase{ -0.0, 3, "-0x000"sv },
75
        TestCase{ 12.345, 1, "12x3"sv },
76
        TestCase{ -12.346, 2, "-12x35"sv },
77
        TestCase{ 123.98, 1, "124x0"sv },
78
        TestCase{ -123.98, 1, "-124x0"sv },
79
        TestCase{ 1234.12, 1, "1_234x1"sv },
80
        TestCase{ -1234.12, 1, "-1_234x1"sv },
81
        TestCase{ 12345.12, 2, "12_345x12"sv },
82
        TestCase{ -12345.12, 2, "-12_345x12"sv },
83
        TestCase{ 123456.1, 0, "1_23_456"sv },
84
        TestCase{ -123456.1, 0, "-1_23_456"sv },
85
        TestCase{ 192837123, 0, "19_28_37_123"sv },
86
        TestCase{ -192837123, 0, "-19_28_37_123"sv },
87
    };
88

89
    for (const auto& [value, precision, expected] : testCases)
90
    {
91
        auto actual = fmt::format("{}", formatter.format(value, precision, format));
92
        REQUIRE(expected == actual);
93
    }
94
}
95

96
TEST_CASE("NumberFormat SignificantFigures")
97
{
98
    constexpr auto format = NumberFormat::SignificantFigures;
99

100
    std::locale testloc{ std::locale("C"), new TestNumpunct() };
101

102
    NumberFormatter formatter(testloc);
103

104
    constexpr std::array testCases
105
    {
106
        TestCase{ 1.5292, 2, "1x5"sv },
107
        TestCase{ -1.5292, 2, "-1x5"sv },
108
        TestCase{ 15.292, 2, "15"sv },
109
        TestCase{ -15.292, 2, "-15"sv },
110
        TestCase{ 152.92, 2, "150"sv },
111
        TestCase{ -152.92, 2, "-150"sv },
112
        TestCase{ 1529.2, 2, "1500"sv },
113
        TestCase{ -1529.2, 2, "-1500"sv },
114
        TestCase{ 1529200, 2, "1500000"sv},
115
        TestCase{ -1529200, 2, "-1500000"sv},
116
        TestCase{ 0.00015292, 2, "0x00015"sv },
117
        TestCase{ -0.00015292, 2, "-0x00015"sv },
118
    };
119

120
    for (const auto& [value, precision, expected] : testCases)
121
    {
122
        auto actual = fmt::format("{}", formatter.format(value, precision, format));
123
        REQUIRE(expected == actual);
124
    }
125
}
126

127
TEST_CASE("NumberFormat GroupThousands+SignificantFigures")
128
{
129
    constexpr auto format = NumberFormat::GroupThousands | NumberFormat::SignificantFigures;
130

131
    std::locale testloc{ std::locale("C"), new TestNumpunct() };
132

133
    NumberFormatter formatter(testloc);
134

135
    constexpr std::array testCases
136
    {
137
        TestCase{ 1.5292, 2, "1x5"sv },
138
        TestCase{ -1.5292, 2, "-1x5"sv },
139
        TestCase{ 15.292, 2, "15"sv },
140
        TestCase{ -15.292, 2, "-15"sv },
141
        TestCase{ 152.92, 2, "150"sv },
142
        TestCase{ -152.92, 2, "-150"sv },
143
        TestCase{ 1529.2, 2, "1_500"sv },
144
        TestCase{ -1529.2, 2, "-1_500"sv },
145
        TestCase{ 1529200, 2, "15_00_000"sv},
146
        TestCase{ -1529200, 2, "-15_00_000"sv},
147
        TestCase{ 0.00015292, 2, "0x00015"sv },
148
        TestCase{ -0.00015292, 2, "-0x00015"sv },
149
    };
150

151
    for (const auto& [value, precision, expected] : testCases)
152
    {
153
        auto actual = fmt::format("{}", formatter.format(value, precision, format));
154
        REQUIRE(expected == actual);
155
    }
156
}
157

158
TEST_SUITE_END();
159

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.