• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 com.android.internal.util;
18 
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.fail;
22 
23 import androidx.test.filters.SmallTest;
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 @SmallTest
30 @RunWith(AndroidJUnit4.class)
31 public class RingBufferTest {
32 
33     @Test
testEmptyRingBuffer()34     public void testEmptyRingBuffer() {
35         RingBuffer<String> buffer = new RingBuffer<>(String.class, 100);
36 
37         assertArrayEquals(new String[0], buffer.toArray());
38     }
39 
40     @Test
testIncorrectConstructorArguments()41     public void testIncorrectConstructorArguments() {
42         try {
43             RingBuffer<String> buffer = new RingBuffer<>(String.class, -10);
44             fail("Should not be able to create a negative capacity RingBuffer");
45         } catch (IllegalArgumentException expected) {
46         }
47 
48         try {
49             RingBuffer<String> buffer = new RingBuffer<>(String.class, 0);
50             fail("Should not be able to create a 0 capacity RingBuffer");
51         } catch (IllegalArgumentException expected) {
52         }
53     }
54 
55     @Test
testRingBufferWithNoWrapping()56     public void testRingBufferWithNoWrapping() {
57         RingBuffer<String> buffer = new RingBuffer<>(String.class, 100);
58 
59         buffer.append("a");
60         buffer.append("b");
61         buffer.append("c");
62         buffer.append("d");
63         buffer.append("e");
64 
65         String[] expected = {"a", "b", "c", "d", "e"};
66         assertArrayEquals(expected, buffer.toArray());
67     }
68 
69     @Test
testRingBufferWithCapacity1()70     public void testRingBufferWithCapacity1() {
71         RingBuffer<String> buffer = new RingBuffer<>(String.class, 1);
72 
73         buffer.append("a");
74         assertArrayEquals(new String[]{"a"}, buffer.toArray());
75 
76         buffer.append("b");
77         assertArrayEquals(new String[]{"b"}, buffer.toArray());
78 
79         buffer.append("c");
80         assertArrayEquals(new String[]{"c"}, buffer.toArray());
81 
82         buffer.append("d");
83         assertArrayEquals(new String[]{"d"}, buffer.toArray());
84 
85         buffer.append("e");
86         assertArrayEquals(new String[]{"e"}, buffer.toArray());
87     }
88 
89     @Test
testRingBufferWithWrapping()90     public void testRingBufferWithWrapping() {
91         int capacity = 100;
92         RingBuffer<String> buffer = new RingBuffer<>(String.class, capacity);
93 
94         buffer.append("a");
95         buffer.append("b");
96         buffer.append("c");
97         buffer.append("d");
98         buffer.append("e");
99 
100         String[] expected1 = {"a", "b", "c", "d", "e"};
101         assertArrayEquals(expected1, buffer.toArray());
102 
103         String[] expected2 = new String[capacity];
104         int firstIndex = 0;
105         int lastIndex = capacity - 1;
106 
107         expected2[firstIndex] = "e";
108         for (int i = 1; i < capacity; i++) {
109             buffer.append("x");
110             expected2[i] = "x";
111         }
112         assertArrayEquals(expected2, buffer.toArray());
113 
114         buffer.append("x");
115         expected2[firstIndex] = "x";
116         assertArrayEquals(expected2, buffer.toArray());
117 
118         for (int i = 0; i < 10; i++) {
119             for (String s : expected2) {
120                 buffer.append(s);
121             }
122         }
123         assertArrayEquals(expected2, buffer.toArray());
124 
125         buffer.append("a");
126         expected2[lastIndex] = "a";
127         assertArrayEquals(expected2, buffer.toArray());
128     }
129 
130     @Test
testGetNextSlot()131     public void testGetNextSlot() {
132         int capacity = 100;
133         RingBuffer<DummyClass1> buffer = new RingBuffer<>(DummyClass1.class, capacity);
134 
135         final DummyClass1[] actual = new DummyClass1[capacity];
136         final DummyClass1[] expected = new DummyClass1[capacity];
137         for (int i = 0; i < capacity; ++i) {
138             final DummyClass1 obj = buffer.getNextSlot();
139             obj.x = capacity * i;
140             actual[i] = obj;
141             expected[i] = new DummyClass1();
142             expected[i].x = capacity * i;
143         }
144         assertArrayEquals(expected, buffer.toArray());
145 
146         for (int i = 0; i < capacity; ++i) {
147             if (actual[i] != buffer.getNextSlot()) {
148                 fail("getNextSlot() should re-use objects if available");
149             }
150         }
151 
152         RingBuffer<DummyClass2> buffer2 = new RingBuffer<>(DummyClass2.class, capacity);
153         assertNull("getNextSlot() should return null if the object can't be initiated "
154                 + "(No nullary constructor)", buffer2.getNextSlot());
155 
156         RingBuffer<DummyClass3> buffer3 = new RingBuffer<>(DummyClass3.class, capacity);
157         assertNull("getNextSlot() should return null if the object can't be initiated "
158                 + "(Inaccessible class)", buffer3.getNextSlot());
159     }
160 
161     public static final class DummyClass1 {
162         int x;
163 
equals(Object o)164         public boolean equals(Object o) {
165             if (o instanceof DummyClass1) {
166                 final DummyClass1 other = (DummyClass1) o;
167                 return other.x == this.x;
168             }
169             return false;
170         }
171     }
172 
173     public static final class DummyClass2 {
DummyClass2(int x)174         public DummyClass2(int x) {}
175     }
176 
177     private static final class DummyClass3 {}
178 }
179