summaryrefslogtreecommitdiff
path: root/src/LivingVillage.Desktop.Tests/P68MapSnapshotTests.fs
blob: 4109aaef05631caa183bf92c9f19ba1ae34c8c99 (plain)
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
namespace LivingVillage.Desktop.Tests

open Microsoft.VisualStudio.TestTools.UnitTesting
open LivingVillage.Desktop

/// P68:512×384 整图离线渲染(MapSnapshot)的可验收护栏。
/// 断言尺寸/要素像素存在性/确定性;调色板口径与 scripts/analyze-p68.py 一致。
[<TestClass>]
type P68MapSnapshotTests () =

    let map = MapGen.generateWithSize 512 384 (uint64 4242)

    let be32 (bytes: byte array) (offset: int) : int =
        (int bytes.[offset] <<< 24)
        ||| (int bytes.[offset + 1] <<< 16)
        ||| (int bytes.[offset + 2] <<< 8)
        ||| int bytes.[offset + 3]

    let countColor (rgb: byte array) (color: byte * byte * byte) : int =
        let r, g, b = color
        let mutable count = 0
        let mutable i = 0
        while i < rgb.Length do
            if rgb.[i] = r && rgb.[i + 1] = g && rgb.[i + 2] = b then count <- count + 1
            i <- i + 3
        count

    [<TestMethod>]
    member _.RenderPngHasFullMapDimensions () =
        let png = MapSnapshot.renderPng map 4
        let signature = [| 137uy; 80uy; 78uy; 71uy; 13uy; 10uy; 26uy; 10uy |]
        CollectionAssert.AreEqual(signature, png.[0..7])
        Assert.AreEqual<string>("IHDR", System.Text.Encoding.ASCII.GetString(png.[12..15]))
        Assert.AreEqual<int>(512 * 4, be32 png 16)
        Assert.AreEqual<int>(384 * 4, be32 png 20)

    [<TestMethod>]
    member _.RenderRgbContainsRiverRoadFarmAndBuildingPixels () =
        let rgb = MapSnapshot.renderRgb map 8
        Assert.AreEqual<int>(512 * 8 * 384 * 8 * 3, rgb.Length)
        Assert.IsTrue(countColor rgb (52uy, 110uy, 180uy) > 0, "river (water) pixels missing")
        Assert.IsTrue(
            countColor rgb (120uy, 168uy, 84uy) + countColor rgb (154uy, 186uy, 96uy) > 0,
            "farmland pixels missing")
        Assert.IsTrue(countColor rgb (214uy, 190uy, 128uy) > 0, "main-road pixels missing")
        Assert.IsTrue(countColor rgb (196uy, 176uy, 120uy) > 0, "path pixels missing")
        Assert.IsTrue(countColor rgb (58uy, 58uy, 70uy) > 0, "building roof pixels missing")

    [<TestMethod>]
    member _.RenderIsByteIdenticalAcrossRuns () =
        let first = MapSnapshot.renderPng map 8
        let second = MapSnapshot.renderPng (MapGen.generateWithSize 512 384 (uint64 4242)) 8
        CollectionAssert.AreEqual(first, second)
        Assert.IsTrue(MapSnapshot.renderPng map 4 <> first, "different scale must produce a different image")