-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTreeHeaderPageTest.java
188 lines (159 loc) · 4.98 KB
/
BTreeHeaderPageTest.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
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
package simpledb;
import simpledb.TestUtil.SkeletonFile;
import simpledb.systemtest.SimpleDbTestBase;
import simpledb.systemtest.SystemTestUtil;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import junit.framework.JUnit4TestAdapter;
public class BTreeHeaderPageTest extends SimpleDbTestBase {
private BTreePageId pid;
public static final byte[] EXAMPLE_DATA;
static {
EXAMPLE_DATA = BTreeHeaderPage.createEmptyPageData();
}
/**
* Set up initial resources for each unit test.
*/
@Before public void addTable() throws Exception {
this.pid = new BTreePageId(-1, -1, BTreePageId.HEADER);
Database.getCatalog().addTable(new SkeletonFile(-1, Utility.getTupleDesc(2)), SystemTestUtil.getUUID());
}
/**
* Unit test for BTreeHeaderPage.getId()
*/
@Test public void getId() throws Exception {
BTreeHeaderPage page = new BTreeHeaderPage(pid, EXAMPLE_DATA);
assertEquals(pid, page.getId());
}
/**
* Unit test for BTreeHeaderPage.getPrevPageId()
*/
@Test public void getPrevPageId() throws Exception {
BTreeHeaderPage page = new BTreeHeaderPage(pid, EXAMPLE_DATA);
assertTrue(page.getPrevPageId() == null);
}
/**
* Unit test for BTreeHeaderPage.getNextPageId()
*/
@Test public void getNextPageId() throws Exception {
BTreeHeaderPage page = new BTreeHeaderPage(pid, EXAMPLE_DATA);
assertTrue(page.getNextPageId() == null);
}
/**
* Unit test for BTreeHeaderPage.setPrevPageId()
*/
@Test public void setPrevPageId() throws Exception {
BTreeHeaderPage page = new BTreeHeaderPage(pid, EXAMPLE_DATA);
BTreePageId id = new BTreePageId(pid.getTableId(), 1, BTreePageId.HEADER);
page.setPrevPageId(id);
assertEquals(id, page.getPrevPageId());
id = new BTreePageId(pid.getTableId(), 1, BTreePageId.INTERNAL);
try {
page.setPrevPageId(id);
throw new Exception("should not be able to set prevPageId to internal node; expected DbException");
} catch (DbException e) {
// explicitly ignored
}
}
/**
* Unit test for BTreeHeaderPage.setNextPageId()
*/
@Test public void setNextPageId() throws Exception {
BTreeHeaderPage page = new BTreeHeaderPage(pid, EXAMPLE_DATA);
BTreePageId id = new BTreePageId(pid.getTableId(), 1, BTreePageId.HEADER);
page.setNextPageId(id);
assertEquals(id, page.getNextPageId());
id = new BTreePageId(pid.getTableId() + 1, 1, BTreePageId.HEADER);
try {
page.setNextPageId(id);
throw new Exception("should not be able to set nextPageId to a page from a different table; expected DbException");
} catch (DbException e) {
// explicitly ignored
}
}
/**
* Unit test for BTreeHeaderPage.numSlots()
*/
@Test public void numSlots() throws Exception {
assertEquals(32704, BTreeHeaderPage.getNumSlots());
}
/**
* Unit test for BTreeHeaderPage.getEmptySlot()
*/
@Test public void getEmptySlot() throws Exception {
BTreeHeaderPage page = new BTreeHeaderPage(pid, EXAMPLE_DATA);
assertEquals(0, page.getEmptySlot());
page.init();
assertEquals(-1, page.getEmptySlot());
page.markSlotUsed(50, false);
assertEquals(50, page.getEmptySlot());
}
/**
* Unit test for BTreeHeaderPage.isSlotUsed() and BTreeHeaderPage.markSlotUsed()
*/
@Test public void getSlot() throws Exception {
BTreeHeaderPage page = new BTreeHeaderPage(pid, EXAMPLE_DATA);
page.init();
for (int i = 0; i < 20; ++i) {
page.markSlotUsed(i, false);
}
for (int i = 0; i < 20; i += 2) {
page.markSlotUsed(i, true);
}
for (int i = 0; i < 20; ++i) {
if(i % 2 == 0)
assertTrue(page.isSlotUsed(i));
else
assertFalse(page.isSlotUsed(i));
}
for (int i = 20; i < 32704; ++i)
assertTrue(page.isSlotUsed(i));
assertEquals(1, page.getEmptySlot());
}
/**
* Unit test for BTreeHeaderPage.getPageData()
*/
@Test public void getPageData() throws Exception {
BTreeHeaderPage page0 = new BTreeHeaderPage(pid, EXAMPLE_DATA);
page0.init();
for (int i = 0; i < 20; ++i) {
page0.markSlotUsed(i, false);
}
for (int i = 0; i < 20; i += 2) {
page0.markSlotUsed(i, true);
}
BTreeHeaderPage page = new BTreeHeaderPage(pid, page0.getPageData());
for (int i = 0; i < 20; ++i) {
if(i % 2 == 0)
assertTrue(page.isSlotUsed(i));
else
assertFalse(page.isSlotUsed(i));
}
for (int i = 20; i < 32704; ++i)
assertTrue(page.isSlotUsed(i));
assertEquals(1, page.getEmptySlot());
}
/**
* Unit test for BTreeHeaderPage.isDirty()
*/
@Test public void testDirty() throws Exception {
TransactionId tid = new TransactionId();
BTreeHeaderPage page = new BTreeHeaderPage(pid, 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);
}
/**
* JUnit suite target
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(BTreeHeaderPageTest.class);
}
}