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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
// This file is part of the 64k demo project.
// It tests the procedural generation system.
#include "procedural/generator.h"
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
void test_noise() {
std::cout << "Testing Noise Generator..." << std::endl;
int w = 64, h = 64;
std::vector<uint8_t> buffer(w * h * 4);
float params[] = {12345, 1.0f}; // Seed, Intensity
// Test with explicit params
bool res = procedural::gen_noise(buffer.data(), w, h, params, 2);
assert(res);
assert(buffer[3] == 255);
// Check that not all pixels are black
bool nonzero = false;
for (size_t i = 0; i < buffer.size(); i += 4) {
if (buffer[i] > 0) {
nonzero = true;
break;
}
}
assert(nonzero);
// Test with default params
std::fill(buffer.begin(), buffer.end(), 0);
res = procedural::gen_noise(buffer.data(), w, h, nullptr, 0);
assert(res);
assert(buffer[3] == 255); // Alpha should still be set
}
void test_perlin() {
std::cout << "Testing Perlin Generator..." << std::endl;
int w = 64, h = 64;
std::vector<uint8_t> buffer(w * h * 4);
// Test with explicit params
// Params: Seed, Freq, Amp, Decay, Octaves
float params[] = {12345, 4.0f, 1.0f, 0.5f, 4.0f};
bool res = procedural::gen_perlin(buffer.data(), w, h, params, 5);
assert(res);
assert(buffer[3] == 255);
bool nonzero = false;
for (size_t i = 0; i < buffer.size(); i += 4) {
if (buffer[i] > 0) {
nonzero = true;
break;
}
}
assert(nonzero);
// Test with default params
std::fill(buffer.begin(), buffer.end(), 0);
res = procedural::gen_perlin(buffer.data(), w, h, nullptr, 0);
assert(res);
assert(buffer[3] == 255);
// Test memory allocation failure simulation (large dimensions)
// This is hard to robustly test without mocking, but we can try an
// excessively large allocation if desired. For now, we trust the logic path.
}
void test_grid() {
std::cout << "Testing Grid Generator..." << std::endl;
int w = 100, h = 100;
std::vector<uint8_t> buffer(w * h * 4);
float params[] = {10, 1}; // Size 10, Thickness 1
// Test with explicit params
bool res = procedural::gen_grid(buffer.data(), w, h, params, 2);
assert(res);
// Pixel (0,0) should be white (on line)
assert(buffer[0] == 255);
// Pixel (5,5) should be black (off line, since size=10)
assert(buffer[(5 * w + 5) * 4] == 0);
// Pixel (10,0) should be white (on vertical line)
assert(buffer[(0 * w + 10) * 4] == 255);
// Test with default params
res = procedural::gen_grid(buffer.data(), w, h, nullptr, 0);
assert(res);
// Default size is 32, thickness 2
assert(buffer[0] == 255);
assert(buffer[(0 * w + 32) * 4] == 255);
}
void test_periodic() {
std::cout << "Testing Periodic Blending..." << std::endl;
int w = 64, h = 64;
std::vector<uint8_t> buffer(w * h * 4);
// Fill with horizontal gradient: left=0, right=255
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
int idx = (y * w + x) * 4;
buffer[idx] = (uint8_t)(x * 255 / (w - 1));
buffer[idx + 1] = 0;
buffer[idx + 2] = 0;
buffer[idx + 3] = 255;
}
}
// Pre-check: edges are different
assert(buffer[0] == 0);
assert(buffer[(w - 1) * 4] == 255);
float params[] = {0.1f}; // Blend ratio 10%
bool res = procedural::make_periodic(buffer.data(), w, h, params, 1);
assert(res);
// Post-check: Left edge (x=0) should now be blended with right edge.
// Logic: blend right edge INTO left edge. At x=0, we copy from right side.
// So buffer[0] should be close to 255 (value from right).
assert(buffer[0] > 200);
// Check invalid ratio
float invalid_params[] = {-1.0f};
res = procedural::make_periodic(buffer.data(), w, h, invalid_params, 1);
assert(res); // Should return true but do nothing
}
void test_plasma() {
std::cout << "Testing Plasma Generator..." << std::endl;
int w = 64, h = 64;
std::vector<uint8_t> buffer(w * h * 4);
float params[] = {0.0f, 2.0f};
bool res = procedural::gen_plasma(buffer.data(), w, h, params, 2);
assert(res);
assert(buffer[3] == 255);
// RGB should differ (color output, not grayscale)
bool has_color = false;
for (size_t i = 0; i < buffer.size(); i += 4) {
if (buffer[i] != buffer[i + 1] || buffer[i + 1] != buffer[i + 2]) {
has_color = true;
break;
}
}
assert(has_color);
// Test defaults
res = procedural::gen_plasma(buffer.data(), w, h, nullptr, 0);
assert(res);
}
void test_voronoi() {
std::cout << "Testing Voronoi Generator..." << std::endl;
int w = 64, h = 64;
std::vector<uint8_t> buffer(w * h * 4);
// F1 mode
float params_f1[] = {4.0f, 0.0f, 0.0f};
bool res = procedural::gen_voronoi(buffer.data(), w, h, params_f1, 3);
assert(res);
assert(buffer[3] == 255);
bool nonzero = false;
for (size_t i = 0; i < buffer.size(); i += 4) {
if (buffer[i] > 0) { nonzero = true; break; }
}
assert(nonzero);
// F2-F1 (borders) mode
float params_border[] = {4.0f, 2.0f, 0.0f};
res = procedural::gen_voronoi(buffer.data(), w, h, params_border, 3);
assert(res);
// Test defaults
res = procedural::gen_voronoi(buffer.data(), w, h, nullptr, 0);
assert(res);
}
void test_normalmap() {
std::cout << "Testing Normal Map Generator..." << std::endl;
int w = 64, h = 64;
std::vector<uint8_t> buffer(w * h * 4);
// Fill with a horizontal gradient as height
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
int idx = (y * w + x) * 4;
buffer[idx] = (uint8_t)(x * 255 / (w - 1));
buffer[idx + 1] = buffer[idx];
buffer[idx + 2] = buffer[idx];
buffer[idx + 3] = 255;
}
}
float params[] = {4.0f};
bool res = procedural::gen_normalmap(buffer.data(), w, h, params, 1);
assert(res);
assert(buffer[3] == 255);
// For a horizontal gradient, X-normal (R) should be shifted from 128
// (non-flat normals expected)
bool has_normal_variation = false;
for (size_t i = 0; i < buffer.size(); i += 4) {
if (buffer[i] != 128) { has_normal_variation = true; break; }
}
assert(has_normal_variation);
// Z component (B) should be > 0 (normal faces up)
assert(buffer[2] > 0);
}
int main() {
test_noise();
test_perlin();
test_grid();
test_periodic();
test_plasma();
test_voronoi();
test_normalmap();
std::cout << "--- PROCEDURAL TESTS PASSED ---" << std::endl;
return 0;
}
|