1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.utils;
19
20 import org.apache.hadoop.io.FloatWritable;
21 import org.apache.hadoop.io.LongWritable;
22 import org.apache.hadoop.io.Writable;
23 import org.junit.Assert;
24 import org.junit.Test;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.DataInputStream;
29 import java.io.DataOutputStream;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33
34
35
36
37 public class TestWritableUtils {
38
39
40
41
42
43 @Test
44 public void 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));
53
54 ByteArrayOutputStream bos = new ByteArrayOutputStream();
55 DataOutputStream dos = new DataOutputStream(bos);
56 WritableUtils.writeList(list, dos);
57 dos.close();
58
59 byte[] data = bos.toByteArray();
60
61 DataInputStream input =
62 new DataInputStream(new ByteArrayInputStream(data));
63
64 List<Writable> result = (List<Writable>) WritableUtils.readList(input);
65
66 Assert.assertEquals(list, result);
67
68 }
69
70 @Test
71 public void testIntArray() throws IOException {
72 int[] array = new int[] {1, 2, 3, 4, 5};
73 ByteArrayOutputStream bos = new ByteArrayOutputStream();
74 DataOutputStream dos = new DataOutputStream(bos);
75 WritableUtils.writeIntArray(array, dos);
76 dos.close();
77
78 byte[] data = bos.toByteArray();
79
80 DataInputStream input =
81 new DataInputStream(new ByteArrayInputStream(data));
82
83 int[] result = WritableUtils.readIntArray(input);
84
85 Assert.assertArrayEquals(array, result);
86 }
87
88 @Test
89 public void testLongArray() throws IOException {
90 long[] array = new long[] {1, 2, 3, 4, 5};
91 ByteArrayOutputStream bos = new ByteArrayOutputStream();
92 DataOutputStream dos = new DataOutputStream(bos);
93 WritableUtils.writeLongArray(dos, array);
94 dos.close();
95
96 byte[] data = bos.toByteArray();
97
98 DataInputStream input =
99 new DataInputStream(new ByteArrayInputStream(data));
100
101 long[] result = WritableUtils.readLongArray(input);
102
103 Assert.assertArrayEquals(array, result);
104 }
105
106
107
108 }