1 /*
2  * Copyright (C) 2022 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.server;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 
22 import org.junit.Test;
23 
24 /**
25  * Test {@link CircularQueue}.
26  */
27 public class CircularQueueTest {
28 
29     private CircularQueue<Integer, String> mQueue;
30     private static final int LIMIT = 2;
31 
32     @Test
testQueueInsertionAndDeletion()33     public void testQueueInsertionAndDeletion() {
34         mQueue = new CircularQueue<>(LIMIT);
35         mQueue.put(1, "A");
36         assertEquals(mQueue.getElement(1), "A");
37         mQueue.removeElement(1);
38         assertNull(mQueue.getElement(1));
39     }
40 
41     @Test
testQueueLimit()42     public void testQueueLimit() {
43         mQueue = new CircularQueue<>(LIMIT);
44         mQueue.put(1, "A");
45         mQueue.put(2, "B");
46         String removedElement = mQueue.put(3, "C");
47         assertNull(mQueue.getElement(1));
48         assertEquals(mQueue.getElement(2), "B");
49         assertEquals(mQueue.getElement(3), "C");
50         // Confirming that put is returning the deleted element
51         assertEquals(removedElement, "A");
52     }
53 
54     @Test
testQueueElements()55     public void testQueueElements() {
56         mQueue = new CircularQueue<>(LIMIT);
57         mQueue.put(1, "A");
58         mQueue.put(2, "B");
59         assertEquals(mQueue.values().size(), 2);
60         mQueue.put(3, "C");
61         assertEquals(mQueue.values().size(), 2);
62     }
63 }
64