File: testspacefillingcurves.pas

package info (click to toggle)
castle-game-engine 5.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 164,776 kB
  • ctags: 30,841
  • sloc: pascal: 168,882; cpp: 1,340; objc: 730; makefile: 492; sh: 477; xml: 434; php: 1
file content (104 lines) | stat: -rw-r--r-- 2,912 bytes parent folder | download
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
{
  Copyright 2004-2014 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

unit TestSpaceFillingCurves;

interface

uses
  Classes, SysUtils, fpcunit, testutils, testregistry;

type
  TTestSpaceFillingCurves = class(TTestCase)
  published
    procedure TestSpaceFillingCurves;
  end;

implementation

uses CastleVectors, CastleImages, CastleSpaceFillingCurves, CastleUtils,
  CastleColors;

const
  Red3Byte: TVector3Byte = (255, 0, 0);
  Green3Byte: TVector3Byte = (0, 255, 0);

procedure TTestSpaceFillingCurves.TestSpaceFillingCurves;

  procedure TestCurve(CurveClass: TSpaceFillingCurveClass; Width, Height: Cardinal);
  { sprawdz ze curve wypelnia caly obrazek, i tylko caly obrazek,
    i kazdy pixel obrazka jest wypelniony dokladnie raz. }
  var Img: TCastleImage;
      Curve: TSpaceFillingCurve;
      pixCoords: TVector2Cardinal;
      pix: PVector3Byte;
      OutFileName: string;
  begin
   Curve := nil;
   Img := TRGBImage.Create(Width, Height);
   try
    try
     Curve := CurveClass.Create(Width, Height);

     Img.Clear(Vector4Byte(Green3Byte, 255));

     while not Curve.EndOfPixels do
     begin
      pixCoords := Curve.NextPixel;
      Assert(Between(pixCoords[0], 0, Width-1));
      Assert(Between(pixCoords[1], 0, Height-1));
      pix := PVector3Byte(Img.PixelPtr(pixCoords[0], pixCoords[1]));
      { kazdy pix moze byc podany tylko raz, czyli teraz pix powinien
	byc zielony. }
      Assert(CompareMem(pix, @Green3Byte, SizeOf(TVector3Byte)));
      pix^ := Red3Byte;
     end;

     { na koncu caly obrazek powinien byc czerwony }
     Assert(Img.IsClear(Vector4Byte(Red3Byte, 255)));
    except
     OutFileName := GetTempDir + '/test_space_filling_curves.ppm';
     SaveImage(Img, OutFileName);
     Writeln(Format('and it failed at curveClass %s, Width %d, Height %d',
       [CurveClass.ClassName, Width, Height]), nl,
       'dump written to ' + OutFileName);
     raise;
    end;
   finally
    Curve.Free;
    FreeAndNil(Img)
   end;
  end;

  procedure TestCurves(Width, Height: Cardinal);
  begin
   TestCurve(TSwapScanCurve, Width, Height);
   TestCurve(THilbertCurve, Width, Height);
   TestCurve(TPeanoCurve, Width, Height);
  end;

var i: integer;
begin
 for i := 1 to 20 do TestCurves(Random(100), Random(100));
 { perfidne testy sprawdzajace czy nasze curve'y radza sobie tez gdy jeden
   z Width, Height = 0 }
 TestCurves(0, 0);
 TestCurves(10, 0);
 TestCurves(0, 10);
end;

initialization
 RegisterTest(TTestSpaceFillingCurves);
end.