libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
15CASE_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#if LIBCOPP_MACRO_ENABLE_MULTI_THREAD
32CASE_TEST(coroutine_task, id_allocator_mt) {
33 copp::util::uint64_id_allocator alloc;
34 ((void)alloc);
35
36 std::unique_ptr<std::thread> thds[40];
37 std::set<uint64_t> s[40];
38 for (int i = 0; i < 40; ++i) {
39 std::set<uint64_t> *sp = &s[i];
40 thds[i].reset(new std::thread([sp, &alloc]() {
41 size_t id_num = 36768;
42
43 for (size_t j = 0; j < id_num; ++j) {
44 uint64_t id = alloc.allocate();
45 CASE_EXPECT_TRUE(sp->find(id) == sp->end());
46 sp->insert(id);
47 }
48 }));
49 }
50
51 size_t id_num = 0;
52 for (int i = 0; i < 40; ++i) {
53 thds[i]->join();
54 id_num += 36768;
55
56 if (i != 0) {
57 s[0].insert(s[i].begin(), s[i].end());
58 }
59 }
60
61 CASE_EXPECT_EQ(id_num, s[0].size());
62}
63#endif
constexpr auto size(TCONTAINER &&container) -> decltype(container.size())
Definition span.h:44
#define CASE_EXPECT_EQ(l, r)
Definition test_macros.h:99
#define CASE_TEST(test_name, case_name)
Definition test_macros.h:47
#define CASE_EXPECT_TRUE(c)
Definition test_macros.h:97