This project has retired. For details please refer to its
Attic page.
KryoWritableTest xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.writable.kryo;
19
20 import static org.junit.Assert.assertEquals;
21
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.apache.giraph.types.ops.collections.array.WLongArrayList;
26 import org.apache.giraph.utils.WritableUtils;
27 import org.apache.hadoop.io.LongWritable;
28 import org.junit.Assert;
29 import org.junit.Test;
30
31 import com.google.common.collect.ImmutableMap;
32
33 import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
34 import it.unimi.dsi.fastutil.longs.LongArrayList;
35
36
37
38
39
40 public class KryoWritableTest {
41 public static class TestClassA extends KryoWritable {
42 final String testObject;
43 final List list;
44 final int something;
45
46 public TestClassA(String testObject, List list, int something) {
47 this.testObject = testObject;
48 this.list = list;
49 this.something = something;
50 }
51
52 public TestClassA() {
53 this.testObject = null;
54 this.list = null;
55 this.something = -1;
56 }
57 }
58
59 @Test
60 public void testTestClassA() throws Exception {
61 String testObject = "Hello World!";
62 TestClassA res = new TestClassA();
63 WritableUtils.copyInto(
64 new TestClassA(testObject, Arrays.asList(1, 2, 3), 5), res, true);
65
66 assertEquals(testObject, res.testObject);
67
68 assertEquals(3, res.list.size());
69 assertEquals(1, res.list.get(0));
70 assertEquals(2, res.list.get(1));
71 assertEquals(3, res.list.get(2));
72
73 assertEquals(5, res.something);
74 }
75
76 public static class LongKryoWritable extends KryoWritable {
77 private long value;
78
79 public LongKryoWritable(long value) {
80 this.value = value;
81 }
82
83 public long get() {
84 return value;
85 }
86
87 public void set(long value) {
88 this.value = value;
89 }
90 }
91
92
93 int multiplier = 10;
94 int longTestTimes = 1000 * multiplier;
95
96 @Test
97 public void testLongKryoWritable() throws Exception {
98 LongKryoWritable from = new LongKryoWritable(0);
99 LongKryoWritable to = new LongKryoWritable(0);
100
101 for (int i = 0; i < longTestTimes; i++) {
102 from.set(i);
103 WritableUtils.copyInto(from, to, true);
104 assertEquals(i, to.get());
105 }
106 }
107
108 @Test
109 public void testLongWritable() throws Exception {
110 LongWritable from = new LongWritable(0);
111 LongWritable to = new LongWritable(0);
112
113 for (int i = 0; i < longTestTimes; i++) {
114 from.set(i);
115 WritableUtils.copyInto(from, to, true);
116 assertEquals(i, to.get());
117 }
118 }
119
120 public static class LongListKryoWritable extends KryoWritable {
121 public LongArrayList value;
122
123 public LongListKryoWritable(LongArrayList value) {
124 this.value = value;
125 }
126 }
127
128 int longListTestTimes = 1 * multiplier;
129 int longListTestSize = 100000;
130
131 @Test
132 public void testLongListKryoWritable() throws Exception {
133 LongArrayList list = new LongArrayList(longListTestSize);
134 for (int i = 0; i < longListTestSize; i++) {
135 list.add(i);
136 }
137
138 LongListKryoWritable from = new LongListKryoWritable(list);
139 LongListKryoWritable to = new LongListKryoWritable(null);
140
141 for (int i = 0; i < longListTestTimes; i++) {
142 from.value.set((2 * i) % longListTestSize, 0);
143 WritableUtils.copyInto(from, to, true);
144 }
145 }
146
147 @Test
148 public void testLongListWritable() throws Exception {
149 WLongArrayList from = new WLongArrayList(longListTestSize);
150 LongWritable value = new LongWritable();
151 for (int i = 0; i < longListTestSize; i++) {
152 value.set(i);
153 from.addW(value);
154 }
155
156 WLongArrayList to = new WLongArrayList(longListTestSize);
157 value.set(0);
158
159 for (int i = 0; i < longListTestTimes; i++) {
160 from.setW((2 * i) % longListTestSize, value);
161 WritableUtils.copyInto(from, to, true);
162 }
163 }
164
165 public static class NestedKryoWritable<T> extends KryoWritable {
166 public LongKryoWritable value1;
167 public T value2;
168
169 public NestedKryoWritable(LongKryoWritable value1, T value2) {
170 this.value1 = value1;
171 this.value2 = value2;
172 }
173 }
174
175 @Test
176 public void testNestedKryoWritable() throws Exception {
177 LongKryoWritable inner = new LongKryoWritable(5);
178 NestedKryoWritable<LongKryoWritable> res = new NestedKryoWritable<>(null, null);
179 WritableUtils.copyInto(
180 new NestedKryoWritable<>(inner, inner), res, true);
181
182 assertEquals(5, res.value1.get());
183 Assert.assertTrue(res.value1 == res.value2);
184 }
185
186 @Test
187 public void testRecursiveKryoWritable() throws Exception {
188 LongKryoWritable inner = new LongKryoWritable(5);
189 NestedKryoWritable wanted = new NestedKryoWritable<>(inner, null);
190 wanted.value2 = wanted;
191
192 NestedKryoWritable res = new NestedKryoWritable<>(null, null);
193 WritableUtils.copyInto(wanted, res, true);
194
195 assertEquals(5, res.value1.get());
196 Assert.assertTrue(res == res.value2);
197 }
198
199 @Test
200 public void testKryoImmutableMap() throws Exception {
201 Long2IntOpenHashMap map = new Long2IntOpenHashMap();
202 map.put(1, 2);
203 map.put(10, 20);
204 ImmutableMap<Long, Integer> copy =
205 WritableUtils.createCopy(
206 new KryoWritableWrapper<>(ImmutableMap.copyOf(map))).get();
207 Assert.assertEquals(2, copy.size());
208 Assert.assertEquals(2, copy.get(1L).intValue());
209 Assert.assertEquals(20, copy.get(10L).intValue());
210 }
211 }