-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathmemModel_test.go
89 lines (79 loc) · 1.82 KB
/
memModel_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
package bTree
import (
"bytes"
"encoding/binary"
"encoding/gob"
"fmt"
"testing"
)
func TestDiscModel(t *testing.T) {
d := newDisc()
for i := 0; i < 8; i++ {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, uint32(i))
d.write(i, buf.Bytes())
}
for i := 0; i < 8; i++ {
data, _ := d.read(i)
buf := bytes.NewBuffer(data.([]byte))
var dataI uint32
binary.Read(buf, binary.LittleEndian, &dataI)
if uint32(i) != dataI {
t.Log(i, dataI)
t.Fail()
}
}
}
type testCacheModel struct {
cacheModel
}
func (c *testCacheModel) init(size int, downStreamModel access) *testCacheModel {
c.cacheModel.init(size, downStreamModel, c)
return c
}
func (c *testCacheModel) encIdx(id interface{}) interface{} {
return int(id.(uint32))
}
func (c *testCacheModel) decIdx(id interface{}) interface{} {
return uint32(id.(int))
}
func (c *testCacheModel) encData(data interface{}) interface{} {
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
enc.Encode(data)
return buf.Bytes()
}
func (c *testCacheModel) decData(data interface{}) interface{} {
var result string
buf := bytes.NewBuffer(data.([]byte))
dec := gob.NewDecoder(buf)
dec.Decode(&result)
return result
}
func newTestCacheModel(size int, downStreamModel access) *testCacheModel {
return new(testCacheModel).init(size, downStreamModel)
}
func TestCacheModel(t *testing.T) {
d := newDisc()
c := newTestCacheModel(4, d)
for i := 0; i < 8; i++ {
c.write(uint32(i), string(i))
}
//check data
for i := 0; i < 8; i++ {
data, _ := c.read(uint32(i))
if string(i) != data {
t.Log(i, data)
t.Fail()
}
}
//check pq status
expOrder := []int{7, 6, 5, 4}
for item, i := c.pq.Front(), 0; item != nil; item = item.Next() {
if item.Value.(*cacheItem).key != expOrder[i] {
t.Log(i, fmt.Sprintf("%+v", item.Value))
t.Fail()
}
i++
}
}