From 3e37dafd74f65cfe649ed49ccd0d759dd7d18ac8 Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Thu, 3 Oct 2024 17:00:26 -0700 Subject: [PATCH] Add unit test coverage for CodedOutputStream.write(byte[],int,int) There was no unit test coverage for the out of space scenario here before. PiperOrigin-RevId: 682086240 --- .../protobuf/CodedOutputStreamTest.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java index 0bedc270ff..6503c785ac 100644 --- a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -788,7 +788,36 @@ public class CodedOutputStreamTest { } @Test - public void testWriteByteArrayWithOffsets() throws Exception { + public void testWrite_byteArray() throws Exception { + byte[] bytes = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + Coder coder = outputType.newCoder(100); + CodedOutputStream codedStream = coder.stream(); + + codedStream.write(bytes, 0, bytes.length); + codedStream.flush(); + assertThat(codedStream.getTotalBytesWritten()).isEqualTo(10); + + assertThat(coder.toByteArray()).isEqualTo(bytes); + } + + @Test + // Some coders throw immediately on write, some throw on flush. + @SuppressWarnings("AssertThrowsMultipleStatements") + public void testWrite_byteArray_outOfSpace() throws Exception { + byte[] bytes = new byte[10]; + + for (int i = 0; i < 10; i++) { + Coder coder = outputType.newCoder(i); + CodedOutputStream codedStream = coder.stream(); + assertThrows("i=" + i, OutOfSpaceException.class, () -> { + codedStream.write(bytes, 0, bytes.length); + codedStream.flush(); + }); + } + } + + @Test + public void testWriteByteArrayNoTag_withOffsets() throws Exception { assume().that(outputType).isEqualTo(OutputType.ARRAY); byte[] fullArray = bytes(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88);