-
Notifications
You must be signed in to change notification settings - Fork 15
/
HeapPageWriteTest.java
136 lines (113 loc) · 4.23 KB
/
HeapPageWriteTest.java
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
package simpledb;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import junit.framework.JUnit4TestAdapter;
import org.junit.Before;
import org.junit.Test;
import simpledb.TestUtil.SkeletonFile;
import simpledb.systemtest.SimpleDbTestBase;
import simpledb.systemtest.SystemTestUtil;
public class HeapPageWriteTest extends SimpleDbTestBase {
private HeapPageId pid;
/**
* Set up initial resources for each unit test.
*/
@Before public void addTable() throws IOException {
this.pid = new HeapPageId(-1, -1);
Database.getCatalog().addTable(new SkeletonFile(-1, Utility.getTupleDesc(2)), SystemTestUtil.getUUID());
}
/**
* Unit test for HeapPage.isDirty()
*/
@Test public void testDirty() throws Exception {
TransactionId tid = new TransactionId();
HeapPage page = new HeapPage(pid, HeapPageReadTest.EXAMPLE_DATA);
page.markDirty(true, tid);
TransactionId dirtier = page.isDirty();
assertEquals(true, dirtier != null);
assertEquals(true, dirtier == tid);
page.markDirty(false, tid);
dirtier = page.isDirty();
assertEquals(false, dirtier != null);
}
/**
* Unit test for HeapPage.addTuple()
*/
@Test public void addTuple() throws Exception {
HeapPage page = new HeapPage(pid, HeapPageReadTest.EXAMPLE_DATA);
int free = page.getNumEmptySlots();
// NOTE(ghuo): this nested loop existence check is slow, but it
// shouldn't make a difference for n = 504 slots.
for (int i = 0; i < free; ++i) {
Tuple addition = Utility.getHeapTuple(i, 2);
page.insertTuple(addition);
assertEquals(free-i-1, page.getNumEmptySlots());
// loop through the iterator to ensure that the tuple actually exists
// on the page
Iterator<Tuple >it = page.iterator();
boolean found = false;
while (it.hasNext()) {
Tuple tup = it.next();
if (TestUtil.compareTuples(addition, tup)) {
found = true;
// verify that the RecordId is sane
assertEquals(page.getId(), tup.getRecordId().getPageId());
break;
}
}
assertTrue(found);
}
// now, the page should be full.
try {
page.insertTuple(Utility.getHeapTuple(0, 2));
throw new Exception("page should be full; expected DbException");
} catch (DbException e) {
// explicitly ignored
}
}
/**
* Unit test for HeapPage.deleteTuple() with false tuples
*/
@Test(expected=DbException.class)
public void deleteNonexistentTuple() throws Exception {
HeapPage page = new HeapPage(pid, HeapPageReadTest.EXAMPLE_DATA);
page.deleteTuple(Utility.getHeapTuple(2, 2));
}
/**
* Unit test for HeapPage.deleteTuple()
*/
@Test public void deleteTuple() throws Exception {
HeapPage page = new HeapPage(pid, HeapPageReadTest.EXAMPLE_DATA);
int free = page.getNumEmptySlots();
// first, build a list of the tuples on the page.
Iterator<Tuple> it = page.iterator();
LinkedList<Tuple> tuples = new LinkedList<Tuple>();
while (it.hasNext())
tuples.add(it.next());
Tuple first = tuples.getFirst();
// now, delete them one-by-one from both the front and the end.
int deleted = 0;
while (tuples.size() > 0) {
page.deleteTuple(tuples.removeFirst());
page.deleteTuple(tuples.removeLast());
deleted += 2;
assertEquals(free + deleted, page.getNumEmptySlots());
}
// now, the page should be empty.
try {
page.deleteTuple(first);
throw new Exception("page should be empty; expected DbException");
} catch (DbException e) {
// explicitly ignored
}
}
/**
* JUnit suite target
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(HeapPageWriteTest.class);
}
}