-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapPageReadTest.java
129 lines (112 loc) · 3.67 KB
/
HeapPageReadTest.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
package simpledb;
import simpledb.TestUtil.SkeletonFile;
import simpledb.systemtest.SimpleDbTestBase;
import simpledb.systemtest.SystemTestUtil;
import java.io.File;
import java.io.IOException;
import java.util.*;
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 HeapPageReadTest extends SimpleDbTestBase {
private HeapPageId pid;
public static final int[][] EXAMPLE_VALUES = new int[][] {
{ 31933, 862 },
{ 29402, 56883 },
{ 1468, 5825 },
{ 17876, 52278 },
{ 6350, 36090 },
{ 34784, 43771 },
{ 28617, 56874 },
{ 19209, 23253 },
{ 56462, 24979 },
{ 51440, 56685 },
{ 3596, 62307 },
{ 45569, 2719 },
{ 22064, 43575 },
{ 42812, 44947 },
{ 22189, 19724 },
{ 33549, 36554 },
{ 9086, 53184 },
{ 42878, 33394 },
{ 62778, 21122 },
{ 17197, 16388 }
};
public static final byte[] EXAMPLE_DATA;
static {
// Build the input table
ArrayList<ArrayList<Integer>> table = new ArrayList<ArrayList<Integer>>();
for (int[] tuple : EXAMPLE_VALUES) {
ArrayList<Integer> listTuple = new ArrayList<Integer>();
for (int value : tuple) {
listTuple.add(value);
}
table.add(listTuple);
}
// Convert it to a HeapFile and read in the bytes
try {
File temp = File.createTempFile("table", ".dat");
temp.deleteOnExit();
HeapFileEncoder.convert(table, temp, BufferPool.getPageSize(), 2);
EXAMPLE_DATA = TestUtil.readFileBytes(temp.getAbsolutePath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Set up initial resources for each unit test.
*/
@Before public void addTable() throws Exception {
this.pid = new HeapPageId(-1, -1);
Database.getCatalog().addTable(new SkeletonFile(-1, Utility.getTupleDesc(2)), SystemTestUtil.getUUID());
}
/**
* Unit test for HeapPage.getId()
*/
@Test public void getId() throws Exception {
HeapPage page = new HeapPage(pid, EXAMPLE_DATA);
assertEquals(pid, page.getId());
}
/**
* Unit test for HeapPage.iterator()
*/
@Test public void testIterator() throws Exception {
HeapPage page = new HeapPage(pid, EXAMPLE_DATA);
Iterator<Tuple> it = page.iterator();
int row = 0;
while (it.hasNext()) {
Tuple tup = it.next();
IntField f0 = (IntField) tup.getField(0);
IntField f1 = (IntField) tup.getField(1);
assertEquals(EXAMPLE_VALUES[row][0], f0.getValue());
assertEquals(EXAMPLE_VALUES[row][1], f1.getValue());
row++;
}
}
/**
* Unit test for HeapPage.getNumEmptySlots()
*/
@Test public void getNumEmptySlots() throws Exception {
HeapPage page = new HeapPage(pid, EXAMPLE_DATA);
assertEquals(484, page.getNumEmptySlots());
}
/**
* Unit test for HeapPage.isSlotUsed()
*/
@Test public void getSlot() throws Exception {
HeapPage page = new HeapPage(pid, EXAMPLE_DATA);
for (int i = 0; i < 20; ++i)
assertTrue(page.isSlotUsed(i));
for (int i = 20; i < 504; ++i)
assertFalse(page.isSlotUsed(i));
}
/**
* JUnit suite target
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(HeapPageReadTest.class);
}
}