forked from suyash/ulid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulid_test.cc
172 lines (137 loc) · 4.34 KB
/
ulid_test.cc
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <gtest/gtest.h>
#include <thread>
#ifdef ULIDUINT128
#include "ulid_uint128.hh"
#else
#include "ulid_struct.hh"
#endif // ULIDUINT128
TEST(basic, 1) {
ulid::ULID ulid = ulid::Create(std::time(nullptr), []() { return 4; });
std::string str = ulid::Marshal(ulid);
ASSERT_EQ(26, str.size());
for (char c : str) {
ASSERT_NE(std::string::npos, std::string(ulid::Encoding).find(c));
}
}
TEST(Create, 1) {
ulid::ULID ulid1 = 0;
ulid::Encode(1484581420, []() { return 4; }, ulid1);
auto ulid2 = ulid::Create(1484581420, []() { return 4; });
ASSERT_EQ(0, ulid::CompareULIDs(ulid1, ulid2));
}
TEST(EncodeTimeNow, 1) {
ulid::ULID ulid = 0;
ulid::EncodeTimeNow(ulid);
ulid::EncodeEntropy([]() { return 4; }, ulid);
std::string str = ulid::Marshal(ulid);
ASSERT_EQ(26, str.size());
for (char c : str) {
ASSERT_NE(std::string::npos, std::string(ulid::Encoding).find(c));
}
}
TEST(EncodeTimeSystemClockNow, 1) {
ulid::ULID ulid = 0;
ulid::EncodeTimeSystemClockNow(ulid);
ulid::EncodeEntropy([]() { return 4; }, ulid);
std::string str = ulid::Marshal(ulid);
ASSERT_EQ(26, str.size());
for (char c : str) {
ASSERT_NE(std::string::npos, std::string(ulid::Encoding).find(c));
}
}
TEST(EncodeEntropyRand, 1) {
ulid::ULID ulid = 0;
ulid::EncodeTimeNow(ulid);
ulid::EncodeEntropyRand(ulid);
std::string str = ulid::Marshal(ulid);
ASSERT_EQ(26, str.size());
for (char c : str) {
ASSERT_NE(std::string::npos, std::string(ulid::Encoding).find(c));
}
}
TEST(EncodeEntropyRand, 2) {
time_t timestamp = 1000000;
auto duration = std::chrono::seconds(timestamp);
auto nsduration = std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
auto msduration = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
ulid::ULID ulid1 = 0;
ulid::EncodeTime(msduration.count(), ulid1);
std::srand(nsduration.count());
ulid::EncodeEntropyRand(ulid1);
ulid::ULID ulid2 = 0;
ulid::EncodeTime(msduration.count(), ulid2);
std::srand(nsduration.count());
ulid::EncodeEntropyRand(ulid2);
ASSERT_EQ(0, ulid::CompareULIDs(ulid1, ulid2));
}
TEST(EncodeEntropyMt19937, 1) {
ulid::ULID ulid = 0;
ulid::EncodeTimeNow(ulid);
std::mt19937 generator(4);
ulid::EncodeEntropyMt19937(generator, ulid);
std::string str = ulid::Marshal(ulid);
ASSERT_EQ(26, str.size());
for (char c : str) {
ASSERT_NE(std::string::npos, std::string(ulid::Encoding).find(c));
}
}
TEST(EncodeNowRand, 1) {
ulid::ULID ulid = 0;
ulid::EncodeNowRand(ulid);
std::string str = ulid::Marshal(ulid);
ASSERT_EQ(26, str.size());
for (char c : str) {
ASSERT_NE(std::string::npos, std::string(ulid::Encoding).find(c));
}
}
TEST(CreateNowRand, 1) {
ulid::ULID ulid = ulid::CreateNowRand();
std::string str = ulid::Marshal(ulid);
ASSERT_EQ(26, str.size());
for (char c : str) {
ASSERT_NE(std::string::npos, std::string(ulid::Encoding).find(c));
}
}
TEST(MarshalBinary, 1) {
ulid::ULID ulid = ulid::Create(1484581420, []() { return 4; });
std::vector<uint8_t> b = ulid::MarshalBinary(ulid);
#ifdef ULIDUINT128
for (int i = 15 ; i >= 0 ; i--) {
ASSERT_EQ(static_cast<uint8_t>(ulid), b[i]);
ulid >>= 8;
}
#else
for (int i = 0 ; i < 16 ; i++) {
ASSERT_EQ(ulid.data[i], b[i]);
}
#endif // ULIDUINT128
}
TEST(Unmarshal, 1) {
ulid::ULID ulid = ulid::Unmarshal("0001C7STHC0G2081040G208104");
ulid::ULID ulid_expected = ulid::Create(1484581420, []() { return 4; });
ASSERT_EQ(0, ulid::CompareULIDs(ulid_expected, ulid));
}
TEST(UnmarshalBinary, 1) {
ulid::ULID ulid_expected = ulid::Create(1484581420, []() { return 4; });
std::vector<uint8_t> b = ulid::MarshalBinary(ulid_expected);
ulid::ULID ulid = ulid::UnmarshalBinary(b);
ASSERT_EQ(0, ulid::CompareULIDs(ulid_expected, ulid));
}
TEST(Time, 1) {
ulid::ULID ulid = ulid::Create(1484581420, []() { return 4; });
ASSERT_EQ(1484581420, ulid::Time(ulid));
}
// https://github.com/oklog/ulid/blob/master/ulid_test.go#L160-L169
TEST(AlizainCompatibility, 1) {
ulid::ULID ulid_got = 0;
ulid::EncodeTime(uint64_t(1469918176385), ulid_got);
ulid::ULID ulid_want = ulid::Unmarshal("01ARYZ6S410000000000000000");
ASSERT_EQ(0, ulid::CompareULIDs(ulid_want, ulid_got));
}
TEST(LexicographicalOrder, 1) {
ulid::ULID ulid1 = ulid::CreateNowRand();
std::this_thread::sleep_for(std::chrono::seconds(1));
ulid::ULID ulid2 = ulid::CreateNowRand();
EXPECT_EQ(-1, ulid::CompareULIDs(ulid1, ulid2));
EXPECT_EQ(1, ulid::CompareULIDs(ulid2, ulid1));
}