-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mpq_test.go
133 lines (117 loc) · 2.65 KB
/
mpq_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
package mpq
import (
"bytes"
"fmt"
"io/ioutil"
"path"
"strings"
"testing"
"time"
)
func TestSpeed(t *testing.T) {
const N = 10
files := []string{
"replay.details",
"replay.initData",
"replay.attributes.events",
"replay.message.events",
"replay.game.events",
"replay.tracker.events",
"(attributes)",
}
start := time.Now()
for i := 0; i < N; i++ {
m, _ := NewFromFile("reps/automm.SC2Replay")
for _, file := range files {
m.FileByName(file)
}
m.Close()
}
println(time.Since(start)/1000000/N, "ms")
}
func TestReps(t *testing.T) {
testFolder(t, "reps")
}
func testFolder(t *testing.T, folder string) {
fis, err := ioutil.ReadDir(folder)
if err != nil {
t.Errorf("Can't read folder: %s, error: %v", folder, err)
return
}
for _, fi := range fis {
if fi.IsDir() || !strings.HasSuffix(fi.Name(), ".SC2Replay") {
continue
}
name := path.Join(folder, fi.Name())
testFile(t, name)
}
}
func testFile(t *testing.T, name string) {
fmt.Println("Testing file", name)
// 2 rounds: from file and from memory
for i := 0; i < 2; i++ {
var m *MPQ
var err error
if i == 0 {
m, err = NewFromFile(name)
} else {
var content []byte
content, err = ioutil.ReadFile(name)
if err != nil {
t.Errorf("Failed to read input file: %s, error: %v", name, err)
}
m, err = New(bytes.NewReader(content))
if err != nil {
t.Errorf("Failed to read from memory buffer, error: %v", err)
}
}
if err != nil {
t.Errorf("Can't parse MPQ: %s, error: %v", name, err)
return
}
func() {
defer m.Close()
testMpq(t, name, m)
}()
}
}
func testMpq(t *testing.T, name string, m *MPQ) {
files := []string{
"replay.details",
"replay.initData",
"replay.attributes.events",
"replay.message.events",
"replay.game.events",
"replay.tracker.events",
"replay.sync.events",
"replay.smartcam.events",
"replay.load.info",
"replay.resumable.events",
"replay.server.battlelobby",
"(attributes)",
"(listfile)",
}
for _, file := range files {
if data, err := m.FileByName(file); err != nil {
t.Errorf("Error getting file '%s' from archive: %s, error: %v", name, file, err)
} else if data == nil {
fmt.Println("\tFile not present in archive:", file)
}
}
}
func TestInvalid(t *testing.T) {
names := []string{
"reps/invalid.SC2Replay_",
"I-DONT-EXIST.SC2Replay",
}
for _, name := range names {
m, err := NewFromFile(name)
if m != nil || err == nil {
t.Errorf("Parse should have failed but it succeeded: %s, error: %v", name, err)
}
}
m, err := New(bytes.NewReader([]byte("INVALID")))
if m != nil || err == nil {
t.Errorf("Parse should have failed but it succeeded: error: %v", err)
}
}