Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/tests/InsertionOrderedMap.test.cpp
2723 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#include "Luau/InsertionOrderedMap.h"
3
4
#include <memory>
5
6
#include "doctest.h"
7
8
using namespace Luau;
9
10
struct MapFixture
11
{
12
std::vector<std::unique_ptr<int>> ptrs;
13
14
int* makePtr()
15
{
16
ptrs.push_back(std::make_unique<int>(int{}));
17
return ptrs.back().get();
18
}
19
};
20
21
TEST_SUITE_BEGIN("InsertionOrderedMap");
22
23
TEST_CASE_FIXTURE(MapFixture, "map_insertion")
24
{
25
InsertionOrderedMap<int*, int> map;
26
27
int* a = makePtr();
28
int* b = makePtr();
29
30
map.insert(a, 1);
31
map.insert(b, 2);
32
}
33
34
TEST_CASE_FIXTURE(MapFixture, "map_lookup")
35
{
36
InsertionOrderedMap<int*, int> map;
37
38
int* a = makePtr();
39
map.insert(a, 1);
40
41
int* r = map.get(a);
42
REQUIRE(r != nullptr);
43
CHECK(*r == 1);
44
45
r = map.get(makePtr());
46
CHECK(r == nullptr);
47
}
48
49
TEST_CASE_FIXTURE(MapFixture, "insert_does_not_update")
50
{
51
InsertionOrderedMap<int*, int> map;
52
53
int* k = makePtr();
54
map.insert(k, 1);
55
map.insert(k, 2);
56
57
int* v = map.get(k);
58
REQUIRE(v != nullptr);
59
CHECK(*v == 1);
60
}
61
62
TEST_CASE_FIXTURE(MapFixture, "insertion_order_is_iteration_order")
63
{
64
// This one is a little hard to prove, in that if the ordering guarantees
65
// fail this test isn't guaranteed to fail, but it is strictly better than
66
// nothing.
67
68
InsertionOrderedMap<int*, int> map;
69
int* a = makePtr();
70
int* b = makePtr();
71
int* c = makePtr();
72
map.insert(a, 1);
73
map.insert(b, 1);
74
map.insert(c, 1);
75
76
auto it = map.begin();
77
REQUIRE(it != map.end());
78
CHECK(it->first == a);
79
CHECK(it->second == 1);
80
81
++it;
82
REQUIRE(it != map.end());
83
CHECK(it->first == b);
84
CHECK(it->second == 1);
85
86
++it;
87
REQUIRE(it != map.end());
88
CHECK(it->first == c);
89
CHECK(it->second == 1);
90
91
++it;
92
CHECK(it == map.end());
93
}
94
95
TEST_CASE_FIXTURE(MapFixture, "destructuring_iterator_compiles")
96
{
97
// This test's only purpose is to successfully compile.
98
InsertionOrderedMap<int*, int> map;
99
100
for (auto [k, v] : map)
101
{
102
// Checks here solely to silence unused variable warnings.
103
CHECK(k);
104
CHECK(v > 0);
105
}
106
}
107
108
TEST_CASE_FIXTURE(MapFixture, "map_erasure")
109
{
110
InsertionOrderedMap<int*, int> map;
111
112
int* a = makePtr();
113
int* b = makePtr();
114
115
map.insert(a, 1);
116
map.insert(b, 2);
117
118
map.erase(map.find(a));
119
CHECK(map.size() == 1);
120
CHECK(!map.contains(a));
121
CHECK(map.get(a) == nullptr);
122
123
int* v = map.get(b);
124
REQUIRE(v);
125
}
126
127
TEST_CASE_FIXTURE(MapFixture, "map_clear")
128
{
129
InsertionOrderedMap<int*, int> map;
130
int* a = makePtr();
131
132
map.insert(a, 1);
133
134
map.clear();
135
CHECK(map.size() == 0);
136
CHECK(!map.contains(a));
137
CHECK(map.get(a) == nullptr);
138
}
139
140
TEST_SUITE_END();
141
142