• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.aconfig.storage.test;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.aconfig.storage.ByteBufferReader;
22 
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 import java.nio.ByteBuffer;
28 import java.nio.ByteOrder;
29 import java.nio.charset.StandardCharsets;
30 
31 @RunWith(JUnit4.class)
32 public class ByteBufferReaderTest {
33 
34     @Test
testReadByte()35     public void testReadByte() {
36         ByteBuffer buffer = ByteBuffer.allocate(1);
37         byte expect = 10;
38         buffer.put(expect).rewind();
39 
40         ByteBufferReader reader = new ByteBufferReader(buffer);
41         assertEquals(expect, reader.readByte());
42     }
43 
44     @Test
testReadShort()45     public void testReadShort() {
46         ByteBuffer buffer = ByteBuffer.allocate(4);
47         buffer.order(ByteOrder.LITTLE_ENDIAN);
48         short expect = Short.MAX_VALUE;
49         buffer.putShort(expect).rewind();
50 
51         ByteBufferReader reader = new ByteBufferReader(buffer);
52         assertEquals(expect, reader.readShort());
53     }
54 
55     @Test
testReadInt()56     public void testReadInt() {
57         ByteBuffer buffer = ByteBuffer.allocate(4);
58         buffer.order(ByteOrder.LITTLE_ENDIAN);
59         int expect = 10000;
60         buffer.putInt(expect).rewind();
61 
62         ByteBufferReader reader = new ByteBufferReader(buffer);
63         assertEquals(expect, reader.readInt());
64     }
65 
66     @Test
testReadString()67     public void testReadString() {
68         String expect = "test read string";
69         byte[] bytes = expect.getBytes(StandardCharsets.UTF_8);
70 
71         ByteBuffer buffer = ByteBuffer.allocate(expect.length() * 2 + 4);
72         buffer.order(ByteOrder.LITTLE_ENDIAN);
73         buffer.putInt(expect.length()).put(bytes).rewind();
74 
75         ByteBufferReader reader = new ByteBufferReader(buffer);
76 
77         assertEquals(expect, reader.readString());
78     }
79 }
80