libcopp  2.2.0
id_allocator_test.cpp
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
3 #include <cstdio>
4 #include <cstring>
5 #include <ctime>
6 #include <iostream>
7 #include <memory>
8 #include <set>
9 #include <thread>
10 
11 #include "frame/test_macros.h"
12 
14 
15 CASE_TEST(coroutine_task, id_allocator_st) {
16  copp::util::uint64_id_allocator alloc;
17  ((void)alloc);
18 
19  size_t id_num = 3 * (1 << 8) + 100;
20  std::set<uint64_t> s;
21 
22  for (size_t i = 0; i < id_num; ++i) {
23  uint64_t id = alloc.allocate();
24  CASE_EXPECT_TRUE(s.find(id) == s.end());
25  s.insert(id);
26  }
27 
28  CASE_EXPECT_EQ(id_num, s.size());
29 }
30 
31 CASE_TEST(coroutine_task, id_allocator_mt) {
32  copp::util::uint64_id_allocator alloc;
33  ((void)alloc);
34 
35  std::unique_ptr<std::thread> thds[40];
36  std::set<uint64_t> s[40];
37  for (int i = 0; i < 40; ++i) {
38  std::set<uint64_t> *sp = &s[i];
39  thds[i].reset(new std::thread([sp, &alloc]() {
40  size_t id_num = 36768;
41 
42  for (size_t j = 0; j < id_num; ++j) {
43  uint64_t id = alloc.allocate();
44  CASE_EXPECT_TRUE(sp->find(id) == sp->end());
45  sp->insert(id);
46  }
47  }));
48  }
49 
50  size_t id_num = 0;
51  for (int i = 0; i < 40; ++i) {
52  thds[i]->join();
53  id_num += 36768;
54 
55  if (i != 0) {
56  s[0].insert(s[i].begin(), s[i].end());
57  }
58  }
59 
60  CASE_EXPECT_EQ(id_num, s[0].size());
61 }
CASE_TEST(coroutine_task, id_allocator_st)
constexpr auto size(TCONTAINER &&container) -> decltype(container.size())
Definition: span.h:44
#define CASE_EXPECT_EQ(l, r)
Definition: test_macros.h:96
#define CASE_EXPECT_TRUE(c)
Definition: test_macros.h:94