/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.common.io.stream;

import org.elasticsearch.common.bytes.ByteBufferBytesReference;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;

public class StreamTests extends ESTestCase {
    public void testRandomVLongSerialization() throws IOException {
        for (int i = 0; i < 1024; i++) {
            long write = randomLong();
            BytesStreamOutput out = new BytesStreamOutput();
            out.writeZLong(write);
            long read = out.bytes().streamInput().readZLong();
            assertEquals(write, read);
        }
    }

    public void testSpecificVLongSerialization() throws IOException {
        List<Tuple<Long, byte[]>> values =
                Arrays.asList(
                        new Tuple<>(0L, new byte[]{0}),
                        new Tuple<>(-1L, new byte[]{1}),
                        new Tuple<>(1L, new byte[]{2}),
                        new Tuple<>(-2L, new byte[]{3}),
                        new Tuple<>(2L, new byte[]{4}),
                        new Tuple<>(Long.MIN_VALUE, new byte[]{-1, -1, -1, -1, -1, -1, -1, -1, -1, 1}),
                        new Tuple<>(Long.MAX_VALUE, new byte[]{-2, -1, -1, -1, -1, -1, -1, -1, -1, 1})

                );
        for (Tuple<Long, byte[]> value : values) {
            BytesStreamOutput out = new BytesStreamOutput();
            out.writeZLong(value.v1());
            assertArrayEquals(Long.toString(value.v1()), value.v2(), out.bytes().toBytes());
            ByteBufferBytesReference bytes = new ByteBufferBytesReference(ByteBuffer.wrap(value.v2()));
            assertEquals(Arrays.toString(value.v2()), (long)value.v1(), bytes.streamInput().readZLong());
        }
    }
}

@@ -1059,37 +1059,9 @@
 til.
-Arrays;%0Aimport java.util.List
+*
 ;%0A%0Ap
@@ -2591,11 +2591,1336 @@
  %7D%0A    %7D
+%0A%0A    public void testLinkedHashMap() throws IOException %7B%0A        int size = randomIntBetween(1, 1024);%0A        boolean accessOrder = randomBoolean();%0A        List%3CTuple%3CString, Integer%3E%3E list = new ArrayList%3C%3E(size);%0A        LinkedHashMap%3CString, Integer%3E write = new LinkedHashMap%3C%3E(size, 0.75f, accessOrder);%0A        for (int i = 0; i %3C size; i++) %7B%0A            int value = randomInt();%0A            list.add(new Tuple%3C%3E(Integer.toString(i), value));%0A            write.put(Integer.toString(i), value);%0A        %7D%0A        if (accessOrder) %7B%0A            // randomize access order%0A            Collections.shuffle(list, random());%0A            for (Tuple%3CString, Integer%3E entry : list) %7B%0A                // touch the entries to set the access order%0A                write.get(entry.v1());%0A            %7D%0A        %7D%0A        BytesStreamOutput out = new BytesStreamOutput();%0A        out.writeGenericValue(write);%0A        LinkedHashMap%3CString, Integer%3E read = (LinkedHashMap%3CString, Integer%3E)out.bytes().streamInput().readGenericValue();%0A        assertEquals(size, read.size());%0A        int index = 0;%0A        for (Map.Entry%3CString, Integer%3E entry : read.entrySet()) %7B%0A            assertEquals(list.get(index).v1(), entry.getKey());%0A            assertEquals(list.get(index).v2(), entry.getValue());%0A            index++;%0A        %7D%0A    %7D
 %0A%7D%0A

Add unit test for LinkedHashMap serialization