From 851986b2be318fc006547e95d26636519d4d9e14 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sat, 5 Oct 2024 19:51:23 +0530 Subject: [PATCH 1/7] Enhance class & function documentation in `CircularBuffer.java` --- .../buffers/CircularBuffer.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java b/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java index 15e9a0956226..1276b50d11e6 100644 --- a/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java +++ b/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java @@ -2,12 +2,24 @@ import java.util.concurrent.atomic.AtomicInteger; +/** + * The {@code CircularBuffer} class implements a generic circular (or ring) buffer. + * A circular buffer is a fixed-size data structure that operates in a FIFO (First In, First Out) manner. + * The buffer allows you to overwrite old data when the buffer is full and efficiently use limited memory. + * + * @param The type of elements stored in the circular buffer. + */ public class CircularBuffer { private final Item[] buffer; private final CircularPointer putPointer; private final CircularPointer getPointer; private final AtomicInteger size = new AtomicInteger(0); + /** + * Constructor to initialize the circular buffer with a specified size. + * + * @param size The size of the circular buffer. + */ public CircularBuffer(int size) { // noinspection unchecked this.buffer = (Item[]) new Object[size]; @@ -15,14 +27,29 @@ public CircularBuffer(int size) { this.getPointer = new CircularPointer(0, size); } + /** + * Checks if the circular buffer is empty. + * + * @return {@code true} if the buffer is empty, {@code false} otherwise. + */ public boolean isEmpty() { return size.get() == 0; } + /** + * Checks if the circular buffer is full. + * + * @return {@code true} if the buffer is full, {@code false} otherwise. + */ public boolean isFull() { return size.get() == buffer.length; } + /** + * Retrieves and removes the item at the front of the buffer (FIFO). + * + * @return The item at the front of the buffer, or {@code null} if the buffer is empty. + */ public Item get() { if (isEmpty()) { return null; @@ -33,6 +60,12 @@ public Item get() { return item; } + /** + * Adds an item to the end of the buffer (FIFO). + * + * @param item The item to be added. + * @return {@code true} if the item was successfully added, or {@code false} if the buffer is full. + */ public boolean put(Item item) { if (isFull()) { return false; @@ -43,15 +76,30 @@ public boolean put(Item item) { return true; } + /** + * The {@code CircularPointer} class is a helper class used to track the current index (pointer) + * in the circular buffer. It automatically wraps around when reaching the maximum index. + */ private static class CircularPointer { private int pointer; private final int max; + /** + * Constructor to initialize the circular pointer. + * + * @param pointer The initial position of the pointer. + * @param max The maximum size (capacity) of the circular buffer. + */ CircularPointer(int pointer, int max) { this.pointer = pointer; this.max = max; } + /** + * Increments the pointer by 1 and wraps it around to 0 if it exceeds the maximum value. + * + * @return The current pointer value before incrementing. + */ public int getAndIncrement() { if (pointer == max) { pointer = 0; From 8b3e29f2f318a1c9addad6e7bf7c05e4ff0995ee Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 7 Oct 2024 15:51:25 +0530 Subject: [PATCH 2/7] Add suggested changes --- .../buffers/CircularBuffer.java | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java b/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java index 1276b50d11e6..b709e16fd1f6 100644 --- a/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java +++ b/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java @@ -6,6 +6,7 @@ * The {@code CircularBuffer} class implements a generic circular (or ring) buffer. * A circular buffer is a fixed-size data structure that operates in a FIFO (First In, First Out) manner. * The buffer allows you to overwrite old data when the buffer is full and efficiently use limited memory. + * When the buffer is full, adding a new item will overwrite the oldest data. * * @param The type of elements stored in the circular buffer. */ @@ -19,8 +20,12 @@ public class CircularBuffer { * Constructor to initialize the circular buffer with a specified size. * * @param size The size of the circular buffer. + * @throws IllegalArgumentException if the size is zero or negative. */ public CircularBuffer(int size) { + if (size <= 0) { + throw new IllegalArgumentException("Buffer size must be positive"); + } // noinspection unchecked this.buffer = (Item[]) new Object[size]; this.putPointer = new CircularPointer(0, size); @@ -29,6 +34,7 @@ public CircularBuffer(int size) { /** * Checks if the circular buffer is empty. + * This method is based on the current size of the buffer. * * @return {@code true} if the buffer is empty, {@code false} otherwise. */ @@ -38,6 +44,7 @@ public boolean isEmpty() { /** * Checks if the circular buffer is full. + * The buffer is considered full when its size equals its capacity. * * @return {@code true} if the buffer is full, {@code false} otherwise. */ @@ -47,6 +54,7 @@ public boolean isFull() { /** * Retrieves and removes the item at the front of the buffer (FIFO). + * This operation will move the {@code getPointer} forward. * * @return The item at the front of the buffer, or {@code null} if the buffer is empty. */ @@ -62,23 +70,37 @@ public Item get() { /** * Adds an item to the end of the buffer (FIFO). + * If the buffer is full, this operation will overwrite the oldest data. * * @param item The item to be added. - * @return {@code true} if the item was successfully added, or {@code false} if the buffer is full. + * @throws IllegalArgumentException if the item is null. + * @return {@code true} if the item was successfully added, {@code false} if the buffer was full and the item overwrote existing data. */ public boolean put(Item item) { + if (item == null) { + throw new IllegalArgumentException("Null items are not allowed"); + } + + boolean wasEmpty = isEmpty(); if (isFull()) { - return false; + getPointer.getAndIncrement(); // Move get pointer to discard oldest item + } else { + size.incrementAndGet(); } buffer[putPointer.getAndIncrement()] = item; - size.incrementAndGet(); - return true; + return wasEmpty; } /** * The {@code CircularPointer} class is a helper class used to track the current index (pointer) - * in the circular buffer. It automatically wraps around when reaching the maximum index. + * in the circular buffer. + * The max value represents the capacity of the buffer. + * The `CircularPointer` class ensures that the pointer automatically wraps around to 0 + * when it reaches the maximum index. + * This is achieved in the `getAndIncrement` method, where the pointer + * is incremented and then taken modulo the maximum value (`max`). + * This operation ensures that the pointer always stays within the bounds of the buffer. */ private static class CircularPointer { private int pointer; @@ -96,16 +118,14 @@ private static class CircularPointer { } /** - * Increments the pointer by 1 and wraps it around to 0 if it exceeds the maximum value. + * Increments the pointer by 1 and wraps it around to 0 if it reaches the maximum value. + * This ensures the pointer always stays within the buffer's bounds. * * @return The current pointer value before incrementing. */ public int getAndIncrement() { - if (pointer == max) { - pointer = 0; - } int tmp = pointer; - pointer++; + pointer = (pointer + 1) % max; return tmp; } } From eba622936a448bd93f7dbc3f2619f2e6e27907be Mon Sep 17 00:00:00 2001 From: alxkm Date: Sat, 12 Oct 2024 08:26:36 +0000 Subject: [PATCH 3/7] Update directory --- DIRECTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 30fa2cbee199..9a60fbec929e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -54,6 +54,7 @@ * [CompositeLFSR](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java) * [LFSR](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/LFSR.java) * [Utils](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/Utils.java) + * [ADFGVXCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ADFGVXCipher.java) * [AES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AES.java) * [AESEncryption](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AESEncryption.java) * [AffineCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AffineCipher.java) @@ -133,6 +134,7 @@ * [FordFulkerson](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/FordFulkerson.java) * [Graphs](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java) * [HamiltonianCycle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java) + * [JohnsonsAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/JohnsonsAlgorithm.java) * [KahnsAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java) * [Kosaraju](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java) * [Kruskal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Kruskal.java) @@ -340,6 +342,7 @@ * [EulersFunction](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/EulersFunction.java) * [Factorial](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Factorial.java) * [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FactorialRecursion.java) + * [FastExponentiation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FastExponentiation.java) * [FastInverseSqrt](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java) * [FFT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FFT.java) * [FFTBluestein](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FFTBluestein.java) @@ -682,6 +685,7 @@ * [A5CipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5CipherTest.java) * [A5KeyStreamGeneratorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5KeyStreamGeneratorTest.java) * [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java) + * [ADFGVXCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/ADFGVXCipherTest.java) * [AESEncryptionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java) * [AffineCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AffineCipherTest.java) * [AtbashTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AtbashTest.java) @@ -750,6 +754,7 @@ * [FloydWarshallTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/FloydWarshallTest.java) * [FordFulkersonTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/FordFulkersonTest.java) * [HamiltonianCycleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java) + * [JohnsonsAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/JohnsonsAlgorithmTest.java) * [KosarajuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java) * [TarjansAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithmTest.java) * [WelshPowellTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/WelshPowellTest.java) @@ -902,6 +907,7 @@ * [EulersFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/EulersFunctionTest.java) * [FactorialRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java) * [FactorialTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialTest.java) + * [FastExponentiationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FastExponentiationTest.java) * [FastInverseSqrtTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FastInverseSqrtTests.java) * [FFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FFTTest.java) * [FibonacciJavaStreamsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FibonacciJavaStreamsTest.java) From 08b31f480d640c7b08b2169e4ea649823673907f Mon Sep 17 00:00:00 2001 From: alxkm Date: Sat, 12 Oct 2024 18:59:25 +0000 Subject: [PATCH 4/7] Update directory --- DIRECTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9a60fbec929e..9f03cf2047dd 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -25,9 +25,11 @@ * bitmanipulation * [BinaryPalindromeCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java) * [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java) + * [BooleanAlgebraGates](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java) * [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java) * [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java) * [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java) + * [FindNthBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/FindNthBit.java) * [GrayCodeConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java) * [HammingDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HammingDistance.java) * [HigherLowerPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java) @@ -659,9 +661,11 @@ * bitmanipulation * [BinaryPalindromeCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java) * [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java) + * [BooleanAlgebraGatesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGatesTest.java) * [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java) * [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java) * [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java) + * [FindNthBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/FindNthBitTest.java) * [GrayCodeConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/GrayCodeConversionTest.java) * [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HammingDistanceTest.java) * [HigherLowerPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java) From b9e87b5f7de9332b9f9196a0653df7874789a798 Mon Sep 17 00:00:00 2001 From: alxkm Date: Tue, 15 Oct 2024 09:14:00 +0000 Subject: [PATCH 5/7] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 11ce5b7440a3..653add6df450 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -304,6 +304,7 @@ * [WildcardMatching](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/WildcardMatching.java) * [WineProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/WineProblem.java) * geometry + * [BresenhamLine](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/BresenhamLine.java) * [ConvexHull](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/ConvexHull.java) * [GrahamScan](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/GrahamScan.java) * [Point](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/Point.java) @@ -902,6 +903,7 @@ * [WildcardMatchingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WildcardMatchingTest.java) * [WineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WineProblemTest.java) * geometry + * [BresenhamLineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/BresenhamLineTest.java) * [ConvexHullTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java) * [GrahamScanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java) * greedyalgorithms From cd65fe1dab8b2b51039559e026cfa8a16546a644 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 15 Oct 2024 15:43:05 +0530 Subject: [PATCH 6/7] Fix test cases --- .../buffers/CircularBufferTest.java | 163 ++++++------------ 1 file changed, 54 insertions(+), 109 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/buffers/CircularBufferTest.java b/src/test/java/com/thealgorithms/datastructures/buffers/CircularBufferTest.java index be98fde484fa..f9b021c5fdcb 100644 --- a/src/test/java/com/thealgorithms/datastructures/buffers/CircularBufferTest.java +++ b/src/test/java/com/thealgorithms/datastructures/buffers/CircularBufferTest.java @@ -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 buffer; - - @BeforeEach - void setUp() { - buffer = new CircularBuffer<>(BUFFER_SIZE); - } @Test - void isEmpty() { + void testInitialization() { + CircularBuffer buffer = new CircularBuffer<>(5); assertTrue(buffer.isEmpty()); - buffer.put(generateInt()); - assertFalse(buffer.isEmpty()); + assertEquals(false, buffer.isFull()); } @Test - void isFull() { - assertFalse(buffer.isFull()); - buffer.put(generateInt()); - assertFalse(buffer.isFull()); + void testPutAndGet() { + CircularBuffer buffer = new CircularBuffer<>(3); - for (int i = 1; i < BUFFER_SIZE; i++) { - buffer.put(generateInt()); - } + assertTrue(buffer.put("A")); + assertEquals(false, buffer.isEmpty()); + assertEquals(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 buffer = new CircularBuffer<>(3); + + buffer.put(1); + buffer.put(2); + buffer.put(3); + assertEquals(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 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 resultArray = getSortedListFrom(resultAtomicArray); - for (int i = 0; i < numberOfItems; i++) { - int expectedItem = i + 1; - assertEquals(expectedItem, resultArray.get(i)); - } + @Test + void testFullBuffer() { + CircularBuffer buffer = new CircularBuffer<>(2); + buffer.put('A'); + buffer.put('B'); + assertTrue(buffer.isFull()); + assertEquals(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 buffer = new CircularBuffer<>(1); + assertThrows(IllegalArgumentException.class, () -> buffer.put(null)); } - public List getSortedListFrom(AtomicIntegerArray atomicArray) { - int length = atomicArray.length(); - ArrayList result = new ArrayList<>(length); - for (int i = 0; i < length; i++) { - result.add(atomicArray.get(i)); + @Test + void testLargeBuffer() { + CircularBuffer 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()); } } From 8b3643e7ef38b45a4d29423f9180c9f75195e11c Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 15 Oct 2024 15:48:57 +0530 Subject: [PATCH 7/7] Fix --- .../datastructures/buffers/CircularBufferTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/buffers/CircularBufferTest.java b/src/test/java/com/thealgorithms/datastructures/buffers/CircularBufferTest.java index f9b021c5fdcb..b115fc187b1a 100644 --- a/src/test/java/com/thealgorithms/datastructures/buffers/CircularBufferTest.java +++ b/src/test/java/com/thealgorithms/datastructures/buffers/CircularBufferTest.java @@ -13,7 +13,7 @@ class CircularBufferTest { void testInitialization() { CircularBuffer buffer = new CircularBuffer<>(5); assertTrue(buffer.isEmpty()); - assertEquals(false, buffer.isFull()); + assertEquals(Boolean.FALSE, buffer.isFull()); } @Test @@ -21,8 +21,8 @@ void testPutAndGet() { CircularBuffer buffer = new CircularBuffer<>(3); assertTrue(buffer.put("A")); - assertEquals(false, buffer.isEmpty()); - assertEquals(false, buffer.isFull()); + assertEquals(Boolean.FALSE, buffer.isEmpty()); + assertEquals(Boolean.FALSE, buffer.isFull()); buffer.put("B"); buffer.put("C"); @@ -41,7 +41,7 @@ void testOverwrite() { buffer.put(1); buffer.put(2); buffer.put(3); - assertEquals(false, buffer.put(4)); // This should overwrite 1 + assertEquals(Boolean.FALSE, buffer.put(4)); // This should overwrite 1 assertEquals(2, buffer.get()); assertEquals(3, buffer.get()); @@ -61,7 +61,7 @@ void testFullBuffer() { buffer.put('A'); buffer.put('B'); assertTrue(buffer.isFull()); - assertEquals(false, buffer.put('C')); // This should overwrite 'A' + assertEquals(Boolean.FALSE, buffer.put('C')); // This should overwrite 'A' assertEquals('B', buffer.get()); assertEquals('C', buffer.get()); }