1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.utils;
1920import org.apache.hadoop.io.FloatWritable;
21import org.apache.hadoop.io.LongWritable;
22import org.apache.hadoop.io.Writable;
23import org.junit.Assert;
24import org.junit.Test;
2526import java.io.ByteArrayInputStream;
27import java.io.ByteArrayOutputStream;
28import java.io.DataInputStream;
29import java.io.DataOutputStream;
30import java.io.IOException;
31import java.util.ArrayList;
32import java.util.List;
3334/**35 * Test case for WritableUtils.36 */37publicclassTestWritableUtils {
3839/**40 * Tests readList and writeList functions in writable utils.41 * @throws IOException42 */43 @Test
44publicvoid testListSerialization() throws IOException {
45 List<Writable> list = new ArrayList<>();
46 list.add(new LongWritable(1));
47 list.add(new LongWritable(2));
48 list.add(null);
49 list.add(new FloatWritable(3));
50 list.add(new FloatWritable(4));
51 list.add(new LongWritable(5));
52 list.add(new LongWritable(6));
5354 ByteArrayOutputStream bos = new ByteArrayOutputStream();
55 DataOutputStream dos = new DataOutputStream(bos);
56 WritableUtils.writeList(list, dos);
57 dos.close();
5859 byte[] data = bos.toByteArray();
6061 DataInputStream input =
62new DataInputStream(new ByteArrayInputStream(data));
6364 List<Writable> result = (List<Writable>) WritableUtils.readList(input);
6566 Assert.assertEquals(list, result);
6768 }
6970 @Test
71publicvoid testIntArray() throws IOException {
72int[] array = newint[] {1, 2, 3, 4, 5};
73 ByteArrayOutputStream bos = new ByteArrayOutputStream();
74 DataOutputStream dos = new DataOutputStream(bos);
75 WritableUtils.writeIntArray(array, dos);
76 dos.close();
7778 byte[] data = bos.toByteArray();
7980 DataInputStream input =
81new DataInputStream(new ByteArrayInputStream(data));
8283int[] result = WritableUtils.readIntArray(input);
8485 Assert.assertArrayEquals(array, result);
86 }
8788 @Test
89publicvoid testLongArray() throws IOException {
90long[] array = newlong[] {1, 2, 3, 4, 5};
91 ByteArrayOutputStream bos = new ByteArrayOutputStream();
92 DataOutputStream dos = new DataOutputStream(bos);
93 WritableUtils.writeLongArray(dos, array);
94 dos.close();
9596 byte[] data = bos.toByteArray();
9798 DataInputStream input =
99new DataInputStream(new ByteArrayInputStream(data));
100101long[] result = WritableUtils.readLongArray(input);
102103 Assert.assertArrayEquals(array, result);
104 }
105106107108 }