-
Notifications
You must be signed in to change notification settings - Fork 15
/
HeapFileReadTest.java
130 lines (112 loc) · 3.43 KB
/
HeapFileReadTest.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
package simpledb;
import simpledb.systemtest.SimpleDbTestBase;
import simpledb.systemtest.SystemTestUtil;
import java.util.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import junit.framework.JUnit4TestAdapter;
public class HeapFileReadTest extends SimpleDbTestBase {
private HeapFile hf;
private TransactionId tid;
private TupleDesc td;
/**
* Set up initial resources for each unit test.
*/
@Before
public void setUp() throws Exception {
hf = SystemTestUtil.createRandomHeapFile(2, 20, null, null);
td = Utility.getTupleDesc(2);
tid = new TransactionId();
}
@After
public void tearDown() throws Exception {
Database.getBufferPool().transactionComplete(tid);
}
/**
* Unit test for HeapFile.getId()
*/
@Test
public void getId() throws Exception {
int id = hf.getId();
// NOTE(ghuo): the value could be anything. test determinism, at least.
assertEquals(id, hf.getId());
assertEquals(id, hf.getId());
HeapFile other = SystemTestUtil.createRandomHeapFile(1, 1, null, null);
assertTrue(id != other.getId());
}
/**
* Unit test for HeapFile.getTupleDesc()
*/
@Test
public void getTupleDesc() throws Exception {
assertEquals(td, hf.getTupleDesc());
}
/**
* Unit test for HeapFile.numPages()
*/
@Test
public void numPages() throws Exception {
assertEquals(1, hf.numPages());
// assertEquals(1, empty.numPages());
}
/**
* Unit test for HeapFile.readPage()
*/
@Test
public void readPage() throws Exception {
HeapPageId pid = new HeapPageId(hf.getId(), 0);
HeapPage page = (HeapPage) hf.readPage(pid);
// NOTE(ghuo): we try not to dig too deeply into the Page API here; we
// rely on HeapPageTest for that. perform some basic checks.
assertEquals(484, page.getNumEmptySlots());
assertTrue(page.isSlotUsed(1));
assertFalse(page.isSlotUsed(20));
}
@Test
public void testIteratorBasic() throws Exception {
HeapFile smallFile = SystemTestUtil.createRandomHeapFile(2, 3, null,
null);
DbFileIterator it = smallFile.iterator(tid);
// Not open yet
assertFalse(it.hasNext());
try {
it.next();
fail("expected exception");
} catch (NoSuchElementException e) {
}
it.open();
int count = 0;
while (it.hasNext()) {
assertNotNull(it.next());
count += 1;
}
assertEquals(3, count);
it.close();
}
@Test
public void testIteratorClose() throws Exception {
// make more than 1 page. Previous closed iterator would start fetching
// from page 1.
HeapFile twoPageFile = SystemTestUtil.createRandomHeapFile(2, 520,
null, null);
DbFileIterator it = twoPageFile.iterator(tid);
it.open();
assertTrue(it.hasNext());
it.close();
try {
it.next();
fail("expected exception");
} catch (NoSuchElementException e) {
}
// close twice is harmless
it.close();
}
/**
* JUnit suite target
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(HeapFileReadTest.class);
}
}