-
Notifications
You must be signed in to change notification settings - Fork 0
/
TupleDescTest.java
177 lines (146 loc) · 5.73 KB
/
TupleDescTest.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
package simpledb;
import java.util.NoSuchElementException;
import org.junit.Test;
import simpledb.systemtest.SimpleDbTestBase;
import static org.junit.Assert.*;
import junit.framework.Assert;
import junit.framework.JUnit4TestAdapter;
public class TupleDescTest extends SimpleDbTestBase {
/**
* Unit test for TupleDesc.combine()
*/
@Test public void combine() {
TupleDesc td1, td2, td3;
td1 = Utility.getTupleDesc(1, "td1");
td2 = Utility.getTupleDesc(2, "td2");
// test td1.combine(td2)
td3 = TupleDesc.merge(td1, td2);
assertEquals(3 , td3.numFields());
assertEquals(3 * Type.INT_TYPE.getLen(), td3.getSize());
for (int i = 0; i < 3; ++i)
assertEquals(Type.INT_TYPE, td3.getFieldType(i));
assertEquals(combinedStringArrays(td1, td2, td3), true);
// test td2.combine(td1)
td3 = TupleDesc.merge(td2, td1);
assertEquals(3 , td3.numFields());
assertEquals(3 * Type.INT_TYPE.getLen(), td3.getSize());
for (int i = 0; i < 3; ++i)
assertEquals(Type.INT_TYPE, td3.getFieldType(i));
assertEquals(combinedStringArrays(td2, td1, td3), true);
// test td2.combine(td2)
td3 = TupleDesc.merge(td2, td2);
assertEquals(4 , td3.numFields());
assertEquals(4 * Type.INT_TYPE.getLen(), td3.getSize());
for (int i = 0; i < 4; ++i)
assertEquals(Type.INT_TYPE, td3.getFieldType(i));
assertEquals(combinedStringArrays(td2, td2, td3), true);
}
/**
* Ensures that combined's field names = td1's field names + td2's field names
*/
private boolean combinedStringArrays(TupleDesc td1, TupleDesc td2, TupleDesc combined) {
for (int i = 0; i < td1.numFields(); i++) {
if (!(((td1.getFieldName(i) == null) && (combined.getFieldName(i) == null)) ||
td1.getFieldName(i).equals(combined.getFieldName(i)))) {
return false;
}
}
for (int i = td1.numFields(); i < td1.numFields() + td2.numFields(); i++) {
if (!(((td2.getFieldName(i-td1.numFields()) == null) && (combined.getFieldName(i) == null)) ||
td2.getFieldName(i-td1.numFields()).equals(combined.getFieldName(i)))) {
return false;
}
}
return true;
}
/**
* Unit test for TupleDesc.getType()
*/
@Test public void getType() {
int[] lengths = new int[] { 1, 2, 1000 };
for (int len: lengths) {
TupleDesc td = Utility.getTupleDesc(len);
for (int i = 0; i < len; ++i)
assertEquals(Type.INT_TYPE, td.getFieldType(i));
}
}
/**
* Unit test for TupleDesc.nameToId()
*/
@Test public void nameToId() {
int[] lengths = new int[] { 1, 2, 1000 };
String prefix = "test";
for (int len: lengths) {
// Make sure you retrieve well-named fields
TupleDesc td = Utility.getTupleDesc(len, prefix);
for (int i = 0; i < len; ++i) {
assertEquals(i, td.fieldNameToIndex(prefix + i));
}
// Make sure you throw exception for non-existent fields
try {
td.fieldNameToIndex("foo");
Assert.fail("foo is not a valid field name");
} catch (NoSuchElementException e) {
// expected to get here
}
// Make sure you throw exception for null searches
try {
td.fieldNameToIndex(null);
Assert.fail("null is not a valid field name");
} catch (NoSuchElementException e) {
// expected to get here
}
// Make sure you throw exception when all field names are null
td = Utility.getTupleDesc(len);
try {
td.fieldNameToIndex(prefix);
Assert.fail("no fields are named, so you can't find it");
} catch (NoSuchElementException e) {
// expected to get here
}
}
}
/**
* Unit test for TupleDesc.getSize()
*/
@Test public void getSize() {
int[] lengths = new int[] { 1, 2, 1000 };
for (int len: lengths) {
TupleDesc td = Utility.getTupleDesc(len);
assertEquals(len * Type.INT_TYPE.getLen(), td.getSize());
}
}
/**
* Unit test for TupleDesc.numFields()
*/
@Test public void numFields() {
int[] lengths = new int[] { 1, 2, 1000 };
for (int len : lengths) {
TupleDesc td = Utility.getTupleDesc(len);
assertEquals(len, td.numFields());
}
}
@Test public void testEquals() {
TupleDesc singleInt = new TupleDesc(new Type[]{Type.INT_TYPE});
TupleDesc singleInt2 = new TupleDesc(new Type[]{Type.INT_TYPE});
TupleDesc intString = new TupleDesc(new Type[]{Type.INT_TYPE, Type.STRING_TYPE});
// .equals() with null should return false
assertFalse(singleInt.equals(null));
// .equals() with the wrong type should return false
assertFalse(singleInt.equals(new Object()));
assertTrue(singleInt.equals(singleInt));
assertTrue(singleInt.equals(singleInt2));
assertTrue(singleInt2.equals(singleInt));
assertTrue(intString.equals(intString));
assertFalse(singleInt.equals(intString));
assertFalse(singleInt2.equals(intString));
assertFalse(intString.equals(singleInt));
assertFalse(intString.equals(singleInt2));
}
/**
* JUnit suite target
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(TupleDescTest.class);
}
}