-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBMTester.java
More file actions
261 lines (217 loc) · 9.08 KB
/
Copy pathBMTester.java
File metadata and controls
261 lines (217 loc) · 9.08 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import java.io.*;
public class BMTester
{
public static interface Testable
{
void test(BufferManager bufMgr, String filename) throws Exception;
}
public static class TestFailedException extends RuntimeException
{
public TestFailedException(String explanation)
{
super(explanation);
}
}
//----------------------------------------------------
// test 1
// Testing pinPage, unpinPage, and whether a dirty page
// is written to disk
//----------------------------------------------------
public static class Test1 implements Testable
{
public void test(BufferManager bufMgr, String filename)
throws Exception
{
int first = 5;
int last = first + bufMgr.poolSize() + 5;
// Allocate some pages
bufMgr.newPage(last+10,filename);
bufMgr.unpinPage(0,filename,false);
System.out.println("------- Test 1 -------");
for (int i=first; i<=last; i++)
{
Page page = bufMgr.pinPage(i,filename,false);
if (page == null)
throw new TestFailedException("Unable to pin page " +
"1st time");
System.out.println("after pinPage " + i);
byte[] data = ("This is test 1 for page " + i).getBytes();
System.arraycopy(data,0,page.data,0,data.length);
bufMgr.unpinPage(i,filename,true);
System.out.println("after unpinPage " + i);
}
System.out.println();
for (int i=first; i<=last; i++)
{
Page page = bufMgr.pinPage(i,filename,false);
if (page == null)
throw new TestFailedException("Unable to pin page " +
"2nd time");
String readBack = new String(page.data);
String orig = "This is test 1 for page " + i;
System.out.println("readback: " + readBack);
System.out.println("PAGE[" + i + "]: " +
readBack.substring(0,orig.length()));
if (!readBack.regionMatches(0,orig,0,orig.length()))
throw new TestFailedException("Page content incorrect");
bufMgr.unpinPage(i,filename,false);
}
}
}
//-----------------------------------------------------------
// test 2
// Testing replacement policy
//------------------------------------------------------------
public static class Test2 implements Testable
{
public void test(BufferManager bufMgr, String filename)
throws Exception
{
System.out.println("------- Test 2 -------");
// Allocate some pages
bufMgr.newPage(5*bufMgr.poolSize(),filename);
bufMgr.unpinPage(0,filename,false);
// Pin and unpin a series of pages, the first half are loved,
// the latter half are hated.
//Pin all pages in the buffer and unpin them in reverse order
//Clock will behave as MRU in that case
int frame[] = new int[bufMgr.poolSize()];
Page page = null;
for (int i=0; i<bufMgr.poolSize(); i++)
{
page = bufMgr.pinPage(i+5,filename,false);
if (page == null)
throw new TestFailedException("Unable to pin page");
frame[i] = bufMgr.findFrame(i+5,filename);
if (frame[i] < 0 || frame[i] >= bufMgr.poolSize())
throw new TestFailedException("Invalid frame returned");
System.out.println("Page " + (i+5) +" at frame " + frame[i] +
" is pinned.");
}
//Try pinning an extra page
page = bufMgr.pinPage(bufMgr.poolSize()+6,filename,false);
if (page != null)
throw new TestFailedException("Pinned page in full buffer");
//Start unpinning pages
for (int i=bufMgr.poolSize()-1; i>=0 ;i--)
{
bufMgr.unpinPage(i+5,filename,true);
System.out.println("Page " + (i+5) +" at frame " + frame[i] +
" is unpinned.");
}
//Start pinning a new set of pages again. The page frames
//should be exactly the same order as the previous one
//Clock in that case will resemble MRU
for (int i=bufMgr.poolSize(); i < 2*bufMgr.poolSize(); i++){
page = bufMgr.pinPage(i+5,filename,false);
if (page == null)
throw new TestFailedException("Unable to pin page");
int spot = bufMgr.findFrame(i+5,filename);
System.out.println("Page " + (i+5) + " pinned in frame "
+ spot);
if (spot != frame[i-bufMgr.poolSize()])
throw new TestFailedException("Frame number incorrect");
}
//Unpin half the pages in order
for (int i=bufMgr.poolSize(); i < 2*bufMgr.poolSize(); i+=2)
{
bufMgr.unpinPage(i+5,filename,true);
System.out.println("Page " + (i+5) +" at frame " +
frame[i-bufMgr.poolSize()] + " is unpinned.");
}
//Now, pin a new set of pages
//Again, it should resemble the previous sequence
//In this case, Clock behaves as LRU
for (int i=2*bufMgr.poolSize(); i < 3*bufMgr.poolSize(); i+=2)
{
page = bufMgr.pinPage(i+5,filename,false);
if (page == null)
throw new TestFailedException("Unable to pin page");
int spot = bufMgr.findFrame(i+5,filename);
bufMgr.unpinPage(i+5,filename,true);
bufMgr.unpinPage(i-bufMgr.poolSize()+6,filename,true);
System.out.println("Page "+(i+5)+" pinned in frame " + spot);
if (spot != frame[i-2*bufMgr.poolSize()])
throw new TestFailedException("Frame number incorrect");
}
}
}
public static class Test3 implements Testable
{
public void test(BufferManager bufMgr, String filename)
throws Exception
{
// Allocate some pages
bufMgr.newPage(20,filename);
System.out.println("------- Test 3 -------");
for (int i=0; i < 20; i++)
{
Page page = bufMgr.pinPage(i,filename,false);
if (page == null)
throw new TestFailedException("Unable to pin page " +
"1st time");
System.out.println("after pinPage " + i);
byte[] data = ("This is test 1 for page " + i).getBytes();
System.arraycopy(data,0,page.data,0,data.length);
bufMgr.unpinPage(i,filename,true);
}
if(bufMgr.pinPage(19, filename, false) == null) {
System.out.println("tried to pin when bufferpool is full.");
}
//flush all
bufMgr.flushAllPages();
Page page = new Page();
DBFile file = new DBFile(filename);
for (int i=0; i < 20; i++)
{
file.readPage(i, page);
String readBack = new String(page.data);
String orig = "This is test 1 for page " + i;
System.out.println("readback: " + readBack);
System.out.println("PAGE[" + i + "]: " +
readBack.substring(0,orig.length()));
if (!readBack.regionMatches(0,orig,0,orig.length()))
throw new TestFailedException("Page content incorrect");
}
//should throw page pinned exception.
//bufMgr.freePage(0, filename);
//bufMgr.unpinPage(0, filename, false);
//run without error
bufMgr.freePage(2,filename);
//should throw page not pinned execption.
//bufMgr.unpinPage(50, filename, true);
System.out.println();
}
}
public static final String FILENAME = "__testing";
public static final int NUMBUF = 20;
public static boolean runTest(Testable testObj)
{
boolean success = true;
DBFile dbfile = null;
try
{
dbfile = new DBFile(FILENAME,NUMBUF+500);
BufferManager bufMgr = new BufferManager(NUMBUF);
testObj.test(bufMgr,FILENAME);
}
catch (Exception e)
{
success = false;
e.printStackTrace();
}
DBFile.erase(FILENAME);
return success;
}
public static void main(String[] args)
{
System.out.println("Running buffer manager tests.");
DBFile.erase(FILENAME);
// Run the tests.
runTest(new Test1());
runTest(new Test2());
runTest(new Test3());
// Clean up
DBFile.erase(FILENAME);
}
}