-
Notifications
You must be signed in to change notification settings - Fork 20k
Enhance class & function documentation in CircularBuffer.java
#5582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alxkm
merged 12 commits into
TheAlgorithms:master
from
Hardvan:circular_buffer_improvement
Oct 15, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
851986b
Enhance class & function documentation in `CircularBuffer.java`
Hardvan 8b3e29f
Add suggested changes
Hardvan bfe4aee
Merge branch 'master' into circular_buffer_improvement
alxkm eba6229
Update directory
alxkm 67e3377
Merge branch 'master' into circular_buffer_improvement
alxkm 08b31f4
Update directory
alxkm 878a163
Merge branch 'master' into circular_buffer_improvement
alxkm 5c7ea2b
Merge branch 'master' into circular_buffer_improvement
alxkm b9e87b5
Update directory
alxkm cd65fe1
Fix test cases
Hardvan 8b3643e
Fix
Hardvan c8f60d2
Merge branch 'master' into circular_buffer_improvement
alxkm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 54 additions & 109 deletions
163
src/test/java/com/thealgorithms/datastructures/buffers/CircularBufferTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,143 +1,88 @@ | ||
package com.thealgorithms.datastructures.buffers; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicIntegerArray; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.RepeatedTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CircularBufferTest { | ||
private static final int BUFFER_SIZE = 10; | ||
private CircularBuffer<Integer> buffer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
buffer = new CircularBuffer<>(BUFFER_SIZE); | ||
} | ||
|
||
@Test | ||
void isEmpty() { | ||
void testInitialization() { | ||
CircularBuffer<Integer> buffer = new CircularBuffer<>(5); | ||
assertTrue(buffer.isEmpty()); | ||
buffer.put(generateInt()); | ||
assertFalse(buffer.isEmpty()); | ||
assertEquals(Boolean.FALSE, buffer.isFull()); | ||
} | ||
|
||
@Test | ||
void isFull() { | ||
assertFalse(buffer.isFull()); | ||
buffer.put(generateInt()); | ||
assertFalse(buffer.isFull()); | ||
void testPutAndGet() { | ||
CircularBuffer<String> buffer = new CircularBuffer<>(3); | ||
|
||
for (int i = 1; i < BUFFER_SIZE; i++) { | ||
buffer.put(generateInt()); | ||
} | ||
assertTrue(buffer.put("A")); | ||
assertEquals(Boolean.FALSE, buffer.isEmpty()); | ||
assertEquals(Boolean.FALSE, buffer.isFull()); | ||
|
||
buffer.put("B"); | ||
buffer.put("C"); | ||
assertTrue(buffer.isFull()); | ||
|
||
assertEquals("A", buffer.get()); | ||
assertEquals("B", buffer.get()); | ||
assertEquals("C", buffer.get()); | ||
assertTrue(buffer.isEmpty()); | ||
} | ||
|
||
@Test | ||
void get() { | ||
assertNull(buffer.get()); | ||
for (int i = 0; i < 100; i++) { | ||
buffer.put(i); | ||
} | ||
for (int i = 0; i < BUFFER_SIZE; i++) { | ||
assertEquals(i, buffer.get()); | ||
} | ||
void testOverwrite() { | ||
CircularBuffer<Integer> buffer = new CircularBuffer<>(3); | ||
|
||
buffer.put(1); | ||
buffer.put(2); | ||
buffer.put(3); | ||
assertEquals(Boolean.FALSE, buffer.put(4)); // This should overwrite 1 | ||
|
||
assertEquals(2, buffer.get()); | ||
assertEquals(3, buffer.get()); | ||
assertEquals(4, buffer.get()); | ||
assertNull(buffer.get()); | ||
} | ||
|
||
@Test | ||
void put() { | ||
for (int i = 0; i < BUFFER_SIZE; i++) { | ||
assertTrue(buffer.put(generateInt())); | ||
} | ||
assertFalse(buffer.put(generateInt())); | ||
void testEmptyBuffer() { | ||
CircularBuffer<Double> buffer = new CircularBuffer<>(2); | ||
assertNull(buffer.get()); | ||
} | ||
|
||
@RepeatedTest(1000) | ||
void concurrentTest() throws InterruptedException { | ||
final int numberOfThreadsForProducers = 3; | ||
final int numberOfThreadsForConsumers = 2; | ||
final int numberOfItems = 300; | ||
final CountDownLatch producerCountDownLatch = new CountDownLatch(numberOfItems); | ||
final CountDownLatch consumerCountDownLatch = new CountDownLatch(numberOfItems); | ||
final AtomicIntegerArray resultAtomicArray = new AtomicIntegerArray(numberOfItems); | ||
|
||
// We are running 2 ExecutorService simultaneously 1 - producer, 2 - consumer | ||
// Run producer threads to populate buffer. | ||
ExecutorService putExecutors = Executors.newFixedThreadPool(numberOfThreadsForProducers); | ||
putExecutors.execute(() -> { | ||
while (producerCountDownLatch.getCount() > 0) { | ||
int count = (int) producerCountDownLatch.getCount(); | ||
boolean put = buffer.put(count); | ||
while (!put) { | ||
put = buffer.put(count); | ||
} | ||
producerCountDownLatch.countDown(); | ||
} | ||
}); | ||
|
||
// Run consumer threads to retrieve the data from buffer. | ||
ExecutorService getExecutors = Executors.newFixedThreadPool(numberOfThreadsForConsumers); | ||
getExecutors.execute(() -> { | ||
while (consumerCountDownLatch.getCount() > 0) { | ||
int count = (int) consumerCountDownLatch.getCount(); | ||
Integer item = buffer.get(); | ||
while (item == null) { | ||
item = buffer.get(); | ||
} | ||
resultAtomicArray.set(count - 1, item); | ||
consumerCountDownLatch.countDown(); | ||
} | ||
}); | ||
|
||
producerCountDownLatch.await(); | ||
consumerCountDownLatch.await(); | ||
putExecutors.shutdown(); | ||
getExecutors.shutdown(); | ||
shutDownExecutorSafely(putExecutors); | ||
shutDownExecutorSafely(getExecutors); | ||
|
||
List<Integer> resultArray = getSortedListFrom(resultAtomicArray); | ||
for (int i = 0; i < numberOfItems; i++) { | ||
int expectedItem = i + 1; | ||
assertEquals(expectedItem, resultArray.get(i)); | ||
} | ||
@Test | ||
void testFullBuffer() { | ||
CircularBuffer<Character> buffer = new CircularBuffer<>(2); | ||
buffer.put('A'); | ||
buffer.put('B'); | ||
assertTrue(buffer.isFull()); | ||
assertEquals(Boolean.FALSE, buffer.put('C')); // This should overwrite 'A' | ||
assertEquals('B', buffer.get()); | ||
assertEquals('C', buffer.get()); | ||
} | ||
|
||
private int generateInt() { | ||
return ThreadLocalRandom.current().nextInt(0, 100); | ||
} | ||
@Test | ||
void testIllegalArguments() { | ||
assertThrows(IllegalArgumentException.class, () -> new CircularBuffer<>(0)); | ||
assertThrows(IllegalArgumentException.class, () -> new CircularBuffer<>(-1)); | ||
|
||
private void shutDownExecutorSafely(ExecutorService executorService) { | ||
try { | ||
if (!executorService.awaitTermination(1_000, TimeUnit.MILLISECONDS)) { | ||
executorService.shutdownNow(); | ||
} | ||
} catch (InterruptedException e) { | ||
executorService.shutdownNow(); | ||
} | ||
CircularBuffer<String> buffer = new CircularBuffer<>(1); | ||
assertThrows(IllegalArgumentException.class, () -> buffer.put(null)); | ||
} | ||
|
||
public List<Integer> getSortedListFrom(AtomicIntegerArray atomicArray) { | ||
int length = atomicArray.length(); | ||
ArrayList<Integer> result = new ArrayList<>(length); | ||
for (int i = 0; i < length; i++) { | ||
result.add(atomicArray.get(i)); | ||
@Test | ||
void testLargeBuffer() { | ||
CircularBuffer<Integer> buffer = new CircularBuffer<>(1000); | ||
for (int i = 0; i < 1000; i++) { | ||
buffer.put(i); | ||
} | ||
result.sort(Comparator.comparingInt(o -> o)); | ||
return result; | ||
assertTrue(buffer.isFull()); | ||
buffer.put(1000); // This should overwrite 0 | ||
assertEquals(1, buffer.get()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.