-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.go
243 lines (191 loc) · 4.76 KB
/
unit_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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import(
"testing"
)
// func unique_time
func TestUniqueTime(t *testing.T) {
unique_time_ct = 0;
// get fake time
myTime := 0;
unique_time(&myTime);
// check that the time is 1
if myTime != 1 {
t.Errorf("f_time not working, set value to %d\n", myTime);
}
}
// func init_pt && init_ft
func TestPageTableInit(t *testing.T) {
pt := [NUM_PAGES]Page{};
init_pt(&pt)
// test stored values
if pt[0] != (Page{0, -1}) &&
pt[1] != (Page{1, -1}) &&
pt[2] != (Page{2, -1}) {
t.Errorf("Page initialization returned unexpected value %v\n", pt)
}
}
func TestFrameTableInit(t *testing.T) {
ft := [NUM_FRAMES]Frame{};
init_ft(&ft)
// test stored values
if ft[0] != (Frame{0, -1, 0, -1, -1}) &&
ft[1] != (Frame{1, -1, 0, -1, -1}) &&
ft[2] != (Frame{2, -1, 0, -1, -1}) {
t.Errorf("Frame initialization returned unexpected value %v\n", ft)
}
}
// func findEmptyFrame
func TestFindEmptyFrame_empty(t *testing.T) {
// create empty frame table
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
// find empty frame
vf_index, _ := findEmptyFrame(ft)
if vf_index != 0 {
t.Errorf("FindEmptyFrame returned %v, expected 0", vf_index)
}
}
func TestFindEmptyFrame_partial(t *testing.T) {
// create frame table with 1 entry
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
ft[0] = Frame{0, 3, 0, 0, 0}
// find empty frame
vf_index, _ := findEmptyFrame(ft)
if vf_index != 1 {
t.Errorf("FindEmptyFrame returned %v, expected 1", vf_index)
}
}
func TestFindEmptyFrame_full(t *testing.T) {
// create full frametable
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
for i := 0; i < NUM_FRAMES; i++ {
ft[i].pageNumber = i
}
// attempt finding empty frame
vf_index, _ := findEmptyFrame(ft)
if vf_index != -1 {
t.Errorf("FindEmptyFrame returned %v, expected %v",
vf_index, -1)
}
}
// func accessPage
func TestAccessPage_exists(t *testing.T) {
// mock page exists
pt := [NUM_PAGES]Page{}
init_pt(&pt)
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
unique_time_ct = 0
pt[2] = Page{2, 1}
ft[1] = Frame{1, 2, 0, -1, -1}
// run accesspage
if !accessPage(2, pt, &ft) {
t.Errorf("Access page did not find page, expected to find page")
}
}
func TestAccessPage_timeUpdate(t *testing.T) {
// mock page exists
pt := [NUM_PAGES]Page{}
init_pt(&pt)
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
unique_time_ct = 0
// run access page
pt[2] = Page{2, 1}
ft[1] = Frame{1, 2, 0, -1, -1}
accessPage(2, pt, &ft)
// check if time was updated
if ft[1].lastReference != 1 {
t.Errorf("Access page did not update page")
}
}
func TestAccessPage_pageFault(t *testing.T) {
// mock page exists
pt := [NUM_PAGES]Page{}
init_pt(&pt)
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
unique_time_ct = 0
// check that accessPage fails
if accessPage(2, pt, &ft) {
t.Errorf("Access page returned true and expected false")
}
}
// func fifo
func TestFIFO(t *testing.T) {
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
// mock ascending allocation times so last frame will be used
for i := 0; i < NUM_FRAMES; i++ {
ft[i].lastAllocation = NUM_FRAMES - i;
}
victim := fifo(ft)
if victim != NUM_FRAMES - 1 {
t.Errorf("fifo algorithm returned %v, expected %v",
victim, NUM_FRAMES - 1)
}
}
// func lru
func TestLRU(t *testing.T) {
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
// mock ascending allocation times so last frame will be used
for i := 0; i < NUM_FRAMES; i++ {
ft[i].lastReference = NUM_FRAMES - i;
}
victim := lru(ft)
if victim != NUM_FRAMES - 1 {
t.Errorf("lru algorithm returned %v, expected %v",
victim, NUM_FRAMES - 1)
}
}
// func handlePageFault
func TestHandlePageFault_empty(t *testing.T) {
// mock page exists
pt := [NUM_PAGES]Page{}
init_pt(&pt)
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
unique_time_ct = 0
handlePageFault(2, &pt, &ft, fifo)
// check that page was inserted
if !accessPage(2, pt, &ft) {
t.Errorf("handlePageFault did not replace %v", 2)
}
}
func TestHandlePageFault_full_fifo(t *testing.T) {
// mock page exists
pt := [NUM_PAGES]Page{}
init_pt(&pt)
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
unique_time_ct = 0
// mock ascending allocation times so last frame will be used
for i := 0; i < NUM_FRAMES; i++ {
ft[i].lastAllocation = NUM_FRAMES - i
ft[i].pageNumber = 1
}
handlePageFault(2, &pt, &ft, fifo)
// check that page was inserted
if pt[2].frameNumber != NUM_FRAMES - 1 {
t.Errorf("Handle Pagefault used pra incorrectly")
}
}
// func simulate
func TestSimulate(t *testing.T) {
// mock page exists
pt := [NUM_PAGES]Page{}
init_pt(&pt)
ft := [NUM_FRAMES]Frame{}
init_ft(&ft)
unique_time_ct = 0
for i := 0; i < 3; i++ {
simulate(i, &pt, &ft, fifo)
}
if pt[2] != (Page{2, 2}) || ft[2] != (Frame{2, 2, 1, 5, 6}) {
t.Errorf("Simulate failed, post state was PageTable: %v, FrameTable: %v",
pt, ft)
}
}