|
1 | 1 | package com.thealgorithms.datastructures.buffers; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | | -import static org.junit.jupiter.api.Assertions.assertFalse; |
5 | 4 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
6 | 6 | import static org.junit.jupiter.api.Assertions.assertTrue; |
7 | 7 |
|
8 | | -import java.util.ArrayList; |
9 | | -import java.util.Comparator; |
10 | | -import java.util.List; |
11 | | -import java.util.concurrent.CountDownLatch; |
12 | | -import java.util.concurrent.ExecutorService; |
13 | | -import java.util.concurrent.Executors; |
14 | | -import java.util.concurrent.ThreadLocalRandom; |
15 | | -import java.util.concurrent.TimeUnit; |
16 | | -import java.util.concurrent.atomic.AtomicIntegerArray; |
17 | | -import org.junit.jupiter.api.BeforeEach; |
18 | | -import org.junit.jupiter.api.RepeatedTest; |
19 | 8 | import org.junit.jupiter.api.Test; |
20 | 9 |
|
21 | 10 | class CircularBufferTest { |
22 | | - private static final int BUFFER_SIZE = 10; |
23 | | - private CircularBuffer<Integer> buffer; |
24 | | - |
25 | | - @BeforeEach |
26 | | - void setUp() { |
27 | | - buffer = new CircularBuffer<>(BUFFER_SIZE); |
28 | | - } |
29 | 11 |
|
30 | 12 | @Test |
31 | | - void isEmpty() { |
| 13 | + void testInitialization() { |
| 14 | + CircularBuffer<Integer> buffer = new CircularBuffer<>(5); |
32 | 15 | assertTrue(buffer.isEmpty()); |
33 | | - buffer.put(generateInt()); |
34 | | - assertFalse(buffer.isEmpty()); |
| 16 | + assertEquals(Boolean.FALSE, buffer.isFull()); |
35 | 17 | } |
36 | 18 |
|
37 | 19 | @Test |
38 | | - void isFull() { |
39 | | - assertFalse(buffer.isFull()); |
40 | | - buffer.put(generateInt()); |
41 | | - assertFalse(buffer.isFull()); |
| 20 | + void testPutAndGet() { |
| 21 | + CircularBuffer<String> buffer = new CircularBuffer<>(3); |
42 | 22 |
|
43 | | - for (int i = 1; i < BUFFER_SIZE; i++) { |
44 | | - buffer.put(generateInt()); |
45 | | - } |
| 23 | + assertTrue(buffer.put("A")); |
| 24 | + assertEquals(Boolean.FALSE, buffer.isEmpty()); |
| 25 | + assertEquals(Boolean.FALSE, buffer.isFull()); |
| 26 | + |
| 27 | + buffer.put("B"); |
| 28 | + buffer.put("C"); |
46 | 29 | assertTrue(buffer.isFull()); |
| 30 | + |
| 31 | + assertEquals("A", buffer.get()); |
| 32 | + assertEquals("B", buffer.get()); |
| 33 | + assertEquals("C", buffer.get()); |
| 34 | + assertTrue(buffer.isEmpty()); |
47 | 35 | } |
48 | 36 |
|
49 | 37 | @Test |
50 | | - void get() { |
51 | | - assertNull(buffer.get()); |
52 | | - for (int i = 0; i < 100; i++) { |
53 | | - buffer.put(i); |
54 | | - } |
55 | | - for (int i = 0; i < BUFFER_SIZE; i++) { |
56 | | - assertEquals(i, buffer.get()); |
57 | | - } |
| 38 | + void testOverwrite() { |
| 39 | + CircularBuffer<Integer> buffer = new CircularBuffer<>(3); |
| 40 | + |
| 41 | + buffer.put(1); |
| 42 | + buffer.put(2); |
| 43 | + buffer.put(3); |
| 44 | + assertEquals(Boolean.FALSE, buffer.put(4)); // This should overwrite 1 |
| 45 | + |
| 46 | + assertEquals(2, buffer.get()); |
| 47 | + assertEquals(3, buffer.get()); |
| 48 | + assertEquals(4, buffer.get()); |
58 | 49 | assertNull(buffer.get()); |
59 | 50 | } |
60 | 51 |
|
61 | 52 | @Test |
62 | | - void put() { |
63 | | - for (int i = 0; i < BUFFER_SIZE; i++) { |
64 | | - assertTrue(buffer.put(generateInt())); |
65 | | - } |
66 | | - assertFalse(buffer.put(generateInt())); |
| 53 | + void testEmptyBuffer() { |
| 54 | + CircularBuffer<Double> buffer = new CircularBuffer<>(2); |
| 55 | + assertNull(buffer.get()); |
67 | 56 | } |
68 | 57 |
|
69 | | - @RepeatedTest(1000) |
70 | | - void concurrentTest() throws InterruptedException { |
71 | | - final int numberOfThreadsForProducers = 3; |
72 | | - final int numberOfThreadsForConsumers = 2; |
73 | | - final int numberOfItems = 300; |
74 | | - final CountDownLatch producerCountDownLatch = new CountDownLatch(numberOfItems); |
75 | | - final CountDownLatch consumerCountDownLatch = new CountDownLatch(numberOfItems); |
76 | | - final AtomicIntegerArray resultAtomicArray = new AtomicIntegerArray(numberOfItems); |
77 | | - |
78 | | - // We are running 2 ExecutorService simultaneously 1 - producer, 2 - consumer |
79 | | - // Run producer threads to populate buffer. |
80 | | - ExecutorService putExecutors = Executors.newFixedThreadPool(numberOfThreadsForProducers); |
81 | | - putExecutors.execute(() -> { |
82 | | - while (producerCountDownLatch.getCount() > 0) { |
83 | | - int count = (int) producerCountDownLatch.getCount(); |
84 | | - boolean put = buffer.put(count); |
85 | | - while (!put) { |
86 | | - put = buffer.put(count); |
87 | | - } |
88 | | - producerCountDownLatch.countDown(); |
89 | | - } |
90 | | - }); |
91 | | - |
92 | | - // Run consumer threads to retrieve the data from buffer. |
93 | | - ExecutorService getExecutors = Executors.newFixedThreadPool(numberOfThreadsForConsumers); |
94 | | - getExecutors.execute(() -> { |
95 | | - while (consumerCountDownLatch.getCount() > 0) { |
96 | | - int count = (int) consumerCountDownLatch.getCount(); |
97 | | - Integer item = buffer.get(); |
98 | | - while (item == null) { |
99 | | - item = buffer.get(); |
100 | | - } |
101 | | - resultAtomicArray.set(count - 1, item); |
102 | | - consumerCountDownLatch.countDown(); |
103 | | - } |
104 | | - }); |
105 | | - |
106 | | - producerCountDownLatch.await(); |
107 | | - consumerCountDownLatch.await(); |
108 | | - putExecutors.shutdown(); |
109 | | - getExecutors.shutdown(); |
110 | | - shutDownExecutorSafely(putExecutors); |
111 | | - shutDownExecutorSafely(getExecutors); |
112 | | - |
113 | | - List<Integer> resultArray = getSortedListFrom(resultAtomicArray); |
114 | | - for (int i = 0; i < numberOfItems; i++) { |
115 | | - int expectedItem = i + 1; |
116 | | - assertEquals(expectedItem, resultArray.get(i)); |
117 | | - } |
| 58 | + @Test |
| 59 | + void testFullBuffer() { |
| 60 | + CircularBuffer<Character> buffer = new CircularBuffer<>(2); |
| 61 | + buffer.put('A'); |
| 62 | + buffer.put('B'); |
| 63 | + assertTrue(buffer.isFull()); |
| 64 | + assertEquals(Boolean.FALSE, buffer.put('C')); // This should overwrite 'A' |
| 65 | + assertEquals('B', buffer.get()); |
| 66 | + assertEquals('C', buffer.get()); |
118 | 67 | } |
119 | 68 |
|
120 | | - private int generateInt() { |
121 | | - return ThreadLocalRandom.current().nextInt(0, 100); |
122 | | - } |
| 69 | + @Test |
| 70 | + void testIllegalArguments() { |
| 71 | + assertThrows(IllegalArgumentException.class, () -> new CircularBuffer<>(0)); |
| 72 | + assertThrows(IllegalArgumentException.class, () -> new CircularBuffer<>(-1)); |
123 | 73 |
|
124 | | - private void shutDownExecutorSafely(ExecutorService executorService) { |
125 | | - try { |
126 | | - if (!executorService.awaitTermination(1_000, TimeUnit.MILLISECONDS)) { |
127 | | - executorService.shutdownNow(); |
128 | | - } |
129 | | - } catch (InterruptedException e) { |
130 | | - executorService.shutdownNow(); |
131 | | - } |
| 74 | + CircularBuffer<String> buffer = new CircularBuffer<>(1); |
| 75 | + assertThrows(IllegalArgumentException.class, () -> buffer.put(null)); |
132 | 76 | } |
133 | 77 |
|
134 | | - public List<Integer> getSortedListFrom(AtomicIntegerArray atomicArray) { |
135 | | - int length = atomicArray.length(); |
136 | | - ArrayList<Integer> result = new ArrayList<>(length); |
137 | | - for (int i = 0; i < length; i++) { |
138 | | - result.add(atomicArray.get(i)); |
| 78 | + @Test |
| 79 | + void testLargeBuffer() { |
| 80 | + CircularBuffer<Integer> buffer = new CircularBuffer<>(1000); |
| 81 | + for (int i = 0; i < 1000; i++) { |
| 82 | + buffer.put(i); |
139 | 83 | } |
140 | | - result.sort(Comparator.comparingInt(o -> o)); |
141 | | - return result; |
| 84 | + assertTrue(buffer.isFull()); |
| 85 | + buffer.put(1000); // This should overwrite 0 |
| 86 | + assertEquals(1, buffer.get()); |
142 | 87 | } |
143 | 88 | } |
0 commit comments