-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathentity_test.go
96 lines (87 loc) · 2.6 KB
/
entity_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
// Copyright © 2016-2018 Galvanized Logic Inc.
package vu
import (
"testing"
)
// check that the entity IDs are properly allocated.
func TestEntityIDs(t *testing.T) {
t.Run("zero is not a valid entityID", func(t *testing.T) {
ents := &entities{}
if ents.valid(0) {
t.Errorf("expecting invalid for unallocated entity")
}
if ents.valid(1) {
t.Errorf("expecting invalid for unallocated entity")
}
})
t.Run("first valid entityID is one", func(t *testing.T) {
ents := &entities{}
if one := ents.create(); one != 1 {
t.Errorf("expecting first eid to be 1")
}
})
t.Run("disposed eids are not valid", func(t *testing.T) {
ents := &entities{}
one := ents.create()
if !ents.valid(one) {
t.Errorf("expected valid eid:%d edition:%d", one.id(), one.edition())
}
ents.dispose(one)
if ents.valid(one) {
t.Errorf("expected invalid eid:%d edition:%d", one.id(), one.edition())
}
})
t.Run("allocate all entityIDs", func(t *testing.T) {
ents := &entities{}
for cnt := 1; cnt < maxEntID; cnt++ {
if id := ents.create(); int(id) != cnt {
t.Errorf("expecting initial ids to be allocated sequentially.")
}
}
// check that allocating one more than max returns zero.
if id := ents.create(); id != 0 {
t.Errorf("expecting to have exhausted entity ids")
}
})
t.Run("allocate more than max using dispose", func(t *testing.T) {
ents := &entities{}
for cnt := 1; cnt < maxEntID; cnt++ {
ents.create() // create max entities.
}
// should have allocated maxEntID at this point
// free 2*maxFree entities. Check that the free list can grow
// larger than the amount that triggers reuse.
for cnt := 1; cnt <= 2*maxFree; cnt++ {
ents.dispose(eID(cnt)) // should not crash.
}
if len(ents.free) != 2*maxFree {
t.Errorf("expected freelist %d to be %d", len(ents.free), 2*maxFree)
}
// should be able to reuse the disposed 2*maxFree entity IDs.
for cnt := 0; cnt < 2*maxFree; cnt++ {
eid := ents.create()
if eid == 0 {
t.Errorf("expecting to reuse disposed entity ids")
}
}
// check that one more than max is caught.
// Should also generate a design error log.
if eid := ents.create(); eid != 0 {
t.Errorf("expecting to have re-exhausted entity ids")
}
})
}
// Tests
// =============================================================================
// Benchmarks.
// go test -bench=.
// Hammer eids by creating and deleting as fast as possible.
// More of a stress test than a real usage case.
func BenchmarkCreateDelete(b *testing.B) {
ents := &entities{}
var id eID
for cnt := 0; cnt < b.N; cnt++ {
id = ents.create()
ents.dispose(id)
}
}