unit UTestArt; {$mode objfpc}{$H+} interface uses Classes, SysUtils, fpcunit, testregistry, UEnhancedMemoryStream, UGenericIndex, UMulBlock, UArt; type TTestArt = class(TTestCase) private function BuildLandBlock: TMemoryStream; function BuildStaticBlock: TMemoryStream; function LandBlockSize: Integer; published procedure LandRoundtripIsByteIdentical; procedure StaticRoundtripIsByteIdentical; procedure LandRejectsWrongDimensions; procedure StaticRejectsEmptyDimensions; end; implementation function TTestArt.LandBlockSize: Integer; begin //22 rows of 2,4,...,44 words plus 22 rows of 44,42,...,2 words Result := (2 + 44) * 22 * 2; end; function TTestArt.BuildLandBlock: TMemoryStream; var x, y: Integer; startCol, words, i: Integer; color: Word; begin Result := TMemoryStream.Create; for y := 0 to 43 do begin if y < 22 then begin startCol := 21 - y; words := (y + 1) * 2; end else begin startCol := y - 22; words := (44 - y) * 2; end; for i := 0 to words - 1 do begin x := startCol + i; //deterministic pattern, alpha bit set like real land tiles; channel //values are 0 or 31 so they survive the 16 to 32 to 16 bit conversion case (x + y) mod 4 of 0: color := $8000; 1: color := $8000 or $7C00; 2: color := $8000 or $03E0; 3: color := $8000 or $001F; end; Result.Write(color, SizeOf(Word)); end; end; end; function TTestArt.BuildStaticBlock: TMemoryStream; var row, i: Integer; color: Word; offset, runLen: Word; lookup: array[0..3] of Word; begin Result := TMemoryStream.Create; Result.WriteDWord(1); //header Result.WriteWord(8); //width Result.WriteWord(4); //height //row offsets in words, relative to the start of the pixel data //row length: offset + run + 3 colors + end marker = 14 bytes = 7 words //last row: offset + run + 2 colors + end marker = 12 bytes lookup[0] := 0; lookup[1] := 7; lookup[2] := 14; lookup[3] := 21; for i := 0 to 3 do Result.WriteWord(lookup[i]); for row := 0 to 3 do begin if row < 3 then begin offset := 0; runLen := 3; end else begin offset := 1; runLen := 2; end; Result.Write(offset, SizeOf(Word)); Result.Write(runLen, SizeOf(Word)); for i := 0 to runLen - 1 do begin //channels 0 or 31 survive the 16 to 32 to 16 bit conversion case (row + i) mod 4 of 0: color := $0000; 1: color := $7C00; 2: color := $03E0; 3: color := $001F; end; Result.Write(color, SizeOf(Word)); end; offset := 0; runLen := 0; Result.Write(offset, SizeOf(Word)); Result.Write(runLen, SizeOf(Word)); end; end; procedure TTestArt.LandRoundtripIsByteIdentical; var block, output: TMemoryStream; index: TGenericIndex; art: TArt; decoded, encoded: PByteArray; i: Integer; begin block := BuildLandBlock; try index := TGenericIndex.Create(nil); try index.Lookup := 0; index.Size := block.Size; block.Position := 0; art := TArt.Create(block, index, atLand); try AssertEquals('decoded size matches block', Int64(LandBlockSize), Int64(art.GetSize)); output := TMemoryStream.Create; try art.Write(output); AssertEquals('re-encoded size', Int64(LandBlockSize), Int64(output.Size)); decoded := block.Memory; encoded := output.Memory; for i := 0 to LandBlockSize - 1 do if decoded^[i] <> encoded^[i] then begin AssertEquals('pixel data at byte ' + IntToStr(i), decoded^[i], encoded^[i]); Break; end; AssertTrue('roundtrip must be byte identical', True); finally output.Free; end; finally art.Free; end; finally index.Free; end; finally block.Free; end; end; procedure TTestArt.StaticRoundtripIsByteIdentical; var block, output: TMemoryStream; index: TGenericIndex; art: TArt; decoded, encoded: PByteArray; i: Integer; begin block := BuildStaticBlock; try index := TGenericIndex.Create(nil); try index.Lookup := 0; index.Size := block.Size; block.Position := 0; art := TArt.Create(block, index, atStatic); try AssertEquals('decoded size matches block', Int64(block.Size), Int64(art.GetSize)); output := TMemoryStream.Create; try art.Write(output); AssertEquals('re-encoded size', Int64(block.Size), Int64(output.Size)); decoded := block.Memory; encoded := output.Memory; for i := 0 to block.Size - 1 do if decoded^[i] <> encoded^[i] then begin AssertEquals('pixel data at byte ' + IntToStr(i), decoded^[i], encoded^[i]); Break; end; AssertTrue('roundtrip must be byte identical', True); finally output.Free; end; finally art.Free; end; finally index.Free; end; finally block.Free; end; end; procedure TTestArt.LandRejectsWrongDimensions; var block: TMemoryStream; index: TGenericIndex; art: TArt; begin block := TMemoryStream.Create; //not 44x44 worth of data try index := TGenericIndex.Create(nil); try index.Lookup := 0; index.Size := 0; art := TArt.Create(nil, index, atLand); try AssertEquals('empty output for invalid dimensions', Int64(0), Int64(art.GetSize)); finally art.Free; end; finally index.Free; end; finally block.Free; end; end; procedure TTestArt.StaticRejectsEmptyDimensions; var index: TGenericIndex; art: TArt; begin index := TGenericIndex.Create(nil); try index.Lookup := 0; index.Size := 0; art := TArt.Create(nil, index, atStatic); try AssertEquals('empty output for empty dimensions', Int64(0), Int64(art.GetSize)); finally art.Free; end; finally index.Free; end; end; initialization RegisterTests([TTestArt]); end.