-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsimple-tiled-model_test.go
135 lines (122 loc) · 3.63 KB
/
simple-tiled-model_test.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package wfc
import (
"encoding/json"
// "fmt"
"github.com/shawnridgeway/wfc/internal/testutils"
"image"
"io/ioutil"
"strconv"
"testing"
)
// Parsed data supplied by user
type RawData struct {
Path string `json:"path"` // Path to tiles
Unique bool `json:"unique"` // Default to false
TileSize int `json:"tileSize"` // Default to 16
Tiles []RawTile `json:"tiles"` //
Neighbors []RawNeighbor `json:"neighbors"` //
}
// Raw information on a tile
type RawTile struct {
Name string `json:"name"` // Name used to identify the tile
Symmetry string `json:"symmetry"` // Default to ""
Weight float64 `json:"weight"` // Default to 1
}
// Information on which tiles can be neighbors
type RawNeighbor struct {
Left string `json:"left"` // Mathces Tile.Name
LeftNum int `json:"leftNum"` // Default to 0
Right string `json:"right"` // Mathces Tile.Name
RightNum int `json:"rightNum"` // Default to 0
}
func initiateData(dataFileName string) SimpleTiledData {
// Load data file
dataFile, err := ioutil.ReadFile("internal/input/" + dataFileName)
if err != nil {
panic(err)
}
// Parse rawData file
var rawData RawData
if err := json.Unmarshal(dataFile, &rawData); err != nil {
panic(err)
}
// Marshal into data settings struct
tiles := make([]Tile, len(rawData.Tiles))
for i, rt := range rawData.Tiles {
imgs := make([]image.Image, 0)
if rawData.Unique {
i := 1
for {
if img, err := testutils.LoadImage("internal/input/" + rawData.Path + rt.Name + " " + strconv.Itoa(i) + ".png"); err == nil {
imgs = append(imgs, img)
} else {
break
}
i++
}
} else {
img, err := testutils.LoadImage("internal/input/" + rawData.Path + rt.Name + ".png")
if err != nil {
panic(err)
}
imgs = append(imgs, img)
}
weight := rt.Weight
if weight == 0 {
weight = 1
}
tiles[i] = Tile{Name: rt.Name, Symmetry: rt.Symmetry, Weight: weight, Variants: imgs}
}
neighboors := make([]Neighbor, len(rawData.Neighbors))
for i, rn := range rawData.Neighbors {
neighboors[i] = Neighbor{Left: rn.Left, LeftNum: rn.LeftNum, Right: rn.Right, RightNum: rn.RightNum}
}
return SimpleTiledData{Unique: rawData.Unique, TileSize: rawData.TileSize, Tiles: tiles, Neighbors: neighboors}
}
func simpleTiledTest(t *testing.T, dataFileName, snapshotFileName string, iterations int) {
// Set test parameters
periodic := false
width := 20
height := 20
seed := int64(42)
data := initiateData(dataFileName)
// Generate output image
var outputImg image.Image
success, finished := false, false
model := NewSimpleTiledModel(data, width, height, periodic)
model.SetSeed(seed)
if iterations == -1 {
outputImg, success = model.Generate()
if !success {
t.Log("Failed to generate image on the first try.")
t.FailNow()
}
} else {
outputImg, finished, _ = model.Iterate(iterations)
if finished {
t.Log("Test for incomplete state actually finished.")
t.FailNow()
}
}
// Save output
// err := testutils.SaveImage("internal/snapshots/"+snapshotFileName, outputImg)
// if err != nil {
// panic(err)
// }
// Test that files match
snapshotImg, err := testutils.LoadImage("internal/snapshots/" + snapshotFileName)
if err != nil {
panic(err)
}
areEqual := testutils.CompareImages(outputImg, snapshotImg)
if !areEqual {
t.Log("Output image is not the same as the snapshot image.")
t.FailNow()
}
}
func TestSimpleTiledGenerationCompletes(t *testing.T) {
simpleTiledTest(t, "castle_data.json", "castle.png", -1)
}
func TestSimpleTiledIterationIncomplete(t *testing.T) {
simpleTiledTest(t, "castle_data.json", "castle_incomplete.png", 5)
}