upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/descriptor_table_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/descriptor_table_test.cpp')
-rw-r--r--runtime/test/descriptor_table_test.cpp234
1 files changed, 234 insertions, 0 deletions
diff --git a/runtime/test/descriptor_table_test.cpp b/runtime/test/descriptor_table_test.cpp
new file mode 100644
index 000000000..f9c2ba186
--- /dev/null
+++ b/runtime/test/descriptor_table_test.cpp
@@ -0,0 +1,234 @@
1// Unit tests for the descriptor hash table (set 0, binding 2 contract).
2// Tests FNV-1a hashing, T#/S# key generation, probe resolution, dynamic resizing,
3// GPU buffer serialization, and multithreaded insertion.
4#include "../src/descriptor_table.h"
5
6#include <cassert>
7#include <cstdio>
8#include <cstring>
9#include <random>
10#include <thread>
11#include <vector>
12
13#define EXPECT(condition) do { \
14 if (!(condition)) { \
15 std::fprintf(stderr, "assertion failed: %s at %s:%d\n", #condition, __FILE__, __LINE__); \
16 return 1; \
17 } \
18} while (0)
19
20using namespace leonida;
21
22int test_fnv1a_hash_matches_contract() {
23 // Verified against vk_texture_test.cpp key_hash and rdna2spirv.py desc_probe
24 const std::uint32_t zeros[8] = {0, 0, 0, 0, 0, 0, 0, 0};
25 std::uint32_t expected_zero_hash = 2166136261u;
26 for (int i = 0; i < 8; ++i) expected_zero_hash = (expected_zero_hash ^ 0) * 16777619u;
27 EXPECT(hash_descriptor_key(zeros) == expected_zero_hash);
28
29 const std::uint32_t sample_key[8] = {
30 0x12345678, 0x9abcdef0, 0x11223344, 0x55667788,
31 0x99aabbcc, 0xddeeff00, 0x01020304, 0x05060708
32 };
33 std::uint32_t expected_hash = 2166136261u;
34 for (int i = 0; i < 8; ++i) expected_hash = (expected_hash ^ sample_key[i]) * 16777619u;
35 EXPECT(hash_descriptor_key(sample_key) == expected_hash);
36 return 0;
37}
38
39int test_ssharp_key_padding() {
40 const std::uint32_t ssharp[4] = {0x92, 0xc00000, 0x14500000, 0x0};
41 const auto key = make_ssharp_key(ssharp);
42 EXPECT(key[0] == 0x92);
43 EXPECT(key[1] == 0xc00000);
44 EXPECT(key[2] == 0x14500000);
45 EXPECT(key[3] == 0x0);
46 EXPECT(key[4] == 0xFFFFFFFFu);
47 EXPECT(key[5] == 0xFFFFFFFFu);
48 EXPECT(key[6] == 0xFFFFFFFFu);
49 EXPECT(key[7] == 0xFFFFFFFFu);
50 return 0;
51}
52
53int test_basic_insertion_and_lookup() {
54 DescriptorTable table(16);
55 EXPECT(table.capacity() == 16);
56 EXPECT(table.mask() == 15);
57 EXPECT(table.size() == 0);
58
59 const std::uint32_t tsharp[8] = {
60 0x1000, 0x2000, 0x3000, 0x4000,
61 0x5000, 0x6000, 0x7000, 0x8000
62 };
63 EXPECT(table.lookup(tsharp) == 0);
64 EXPECT(!table.contains(tsharp));
65
66 EXPECT(table.insert(tsharp, 1));
67 EXPECT(table.size() == 1);
68 EXPECT(table.contains(tsharp));
69 EXPECT(table.lookup(tsharp) == 1);
70
71 // Duplicate key updates in place
72 EXPECT(table.insert(tsharp, 42));
73 EXPECT(table.size() == 1);
74 EXPECT(table.lookup(tsharp) == 42);
75
76 // S# key
77 const std::uint32_t ssharp[4] = {0xa, 0xb, 0xc, 0xd};
78 const auto skey = make_ssharp_key(ssharp);
79 EXPECT(table.insert(skey, 7));
80 EXPECT(table.size() == 2);
81 EXPECT(table.lookup(skey) == 7);
82 EXPECT(table.lookup(tsharp) == 42);
83 return 0;
84}
85
86int test_load_factor_and_growth() {
87 // Start with 16 slots. Load factor threshold 50% means max 8 entries before growth.
88 DescriptorTable table(16);
89 EXPECT(table.capacity() == 16);
90
91 std::vector<DescriptorKey> keys;
92 for (std::uint32_t i = 1; i <= 20; ++i) {
93 std::uint32_t k[8] = {i, i * 2, i * 3, i * 4, i * 5, i * 6, i * 7, i * 8};
94 keys.push_back(make_tsharp_key(k));
95 EXPECT(table.insert(keys.back(), i + 100));
96 }
97
98 EXPECT(table.size() == 20);
99 EXPECT(table.capacity() >= 64); // Doubled from 16 -> 32 -> 64
100 EXPECT(table.mask() == table.capacity() - 1);
101
102 // Verify all 20 keys resolve to their exact indices after growths
103 for (std::uint32_t i = 1; i <= 20; ++i) {
104 EXPECT(table.lookup(keys[i - 1]) == i + 100);
105 }
106 return 0;
107}
108
109int test_probe_collision_resolution() {
110 DescriptorTable table(64);
111
112 // Create 4 distinct keys that share the exact same hash
113 // We can forge keys that share hash or test linear collision handling
114 std::uint32_t k1[8] = {1, 0, 0, 0, 0, 0, 0, 0};
115 std::uint32_t k2[8] = {2, 0, 0, 0, 0, 0, 0, 0};
116 std::uint32_t k3[8] = {3, 0, 0, 0, 0, 0, 0, 0};
117
118 EXPECT(table.insert(k1, 10));
119 EXPECT(table.insert(k2, 20));
120 EXPECT(table.insert(k3, 30));
121
122 EXPECT(table.lookup(k1) == 10);
123 EXPECT(table.lookup(k2) == 20);
124 EXPECT(table.lookup(k3) == 30);
125 return 0;
126}
127
128int test_gpu_buffer_serialization() {
129 DescriptorTable table(16);
130 const std::uint32_t t1[8] = {10, 20, 30, 40, 50, 60, 70, 80};
131 const std::uint32_t s1[4] = {1, 2, 3, 4};
132 const auto skey = make_ssharp_key(s1);
133
134 EXPECT(table.insert(t1, 5));
135 EXPECT(table.insert(skey, 9));
136
137 const auto gpu_buf = table.build_gpu_buffer(false);
138 EXPECT(gpu_buf.size() == 1 + 16 * 10);
139 EXPECT(gpu_buf[0] == 15); // mask
140
141 // Verify lookup by traversing the generated buffer using the exact shader logic
142 const auto shader_lookup = [&](const std::uint32_t key[8]) -> std::uint32_t {
143 const std::uint32_t mask = gpu_buf[0];
144 const std::uint32_t h = hash_descriptor_key(key);
145 for (std::uint32_t k = 0; k < 8; ++k) {
146 const std::uint32_t slot = (h + k) & mask;
147 const std::size_t base = 1 + slot * 10;
148 bool match = true;
149 for (std::size_t j = 0; j < 8; ++j) {
150 if (gpu_buf[base + j] != key[j]) {
151 match = false;
152 break;
153 }
154 }
155 if (match) {
156 return gpu_buf[base + 8]; // index
157 }
158 }
159 return 0;
160 };
161
162 EXPECT(shader_lookup(t1) == 5);
163 EXPECT(shader_lookup(skey.data()) == 9);
164
165 const std::uint32_t missing[8] = {99, 99, 99, 99, 99, 99, 99, 99};
166 EXPECT(shader_lookup(missing) == 0);
167
168 // Test write_to_buffer
169 std::vector<std::uint32_t> raw_copy(gpu_buf.size());
170 table.write_to_buffer(raw_copy.data(), true);
171 EXPECT(raw_copy[0] == 15);
172 for (std::size_t s = 0; s < 16; ++s) {
173 EXPECT(raw_copy[1 + s * 10 + 9] == 0); // clear_pad = true
174 }
175 return 0;
176}
177
178int test_multithreaded_insert_and_lookup() {
179 DescriptorTable table(32);
180 constexpr int kThreads = 4;
181 constexpr int kPerThread = 50;
182
183 std::vector<std::thread> workers;
184 for (int t = 0; t < kThreads; ++t) {
185 workers.emplace_back([&table, t] {
186 for (int i = 0; i < kPerThread; ++i) {
187 const std::uint32_t id = static_cast<std::uint32_t>(t * kPerThread + i + 1);
188 std::uint32_t key[8] = {id, id * 3, id * 7, id * 11, id * 13, id * 17, id * 19, id * 23};
189 table.insert(key, id);
190 }
191 });
192 }
193
194 for (auto& w : workers) {
195 w.join();
196 }
197
198 EXPECT(table.size() == kThreads * kPerThread);
199 for (int t = 0; t < kThreads; ++t) {
200 for (int i = 0; i < kPerThread; ++i) {
201 const std::uint32_t id = static_cast<std::uint32_t>(t * kPerThread + i + 1);
202 std::uint32_t key[8] = {id, id * 3, id * 7, id * 11, id * 13, id * 17, id * 19, id * 23};
203 EXPECT(table.lookup(key) == id);
204 }
205 }
206 return 0;
207}
208
209int test_clear_resets_table() {
210 DescriptorTable table(16);
211 std::uint32_t k[8] = {1, 2, 3, 4, 5, 6, 7, 8};
212 table.insert(k, 100);
213 EXPECT(table.size() == 1);
214 EXPECT(table.lookup(k) == 100);
215
216 table.clear();
217 EXPECT(table.size() == 0);
218 EXPECT(table.lookup(k) == 0);
219 return 0;
220}
221
222int main() {
223 EXPECT(test_fnv1a_hash_matches_contract() == 0);
224 EXPECT(test_ssharp_key_padding() == 0);
225 EXPECT(test_basic_insertion_and_lookup() == 0);
226 EXPECT(test_load_factor_and_growth() == 0);
227 EXPECT(test_probe_collision_resolution() == 0);
228 EXPECT(test_gpu_buffer_serialization() == 0);
229 EXPECT(test_multithreaded_insert_and_lookup() == 0);
230 EXPECT(test_clear_resets_table() == 0);
231
232 std::printf("descriptor_table_test: all tests passed (0 failures)\n");
233 return 0;
234}