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