-
Notifications
You must be signed in to change notification settings - Fork 20k
#4382 Bug Fix #4384
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
vil02
merged 14 commits into
TheAlgorithms:master
from
govardhanshah456:MedianOfRunningArray-Bug-Fix
Sep 21, 2023
Merged
#4382 Bug Fix #4384
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3b0ce38
#4382 Bug Fix
govardhanshah456 6ed1189
#4382 Bug Fix
govardhanshah456 eaa0533
Made Requested Changes
govardhanshah456 8542fa0
Made Requested Changes
govardhanshah456 cc61bfe
Made Requested Changes
govardhanshah456 bc135cf
Made Requested Changes
govardhanshah456 640c23b
Made Requested Changes
govardhanshah456 15acdbb
Made Requested Changes
govardhanshah456 92ccdd3
Made Requested Changes
govardhanshah456 0b7d2a3
Update src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java
govardhanshah456 81e0d3d
Update src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java
govardhanshah456 6c4c057
Update src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.…
govardhanshah456 300b1eb
Update src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.…
govardhanshah456 23de23a
Merge branch 'master' into MedianOfRunningArray-Bug-Fix
vil02 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
161 changes: 161 additions & 0 deletions
161
src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.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 |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package com.thealgorithms.misc; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Test case for Two sum Problem. | ||
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) | ||
*/ | ||
|
||
public class MedianOfRunningArrayTest { | ||
private static final String EXCEPTION_MESSAGE = "Enter at least 1 element, Median of empty list is not defined!"; | ||
|
||
@Test | ||
public void testWhenInvalidInoutProvidedShouldThrowException() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> stream.median()); | ||
assertEquals(exception.getMessage(), EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Test | ||
public void testWithNegativeValues() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(-1); | ||
assertEquals(-1, stream.median()); | ||
stream.insert(-2); | ||
assertEquals(-1.5, stream.median()); | ||
stream.insert(-3); | ||
assertEquals(-2, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithSingleValues() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(-1); | ||
assertEquals(-1, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithRandomValues() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(10); | ||
assertEquals(10.0, stream.median()); | ||
|
||
stream.insert(5); | ||
assertEquals(7.5, stream.median()); | ||
|
||
stream.insert(20); | ||
assertEquals(10.0, stream.median()); | ||
|
||
stream.insert(15); | ||
assertEquals(12.5, stream.median()); | ||
|
||
stream.insert(25); | ||
assertEquals(15.0, stream.median()); | ||
|
||
stream.insert(30); | ||
assertEquals(17.5, stream.median()); | ||
|
||
stream.insert(35); | ||
assertEquals(20.0, stream.median()); | ||
|
||
stream.insert(1); | ||
assertEquals(17.5, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithNegativeAndPositiveValues() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(-1); | ||
assertEquals(-1, stream.median()); | ||
stream.insert(2); | ||
assertEquals(0.5, stream.median()); | ||
stream.insert(-3); | ||
assertEquals(-1, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithDuplicateValues() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(-1); | ||
assertEquals(-1, stream.median()); | ||
stream.insert(-1); | ||
assertEquals(-1, stream.median()); | ||
stream.insert(-1); | ||
assertEquals(-1, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithDuplicateValuesB() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(10); | ||
stream.insert(20); | ||
stream.insert(10); | ||
stream.insert(10); | ||
stream.insert(20); | ||
stream.insert(0); | ||
stream.insert(50); | ||
assertEquals(10, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithLargeValues() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(1000000); | ||
assertEquals(1000000, stream.median()); | ||
stream.insert(12000); | ||
assertEquals(506000, stream.median()); | ||
stream.insert(15000000); | ||
assertEquals(1000000, stream.median()); | ||
stream.insert(2300000); | ||
assertEquals(1650000.00, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithLargeCountOfValues() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
for (int i = 1; i <= 1000; i++) stream.insert(i); | ||
assertEquals(500.5, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithThreeValuesInDescendingOrder() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(30); | ||
stream.insert(20); | ||
stream.insert(10); | ||
assertEquals(20.0, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithThreeValuesInOrder() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(10); | ||
stream.insert(20); | ||
stream.insert(30); | ||
assertEquals(20.0, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithThreeValuesNotInOrderA() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(30); | ||
stream.insert(10); | ||
stream.insert(20); | ||
assertEquals(20.0, stream.median()); | ||
} | ||
|
||
@Test | ||
public void testWithThreeValuesNotInOrderB() { | ||
MedianOfRunningArray stream = new MedianOfRunningArray(); | ||
stream.insert(20); | ||
stream.insert(10); | ||
stream.insert(30); | ||
assertEquals(20.0, stream.median()); | ||
} | ||
} | ||
govardhanshah456 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.