-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressor_test.go
69 lines (52 loc) · 1.25 KB
/
compressor_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
package compressor
import (
"bytes"
"os"
"testing"
)
var before *bytes.Buffer
var compressed bytes.Buffer
var after bytes.Buffer
// With more time, there would be more thorough unit testing and more tests
func TestMain(m *testing.M) {
testData := ""
for i := 0; i < 5000; i++ {
if i%7 == 0 {
testData += "a"
} else if i%3 == 0 {
testData += "b"
} else {
testData += "c"
}
}
before = bytes.NewBufferString(testData)
returnCode := m.Run()
os.Exit(returnCode)
}
func TestBuildFrequencyTable(t *testing.T) {
temp := before.Bytes()
e := encoder{}
e.rawData = &temp
e.frequencies = make(map[byte]int)
e.buildFrequencyTable()
if len(e.frequencies) != 3 {
t.Fatal("The encoder should build a table that has the correct number of characters")
}
if e.frequencies[byte(97)] != 715 {
t.Fatal("The encoder did not count the correct number of occurences for \"a\"")
}
}
func TestCompression(t *testing.T) {
temp := before.Bytes()
Encode(&temp, &compressed)
if compressed.Len() >= 5000 {
t.Fatal("The data was not compressed")
}
}
func TestDecompression(t *testing.T) {
compressedReader := bytes.NewReader(compressed.Bytes())
Decode(compressedReader, &after)
if after.Len() != 5000 {
t.Fatal("The data was not uncompressed")
}
}