unit UTestLandscapeBounds; {$mode objfpc}{$H+} interface uses Classes, SysUtils, fpcunit, testregistry, UMap, UTiledata, ULandscape; type TTestLandscapeBounds = class(TTestCase) private FMap: TMemoryStream; FStatics: TMemoryStream; FStaIdx: TMemoryStream; FTiledata: TMemoryStream; FRadarColPath: string; FLandscape: TLandscape; procedure BuildStreams; procedure BuildLandscape; protected procedure SetUp; override; procedure TearDown; override; published procedure ValidStreamSizesValidate; procedure BlockSubscriptionAtOrigin; procedure BlockSubscriptionOutsideIsNil; procedure MapCellInsideBlock; procedure MapCellOutsideIsNil; end; implementation uses UEnhancedMemoryStream; procedure TTestLandscapeBounds.SetUp; begin FMap := nil; FStatics := nil; FStaIdx := nil; FTiledata := nil; FLandscape := nil; end; procedure TTestLandscapeBounds.TearDown; begin FreeAndNil(FLandscape); FreeAndNil(FMap); FreeAndNil(FStatics); FreeAndNil(FStaIdx); FreeAndNil(FTiledata); if FRadarColPath <> '' then DeleteFile(FRadarColPath); end; procedure TTestLandscapeBounds.BuildStreams; var i: Integer; begin //one map block: header + 64 cells (tile id word + altitude byte) FMap := TMemoryStream.Create; FMap.WriteDWord(0); for i := 0 to 63 do begin FMap.WriteWord(0); FMap.WriteByte(5); end; //one empty static index entry FStaIdx := TMemoryStream.Create; FStaIdx.WriteDWord(Cardinal($FFFFFFFF)); FStaIdx.WriteDWord(0); FStaIdx.WriteDWord(0); FStatics := TMemoryStream.Create; //legacy tiledata: 512 land groups worth of zeroed entries FTiledata := TMemoryStream.Create; for i := 0 to 512 * LandTileGroupSize - 1 do FTiledata.WriteByte(0); //radarcol: one color, must live on disk FRadarColPath := GetTempDir + 'centred-test-radarcol.mul'; with TFileStream.Create(FRadarColPath, fmCreate) do try WriteWord(Word($3173)); finally Free; end; end; procedure TTestLandscapeBounds.BuildLandscape; var valid: Boolean; begin FLandscape := TLandscape.Create(FMap, FStatics, FStaIdx, FTiledata, FRadarColPath, 1, 1, valid); AssertTrue('fixture landscape must validate', valid); end; procedure TTestLandscapeBounds.ValidStreamSizesValidate; begin BuildStreams; BuildLandscape; end; procedure TTestLandscapeBounds.BlockSubscriptionAtOrigin; begin BuildStreams; BuildLandscape; AssertTrue('subscription list exists for block 0,0', FLandscape.BlockSubscriptions[0, 0] <> nil); end; procedure TTestLandscapeBounds.BlockSubscriptionOutsideIsNil; begin BuildStreams; BuildLandscape; //x = width must be rejected (used to read past the array) AssertTrue('x outside must be nil', FLandscape.BlockSubscriptions[1, 0] = nil); //y = height must be rejected AssertTrue('y outside must be nil', FLandscape.BlockSubscriptions[0, 1] = nil); AssertTrue('both outside must be nil', FLandscape.BlockSubscriptions[1, 1] = nil); end; procedure TTestLandscapeBounds.MapCellInsideBlock; var cell: TMapCell; begin BuildStreams; BuildLandscape; cell := FLandscape.MapCell[0, 0]; AssertTrue('cell 0,0 exists', cell <> nil); AssertEquals('altitude from the fixture', 5, cell.Altitude); cell := FLandscape.MapCell[7, 7]; AssertTrue('cell 7,7 exists', cell <> nil); end; procedure TTestLandscapeBounds.MapCellOutsideIsNil; begin BuildStreams; BuildLandscape; AssertTrue('x = cell width is outside', FLandscape.MapCell[8, 0] = nil); AssertTrue('y = cell height is outside', FLandscape.MapCell[0, 8] = nil); end; initialization RegisterTests([TTestLandscapeBounds]); end.