-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_luaallocator.cpp
More file actions
189 lines (148 loc) · 5.02 KB
/
test_luaallocator.cpp
File metadata and controls
189 lines (148 loc) · 5.02 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
** Test program for LuaAllocator
** Demonstrates that the allocator works correctly with std::vector
*/
#include <iostream>
#include <vector>
#include <string>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "luaallocator.h"
#include "lstate.h"
// Test 1: Basic allocation and deallocation
static void test_basic_vector(lua_State* L) {
std::cout << "Test 1: Basic vector operations... ";
std::vector<int, LuaAllocator<int>> vec{LuaAllocator<int>(L)};
// Push some elements
for (int i = 0; i < 100; i++) {
vec.push_back(i);
}
// Verify contents
for (int i = 0; i < 100; i++) {
if (vec[i] != i) {
std::cout << "FAILED: Element " << i << " is " << vec[i] << " instead of " << i << std::endl;
return;
}
}
std::cout << "PASSED" << std::endl;
}
// Test 2: Reallocation (vector growth)
static void test_growth(lua_State* L) {
std::cout << "Test 2: Vector growth and reallocation... ";
std::vector<int, LuaAllocator<int>> vec{LuaAllocator<int>(L)};
// Force multiple reallocations
for (int i = 0; i < 10000; i++) {
vec.push_back(i);
}
// Verify all elements
for (int i = 0; i < 10000; i++) {
if (vec[i] != i) {
std::cout << "FAILED: Element " << i << " is " << vec[i] << std::endl;
return;
}
}
std::cout << "PASSED" << std::endl;
}
// Test 3: Different types
static void test_different_types(lua_State* L) {
std::cout << "Test 3: Different types (double, struct)... ";
// Test with double
std::vector<double, LuaAllocator<double>> dvec{LuaAllocator<double>(L)};
for (int i = 0; i < 100; i++) {
dvec.push_back(i * 1.5);
}
for (int i = 0; i < 100; i++) {
if (dvec[i] != i * 1.5) {
std::cout << "FAILED: Double element " << i << " is incorrect" << std::endl;
return;
}
}
// Test with struct
struct TestStruct {
int x;
double y;
char z;
};
std::vector<TestStruct, LuaAllocator<TestStruct>> svec{LuaAllocator<TestStruct>(L)};
for (int i = 0; i < 100; i++) {
svec.push_back({i, i * 2.0, static_cast<char>('A' + (i % 26))});
}
for (int i = 0; i < 100; i++) {
if (svec[i].x != i || svec[i].y != i * 2.0) {
std::cout << "FAILED: Struct element " << i << " is incorrect" << std::endl;
return;
}
}
std::cout << "PASSED" << std::endl;
}
// Test 4: Memory accounting
static void test_memory_accounting(lua_State* L) {
std::cout << "Test 4: Memory accounting... ";
l_mem before = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
{
std::vector<int, LuaAllocator<int>> vec{LuaAllocator<int>(L)};
// Allocate ~1MB
vec.resize(256 * 1024);
for (size_t i = 0; i < vec.size(); i++) {
vec[i] = static_cast<int>(i);
}
l_mem during = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
// Should have increased
if (during <= before) {
std::cout << "FAILED: Memory not tracked (before=" << before << ", during=" << during << ")" << std::endl;
return;
}
}
// Force GC
lua_gc(L, LUA_GCCOLLECT, 0);
l_mem after = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
// Should be close to original (within some tolerance for overhead)
if (after > before + 100000) { // 100KB tolerance
std::cout << "FAILED: Memory not freed (before=" << before << ", after=" << after << ")" << std::endl;
return;
}
std::cout << "PASSED" << std::endl;
}
// Test 5: Exception safety (allocation failure)
static void test_exception_safety(lua_State* L) {
std::cout << "Test 5: Exception safety... ";
// This test verifies that allocation failures are handled correctly
// In normal operation, Lua's allocator will throw on failure
try {
std::vector<int, LuaAllocator<int>> vec{LuaAllocator<int>(L)};
// Normal allocations should work
vec.push_back(42);
vec.push_back(84);
if (vec[0] != 42 || vec[1] != 84) {
std::cout << "FAILED: Values incorrect" << std::endl;
return;
}
std::cout << "PASSED" << std::endl;
} catch (const std::bad_alloc&) {
std::cout << "FAILED: Unexpected allocation failure" << std::endl;
} catch (...) {
std::cout << "FAILED: Unexpected exception" << std::endl;
}
}
int main() {
std::cout << "=== LuaAllocator Test Suite ===" << std::endl;
std::cout << std::endl;
// Create a new Lua state
lua_State* L = luaL_newstate();
if (!L) {
std::cerr << "Failed to create Lua state" << std::endl;
return 1;
}
luaL_openlibs(L);
// Run tests
test_basic_vector(L);
test_growth(L);
test_different_types(L);
test_memory_accounting(L);
test_exception_safety(L);
std::cout << std::endl;
std::cout << "=== All tests completed ===" << std::endl;
lua_close(L);
return 0;
}