1/******************************************************************************* 2 * Copyright (c) 2009, 2015 Mountainminds GmbH & Co. KG and Contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Marc R. Hoffmann - initial API and implementation 10 * 11 *******************************************************************************/ 12package org.jacoco.core.internal.data; 13 14import static org.junit.Assert.assertEquals; 15 16import java.io.IOException; 17import java.io.PipedInputStream; 18import java.io.PipedOutputStream; 19 20import org.junit.Before; 21import org.junit.Test; 22 23/** 24 * Unit tests for {@link CompactDataInput} and {@link CompactDataOutput}. The 25 * tests don't care about the written binary format, they just verify symmetry. 26 */ 27public class CompactDataInputOutputTest { 28 29 private CompactDataOutput out; 30 31 private CompactDataInput in; 32 33 @Before 34 public void setup() throws IOException { 35 PipedOutputStream pipe = new PipedOutputStream(); 36 out = new CompactDataOutput(pipe); 37 in = new CompactDataInput(new PipedInputStream(pipe)); 38 } 39 40 @Test 41 public void testVarInt0x00000000() throws IOException { 42 testVarInt(0x00000000); 43 } 44 45 @Test 46 public void testVarInt0x0000007F() throws IOException { 47 testVarInt(0x0000007F); 48 } 49 50 @Test 51 public void testVarInt0x00000080() throws IOException { 52 testVarInt(0x00000080); 53 } 54 55 @Test 56 public void testVarInt0x00000100() throws IOException { 57 testVarInt(0x00000100); 58 } 59 60 @Test 61 public void testVarInt0x12345678() throws IOException { 62 testVarInt(0x12345678); 63 } 64 65 @Test 66 public void testVarIntMinus1() throws IOException { 67 testVarInt(-1); 68 } 69 70 @Test 71 public void testVarIntMinValue() throws IOException { 72 testVarInt(Integer.MIN_VALUE); 73 } 74 75 @Test 76 public void testVarIntMaxValue() throws IOException { 77 testVarInt(Integer.MAX_VALUE); 78 } 79 80 private void testVarInt(int value) throws IOException { 81 out.writeVarInt(value); 82 out.close(); 83 assertEquals(Long.valueOf(value), Long.valueOf(in.readVarInt())); 84 assertEquals(Integer.valueOf(-1), Integer.valueOf(in.read())); 85 } 86 87 @Test 88 public void testPackedBooleanEmpty() throws IOException { 89 testPackedBoolean(); 90 } 91 92 @Test 93 public void testPackedBoolean3() throws IOException { 94 testPackedBoolean(false, false, true); 95 } 96 97 @Test 98 public void testPackedBoolean8() throws IOException { 99 testPackedBoolean(true, false, true, false, false, true, false, true); 100 } 101 102 @Test 103 public void testPackedBoolean9() throws IOException { 104 testPackedBoolean(true, true, false, true, false, false, true, false, 105 true); 106 } 107 108 private void testPackedBoolean(boolean... values) throws IOException { 109 out.writeBooleanArray(values); 110 out.close(); 111 final boolean[] actual = in.readBooleanArray(); 112 for (int i = 0; i < values.length; i++) { 113 assertEquals("Index " + i, Boolean.valueOf(values[i]), 114 Boolean.valueOf(actual[i])); 115 } 116 } 117 118} 119