-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
156 lines (135 loc) · 3.96 KB
/
tests.cpp
File metadata and controls
156 lines (135 loc) · 3.96 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
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
#include "my_colony.hpp"
#include <iostream>
#include <vector>
// Destructions tracker:
static int active_objects = 0;
struct Tracker {
int id;
Tracker(int _id) : id(_id) { active_objects++; }
Tracker(const Tracker &other) : id(other.id) { active_objects++; }
~Tracker() { active_objects--; }
};
void test_section(std::string name) {
std::cout << "\nTesting: " << name << std::endl;
}
int main() {
using namespace my_lib;
// TEST 1: Insertion and Iteration
{
test_section("Basic Insertion");
Colony<int> c;
for (int i = 0; i < 5; ++i)
c.insert(i * 10);
int count = 0;
for (auto it = c.begin(); it != c.end(); ++it) {
assert(*it == count * 10);
count++;
}
assert(c.total_size == 5);
std::cout << "Successfully inserted and iterated 5 elements." << std::endl;
}
// TEST 2: Object Lifetime
{
test_section("Destructor Safety");
{
Colony<Tracker> c;
auto it1 = c.insert(Tracker(1));
auto it2 = c.insert(Tracker(2));
assert(active_objects == 2);
c.erase(it1);
assert(active_objects == 1); // it1 should be destroyed immediately
}
assert(active_objects ==
0); // it2 should be destroyed when Colony is deleted
std::cout
<< "Destructors called correctly on erase() and Colony destruction."
<< std::endl;
}
// TEST 3: Hole Reuse
{
test_section("Hole Reuse");
Colony<int> c;
auto it1 = c.insert(100);
auto it2 = c.insert(200);
void *addr1 = &(*it1);
c.erase(it1);
auto it3 = c.insert(300);
void *addr3 = &(*it3);
assert(addr1 ==
addr3); // New item should occupy exactly the same memory slot
assert(*it3 == 300);
std::cout
<< "Confirmed: New elements reuse memory addresses of erased elements."
<< std::endl;
}
// TEST 4: Adaptive Growth
{
test_section("Adaptive Growth");
Colony<int> c;
// MIN_SIZE is 64. Fill 65 elements.
for (int i = 0; i < 65; ++i)
c.insert(i);
assert(c.first != c.last); // Should have created a second group
assert(c.last->allocated_size == 128); // Growth factor 2: 64 * 2 = 128
std::cout << "Confirmed: Colony grew from 64 to 64+128 capacity."
<< std::endl;
}
// TEST 5: Bi-directional Jumps
{
test_section("Bi-directional Jumps");
Colony<int> c;
std::vector<Iterator<int>> its;
for (int i = 0; i < 10; ++i)
its.push_back(c.insert(i));
// Erase 1, 2, 3 and 7, 8
c.erase(its[1]);
c.erase(its[2]);
c.erase(its[3]);
c.erase(its[7]);
c.erase(its[8]);
// Forward check
std::cout << "Forward (skipping 1,2,3 and 7,8): ";
for (auto it = c.begin(); it != c.end(); ++it)
std::cout << *it << " ";
std::cout << "\n";
// Backward check
auto it_rev = its[9];
std::cout << "Backward: ";
while (true) {
std::cout << *it_rev << " ";
if (it_rev == c.begin())
break;
--it_rev;
}
std::cout << std::endl;
std::cout << "Confirmed: Iterators jump over holes in both directions."
<< std::endl;
}
// TEST 6: Small Type Safety
{
test_section("Small Type (char) Safety");
Colony<char> c;
// Insert char (is 1 byte and skipfield_t is 2 bytes, so
// the links struct (4 bytes total) is larger than type).
auto it1 = c.insert('A');
auto it2 = c.insert('B');
c.erase(it1);
// If the Slot union wasn't working, erasing it1 would overwrite 'B's memory
assert(*it2 == 'B');
std::cout
<< "Confirmed: Erasing 1-byte types does not corrupt adjacent memory."
<< std::endl;
}
// TEST 7: Erase Entire Group
{
test_section("Empty Group Handling");
Colony<int> c;
auto it = c.insert(1);
c.erase(it);
assert(c.begin() == c.end()); // Should be effectively empty
std::cout << "Confirmed: Correct behavior when all elements are erased."
<< std::endl;
}
std::cout << "\nALL TESTS PASSED!" << std::endl;
return 0;
}