source: sample/hadoop-0.16/tw/org/nchc/tuple/ListWritableTest.java @ 72

Last change on this file since 72 was 21, checked in by waue, 16 years ago

hadoop 0.16

File size: 7.1 KB
Line 
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
17package tw.org.nchc.tuple;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24import java.io.DataInputStream;
25import java.io.DataOutputStream;
26import java.io.IOException;
27import java.util.ArrayList;
28
29import junit.framework.JUnit4TestAdapter;
30
31import org.apache.hadoop.io.FloatWritable;
32import org.apache.hadoop.io.IntWritable;
33import org.apache.hadoop.io.Text;
34import org.apache.hadoop.io.WritableComparable;
35import org.junit.Test;
36
37public class ListWritableTest {
38
39  @Test
40  public void testBasic() throws IOException {
41    ListWritable<Text> list = new ListWritable<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    ListWritable<Text> list = new ListWritable<Text>();
53
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    ListWritable<Text> newList = new ListWritable<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    ListWritable<FloatWritable> list = new ListWritable<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    ListWritable<FloatWritable> newList = new ListWritable<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 testToString() {
92    ListWritable<Text> list = new ListWritable<Text>();
93
94    list.add(new Text("hi"));
95    list.add(new Text("there"));
96
97    assertEquals(list.toString(), "[hi, there]");
98  }
99
100  @Test
101  public void testClear() {
102    ListWritable<Text> list = new ListWritable<Text>();
103
104    list.add(new Text("hi"));
105    list.add(new Text("there"));
106    list.clear();
107   
108    assertEquals(list.size(), 0);
109  }
110
111  @Test
112  public void testSorting1() {
113    ListWritable<Text> list1 = new ListWritable<Text>();
114    ListWritable<Text> list2 = new ListWritable<Text>();
115
116    list1.add(new Text("a"));
117
118    assertTrue(list1.compareTo(list2) > 0);
119  }
120 
121  @Test
122  public void testSorting2() {
123    ListWritable<Text> list1 = new ListWritable<Text>();
124    ListWritable<Text> list2 = new ListWritable<Text>();
125
126    list1.add(new Text("a"));
127    list2.add(new Text("b"));
128
129    assertTrue(list1.compareTo(list2) < 0);
130    assertTrue(list2.compareTo(list1) > 0);
131   
132    list2.clear();
133    list2.add(new Text("a"));
134   
135    assertTrue(list1.compareTo(list2) == 0);
136   
137    list1.add(new Text("a"));
138    list2.add(new Text("b"));
139   
140    // list 1 is now [a, a]
141    // list 2 is now [a, b]
142    assertTrue(list1.compareTo(list2) < 0);
143    assertTrue(list2.compareTo(list1) > 0);
144
145    // list 1 is now [a, a, a]
146    list1.add(new Text("a"));
147   
148    assertTrue(list1.compareTo(list2) < 0);
149  }
150
151  @Test
152  public void testSorting3() {
153    ListWritable<Text> list1 = new ListWritable<Text>();
154    ListWritable<Text> list2 = new ListWritable<Text>();
155    ListWritable<Text> list3 = new ListWritable<Text>();
156
157    list1.add(new Text("a"));
158   
159    list2.add(new Text("a"));
160    list2.add(new Text("a"));
161   
162    list3.add(new Text("a"));
163    list3.add(new Text("a"));
164   
165    assertTrue(list2.compareTo(list3) == 0);
166
167    list3.add(new Text("a"));
168   
169    // list 1 is [a]
170    // list 2 is [a, a]
171    // list 3 is [a, a, a]
172   
173    assertTrue(list1.compareTo(list2) < 0);
174    assertTrue(list1.compareTo(list3) < 0);
175    assertTrue(list2.compareTo(list1) > 0);
176    assertTrue(list2.compareTo(list3) < 0);
177    assertTrue(list3.compareTo(list1) > 0);
178    assertTrue(list3.compareTo(list2) > 0);
179  }
180 
181  @Test
182  public void testEmpty() throws IOException {
183    ListWritable<Text> list = new ListWritable<Text>();
184   
185    assertTrue(list.size() == 0);
186   
187    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
188    DataOutputStream dataOut = new DataOutputStream(bytesOut);
189
190    list.write(dataOut);
191
192    ListWritable<Text> newList = new ListWritable<Text>();
193    newList.readFields(new DataInputStream(new ByteArrayInputStream(
194        bytesOut.toByteArray())));
195    assertTrue(newList.size() == 0);
196   
197    newList.add(new Text("Hey"));
198    assertEquals(newList.get(0),new Text("Hey"));
199
200  }
201 
202  @Test
203  public void testTypeSafety() {
204    ListWritable<WritableComparable> list = new ListWritable<WritableComparable> ();
205    list.add(new Text("Hello"));
206    list.add(new Text("Are you there"));
207   
208    try {
209      list.add(new IntWritable(5));
210      assertTrue(false); // should throw an exception before reaching this line.
211    } catch (IllegalArgumentException e) {
212      assertTrue(true);
213    }
214   
215    ArrayList<WritableComparable> otherList = new ArrayList<WritableComparable>();
216    otherList.add(new Text("Test"));
217    otherList.add(new Text("Test 2"));
218   
219    assertTrue(list.addAll(otherList));
220   
221    otherList.add(new IntWritable(6));
222    try {
223      list.addAll(otherList);
224      assertTrue(false);
225    } catch (IllegalArgumentException e) {
226      assertTrue(true);
227    }
228  }
229 
230  @Test 
231  public void testListMethods() {
232    IntWritable a = new IntWritable(1);
233    IntWritable b = new IntWritable(2);
234    IntWritable c = new IntWritable(3);
235    IntWritable d = new IntWritable(4);
236    IntWritable e = new IntWritable(5);
237   
238    ListWritable<IntWritable> list = new ListWritable<IntWritable>();
239    assertTrue(list.isEmpty());
240    list.add(a);
241    list.add(b);
242    list.add(c);
243    list.add(d);
244    list.add(e);
245   
246    int pos = 0;
247    for (IntWritable i : list) {
248      assertEquals(i, list.get(pos));
249      ++pos;
250    }
251   
252    assertTrue(list.indexOf(d) == 3);
253    list.add(2, a);
254    assertTrue(list.lastIndexOf(a) == 2);
255    assertEquals(list.get(2), list.get(0));
256    assertTrue(list.size() == 6);
257   
258    assertTrue(list.contains(c));
259    assertTrue(!list.contains(new IntWritable(123)));
260   
261    ArrayList<IntWritable> otherList = new ArrayList<IntWritable>();
262    otherList.add(a);
263    otherList.add(b);
264    otherList.add(c);
265   
266    assertTrue(list.containsAll(otherList));
267   
268    otherList.add(new IntWritable(200));
269    assertTrue(!list.containsAll(otherList));
270   
271    assertEquals(a, otherList.remove(0));
272    assertTrue(list.remove(d));
273   
274  }
275 
276  public static junit.framework.Test suite() {
277    return new JUnit4TestAdapter(ListWritableTest.class);
278  }
279
280}
Note: See TracBrowser for help on using the repository browser.