-
Notifications
You must be signed in to change notification settings - Fork 5
/
flyweight_test.go
60 lines (54 loc) · 1.34 KB
/
flyweight_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
package flyweight
import (
"testing"
)
func TestFactoryCreatesObjects(t *testing.T) {
f := NewObjectFactory()
firstObject := f.GetObject(TYPE_ONE)
if firstObject == nil {
t.Error("The pointer to the TYPE_ONE was nil")
}
}
func TestFactoryCreatesTwoObjects(t *testing.T) {
f := NewObjectFactory()
_ = f.GetObject(TYPE_ONE)
secondObject := f.GetObject(TYPE_ONE)
if secondObject == nil {
t.Error("The pointer to the TYPE_ONE was nil")
}
}
func TestFactoryCreatesJustObjectOfTypes(t *testing.T) {
f := NewObjectFactory()
firstObject := f.GetObject(TYPE_ONE)
secondObject := f.GetObject(TYPE_ONE)
if firstObject != secondObject {
t.Error("TYPE_ONE pointers weren't the same")
}
}
func TestNumberOfObjectsIsAlwaysNumberOfTypeOfObjectCreated(t *testing.T) {
f := NewObjectFactory()
_ = f.GetObject(TYPE_ONE)
_ = f.GetObject(TYPE_ONE)
if f.GetNumberOfObjects() != 1 {
t.Errorf(
"The number of objects created was not 1: %d\n",
f.GetNumberOfObjects(),
)
}
}
func TestHighVolume(t *testing.T) {
f := NewObjectFactory()
objects := make([]*Object, 500000*2)
for i := 0; i < 500000; i++ {
objects[i] = f.GetObject(TYPE_ONE)
}
for i := 500000; i < 2*500000; i++ {
objects[i] = f.GetObject(TYPE_TWO)
}
if f.GetNumberOfObjects() != 2 {
t.Errorf(
"The number of objects created was not 2: %d\n",
f.GetNumberOfObjects(),
)
}
}