Merge pull request #14812 from jasontedor/linked-hash-map-stream

Add unit test for LinkedHashMap serialization
This commit is contained in:
Jason Tedor 2015-11-17 20:24:49 -05:00
commit 3ee9be1891
1 changed files with 31 additions and 2 deletions

View File

@ -25,8 +25,7 @@ import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.*;
public class StreamTests extends ESTestCase {
public void testRandomVLongSerialization() throws IOException {
@ -59,4 +58,34 @@ public class StreamTests extends ESTestCase {
assertEquals(Arrays.toString(value.v2()), (long)value.v1(), bytes.streamInput().readZLong());
}
}
public void testLinkedHashMap() throws IOException {
int size = randomIntBetween(1, 1024);
boolean accessOrder = randomBoolean();
List<Tuple<String, Integer>> list = new ArrayList<>(size);
LinkedHashMap<String, Integer> write = new LinkedHashMap<>(size, 0.75f, accessOrder);
for (int i = 0; i < size; i++) {
int value = randomInt();
list.add(new Tuple<>(Integer.toString(i), value));
write.put(Integer.toString(i), value);
}
if (accessOrder) {
// randomize access order
Collections.shuffle(list, random());
for (Tuple<String, Integer> entry : list) {
// touch the entries to set the access order
write.get(entry.v1());
}
}
BytesStreamOutput out = new BytesStreamOutput();
out.writeGenericValue(write);
LinkedHashMap<String, Integer> read = (LinkedHashMap<String, Integer>)out.bytes().streamInput().readGenericValue();
assertEquals(size, read.size());
int index = 0;
for (Map.Entry<String, Integer> entry : read.entrySet()) {
assertEquals(list.get(index).v1(), entry.getKey());
assertEquals(list.get(index).v2(), entry.getValue());
index++;
}
}
}