-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanimdata_test.go
More file actions
156 lines (126 loc) · 2.74 KB
/
animdata_test.go
File metadata and controls
156 lines (126 loc) · 2.74 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package cof
import (
"log"
"os"
"testing"
)
func TestLoad(t *testing.T) {
testFile, fileErr := os.Open("testdata/AnimData.d2")
if fileErr != nil {
t.Error("cannot open test data file")
return
}
data := make([]byte, 0)
buf := make([]byte, 16)
for {
numRead, err := testFile.Read(buf)
data = append(data, buf[:numRead]...)
if err != nil {
break
}
}
_, loadErr := Load(data)
if loadErr != nil {
t.Error(loadErr)
}
err := testFile.Close()
if err != nil {
t.Fail()
log.Print(err)
}
}
func TestLoad_BadData(t *testing.T) {
testFile, fileErr := os.Open("testdata/BadData.d2")
if fileErr != nil {
t.Error("cannot open test data file")
return
}
data := make([]byte, 0)
buf := make([]byte, 16)
for {
numRead, err := testFile.Read(buf)
data = append(data, buf[:numRead]...)
if err != nil {
break
}
}
_, loadErr := Load(data)
if loadErr == nil {
t.Error("bad data file should not be parsed")
}
err := testFile.Close()
if err != nil {
t.Fail()
log.Print(err)
}
}
func TestAnimationData_GetRecordNames(t *testing.T) {
animdata := &AnimationData{
hashTable: hashTable{},
blocks: [256]*block{},
entries: map[string][]*AnimationDataRecord{
"a": {{}},
"b": {{}},
"c": {{}},
},
}
names := animdata.GetRecordNames()
if len(names) != 3 {
t.Error("record name count mismatch")
}
}
func TestAnimationData_GetRecords(t *testing.T) {
animdata := &AnimationData{
hashTable: hashTable{},
blocks: [256]*block{},
entries: map[string][]*AnimationDataRecord{
"a": {
{name: "a", speed: 1, framesPerDirection: 1},
{name: "a", speed: 2, framesPerDirection: 2},
{name: "a", speed: 3, framesPerDirection: 3},
},
},
}
if len(animdata.GetRecords("a")) != 3 {
t.Error("record count is incorrect")
}
if len(animdata.GetRecords("b")) > 0 {
t.Error("retrieved records for unknown record name")
}
}
func TestAnimationData_GetRecord(t *testing.T) {
animdata := &AnimationData{
hashTable: hashTable{},
blocks: [256]*block{},
entries: map[string][]*AnimationDataRecord{
"a": {
{name: "a", speed: 1, framesPerDirection: 1},
{name: "a", speed: 2, framesPerDirection: 2},
{name: "a", speed: 3, framesPerDirection: 3},
},
},
}
record := animdata.GetRecord("a")
if record.speed != 3 {
t.Error("record returned is incorrect")
}
}
func TestAnimationDataRecord_FPS(t *testing.T) {
record := &AnimationDataRecord{}
var fps float64
record.speed = 256
fps = record.FPS()
if fps != float64(speedBaseFPS) {
t.Error("incorrect fps")
}
record.speed = 512
fps = record.FPS()
if fps != float64(speedBaseFPS)*2 {
t.Error("incorrect fps")
}
record.speed = 128
fps = record.FPS()
if fps != float64(speedBaseFPS)/2 {
t.Error("incorrect fps")
}
}