|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.common.io.stream; |
| 11 | + |
| 12 | +import org.elasticsearch.common.bytes.BytesArray; |
| 13 | +import org.elasticsearch.common.bytes.BytesReference; |
| 14 | +import org.elasticsearch.common.util.BigArrays; |
| 15 | +import org.elasticsearch.test.ESTestCase; |
| 16 | +import org.junit.Before; |
| 17 | + |
| 18 | +public class BytesStreamOutputTests extends ESTestCase { |
| 19 | + |
| 20 | + private BytesStreamOutput stream; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void setUp() throws Exception { |
| 24 | + super.setUp(); |
| 25 | + stream = new BytesStreamOutput(); |
| 26 | + } |
| 27 | + |
| 28 | + public void testDefaultConstructor() { |
| 29 | + assertEquals(0, stream.size()); |
| 30 | + assertEquals(0, stream.position()); |
| 31 | + assertEquals(BytesArray.EMPTY, stream.bytes()); |
| 32 | + } |
| 33 | + |
| 34 | + public void testConstructorWithExpectedSize() { |
| 35 | + BytesStreamOutput s = new BytesStreamOutput(randomNonNegativeInt()); |
| 36 | + assertEquals(0, s.size()); |
| 37 | + assertEquals(0, s.position()); |
| 38 | + } |
| 39 | + |
| 40 | + public void testWriteByte() { |
| 41 | + byte i1 = randomByte(); |
| 42 | + byte i2 = randomByte(); |
| 43 | + stream.writeByte(i1); |
| 44 | + stream.writeByte(i2); |
| 45 | + assertEquals(2, stream.size()); |
| 46 | + assertEquals(2, stream.position()); |
| 47 | + assertEquals(i1, stream.bytes().get(0)); |
| 48 | + assertEquals(i2, stream.bytes().get(1)); |
| 49 | + } |
| 50 | + |
| 51 | + public void testWriteBytes() { |
| 52 | + int maxSize = randomIntBetween(10, 100); |
| 53 | + byte[] data = randomByteArrayOfLength(maxSize); |
| 54 | + int length = randomIntBetween(0, maxSize); |
| 55 | + stream.writeBytes(data, 0, length); |
| 56 | + assertEquals(length, stream.size()); |
| 57 | + for (int i = 0; i < length; i++) { |
| 58 | + assertEquals(data[i], stream.bytes().get(i)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public void testWriteBytesWithOffset() { |
| 63 | + int maxSize = randomIntBetween(10, 100); |
| 64 | + byte[] data = randomByteArrayOfLength(maxSize); |
| 65 | + int offset = randomIntBetween(0, maxSize); |
| 66 | + int length = randomIntBetween(0, maxSize - offset); |
| 67 | + stream.writeBytes(data, offset, length); |
| 68 | + assertEquals(length, stream.size()); |
| 69 | + for (int i = 0; i < stream.size(); i++) { |
| 70 | + assertEquals(stream.bytes().get(i), data[i + offset]); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public void testWriteBytesWithLengthGreaterThanData() { |
| 75 | + BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); |
| 76 | + int size = randomIntBetween(0, 100); |
| 77 | + assertThrows(IndexOutOfBoundsException.class, () -> s.writeBytes(randomByteArrayOfLength(size), 0, size + 1)); |
| 78 | + } |
| 79 | + |
| 80 | + public void testResetShrinksAndResetsCount() { |
| 81 | + int byteArrayLength = randomIntBetween(0, 100); |
| 82 | + stream.writeBytes(randomByteArrayOfLength(byteArrayLength), 0, byteArrayLength); |
| 83 | + assertEquals(byteArrayLength, stream.size()); |
| 84 | + stream.reset(); |
| 85 | + assertEquals(0, stream.size()); |
| 86 | + // After reset, should be able to write again |
| 87 | + byte b = randomByte(); |
| 88 | + stream.writeByte(b); |
| 89 | + assertEquals(1, stream.size()); |
| 90 | + assertEquals(b, stream.bytes().get(0)); |
| 91 | + } |
| 92 | + |
| 93 | + public void testSeek() { |
| 94 | + // Do some writing |
| 95 | + BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); |
| 96 | + int byteArrayLength = randomIntBetween(0, 100); |
| 97 | + byte[] byteArray = randomByteArrayOfLength(byteArrayLength); |
| 98 | + s.writeBytes(byteArray, 0, byteArrayLength); |
| 99 | + |
| 100 | + // Seek either over what we've already written, or what we have yet to write |
| 101 | + int seekPosition = randomIntBetween(0, 200); |
| 102 | + s.seek(seekPosition); |
| 103 | + assertEquals(seekPosition, s.size()); |
| 104 | + } |
| 105 | + |
| 106 | + public void testSeekAtBoundaries() { |
| 107 | + // Do some arbitrary writing |
| 108 | + BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); |
| 109 | + int byteArrayLength = randomIntBetween(0, 100); |
| 110 | + byte[] byteArray = randomByteArrayOfLength(byteArrayLength); |
| 111 | + s.writeBytes(byteArray, 0, byteArrayLength); |
| 112 | + |
| 113 | + s.seek(0); |
| 114 | + assertEquals(0, s.size()); |
| 115 | + s.seek(Integer.MAX_VALUE); |
| 116 | + assertEquals(Integer.MAX_VALUE, s.size()); |
| 117 | + } |
| 118 | + |
| 119 | + public void testSeekWithOverflow() { |
| 120 | + // Do some arbitrary writing and then overflow |
| 121 | + BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); |
| 122 | + int byteArrayLength = randomIntBetween(0, 100); |
| 123 | + byte[] byteArray = randomByteArrayOfLength(byteArrayLength); |
| 124 | + s.writeBytes(byteArray, 0, byteArrayLength); |
| 125 | + assertThrows( |
| 126 | + IllegalArgumentException.class, |
| 127 | + () -> s.seek(randomLongBetween((long) Integer.MAX_VALUE + 1, (long) Integer.MAX_VALUE + 100)) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + public void testSkip() { |
| 132 | + // Do some arbitrary writing |
| 133 | + BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); |
| 134 | + int byteArrayLength = randomIntBetween(0, 100); |
| 135 | + byte[] byteArray = randomByteArrayOfLength(byteArrayLength); |
| 136 | + s.writeBytes(byteArray, 0, byteArrayLength); |
| 137 | + |
| 138 | + int skipValue = randomIntBetween(0, 100); |
| 139 | + s.skip(skipValue); |
| 140 | + assertEquals(byteArrayLength + skipValue, s.size()); |
| 141 | + } |
| 142 | + |
| 143 | + public void testSkipAtBoundaries() { |
| 144 | + BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); |
| 145 | + s.skip(0); |
| 146 | + assertEquals(0, s.size()); |
| 147 | + s.skip(Integer.MAX_VALUE); |
| 148 | + assertEquals(Integer.MAX_VALUE, s.size()); |
| 149 | + } |
| 150 | + |
| 151 | + public void testSkipWithOverflow() { |
| 152 | + // Do some arbitrary writing and then overflow |
| 153 | + BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); |
| 154 | + int byteArrayLength = randomIntBetween(1, 100); |
| 155 | + byte[] byteArray = randomByteArrayOfLength(byteArrayLength); |
| 156 | + s.writeBytes(byteArray, 0, byteArrayLength); |
| 157 | + |
| 158 | + int skipPosition = randomIntBetween(Integer.MAX_VALUE - byteArrayLength + 1, Integer.MAX_VALUE); |
| 159 | + assertThrows(IllegalArgumentException.class, () -> s.skip(skipPosition)); |
| 160 | + } |
| 161 | + |
| 162 | + public void testCopyBytes() { |
| 163 | + int byteArrayLength = randomIntBetween(0, 100); |
| 164 | + byte[] byteArray = randomByteArrayOfLength(byteArrayLength); |
| 165 | + stream.writeBytes(byteArray, 0, byteArrayLength); |
| 166 | + BytesReference copy = stream.copyBytes(); |
| 167 | + assertEquals(byteArrayLength, copy.length()); |
| 168 | + for (int i = 0; i < byteArrayLength; i++) { |
| 169 | + assertEquals(byteArray[i], copy.get(i)); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public void testFlushAndCloseNoop() { |
| 174 | + stream.writeByte(randomByte()); |
| 175 | + stream.flush(); // should do nothing |
| 176 | + stream.close(); // should do nothing |
| 177 | + assertEquals(1, stream.size()); |
| 178 | + } |
| 179 | +} |
0 commit comments