1 | /* |
---|
2 | * Cloud9: A MapReduce Library for Hadoop |
---|
3 | * |
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you |
---|
5 | * may not use this file except in compliance with the License. You may |
---|
6 | * obtain a copy of the License at |
---|
7 | * |
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
9 | * |
---|
10 | * Unless required by applicable law or agreed to in writing, software |
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
---|
13 | * implied. See the License for the specific language governing |
---|
14 | * permissions and limitations under the License. |
---|
15 | */ |
---|
16 | |
---|
17 | package tw.org.nchc.tuple; |
---|
18 | |
---|
19 | import static org.junit.Assert.assertEquals; |
---|
20 | import static org.junit.Assert.assertTrue; |
---|
21 | |
---|
22 | import java.io.ByteArrayInputStream; |
---|
23 | import java.io.ByteArrayOutputStream; |
---|
24 | import java.io.DataInputStream; |
---|
25 | import java.io.DataOutputStream; |
---|
26 | import java.io.IOException; |
---|
27 | import java.util.ArrayList; |
---|
28 | |
---|
29 | import junit.framework.JUnit4TestAdapter; |
---|
30 | |
---|
31 | import org.apache.hadoop.io.FloatWritable; |
---|
32 | import org.apache.hadoop.io.IntWritable; |
---|
33 | import org.apache.hadoop.io.Text; |
---|
34 | import org.apache.hadoop.io.Writable; |
---|
35 | import org.junit.Test; |
---|
36 | |
---|
37 | public class ArrayListWritableTest { |
---|
38 | |
---|
39 | @Test |
---|
40 | public void testBasic() throws IOException { |
---|
41 | ArrayListWritable<Text> list = new ArrayListWritable<Text>(); |
---|
42 | |
---|
43 | list.add(new Text("hi")); |
---|
44 | list.add(new Text("there")); |
---|
45 | |
---|
46 | assertEquals(list.get(0).toString(), "hi"); |
---|
47 | assertEquals(list.get(1).toString(), "there"); |
---|
48 | } |
---|
49 | |
---|
50 | @Test |
---|
51 | public void testSerialize1() throws IOException { |
---|
52 | //ArrayListWritable<Text> list = new ArrayListWritable<Text>(); |
---|
53 | ArrayListWritable<Writable> list = new ArrayListWritable<Writable>(); |
---|
54 | list.add(new Text("hi")); |
---|
55 | list.add(new Text("there")); |
---|
56 | |
---|
57 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
---|
58 | DataOutputStream dataOut = new DataOutputStream(bytesOut); |
---|
59 | |
---|
60 | list.write(dataOut); |
---|
61 | |
---|
62 | ArrayListWritable<Text> newList = new ArrayListWritable<Text>(); |
---|
63 | newList.readFields(new DataInputStream(new ByteArrayInputStream( |
---|
64 | bytesOut.toByteArray()))); |
---|
65 | |
---|
66 | assertEquals(newList.get(0).toString(), "hi"); |
---|
67 | assertEquals(newList.get(1).toString(), "there"); |
---|
68 | } |
---|
69 | |
---|
70 | @Test |
---|
71 | public void testSerialize2() throws IOException { |
---|
72 | ArrayListWritable<FloatWritable> list = new ArrayListWritable<FloatWritable>(); |
---|
73 | |
---|
74 | list.add(new FloatWritable(0.3f)); |
---|
75 | list.add(new FloatWritable(3244.2f)); |
---|
76 | |
---|
77 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
---|
78 | DataOutputStream dataOut = new DataOutputStream(bytesOut); |
---|
79 | |
---|
80 | list.write(dataOut); |
---|
81 | |
---|
82 | ArrayListWritable<FloatWritable> newList = new ArrayListWritable<FloatWritable>(); |
---|
83 | newList.readFields(new DataInputStream(new ByteArrayInputStream( |
---|
84 | bytesOut.toByteArray()))); |
---|
85 | |
---|
86 | assertTrue(newList.get(0).get() == 0.3f); |
---|
87 | assertTrue(newList.get(1).get() == 3244.2f); |
---|
88 | } |
---|
89 | |
---|
90 | @Test |
---|
91 | public void testSerialize3() throws IOException { |
---|
92 | //ArrayListWritable<Text> list = new ArrayListWritable<Text>(); |
---|
93 | ArrayListWritable<Writable> list = new ArrayListWritable<Writable>(); |
---|
94 | list.add(new Text("hi")); |
---|
95 | list.add(new IntWritable(1)); |
---|
96 | |
---|
97 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
---|
98 | DataOutputStream dataOut = new DataOutputStream(bytesOut); |
---|
99 | |
---|
100 | list.write(dataOut); |
---|
101 | |
---|
102 | ArrayListWritable<Writable> newList = new ArrayListWritable<Writable>(); |
---|
103 | newList.readFields(new DataInputStream(new ByteArrayInputStream( |
---|
104 | bytesOut.toByteArray()))); |
---|
105 | |
---|
106 | try { |
---|
107 | assertEquals(newList.get(0).toString(), "hi"); |
---|
108 | IntWritable i=(IntWritable)(newList.get(1)); |
---|
109 | assertEquals(i.get(), 1); |
---|
110 | assertTrue(false); |
---|
111 | } catch (Exception e) { |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | @Test |
---|
116 | public void testToString() { |
---|
117 | ArrayListWritable<Text> list = new ArrayListWritable<Text>(); |
---|
118 | |
---|
119 | list.add(new Text("hi")); |
---|
120 | list.add(new Text("there")); |
---|
121 | |
---|
122 | assertEquals(list.toString(), "[hi, there]"); |
---|
123 | } |
---|
124 | |
---|
125 | @Test |
---|
126 | public void testClear() { |
---|
127 | ArrayListWritable<Text> list = new ArrayListWritable<Text>(); |
---|
128 | |
---|
129 | list.add(new Text("hi")); |
---|
130 | list.add(new Text("there")); |
---|
131 | list.clear(); |
---|
132 | |
---|
133 | assertEquals(list.size(), 0); |
---|
134 | } |
---|
135 | |
---|
136 | @Test |
---|
137 | public void testEmpty() throws IOException { |
---|
138 | ArrayListWritable<Text> list = new ArrayListWritable<Text>(); |
---|
139 | |
---|
140 | assertTrue(list.size() == 0); |
---|
141 | |
---|
142 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
---|
143 | DataOutputStream dataOut = new DataOutputStream(bytesOut); |
---|
144 | |
---|
145 | list.write(dataOut); |
---|
146 | |
---|
147 | ArrayListWritable<Text> newList = new ArrayListWritable<Text>(); |
---|
148 | newList.readFields(new DataInputStream(new ByteArrayInputStream( |
---|
149 | bytesOut.toByteArray()))); |
---|
150 | assertTrue(newList.size() == 0); |
---|
151 | |
---|
152 | newList.add(new Text("Hey")); |
---|
153 | assertEquals(newList.get(0),new Text("Hey")); |
---|
154 | |
---|
155 | } |
---|
156 | |
---|
157 | /*@Test |
---|
158 | public void testTypeSafety() { |
---|
159 | ArrayListWritable<WritableComparable> list = new ArrayListWritable<WritableComparable> (); |
---|
160 | list.add(new Text("Hello")); |
---|
161 | list.add(new Text("Are you there")); |
---|
162 | |
---|
163 | try { |
---|
164 | list.add(new IntWritable(5)); |
---|
165 | assertTrue(false); // should throw an exception before reaching this line. |
---|
166 | } catch (IllegalArgumentException e) { |
---|
167 | assertTrue(true); |
---|
168 | } |
---|
169 | |
---|
170 | ArrayList<WritableComparable> otherList = new ArrayList<WritableComparable>(); |
---|
171 | otherList.add(new Text("Test")); |
---|
172 | otherList.add(new Text("Test 2")); |
---|
173 | |
---|
174 | assertTrue(list.addAll(otherList)); |
---|
175 | |
---|
176 | otherList.add(new IntWritable(6)); |
---|
177 | try { |
---|
178 | list.addAll(otherList); |
---|
179 | assertTrue(false); |
---|
180 | } catch (IllegalArgumentException e) { |
---|
181 | assertTrue(true); |
---|
182 | } |
---|
183 | }*/ |
---|
184 | |
---|
185 | @Test |
---|
186 | public void testListMethods() { |
---|
187 | IntWritable a = new IntWritable(1); |
---|
188 | IntWritable b = new IntWritable(2); |
---|
189 | IntWritable c = new IntWritable(3); |
---|
190 | IntWritable d = new IntWritable(4); |
---|
191 | IntWritable e = new IntWritable(5); |
---|
192 | |
---|
193 | ArrayListWritable<IntWritable> list = new ArrayListWritable<IntWritable>(); |
---|
194 | assertTrue(list.isEmpty()); |
---|
195 | list.add(a); |
---|
196 | list.add(b); |
---|
197 | list.add(c); |
---|
198 | list.add(d); |
---|
199 | list.add(e); |
---|
200 | |
---|
201 | int pos = 0; |
---|
202 | for (IntWritable i : list) { |
---|
203 | assertEquals(i, list.get(pos)); |
---|
204 | ++pos; |
---|
205 | } |
---|
206 | |
---|
207 | assertTrue(list.indexOf(d) == 3); |
---|
208 | list.add(2, a); |
---|
209 | assertTrue(list.lastIndexOf(a) == 2); |
---|
210 | assertEquals(list.get(2), list.get(0)); |
---|
211 | assertTrue(list.size() == 6); |
---|
212 | |
---|
213 | assertTrue(list.contains(c)); |
---|
214 | assertTrue(!list.contains(new IntWritable(123))); |
---|
215 | |
---|
216 | ArrayList<IntWritable> otherList = new ArrayList<IntWritable>(); |
---|
217 | otherList.add(a); |
---|
218 | otherList.add(b); |
---|
219 | otherList.add(c); |
---|
220 | |
---|
221 | assertTrue(list.containsAll(otherList)); |
---|
222 | |
---|
223 | otherList.add(new IntWritable(200)); |
---|
224 | assertTrue(!list.containsAll(otherList)); |
---|
225 | |
---|
226 | assertEquals(a, otherList.remove(0)); |
---|
227 | assertTrue(list.remove(d)); |
---|
228 | |
---|
229 | } |
---|
230 | |
---|
231 | public static junit.framework.Test suite() { |
---|
232 | return new JUnit4TestAdapter(ArrayListWritableTest.class); |
---|
233 | } |
---|
234 | |
---|
235 | } |
---|