|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2016 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.io; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertArrayEquals; |
| 35 | +import static org.junit.Assert.assertEquals; |
| 36 | + |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +/** |
| 41 | + * Abstract superclass for testing {@link ByteBank} implementations. |
| 42 | + * |
| 43 | + * @author Gabriel Einsdorf |
| 44 | + */ |
| 45 | +public abstract class ByteBankTest { |
| 46 | + |
| 47 | + private static byte[] testBytes = { 0, -1, 2, -3, 4, 120, -128, 127, 32, 42 }; |
| 48 | + private ByteBank bank; |
| 49 | + |
| 50 | + /** |
| 51 | + * @return the ByteBank implementation to test |
| 52 | + */ |
| 53 | + public abstract ByteBank createByteBank(); |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setup() { |
| 57 | + bank = createByteBank(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testSetGetBytesArray() { |
| 62 | + // read in full array |
| 63 | + bank.setBytes(0l, testBytes.clone(), 0, testBytes.length); |
| 64 | + |
| 65 | + // read out full array |
| 66 | + assertEqualRead(testBytes.length, 0); |
| 67 | + assertEqualRead(testBytes.length - 4, 2); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testSetGetBytesPartialArray() { |
| 72 | + // read in the partial array |
| 73 | + bank.setBytes(0l, testBytes, 0, testBytes.length - 4); |
| 74 | + |
| 75 | + // read out the partial array |
| 76 | + assertEqualRead(testBytes.length - 4, 0); |
| 77 | + assertEqualRead(testBytes.length - 4, 2); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testSetGetByte() { |
| 82 | + final int numElements = 200_000; |
| 83 | + for (int i = 0; i < numElements; i++) { |
| 84 | + bank.setByte(i, (byte) i); |
| 85 | + } |
| 86 | + |
| 87 | + for (int i = 0; i < numElements; i++) { |
| 88 | + assertEquals((byte) i, bank.getByte(i)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testClear() { |
| 94 | + bank.setBytes(0, testBytes, 0, testBytes.length); |
| 95 | + assertEquals(testBytes.length - 1, bank.getMaxPos()); |
| 96 | + |
| 97 | + bank.clear(); |
| 98 | + assertEquals(0, bank.getMaxPos()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testAppendBytes() { |
| 103 | + // simple append |
| 104 | + bank.appendBytes(testBytes, testBytes.length); |
| 105 | + assertEqualRead(testBytes.length, 0); |
| 106 | + |
| 107 | + // append to buffer that already contains data |
| 108 | + bank.clear(); |
| 109 | + bank.setByte(0l, (byte) 42); |
| 110 | + bank.setByte(1l, (byte) 43); |
| 111 | + bank.appendBytes(testBytes, testBytes.length); |
| 112 | + |
| 113 | + final byte[] expected = new byte[testBytes.length + 2]; |
| 114 | + expected[0] = 42; |
| 115 | + expected[1] = 43; |
| 116 | + System.arraycopy(testBytes, 0, expected, 2, testBytes.length); |
| 117 | + |
| 118 | + final byte[] actuals = new byte[expected.length]; |
| 119 | + bank.getBytes(0, actuals); |
| 120 | + |
| 121 | + assertArrayEquals(expected, actuals); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Asserts that {@link #bank} contains the same bytes as {@link #testBytes}, |
| 126 | + * allows to specify an offset. |
| 127 | + * |
| 128 | + * @param length how many bytes (starting from the offset) of |
| 129 | + * {@link #testBytes} are tested, this allows to test partial reads. |
| 130 | + * @param offset the offset position |
| 131 | + */ |
| 132 | + private void assertEqualRead(final int length, final int offset) { |
| 133 | + // read from offset up to the length of the given array |
| 134 | + final byte[] bytes = new byte[length]; |
| 135 | + final int read = bank.getBytes(offset, bytes); |
| 136 | + |
| 137 | + final byte[] expected = new byte[length]; |
| 138 | + System.arraycopy(testBytes, offset, expected, 0, read); |
| 139 | + assertArrayEquals(expected, bytes); |
| 140 | + |
| 141 | + // read from offset to offset |
| 142 | + final byte[] offsetBytes = new byte[testBytes.length]; |
| 143 | + final int readOffset = bank.getBytes(offset, offsetBytes, offset, length); |
| 144 | + |
| 145 | + final byte[] offsetExpected = new byte[testBytes.length]; |
| 146 | + System.arraycopy(testBytes, offset, offsetExpected, offset, readOffset); |
| 147 | + assertArrayEquals(expected, bytes); |
| 148 | + } |
| 149 | +} |
0 commit comments