-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuuidtest.cpp
More file actions
69 lines (58 loc) · 1.89 KB
/
uuidtest.cpp
File metadata and controls
69 lines (58 loc) · 1.89 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
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <algorithm>
#include <latch>
#include <mutex>
#include <string>
#include <thread>
#include <uuid/uuid.h>
TEST(UUID, uuid_generate_time_safe) {
uuid_t uuid;
#ifdef __APPLE__
uuid_generate_time(uuid);
#else
uuid_generate_time_safe(uuid);
#endif
std::string str(36 + 1, '\0');
uuid_unparse_lower(uuid, str.data());
std::string regex = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
EXPECT_THAT(str, testing::MatchesRegex(regex));
}
TEST(UUID, concurrent_uuid_generate_time_safe) {
auto nThreads = std::min(64u, std::thread::hardware_concurrency());
EXPECT_GT(nThreads, 2);
const int generate_per_thread = 10000;
struct UUID {
uuid_t uuid;
};
std::mutex merge_mutex;
std::latch start_generate(nThreads);
std::vector<UUID> uuids_merged;
auto worker = [&]() {
start_generate.arrive_and_wait();
std::vector<UUID> uuids(generate_per_thread);
for (int i = 0; i < generate_per_thread; ++i) {
#ifdef __APPLE__
uuid_generate_time(uuids[i].uuid);
#else
uuid_generate_time_safe(uuids[i].uuid);
#endif
}
std::lock_guard<std::mutex> guard(merge_mutex);
uuids_merged.insert(uuids_merged.end(), uuids.begin(), uuids.end());
};
std::vector<std::thread> threads;
for (int i = 0; i < nThreads; ++i) {
threads.emplace_back(worker);
}
for (auto& thread : threads) {
thread.join();
}
std::sort(uuids_merged.begin(), uuids_merged.end(), [](UUID& a, UUID& b) {
return uuid_compare(a.uuid, b.uuid) < 0;
});
auto _ = std::unique(uuids_merged.begin(), uuids_merged.end(), [](UUID& a, UUID& b) {
return uuid_compare(a.uuid, b.uuid) == 0;
});
ASSERT_EQ(nThreads * generate_per_thread, uuids_merged.size());
}