Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.common.io.stream;

import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;

public class BytesStreamOutputTests extends ESTestCase {

private BytesStreamOutput stream;

@Before
public void setUp() throws Exception {
super.setUp();
stream = new BytesStreamOutput();
}

public void testDefaultConstructor() {
assertEquals(0, stream.size());
assertEquals(0, stream.position());
assertEquals(BytesArray.EMPTY, stream.bytes());
}

public void testConstructorWithExpectedSize() {
BytesStreamOutput s = new BytesStreamOutput(randomNonNegativeInt());
assertEquals(0, s.size());
assertEquals(0, s.position());
}

public void testWriteByte() {
byte i1 = randomByte();
byte i2 = randomByte();
stream.writeByte(i1);
stream.writeByte(i2);
assertEquals(2, stream.size());
assertEquals(2, stream.position());
assertEquals(i1, stream.bytes().get(0));
assertEquals(i2, stream.bytes().get(1));
}

public void testWriteBytes() {
int maxSize = randomIntBetween(10, 100);
byte[] data = randomByteArrayOfLength(maxSize);
int length = randomIntBetween(0, maxSize);
stream.writeBytes(data, 0, length);
assertEquals(length, stream.size());
for (int i = 0; i < length; i++) {
assertEquals(data[i], stream.bytes().get(i));
}
}

public void testWriteBytesWithOffset() {
int maxSize = randomIntBetween(10, 100);
byte[] data = randomByteArrayOfLength(maxSize);
int offset = randomIntBetween(0, maxSize);
int length = randomIntBetween(0, maxSize - offset);
stream.writeBytes(data, offset, length);
assertEquals(length, stream.size());
for (int i = 0; i < stream.size(); i++) {
assertEquals(stream.bytes().get(i), data[i + offset]);
}
}

public void testWriteBytesWithLengthGreaterThanData() {
BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE);
int size = randomIntBetween(0, 100);
assertThrows(IndexOutOfBoundsException.class, () -> s.writeBytes(randomByteArrayOfLength(size), 0, size + 1));
}

public void testResetShrinksAndResetsCount() {
int byteArrayLength = randomIntBetween(0, 100);
stream.writeBytes(randomByteArrayOfLength(byteArrayLength), 0, byteArrayLength);
stream.reset();
assertEquals(0, stream.size());
// After reset, should be able to write again
byte b = randomByte();
stream.writeByte(b);
assertEquals(1, stream.size());
assertEquals(b, stream.bytes().get(0));
}

public void testSeekAndSkip() {
int byteArrayLength = randomIntBetween(0, 100);
byte[] byteArray = randomByteArrayOfLength(byteArrayLength);
stream.writeBytes(byteArray, 0, byteArrayLength);

int seekPosition = randomIntBetween(byteArrayLength, byteArrayLength + 100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, a seek could be done to somewhere between 0 and byteArrayLength, and then a read can be done to show that it matches the byteArray? Same thing with skip below, though the seek would have to be further constrained to leave some room to test skip on the data set.

Would that be more realistic than setting the read position into space that hasn't been written yet? Or maybe test both?

stream.seek(seekPosition);
assertEquals(seekPosition, stream.size());

int skipLength = randomIntBetween(0, 100);
stream.skip(skipLength);
assertEquals(seekPosition + skipLength, stream.size());
}

public void testCopyBytes() {
int byteArrayLength = randomIntBetween(0, 100);
byte[] byteArray = randomByteArrayOfLength(byteArrayLength);
stream.writeBytes(byteArray, 0, byteArrayLength);
BytesReference copy = stream.copyBytes();
assertEquals(byteArrayLength, copy.length());
for (int i = 0; i < byteArrayLength; i++) {
assertEquals(byteArray[i], copy.get(i));
}
}

public void testFlushAndCloseNoop() {
stream.writeByte(randomByte());
stream.flush(); // should do nothing
stream.close(); // should do nothing
assertEquals(1, stream.size());
}
}