-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathpretty_tests.cpp
More file actions
111 lines (104 loc) · 3.85 KB
/
Copy pathpretty_tests.cpp
File metadata and controls
111 lines (104 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <gtest/gtest.h>
#include <chrono>
#include <iomanip>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
#include "databento/constants.hpp"
#include "databento/datetime.hpp"
#include "databento/pretty.hpp"
namespace databento::pretty::tests {
TEST(PrettyTests, TestPrettyPx) {
std::ostringstream ss;
std::vector<std::pair<int64_t, std::string>> cases{{-100'000, "-0.000100000"},
{32'500'000'000, "32.500000000"},
{101'005'000'000, "101.005000000"},
{0, "0.000000000"},
{kUndefPrice, "UNDEF_PRICE"}};
for (const auto& [num, exp] : cases) {
ss << Px{num};
ASSERT_EQ(ss.str(), exp);
ss.str("");
}
}
TEST(PrettyTests, TestPrecision) {
std::ostringstream ss;
std::vector<std::tuple<int64_t, int, std::string>> cases{
{32'500'000'000, 3, "32.500"},
{101'005'000'000, 5, "101.00500"},
{75'000'000, 5, "0.07500"},
{32'123'456'789, 2, "32.12"}};
for (const auto& [num, precision, exp] : cases) {
ss << std::setprecision(precision) << Px{num};
ASSERT_EQ(ss.str(), exp);
ss.str("");
}
}
TEST(PrettyTests, TestDefaultFill) {
std::ostringstream ss;
std::vector<std::tuple<int64_t, int, int, std::string, std::string>> cases{
{32'500'000'000, 4, 3, "32.500", "32.500"},
{32'500'000'000, 8, 3, " 32.500", "32.500 "},
{101'005'000'000, 10, 5, " 101.00500", "101.00500 "},
{75'000'000, 13, 5, " 0.07500", "0.07500 "},
{32'123'456'789, 7, 2, " 32.12", "32.12 "},
{32'123'456'789, 16, 5, " 32.12345", "32.12345 "}};
for (const auto& [num, width, precision, exp_right, exp_left] : cases) {
// Default
ss << std::setw(width) << std::setprecision(precision) << Px{num};
ASSERT_EQ(ss.str(), exp_right);
ss.str("");
// Left
ss << std::setw(width) << std::left << std::setprecision(precision) << Px{num};
ASSERT_EQ(ss.str(), exp_left);
ss.str("");
// Right
ss << std::setw(width) << std::right << std::setprecision(precision) << Px{num};
ASSERT_EQ(ss.str(), exp_right);
ss.str("");
}
}
TEST(PrettyTests, TestZeroFill) {
std::ostringstream ss;
std::vector<std::tuple<int64_t, int, int, std::string, std::string>> cases{
{32'500'000'000, 4, 3, "32.500", "32.500"},
{32'500'000'000, 8, 3, "0032.500", "32.50000"},
{101'005'000'000, 10, 5, "0101.00500", "101.005000"},
{75'000'000, 13, 5, "0000000.07500", "0.07500000000"},
{32'123'456'789, 7, 2, "0032.12", "32.1200"},
{32'123'456'789, 16, 4, "00000000032.1234", "32.1234000000000"},
};
for (const auto& [num, width, precision, exp_right, exp_left] : cases) {
// Default
ss << std::setw(width) << std::setfill('0') << std::setprecision(precision)
<< Px{num};
ASSERT_EQ(ss.str(), exp_right);
ss.str("");
// Left
ss << std::setw(width) << std::left << std::setfill('0')
<< std::setprecision(precision) << Px{num};
ASSERT_EQ(ss.str(), exp_left);
ss.str("");
// Right
ss << std::setw(width) << std::right << std::setfill('0')
<< std::setprecision(precision) << Px{num};
ASSERT_EQ(ss.str(), exp_right);
ss.str("");
}
}
TEST(PrettyTests, TestPrettyTs) {
std::ostringstream ss;
std::vector<std::pair<int64_t, std::string>> cases{
{0, "1970-01-01T00:00:00.000000000Z"},
{1, "1970-01-01T00:00:00.000000001Z"},
{1622838300000000000, "2021-06-04T20:25:00.000000000Z"},
{kUndefTimestamp - 1, "2554-07-21T23:34:33.709551614Z"},
{kUndefTimestamp, "UNDEF_TIMESTAMP"}};
for (const auto& [num, exp] : cases) {
ss << Ts{UnixNanos{std::chrono::nanoseconds{num}}};
ASSERT_EQ(ss.str(), exp);
ss.str("");
}
}
} // namespace databento::pretty::tests