1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
* DSI utilities
*
* Copyright (C) 2011-2023 Sebastiano Vigna
*
* This program and the accompanying materials are made available under the
* terms of the GNU Lesser General Public License v2.1 or later,
* which is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html,
* or the Apache Software License 2.0, which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
* SPDX-License-Identifier: LGPL-2.1-or-later OR Apache-2.0
*/
package it.unimi.dsi.io;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream;
import it.unimi.dsi.util.SplitMix64Random;
@SuppressWarnings("resource")
public class OutputBitStreamTest {
@Test
public void testPositionWrapped() throws IOException {
final byte[] a = new byte[2];
final OutputBitStream obs = new OutputBitStream(a);
obs.position(8);
obs.writeInt(1, 8);
assertArrayEquals(new byte[] { 0, 1 }, a);
obs.position(0);
obs.writeInt(1, 1);
obs.flush();
assertArrayEquals(new byte[] { -128, 1 }, a);
}
@Test(expected=IllegalArgumentException.class)
public void testPositionUnaligned() throws IOException {
final byte[] a = new byte[2];
final OutputBitStream obs = new OutputBitStream(a);
obs.position(1);
}
@Test
public void testWriteGammas() throws IOException {
final FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
final OutputBitStream obs = new OutputBitStream(fbaos);
SplitMix64Random random = new SplitMix64Random(0);
for(int i = 0; i < 100; i++) {
final int l = random.nextInt(20);
final int[] a = new int[l + 5];
for(int p = 0; p < l; p++) a[p] = random.nextInt(10000);
final long writtenBits = obs.writtenBits();
final long length = obs.writeGammas(a, l);
assertEquals(obs.writtenBits() - writtenBits, length);
}
obs.flush();
final InputBitStream ibs = new InputBitStream(fbaos.array);
random = new SplitMix64Random(0);
for(int i = 0; i < 100; i++) {
final int l = random.nextInt(20);
final int[] a = new int[l];
ibs.readGammas(a, l);
for(int p = 0; p < l; p++) assertEquals(random.nextInt(10000), a[p]);
}
}
@Test
public void testWriteShiftedGammas() throws IOException {
final FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
final OutputBitStream obs = new OutputBitStream(fbaos);
SplitMix64Random random = new SplitMix64Random(0);
for(int i = 0; i < 100; i++) {
final int l = random.nextInt(20);
final int[] a = new int[l + 5];
for(int p = 0; p < l; p++) a[p] = random.nextInt(10000);
final long writtenBits = obs.writtenBits();
final long length = obs.writeShiftedGammas(a, l);
assertEquals(obs.writtenBits() - writtenBits, length);
}
obs.flush();
final InputBitStream ibs = new InputBitStream(fbaos.array);
random = new SplitMix64Random(0);
for(int i = 0; i < 100; i++) {
final int l = random.nextInt(20);
final int[] a = new int[l];
ibs.readShiftedGammas(a, l);
for(int p = 0; p < l; p++) assertEquals(random.nextInt(10000), a[p]);
}
}
@Test
public void testWriteDeltas() throws IOException {
final FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
final OutputBitStream obs = new OutputBitStream(fbaos);
SplitMix64Random random = new SplitMix64Random(0);
for(int i = 0; i < 100; i++) {
final int l = random.nextInt(20);
final int[] a = new int[l + 5];
for(int p = 0; p < l; p++) a[p] = random.nextInt(10000);
final long writtenBits = obs.writtenBits();
final long length = obs.writeDeltas(a, l);
assertEquals(obs.writtenBits() - writtenBits, length);
}
obs.flush();
final InputBitStream ibs = new InputBitStream(fbaos.array);
random = new SplitMix64Random(0);
for(int i = 0; i < 100; i++) {
final int l = random.nextInt(20);
final int[] a = new int[l];
ibs.readDeltas(a, l);
for(int p = 0; p < l; p++) assertEquals(random.nextInt(10000), a[p]);
}
}
}
|