-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.zig
More file actions
2301 lines (1883 loc) · 80.7 KB
/
tests.zig
File metadata and controls
2301 lines (1883 loc) · 80.7 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Comprehensive unit tests for ZigCraft.
//!
//! Coverage includes:
//! - Math: Vec3, Mat4, AABB, Frustum, Plane
//! - World: Chunk, BlockType, PackedLight, coordinate conversion
//! - Worldgen: Noise (determinism, range bounds)
//!
//! Run with: zig build test
const std = @import("std");
const testing = std.testing;
pub const std_options: std.Options = .{
.log_level = .err,
};
const Vec3 = @import("zig-math").Vec3;
const Mat4 = @import("zig-math").Mat4;
const AABB = @import("zig-math").AABB;
const Frustum = @import("zig-math").Frustum;
const Plane = @import("zig-math").Plane;
// World modules
const Chunk = @import("world/chunk.zig").Chunk;
const ChunkMesh = @import("world/chunk_mesh.zig").ChunkMesh;
const NeighborChunks = @import("world/chunk_mesh.zig").NeighborChunks;
const PackedLight = @import("world/chunk.zig").PackedLight;
const CHUNK_SIZE_X = @import("world/chunk.zig").CHUNK_SIZE_X;
const CHUNK_SIZE_Y = @import("world/chunk.zig").CHUNK_SIZE_Y;
const CHUNK_SIZE_Z = @import("world/chunk.zig").CHUNK_SIZE_Z;
const worldToChunk = @import("world/chunk.zig").worldToChunk;
const worldToLocal = @import("world/chunk.zig").worldToLocal;
const BlockType = @import("world/block.zig").BlockType;
const block_registry = @import("world/block_registry.zig");
const TextureAtlas = @import("engine/graphics/texture_atlas.zig").TextureAtlas;
const BiomeId = @import("world/worldgen/biome.zig").BiomeId;
// Meshing stage modules
const ao_calculator = @import("world/meshing/ao_calculator.zig");
const lighting_sampler = @import("world/meshing/lighting_sampler.zig");
const biome_color_sampler = @import("world/meshing/biome_color_sampler.zig");
const boundary = @import("world/meshing/boundary.zig");
// Worldgen modules
const Noise = @import("zig-noise").Noise;
// Issue #147: Modular terrain generation subsystems
const NoiseSampler = @import("world/worldgen/noise_sampler.zig").NoiseSampler;
const HeightSampler = @import("world/worldgen/height_sampler.zig").HeightSampler;
const SurfaceBuilder = @import("world/worldgen/surface_builder.zig").SurfaceBuilder;
const CoastalSurfaceType = @import("world/worldgen/surface_builder.zig").CoastalSurfaceType;
const BiomeSource = @import("world/worldgen/biome.zig").BiomeSource;
// ECS tests
test {
_ = @import("ecs_tests.zig");
_ = @import("engine/graphics/vulkan_device.zig");
_ = @import("engine/graphics/vulkan/ssao_system_tests.zig");
_ = @import("vulkan_tests.zig");
_ = @import("engine/graphics/rhi_tests.zig");
_ = @import("world/worldgen/schematics.zig");
_ = @import("world/worldgen/tree_registry.zig");
_ = @import("world/lod_manager.zig");
_ = @import("world/lod_renderer.zig");
_ = @import("engine/atmosphere/tests.zig");
_ = @import("game/settings/tests.zig");
_ = @import("game/input_settings.zig");
}
test "Vec3 addition" {
const a = Vec3.init(1, 2, 3);
const b = Vec3.init(4, 5, 6);
const c = a.add(b);
try testing.expectEqual(@as(f32, 5), c.x);
try testing.expectEqual(@as(f32, 7), c.y);
try testing.expectEqual(@as(f32, 9), c.z);
}
test "Vec3 subtraction" {
const a = Vec3.init(5, 7, 9);
const b = Vec3.init(1, 2, 3);
const c = a.sub(b);
try testing.expectEqual(@as(f32, 4), c.x);
try testing.expectEqual(@as(f32, 5), c.y);
try testing.expectEqual(@as(f32, 6), c.z);
}
test "Vec3 scaling" {
const a = Vec3.init(1, 2, 3);
const b = a.scale(2.0);
try testing.expectEqual(@as(f32, 2), b.x);
try testing.expectEqual(@as(f32, 4), b.y);
try testing.expectEqual(@as(f32, 6), b.z);
}
test "Vec3 dot product" {
const a = Vec3.init(1, 0, 0);
const b = Vec3.init(0, 1, 0);
// Orthogonal vectors have dot product of 0
try testing.expectEqual(@as(f32, 0), a.dot(b));
// Parallel vectors
const c = Vec3.init(2, 0, 0);
try testing.expectEqual(@as(f32, 2), a.dot(c));
// General case
const d = Vec3.init(1, 2, 3);
const e = Vec3.init(4, 5, 6);
try testing.expectEqual(@as(f32, 32), d.dot(e)); // 1*4 + 2*5 + 3*6 = 32
}
test "Vec3 cross product" {
const x = Vec3.init(1, 0, 0);
const y = Vec3.init(0, 1, 0);
const z = x.cross(y);
try testing.expectEqual(@as(f32, 0), z.x);
try testing.expectEqual(@as(f32, 0), z.y);
try testing.expectEqual(@as(f32, 1), z.z);
// Cross product is anti-commutative
const neg_z = y.cross(x);
try testing.expectEqual(@as(f32, -1), neg_z.z);
}
test "Vec3 length and lengthSquared" {
const v = Vec3.init(3, 4, 0);
try testing.expectEqual(@as(f32, 25), v.lengthSquared());
try testing.expectEqual(@as(f32, 5), v.length());
// 3D case: 1^2 + 2^2 + 2^2 = 9
const v2 = Vec3.init(1, 2, 2);
try testing.expectEqual(@as(f32, 9), v2.lengthSquared());
try testing.expectEqual(@as(f32, 3), v2.length());
}
test "Vec3 normalize" {
const v = Vec3.init(3, 4, 0);
const n = v.normalize();
try testing.expectApproxEqAbs(@as(f32, 0.6), n.x, 0.0001);
try testing.expectApproxEqAbs(@as(f32, 0.8), n.y, 0.0001);
try testing.expectApproxEqAbs(@as(f32, 0), n.z, 0.0001);
try testing.expectApproxEqAbs(@as(f32, 1), n.length(), 0.0001);
// Zero vector normalization should return zero
const zero = Vec3.zero.normalize();
try testing.expectEqual(@as(f32, 0), zero.x);
try testing.expectEqual(@as(f32, 0), zero.y);
try testing.expectEqual(@as(f32, 0), zero.z);
}
test "Vec3 negate" {
const v = Vec3.init(1, -2, 3);
const neg = v.negate();
try testing.expectEqual(@as(f32, -1), neg.x);
try testing.expectEqual(@as(f32, 2), neg.y);
try testing.expectEqual(@as(f32, -3), neg.z);
}
test "Vec3 lerp" {
const a = Vec3.init(0, 0, 0);
const b = Vec3.init(10, 20, 30);
const mid = a.lerp(b, 0.5);
try testing.expectEqual(@as(f32, 5), mid.x);
try testing.expectEqual(@as(f32, 10), mid.y);
try testing.expectEqual(@as(f32, 15), mid.z);
// t=0 should return a
const start = a.lerp(b, 0);
try testing.expectEqual(a.x, start.x);
// t=1 should return b
const end = a.lerp(b, 1);
try testing.expectEqual(b.x, end.x);
}
test "Vec3 distance" {
const a = Vec3.init(0, 0, 0);
const b = Vec3.init(3, 4, 0);
try testing.expectEqual(@as(f32, 5), a.distance(b));
}
test "Vec3 constants" {
try testing.expectEqual(@as(f32, 0), Vec3.zero.x);
try testing.expectEqual(@as(f32, 1), Vec3.one.x);
try testing.expectEqual(@as(f32, 1), Vec3.up.y);
try testing.expectEqual(@as(f32, -1), Vec3.down.y);
try testing.expectEqual(@as(f32, 1), Vec3.right.x);
try testing.expectEqual(@as(f32, -1), Vec3.left.x);
}
// ============================================================================
// Mat4 Tests
// ============================================================================
test "Mat4 identity" {
const id = Mat4.identity;
// Diagonal should be 1
try testing.expectEqual(@as(f32, 1), id.data[0][0]);
try testing.expectEqual(@as(f32, 1), id.data[1][1]);
try testing.expectEqual(@as(f32, 1), id.data[2][2]);
try testing.expectEqual(@as(f32, 1), id.data[3][3]);
// Off-diagonal should be 0
try testing.expectEqual(@as(f32, 0), id.data[0][1]);
try testing.expectEqual(@as(f32, 0), id.data[1][0]);
}
test "Mat4 multiply identity" {
const id = Mat4.identity;
const result = id.multiply(id);
// Identity * Identity = Identity
try testing.expectEqual(@as(f32, 1), result.data[0][0]);
try testing.expectEqual(@as(f32, 1), result.data[1][1]);
try testing.expectEqual(@as(f32, 0), result.data[0][1]);
}
test "Mat4 translate" {
const t = Mat4.translate(Vec3.init(5, 10, 15));
try testing.expectEqual(@as(f32, 5), t.data[3][0]);
try testing.expectEqual(@as(f32, 10), t.data[3][1]);
try testing.expectEqual(@as(f32, 15), t.data[3][2]);
// Transform a point
const point = Vec3.init(1, 2, 3);
const transformed = t.transformPoint(point);
try testing.expectEqual(@as(f32, 6), transformed.x);
try testing.expectEqual(@as(f32, 12), transformed.y);
try testing.expectEqual(@as(f32, 18), transformed.z);
}
test "Mat4 scale" {
const s = Mat4.scale(Vec3.init(2, 3, 4));
try testing.expectEqual(@as(f32, 2), s.data[0][0]);
try testing.expectEqual(@as(f32, 3), s.data[1][1]);
try testing.expectEqual(@as(f32, 4), s.data[2][2]);
const point = Vec3.init(1, 1, 1);
const scaled = s.transformPoint(point);
try testing.expectEqual(@as(f32, 2), scaled.x);
try testing.expectEqual(@as(f32, 3), scaled.y);
try testing.expectEqual(@as(f32, 4), scaled.z);
}
test "Mat4 transformDirection ignores translation" {
const t = Mat4.translate(Vec3.init(100, 200, 300));
const dir = Vec3.init(1, 0, 0);
const transformed = t.transformDirection(dir);
// Direction should be unchanged by translation
try testing.expectEqual(@as(f32, 1), transformed.x);
try testing.expectEqual(@as(f32, 0), transformed.y);
try testing.expectEqual(@as(f32, 0), transformed.z);
}
test "Mat4 inverse of identity" {
const id = Mat4.identity;
const inv = id.inverse();
// Inverse of identity is identity
try testing.expectApproxEqAbs(@as(f32, 1), inv.data[0][0], 0.0001);
try testing.expectApproxEqAbs(@as(f32, 1), inv.data[1][1], 0.0001);
try testing.expectApproxEqAbs(@as(f32, 0), inv.data[0][1], 0.0001);
}
test "Mat4 inverse multiplied by original gives identity" {
// Test with scale matrix (simpler case)
const s = Mat4.scale(Vec3.init(2, 3, 4));
const inv = s.inverse();
const product = s.multiply(inv);
// Should be close to identity
try testing.expectApproxEqAbs(@as(f32, 1), product.data[0][0], 0.0001);
try testing.expectApproxEqAbs(@as(f32, 1), product.data[1][1], 0.0001);
try testing.expectApproxEqAbs(@as(f32, 1), product.data[2][2], 0.0001);
try testing.expectApproxEqAbs(@as(f32, 1), product.data[3][3], 0.0001);
try testing.expectApproxEqAbs(@as(f32, 0), product.data[0][1], 0.0001);
}
test "Mat4 rotation preserves length" {
const rot = Mat4.rotateY(std.math.pi / 4.0); // 45 degrees
const v = Vec3.init(1, 0, 0);
const rotated = rot.transformDirection(v);
// Length should be preserved
try testing.expectApproxEqAbs(@as(f32, 1), rotated.length(), 0.0001);
}
test "Mat4 perspective has correct structure" {
const p = Mat4.perspective(std.math.pi / 4.0, 16.0 / 9.0, 0.1, 1000.0);
// Perspective matrix should have -1 at [2][3]
try testing.expectEqual(@as(f32, -1), p.data[2][3]);
// And 0 at [3][3]
try testing.expectEqual(@as(f32, 0), p.data[3][3]);
}
// ============================================================================
// AABB Tests
// ============================================================================
test "AABB init and accessors" {
const aabb = AABB.init(Vec3.init(0, 0, 0), Vec3.init(10, 20, 30));
try testing.expectEqual(@as(f32, 0), aabb.min.x);
try testing.expectEqual(@as(f32, 30), aabb.max.z);
const center = aabb.center();
try testing.expectEqual(@as(f32, 5), center.x);
try testing.expectEqual(@as(f32, 10), center.y);
try testing.expectEqual(@as(f32, 15), center.z);
const size = aabb.size();
try testing.expectEqual(@as(f32, 10), size.x);
try testing.expectEqual(@as(f32, 20), size.y);
try testing.expectEqual(@as(f32, 30), size.z);
}
test "AABB fromCenterSize" {
const aabb = AABB.fromCenterSize(Vec3.init(5, 5, 5), Vec3.init(10, 10, 10));
try testing.expectEqual(@as(f32, 0), aabb.min.x);
try testing.expectEqual(@as(f32, 10), aabb.max.x);
}
test "AABB contains point" {
const aabb = AABB.init(Vec3.init(0, 0, 0), Vec3.init(10, 10, 10));
// Point inside
try testing.expect(aabb.contains(Vec3.init(5, 5, 5)));
// Point on boundary
try testing.expect(aabb.contains(Vec3.init(0, 0, 0)));
try testing.expect(aabb.contains(Vec3.init(10, 10, 10)));
// Point outside
try testing.expect(!aabb.contains(Vec3.init(-1, 5, 5)));
try testing.expect(!aabb.contains(Vec3.init(11, 5, 5)));
}
test "AABB intersects" {
const a = AABB.init(Vec3.init(0, 0, 0), Vec3.init(10, 10, 10));
const b = AABB.init(Vec3.init(5, 5, 5), Vec3.init(15, 15, 15));
const c = AABB.init(Vec3.init(20, 20, 20), Vec3.init(30, 30, 30));
// Overlapping boxes
try testing.expect(a.intersects(b));
try testing.expect(b.intersects(a));
// Non-overlapping boxes
try testing.expect(!a.intersects(c));
try testing.expect(!c.intersects(a));
}
test "AABB expand and translate" {
const aabb = AABB.init(Vec3.init(0, 0, 0), Vec3.init(10, 10, 10));
const expanded = aabb.expand(Vec3.init(1, 1, 1));
try testing.expectEqual(@as(f32, -1), expanded.min.x);
try testing.expectEqual(@as(f32, 11), expanded.max.x);
const translated = aabb.translate(Vec3.init(5, 5, 5));
try testing.expectEqual(@as(f32, 5), translated.min.x);
try testing.expectEqual(@as(f32, 15), translated.max.x);
}
// ============================================================================
// Plane and Frustum Tests
// ============================================================================
test "Plane signedDistance" {
// XY plane at z=0, normal pointing +Z
const plane = Plane.init(Vec3.init(0, 0, 1), 0);
// Point in front of plane
try testing.expectEqual(@as(f32, 5), plane.signedDistance(Vec3.init(0, 0, 5)));
// Point behind plane
try testing.expectEqual(@as(f32, -3), plane.signedDistance(Vec3.init(0, 0, -3)));
// Point on plane
try testing.expectEqual(@as(f32, 0), plane.signedDistance(Vec3.init(0, 0, 0)));
}
test "Plane normalize" {
const plane = Plane.init(Vec3.init(0, 0, 2), 4);
const normalized = plane.normalize();
try testing.expectApproxEqAbs(@as(f32, 1), normalized.normal.z, 0.0001);
try testing.expectApproxEqAbs(@as(f32, 2), normalized.distance, 0.0001);
}
test "Frustum intersectsSphere" {
// Create a simple view-projection matrix (identity for testing)
// This creates a frustum that contains points near origin
const vp = Mat4.identity;
const frustum = Frustum.fromViewProj(vp);
// Sphere at origin should be inside
try testing.expect(frustum.intersectsSphere(Vec3.init(0, 0, 0), 0.5));
}
// ============================================================================
// PackedLight Tests
// ============================================================================
test "PackedLight init and accessors" {
const light = PackedLight.init(15, 10);
try testing.expectEqual(@as(u4, 15), light.getSkyLight());
try testing.expectEqual(@as(u4, 10), light.getBlockLight());
try testing.expectEqual(@as(u4, 15), light.getMaxLight());
}
test "PackedLight setters" {
var light = PackedLight.init(0, 0);
light.setSkyLight(12);
light.setBlockLight(8);
try testing.expectEqual(@as(u4, 12), light.getSkyLight());
try testing.expectEqual(@as(u4, 8), light.getBlockLight());
}
test "PackedLight brightness" {
const full = PackedLight.init(15, 0);
try testing.expectEqual(@as(f32, 1.0), full.getBrightness());
const half = PackedLight.init(7, 0);
try testing.expectApproxEqAbs(@as(f32, 7.0 / 15.0), half.getBrightness(), 0.001);
const zero = PackedLight.init(0, 0);
try testing.expectEqual(@as(f32, 0.0), zero.getBrightness());
}
// ============================================================================
// Chunk Coordinate Conversion Tests
// ============================================================================
test "worldToChunk positive coordinates" {
const result = worldToChunk(32, 48);
try testing.expectEqual(@as(i32, 2), result.chunk_x);
try testing.expectEqual(@as(i32, 3), result.chunk_z);
}
test "worldToChunk negative coordinates" {
// -1 should be in chunk -1 (floor division)
const result = worldToChunk(-1, -1);
try testing.expectEqual(@as(i32, -1), result.chunk_x);
try testing.expectEqual(@as(i32, -1), result.chunk_z);
// -16 should be in chunk -1
const result2 = worldToChunk(-16, -16);
try testing.expectEqual(@as(i32, -1), result2.chunk_x);
// -17 should be in chunk -2
const result3 = worldToChunk(-17, -17);
try testing.expectEqual(@as(i32, -2), result3.chunk_x);
}
test "worldToChunk zero" {
const result = worldToChunk(0, 0);
try testing.expectEqual(@as(i32, 0), result.chunk_x);
try testing.expectEqual(@as(i32, 0), result.chunk_z);
}
test "worldToLocal positive coordinates" {
const result = worldToLocal(35, 50);
try testing.expectEqual(@as(u32, 3), result.x); // 35 % 16 = 3
try testing.expectEqual(@as(u32, 2), result.z); // 50 % 16 = 2
}
test "worldToLocal negative coordinates" {
// -1 should map to 15 (proper modulo behavior)
const result = worldToLocal(-1, -1);
try testing.expectEqual(@as(u32, 15), result.x);
try testing.expectEqual(@as(u32, 15), result.z);
// -17 should map to 15
const result2 = worldToLocal(-17, -17);
try testing.expectEqual(@as(u32, 15), result2.x);
}
// ============================================================================
// Chunk Tests
// ============================================================================
test "Chunk init" {
const chunk = Chunk.init(5, -3);
try testing.expectEqual(@as(i32, 5), chunk.chunk_x);
try testing.expectEqual(@as(i32, -3), chunk.chunk_z);
try testing.expectEqual(Chunk.State.missing, chunk.state);
try testing.expect(chunk.dirty);
}
test "Chunk getBlock and setBlock" {
var chunk = Chunk.init(0, 0);
// Default is air
try testing.expectEqual(BlockType.air, chunk.getBlock(0, 0, 0));
// Set and get
chunk.setBlock(5, 64, 10, .stone);
try testing.expectEqual(BlockType.stone, chunk.getBlock(5, 64, 10));
// Other blocks unchanged
try testing.expectEqual(BlockType.air, chunk.getBlock(0, 64, 0));
}
test "Chunk getBlockSafe bounds checking" {
var chunk = Chunk.init(0, 0);
chunk.setBlock(0, 0, 0, .stone);
// Valid access
try testing.expectEqual(BlockType.stone, chunk.getBlockSafe(0, 0, 0));
// Out of bounds returns air
try testing.expectEqual(BlockType.air, chunk.getBlockSafe(-1, 0, 0));
try testing.expectEqual(BlockType.air, chunk.getBlockSafe(16, 0, 0));
try testing.expectEqual(BlockType.air, chunk.getBlockSafe(0, -1, 0));
try testing.expectEqual(BlockType.air, chunk.getBlockSafe(0, 256, 0));
}
test "Chunk light operations" {
var chunk = Chunk.init(0, 0);
// Default light is 0
try testing.expectEqual(@as(u4, 0), chunk.getSkyLight(0, 0, 0));
try testing.expectEqual(@as(u4, 0), chunk.getBlockLight(0, 0, 0));
// Set and get
chunk.setSkyLight(5, 64, 10, 15);
chunk.setBlockLight(5, 64, 10, 8);
try testing.expectEqual(@as(u4, 15), chunk.getSkyLight(5, 64, 10));
try testing.expectEqual(@as(u4, 8), chunk.getBlockLight(5, 64, 10));
}
test "Chunk getWorldX and getWorldZ" {
const chunk = Chunk.init(3, -2);
try testing.expectEqual(@as(i32, 48), chunk.getWorldX()); // 3 * 16
try testing.expectEqual(@as(i32, -32), chunk.getWorldZ()); // -2 * 16
}
test "Chunk fill and fillLayer" {
var chunk = Chunk.init(0, 0);
chunk.fillLayer(0, .bedrock);
for (0..CHUNK_SIZE_X) |x| {
for (0..CHUNK_SIZE_Z) |z| {
try testing.expectEqual(BlockType.bedrock, chunk.getBlock(@intCast(x), 0, @intCast(z)));
}
}
// Layer 1 should still be air
try testing.expectEqual(BlockType.air, chunk.getBlock(0, 1, 0));
}
test "Chunk pin and unpin" {
var chunk = Chunk.init(0, 0);
try testing.expect(!chunk.isPinned());
chunk.pin();
try testing.expect(chunk.isPinned());
chunk.pin();
try testing.expect(chunk.isPinned()); // Still pinned
chunk.unpin();
try testing.expect(chunk.isPinned()); // Still pinned (count = 1)
chunk.unpin();
try testing.expect(!chunk.isPinned()); // Now unpinned
}
// ============================================================================
// BlockType Tests
// ============================================================================
test "BlockType isSolid" {
try testing.expect(!block_registry.getBlockDefinition(BlockType.air).is_solid);
try testing.expect(!block_registry.getBlockDefinition(BlockType.water).is_solid);
try testing.expect(block_registry.getBlockDefinition(BlockType.stone).is_solid);
try testing.expect(block_registry.getBlockDefinition(BlockType.dirt).is_solid);
try testing.expect(block_registry.getBlockDefinition(BlockType.grass).is_solid);
try testing.expect(block_registry.getBlockDefinition(BlockType.leaves).is_solid);
}
test "BlockType isTransparent" {
try testing.expect(block_registry.getBlockDefinition(BlockType.air).is_transparent);
try testing.expect(block_registry.getBlockDefinition(BlockType.water).is_transparent);
try testing.expect(block_registry.getBlockDefinition(BlockType.glass).is_transparent);
try testing.expect(block_registry.getBlockDefinition(BlockType.leaves).is_transparent);
try testing.expect(!block_registry.getBlockDefinition(BlockType.stone).is_transparent);
try testing.expect(!block_registry.getBlockDefinition(BlockType.dirt).is_transparent);
}
test "BlockType isOpaque" {
try testing.expect(!block_registry.getBlockDefinition(BlockType.air).isOpaque());
try testing.expect(!block_registry.getBlockDefinition(BlockType.water).isOpaque());
try testing.expect(!block_registry.getBlockDefinition(BlockType.glass).isOpaque());
try testing.expect(block_registry.getBlockDefinition(BlockType.stone).isOpaque());
try testing.expect(block_registry.getBlockDefinition(BlockType.dirt).isOpaque());
}
test "BlockType isAir" {
try testing.expect(BlockType.air == .air);
try testing.expect(BlockType.stone != .air);
try testing.expect(BlockType.water != .air);
}
test "BlockType getLightEmission" {
try testing.expectEqual(@as(u4, 15), block_registry.getBlockDefinition(BlockType.glowstone).getLightEmissionLevel());
try testing.expectEqual(@as(u4, 0), block_registry.getBlockDefinition(BlockType.stone).getLightEmissionLevel());
try testing.expectEqual(@as(u4, 0), block_registry.getBlockDefinition(BlockType.water).getLightEmissionLevel());
}
test "BlockType getColor returns valid RGB" {
const colors = block_registry.getBlockDefinition(BlockType.stone).default_color;
try testing.expect(colors[0] >= 0 and colors[0] <= 1);
try testing.expect(colors[1] >= 0 and colors[1] <= 1);
try testing.expect(colors[2] >= 0 and colors[2] <= 1);
}
// ============================================================================
// Noise Tests
// ============================================================================
test "Noise deterministic with same seed" {
const noise1 = Noise.init(12345);
const noise2 = Noise.init(12345);
const val1 = noise1.perlin2D(1.5, 2.5);
const val2 = noise2.perlin2D(1.5, 2.5);
try testing.expectEqual(val1, val2);
}
test "Noise different with different seed" {
const noise1 = Noise.init(12345);
const noise2 = Noise.init(54321);
const val1 = noise1.perlin2D(1.5, 2.5);
const val2 = noise2.perlin2D(1.5, 2.5);
try testing.expect(val1 != val2);
}
test "Noise perlin2D range" {
const noise = Noise.init(42);
// Sample many points and verify range is approximately [-1, 1]
var min_val: f32 = 1.0;
var max_val: f32 = -1.0;
var y: f32 = 0;
while (y < 10) : (y += 0.5) {
var x: f32 = 0;
while (x < 10) : (x += 0.5) {
const val = noise.perlin2D(x, y);
min_val = @min(min_val, val);
max_val = @max(max_val, val);
}
}
// Perlin noise should be in [-1, 1] range
try testing.expect(min_val >= -1.0);
try testing.expect(max_val <= 1.0);
}
test "Noise perlin3D range" {
const noise = Noise.init(42);
var min_val: f32 = 1.0;
var max_val: f32 = -1.0;
var z: f32 = 0;
while (z < 5) : (z += 1) {
var y: f32 = 0;
while (y < 5) : (y += 1) {
var x: f32 = 0;
while (x < 5) : (x += 1) {
const val = noise.perlin3D(x, y, z);
min_val = @min(min_val, val);
max_val = @max(max_val, val);
}
}
}
try testing.expect(min_val >= -1.0);
try testing.expect(max_val <= 1.0);
}
test "Noise fbm2D produces varied output" {
const noise = Noise.init(42);
// Sample at positions that are far apart for noticeable variation
const val1 = noise.fbm2D(0.5, 0.5, 4, 2.0, 0.5, 0.01);
const val2 = noise.fbm2D(50.5, 50.5, 4, 2.0, 0.5, 0.01);
const val3 = noise.fbm2D(100.5, 100.5, 4, 2.0, 0.5, 0.01);
// At least two of three values should differ (noise is continuous)
const all_same = (val1 == val2) and (val2 == val3);
try testing.expect(!all_same);
}
test "Noise fbm2DNormalized range" {
const noise = Noise.init(42);
var y: f32 = 0;
while (y < 10) : (y += 0.5) {
var x: f32 = 0;
while (x < 10) : (x += 0.5) {
const val = noise.fbm2DNormalized(x, y, 4, 2.0, 0.5, 0.1);
// Normalized should be in [0, 1]
try testing.expect(val >= 0.0);
try testing.expect(val <= 1.0);
}
}
}
test "Noise ridged2D range" {
const noise = Noise.init(42);
var y: f32 = 0;
while (y < 10) : (y += 0.5) {
var x: f32 = 0;
while (x < 10) : (x += 0.5) {
const val = noise.ridged2D(x, y, 4, 2.0, 0.5, 0.1);
// Ridged should be in [0, 1]
try testing.expect(val >= 0.0);
try testing.expect(val <= 1.0);
}
}
}
test "Noise handles large coordinates" {
const noise = Noise.init(42);
// Large coordinates should not panic (used i64 internally to avoid overflow)
const val = noise.perlin2D(100000.5, -100000.5);
try testing.expect(val >= -1.0 and val <= 1.0);
}
test "Noise getHeight returns normalized value" {
const noise = Noise.init(42);
const height = noise.getHeight(10.0, 20.0, 64.0);
try testing.expect(height >= 0.0 and height <= 1.0);
}
// ============================================================================
// NoiseParams Tests (Issue #104)
// ============================================================================
const noise_mod = @import("world/worldgen/noise.zig");
test "Vec3f init and uniform" {
const v1 = noise_mod.Vec3f.init(1.0, 2.0, 3.0);
try testing.expectEqual(@as(f32, 1.0), v1.x);
try testing.expectEqual(@as(f32, 2.0), v1.y);
try testing.expectEqual(@as(f32, 3.0), v1.z);
const v2 = noise_mod.Vec3f.uniform(500.0);
try testing.expectEqual(@as(f32, 500.0), v2.x);
try testing.expectEqual(@as(f32, 500.0), v2.y);
try testing.expectEqual(@as(f32, 500.0), v2.z);
}
test "NoiseParams default values" {
const params = noise_mod.NoiseParams{ .seed = 12345 };
try testing.expectEqual(@as(f32, 0), params.offset);
try testing.expectEqual(@as(f32, 1), params.scale);
try testing.expectEqual(@as(f32, 600), params.spread.x);
try testing.expectEqual(@as(f32, 600), params.spread.y);
try testing.expectEqual(@as(f32, 600), params.spread.z);
try testing.expectEqual(@as(u16, 4), params.octaves);
try testing.expectEqual(@as(f32, 0.5), params.persist);
try testing.expectEqual(@as(f32, 2.0), params.lacunarity);
try testing.expect(params.flags.eased);
try testing.expect(!params.flags.absvalue);
}
test "NoiseParams frequency from spread" {
const params = noise_mod.NoiseParams{
.seed = 12345,
.spread = noise_mod.Vec3f.uniform(500),
};
try testing.expectApproxEqAbs(@as(f32, 0.002), params.getFrequency2D(), 0.0001);
// Anisotropic spread
const params2 = noise_mod.NoiseParams{
.seed = 12345,
.spread = noise_mod.Vec3f.init(250, 350, 250),
};
const freq3d = params2.getFrequency3D();
try testing.expectApproxEqAbs(@as(f32, 1.0 / 250.0), freq3d.x, 0.0001);
try testing.expectApproxEqAbs(@as(f32, 1.0 / 350.0), freq3d.y, 0.0001);
try testing.expectApproxEqAbs(@as(f32, 1.0 / 250.0), freq3d.z, 0.0001);
}
test "NoiseFlags packed struct size" {
// NoiseFlags should be 1 byte
try testing.expectEqual(@as(usize, 1), @sizeOf(noise_mod.NoiseFlags));
}
test "ConfiguredNoise init" {
const params = noise_mod.NoiseParams{
.seed = 42,
.scale = 70.0,
.offset = 4.0,
.spread = noise_mod.Vec3f.uniform(600),
.octaves = 5,
.persist = 0.6,
};
const cn = noise_mod.ConfiguredNoise.init(params);
try testing.expectEqual(@as(u64, 42), cn.params.seed);
try testing.expectEqual(@as(f32, 70.0), cn.params.scale);
try testing.expectEqual(@as(f32, 4.0), cn.params.offset);
}
test "ConfiguredNoise get2D returns value with offset and scale" {
const params = noise_mod.NoiseParams{
.seed = 42,
.scale = 10.0,
.offset = 5.0,
.spread = noise_mod.Vec3f.uniform(100),
.octaves = 3,
};
const cn = noise_mod.ConfiguredNoise.init(params);
const val = cn.get2D(100, 100);
// FBM output is roughly -1 to 1
// After scale and offset: offset + scale * fbm = 5 + 10 * [-1,1] = [-5, 15]
try testing.expect(val >= -10 and val <= 20);
}
test "ConfiguredNoise absvalue flag produces non-negative" {
const params = noise_mod.NoiseParams{
.seed = 42,
.scale = 1.0,
.offset = 0,
.spread = noise_mod.Vec3f.uniform(50),
.flags = .{ .absvalue = true },
};
const cn = noise_mod.ConfiguredNoise.init(params);
// Sample multiple points - all should be >= 0
var all_non_negative = true;
var x: f32 = 0;
while (x < 100) : (x += 10) {
var z: f32 = 0;
while (z < 100) : (z += 10) {
const val = cn.get2D(x, z);
if (val < 0) all_non_negative = false;
}
}
try testing.expect(all_non_negative);
}
test "ConfiguredNoise get3D anisotropic spread" {
const params = noise_mod.NoiseParams{
.seed = 42,
.scale = 1.0,
.offset = 0,
.spread = noise_mod.Vec3f.init(250, 350, 250),
.octaves = 3,
};
const cn = noise_mod.ConfiguredNoise.init(params);
const val = cn.get3D(100, 50, 100);
// Should be in valid range
try testing.expect(val >= -2 and val <= 2);
}
test "ConfiguredNoise get2DNormalized returns 0-1 range" {
const params = noise_mod.NoiseParams{
.seed = 42,
.scale = 1.0,
.offset = 0,
.spread = noise_mod.Vec3f.uniform(100),
.octaves = 4,
};
const cn = noise_mod.ConfiguredNoise.init(params);
var x: f32 = 0;
while (x < 50) : (x += 5) {
var z: f32 = 0;
while (z < 50) : (z += 5) {
const val = cn.get2DNormalized(x, z);
try testing.expect(val >= 0.0);
try testing.expect(val <= 1.0);
}
}
}
test "ConfiguredNoise get2DRidged produces ridged output" {
const params = noise_mod.NoiseParams{
.seed = 42,
.scale = 1.0,
.offset = 0,
.spread = noise_mod.Vec3f.uniform(100),
.octaves = 4,
};
const cn = noise_mod.ConfiguredNoise.init(params);
// Ridged noise should be in [0, 1] range regardless of flags
var x: f32 = 0;
while (x < 50) : (x += 5) {
var z: f32 = 0;
while (z < 50) : (z += 5) {
const val = cn.get2DRidged(x, z);
try testing.expect(val >= 0.0);
try testing.expect(val <= 1.0);
}
}
}
test "ConfiguredNoise determinism with same params" {
const params = noise_mod.NoiseParams{
.seed = 12345,
.scale = 50.0,
.offset = 10.0,
.spread = noise_mod.Vec3f.uniform(300),
.octaves = 5,
.persist = 0.6,
};
const cn1 = noise_mod.ConfiguredNoise.init(params);
const cn2 = noise_mod.ConfiguredNoise.init(params);
const val1 = cn1.get2D(123.456, 789.012);
const val2 = cn2.get2D(123.456, 789.012);
try testing.expectEqual(val1, val2);
}
// ============================================================================
// WorldGen Determinism Tests
// ============================================================================
const OverworldGenerator = @import("world/worldgen/overworld_generator.zig").OverworldGenerator;
const deco_registry = @import("world/worldgen/decoration_registry.zig");
const Generator = @import("world/worldgen/generator_interface.zig").Generator;
fn chunkFingerprint(chunk: *const Chunk) u64 {
var hasher = std.hash.Wyhash.init(0);
hasher.update(std.mem.asBytes(&chunk.blocks));
hasher.update(std.mem.asBytes(&chunk.biomes));
hasher.update(std.mem.asBytes(&chunk.heightmap));
return hasher.final();
}
test "WorldGen same seed produces identical blocks at origin" {
const allocator = testing.allocator;
var gen1 = OverworldGenerator.init(12345, allocator, deco_registry.StandardDecorationProvider.provider());
var gen2 = OverworldGenerator.init(12345, allocator, deco_registry.StandardDecorationProvider.provider());
var chunk1 = Chunk.init(0, 0);
var chunk2 = Chunk.init(0, 0);
gen1.generate(&chunk1, null);
gen2.generate(&chunk2, null);
try testing.expectEqualSlices(BlockType, &chunk1.blocks, &chunk2.blocks);
}
test "WorldGen same seed produces identical biomes at origin" {
const allocator = testing.allocator;
var gen1 = OverworldGenerator.init(12345, allocator, deco_registry.StandardDecorationProvider.provider());
var gen2 = OverworldGenerator.init(12345, allocator, deco_registry.StandardDecorationProvider.provider());
var chunk1 = Chunk.init(0, 0);
var chunk2 = Chunk.init(0, 0);
gen1.generate(&chunk1, null);
gen2.generate(&chunk2, null);
try testing.expectEqualSlices(BiomeId, &chunk1.biomes, &chunk2.biomes);
}
test "WorldGen same seed produces identical blocks at different positions" {
const allocator = testing.allocator;
const seed: u64 = 54321;
var gen1 = OverworldGenerator.init(seed, allocator, deco_registry.StandardDecorationProvider.provider());
var chunk1a = Chunk.init(0, 0);
var chunk1b = Chunk.init(1, 0);
var chunk1c = Chunk.init(0, 1);
gen1.generate(&chunk1a, null);
gen1.generate(&chunk1b, null);
gen1.generate(&chunk1c, null);
var gen2 = OverworldGenerator.init(seed, allocator, deco_registry.StandardDecorationProvider.provider());
var chunk2a = Chunk.init(0, 0);
var chunk2b = Chunk.init(1, 0);
var chunk2c = Chunk.init(0, 1);
gen2.generate(&chunk2a, null);
gen2.generate(&chunk2b, null);
gen2.generate(&chunk2c, null);
try testing.expectEqualSlices(BlockType, &chunk1a.blocks, &chunk2a.blocks);
try testing.expectEqualSlices(BlockType, &chunk1b.blocks, &chunk2b.blocks);
try testing.expectEqualSlices(BlockType, &chunk1c.blocks, &chunk2c.blocks);
try testing.expect(!std.mem.eql(BlockType, &chunk1a.blocks, &chunk1b.blocks));
try testing.expect(!std.mem.eql(BlockType, &chunk1a.blocks, &chunk1c.blocks));
}