Debian Codegraph @

tests/code/testcases/testcastlespacefillingcurves.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestSpaceFillingCurves" -*-
2 {
3 Copyright 2004-2022 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Test CastleSpaceFillingCurves unit. }
18 unit TestCastleSpaceFillingCurves;
19
20 interface
21
22 uses
23 Classes, SysUtils, CastleTester;
24
25 type
26 TTestSpaceFillingCurves = class(TCastleTestCase)
27 published
28 procedure TestSpaceFillingCurves;
29 end;
30
31 implementation
32
33 uses CastleVectors, CastleImages, CastleInternalSpaceFillingCurves, CastleUtils,
34 CastleColors;
35
36 const
37 Red3Byte : TVector3Byte = (X: 255; Y: 0; Z: 0);
38 Green3Byte: TVector3Byte = (X: 0; Y: 255; Z: 0);
39 Red4Byte : TVector4Byte = (X: 255; Y: 0; Z: 0; W: 255);
40 Green4Byte: TVector4Byte = (X: 0; Y: 255; Z: 0; W: 255);
41
42 procedure TTestSpaceFillingCurves.TestSpaceFillingCurves;
43
44 procedure TestCurve(CurveClass: TSpaceFillingCurveClass; Width, Height: Cardinal);
45 { sprawdz ze curve wypelnia caly obrazek, i tylko caly obrazek,
46 i kazdy pixel obrazka jest wypelniony dokladnie raz. }
47 var Img: TCastleImage;
48 Curve: TSpaceFillingCurve;
49 pixCoords: TVector2Cardinal;
50 pix: PVector3Byte;
51 OutFileName: string;
52 begin
53 Curve := nil;
54 Img := TRGBImage.Create(Width, Height);
55 try
56 try
57 Curve := CurveClass.Create(Width, Height);
58
59 Img.Clear(Green4Byte);
60
61 while not Curve.EndOfPixels do
62 begin
63 pixCoords := Curve.NextPixel;
64 AssertTrue(Between(pixCoords[0], 0, Width-1));
65 AssertTrue(Between(pixCoords[1], 0, Height-1));
66 pix := PVector3Byte(Img.PixelPtr(pixCoords[0], pixCoords[1]));
67 { kazdy pix moze byc podany tylko raz, czyli teraz pix powinien
68 byc zielony. }
69 AssertTrue(CompareMem(pix, @Green3Byte, SizeOf(TVector3Byte)));
70 pix^ := Red3Byte;
71 end;
72
73 { na koncu caly obrazek powinien byc czerwony }
74 AssertTrue(Img.IsClear(Red4Byte));
75 except
76 OutFileName := GetTempDirectory + '/test_space_filling_curves.ppm';
77 SaveImage(Img, OutFileName);
78 Writeln(Format('and it failed at curveClass %s, Width %d, Height %d',
79 [CurveClass.ClassName, Width, Height]), nl,
80 'dump written to ' + OutFileName);
81 raise;
82 end;
83 finally
84 Curve.Free;
85 FreeAndNil(Img)
86 end;
87 end;
88
89 procedure TestCurves(Width, Height: Cardinal);
90 begin
91 TestCurve(TSwapScanCurve, Width, Height);
92 TestCurve(THilbertCurve, Width, Height);
93 TestCurve(TPeanoCurve, Width, Height);
94 end;
95
96 var i: integer;
97 begin
98 for i := 1 to 20 do TestCurves(Random(100), Random(100));
99 { perfidne testy sprawdzajace czy nasze curve'y radza sobie tez gdy jeden
100 z Width, Height = 0 }
101 TestCurves(0, 0);
102 TestCurves(10, 0);
103 TestCurves(0, 10);
104 end;
105
106 initialization
107 RegisterTest(TTestSpaceFillingCurves);
108 end.