-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvector_FP_test.go
More file actions
158 lines (128 loc) · 3.68 KB
/
vector_FP_test.go
File metadata and controls
158 lines (128 loc) · 3.68 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package govector
/* File Prolouge:
Because there is no need to access private variables here, then we use Unit
*/
import (
"testing"
)
//testStruct is a non sensical struct. This is just to see if testVector works with struts
type testStruct struct {
id string
price int
}
func TestMap(t *testing.T) {
var testVector Vector
testVector = testVector.Init()
for i := 0; i < 100000; i++ {
testVector.PushBack(i)
}
if testVector.Size() != 100000 {
t.Errorf("Not enough Elements : %d", testVector.Size())
}
//testing if the default behavior works
defaultCopy := testVector.FpMap(nil)
//testing if the closured like behavior works
closureCopy := testVector.FpMap(func(num T) T {
return num.(int) * 2
})
for i := 0; i < closureCopy.Size(); i++ {
if closureCopy.At(i) != testVector.At(i).(int)*2 {
t.Error("Mismatching values with closure copy")
}
if defaultCopy.At(i) != testVector.At(i) {
t.Error("Mismatching values with default copy")
}
}
}
func TestFpReduce(t *testing.T) {
var testVector Vector
testVector = testVector.Init()
for i := 0; i < 5; i++ {
testVector.PushBack(i)
}
if testVector.Size() != 5 {
t.Errorf("Not enough Elements : %d", testVector.Size())
}
//testing default behavior
var reduced = testVector.FpReduce(nil).(int)
if reduced != 10 {
t.Errorf("Reduced supposed to be 10 not %d", reduced)
}
//testing struct behavior
var structVector Vector
structVector = structVector.Init()
var structure1 = testStruct{id: "it's half of over 9000", price: 4501}
var structure2 = testStruct{id: "it's half of 9000", price: 4500}
structVector.PushBack(structure1)
structVector.PushBack(structure2)
var structReduced = structVector.FpReduce(func(v1 T, v2 T) T {
return v1.(testStruct).price + v2.(testStruct).price
}).(int)
if structReduced <= 9000 {
t.Error("Struct Reduce is not over 9000!!!!! >:(")
}
}
func TestFpFilter(t *testing.T) {
var testVector Vector
testVector = testVector.Init()
//size should be 5
for i := 0; i < 5; i++ {
testVector.PushBack(i)
}
var filteredVector = testVector.FpFilter(func(val T) bool {
return val.(int) < 2
})
if filteredVector.Size() != 2 {
t.Errorf("Vecotr size supoosed to be 2 not %d", filteredVector.Size())
}
if filteredVector.At(0) != 0 && filteredVector.At(1) != 1 {
t.Error("Vector incorrectly filtered itself")
}
//testing struct behavior
var structVector Vector
structVector = structVector.Init()
var structure1 = testStruct{id: "it's half of over 9000", price: 4501}
var structure2 = testStruct{id: "it's half of 9000", price: 4500}
structVector.PushBack(structure1)
structVector.PushBack(structure2)
filteredVector = structVector.FpFilter(func(val T) bool {
return val.(testStruct).price > 4500
})
if filteredVector.Size() != 1 {
t.Error("Filtered Vector incorrectly filtered vector")
}
}
func TestPopBack(t *testing.T) {
/*
This tests the popback. The result shoudl be left with a vector with a size of 1 and
*/
var testVector Vector
testVector = testVector.Init()
testVector.PushBack(9001)
testVector.PushBack(80)
testVector.PopBack()
if testVector.At(0) != 9001 {
t.Error("Value removed is incorrect ")
}
if testVector.Size() > 1 {
t.Error("testVector did not remove element")
}
}
func TestFpIndexOf(t *testing.T) {
/*
This sees whether it returns the correct index in the functional programing indexOf funciton
*/
var testVector Vector
testVector = testVector.Init()
testVector.PushBack(9001)
testVector.PushBack(80)
testVector.PushBack(4500)
testVector.PushBack(324)
//called index1 because it's the desired index
index1 := testVector.FpIndexOf(func(value T) bool {
return value.(int) == 80
})
if index1 != 1 {
t.Error("Wrong index returned")
}
}