|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.firestore.util; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertArrayEquals; |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.ByteBuffer; |
| 22 | +import org.junit.Test; |
| 23 | +import org.robolectric.annotation.Config; |
| 24 | + |
| 25 | +@org.junit.runner.RunWith(org.robolectric.RobolectricTestRunner.class) |
| 26 | +@Config(manifest = Config.NONE) |
| 27 | +public class ByteBufferInputStreamTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void canReadSingleByte() { |
| 31 | + ByteBuffer source = ByteBuffer.wrap(new byte[] {1, 2, 3}); |
| 32 | + ByteBufferInputStream inputStream = new ByteBufferInputStream(source); |
| 33 | + |
| 34 | + assertEquals(1, inputStream.read()); |
| 35 | + assertEquals(2, inputStream.read()); |
| 36 | + assertEquals(3, inputStream.read()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void canReadMultipleBytes() throws IOException { |
| 41 | + ByteBuffer source = ByteBuffer.wrap(new byte[] {1, 2, 3}); |
| 42 | + ByteBufferInputStream inputStream = new ByteBufferInputStream(source); |
| 43 | + |
| 44 | + byte[] destination = new byte[3]; |
| 45 | + int bytesRead = inputStream.read(destination); |
| 46 | + |
| 47 | + assertEquals(3, bytesRead); |
| 48 | + assertArrayEquals(new byte[] {1, 2, 3}, destination); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testHandlesEndOfStream() throws IOException { |
| 53 | + ByteBuffer source = ByteBuffer.wrap(new byte[] {1}); |
| 54 | + ByteBufferInputStream inputStream = new ByteBufferInputStream(source); |
| 55 | + |
| 56 | + byte[] destination = new byte[2]; |
| 57 | + int bytesRead = inputStream.read(destination); |
| 58 | + |
| 59 | + assertEquals(1, bytesRead); |
| 60 | + assertArrayEquals(new byte[] {1, 0}, destination); |
| 61 | + |
| 62 | + assertEquals(-1, inputStream.read()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testHandlesNegativeBytes() throws IOException { |
| 67 | + ByteBuffer source = ByteBuffer.wrap(new byte[] {-1}); |
| 68 | + ByteBufferInputStream inputStream = new ByteBufferInputStream(source); |
| 69 | + |
| 70 | + int read = inputStream.read(); |
| 71 | + // The integer value for byte -1 is 255, as the last three bits are set for -1. |
| 72 | + assertEquals(0xFF, read); |
| 73 | + assertEquals(-1, (byte) read); |
| 74 | + } |
| 75 | +} |
0 commit comments