mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 15:02:46 +08:00
@ -8,39 +8,39 @@ class ArrayLeftRotationTest {
|
||||
|
||||
@Test
|
||||
void testForOneElement() {
|
||||
int[] arr = { 3 };
|
||||
int[] arr = {3};
|
||||
int[] result = ArrayLeftRotation.rotateLeft(arr, 3);
|
||||
assertArrayEquals(arr, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForZeroStep() {
|
||||
int[] arr = { 3, 1, 5, 8, 6 };
|
||||
int[] arr = {3, 1, 5, 8, 6};
|
||||
int[] result = ArrayLeftRotation.rotateLeft(arr, 0);
|
||||
assertArrayEquals(arr, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForEqualSizeStep() {
|
||||
int[] arr = { 3, 1, 5, 8, 6 };
|
||||
int[] arr = {3, 1, 5, 8, 6};
|
||||
int[] result = ArrayLeftRotation.rotateLeft(arr, 5);
|
||||
assertArrayEquals(arr, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForLowerSizeStep() {
|
||||
int[] arr = { 3, 1, 5, 8, 6 };
|
||||
int[] arr = {3, 1, 5, 8, 6};
|
||||
int n = 2;
|
||||
int[] expected = { 5, 8, 6, 3, 1 };
|
||||
int[] expected = {5, 8, 6, 3, 1};
|
||||
int[] result = ArrayLeftRotation.rotateLeft(arr, n);
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForHigherSizeStep() {
|
||||
int[] arr = { 3, 1, 5, 8, 6 };
|
||||
int[] arr = {3, 1, 5, 8, 6};
|
||||
int n = 7;
|
||||
int[] expected = { 5, 8, 6, 3, 1 };
|
||||
int[] expected = {5, 8, 6, 3, 1};
|
||||
int[] result = ArrayLeftRotation.rotateLeft(arr, n);
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
@ -19,9 +19,9 @@ class BestFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForUseOfOneBlock() {
|
||||
//test1 - 2 processes shall fit to one block instead of using a different block each
|
||||
sizeOfBlocks = new int[] { 5, 12, 17, 10 };
|
||||
sizeOfProcesses = new int[] { 10, 5, 15, 2 };
|
||||
// test1 - 2 processes shall fit to one block instead of using a different block each
|
||||
sizeOfBlocks = new int[] {5, 12, 17, 10};
|
||||
sizeOfProcesses = new int[] {10, 5, 15, 2};
|
||||
memAllocation = bestFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(3, 0, 2, 2));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -29,9 +29,9 @@ class BestFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForEqualProcecesses() {
|
||||
//test2
|
||||
sizeOfBlocks = new int[] { 5, 12, 17, 10 };
|
||||
sizeOfProcesses = new int[] { 10, 10, 10, 10 };
|
||||
// test2
|
||||
sizeOfBlocks = new int[] {5, 12, 17, 10};
|
||||
sizeOfProcesses = new int[] {10, 10, 10, 10};
|
||||
memAllocation = bestFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(3, 1, 2, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -39,9 +39,9 @@ class BestFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForNoEmptyBlockCell() {
|
||||
//test3 for more processes than blocks - no empty space left to none of the blocks
|
||||
sizeOfBlocks = new int[] { 5, 12, 17 };
|
||||
sizeOfProcesses = new int[] { 5, 12, 10, 7 };
|
||||
// test3 for more processes than blocks - no empty space left to none of the blocks
|
||||
sizeOfBlocks = new int[] {5, 12, 17};
|
||||
sizeOfProcesses = new int[] {5, 12, 10, 7};
|
||||
memAllocation = bestFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(0, 1, 2, 2));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -49,9 +49,9 @@ class BestFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForSameInputDifferentQuery() {
|
||||
//test4 for more processes than blocks - one element does not fit due to input series
|
||||
sizeOfBlocks = new int[] { 5, 12, 17 };
|
||||
sizeOfProcesses = new int[] { 5, 7, 10, 12 };
|
||||
// test4 for more processes than blocks - one element does not fit due to input series
|
||||
sizeOfBlocks = new int[] {5, 12, 17};
|
||||
sizeOfProcesses = new int[] {5, 7, 10, 12};
|
||||
memAllocation = bestFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(0, 1, 2, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -59,9 +59,9 @@ class BestFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForMoreBlocksNoFit() {
|
||||
//test5 for more blocks than processes
|
||||
sizeOfBlocks = new int[] { 5, 4, -1, 3, 6 };
|
||||
sizeOfProcesses = new int[] { 10, 11 };
|
||||
// test5 for more blocks than processes
|
||||
sizeOfBlocks = new int[] {5, 4, -1, 3, 6};
|
||||
sizeOfProcesses = new int[] {10, 11};
|
||||
memAllocation = bestFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(-255, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CRC16Test {
|
||||
|
||||
CRC16 crc = new CRC16();
|
||||
@ -19,5 +19,4 @@ class CRC16Test {
|
||||
// then
|
||||
assertEquals("10FC", resultCRC16);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,27 +1,27 @@
|
||||
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CRCAlgorithmTest {
|
||||
|
||||
@Test
|
||||
void test1(){
|
||||
void test1() {
|
||||
CRCAlgorithm c = new CRCAlgorithm("10010101010100101010010000001010010101010", 10, 0.0);
|
||||
|
||||
//A bit-error rate of 0.0 should not provide any wrong messages
|
||||
// A bit-error rate of 0.0 should not provide any wrong messages
|
||||
c.changeMess();
|
||||
c.divideMessageWithP(false);
|
||||
assertEquals(c.getWrongMess(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void test2(){
|
||||
void test2() {
|
||||
CRCAlgorithm c = new CRCAlgorithm("10010101010100101010010000001010010101010", 10, 1.0);
|
||||
|
||||
//A bit error rate of 1.0 should not provide any correct messages
|
||||
// A bit error rate of 1.0 should not provide any correct messages
|
||||
c.changeMess();
|
||||
c.divideMessageWithP(false);
|
||||
assertEquals(c.getCorrectMess(), 0);
|
||||
|
@ -9,49 +9,49 @@ public class CalculateMaxOfMinTest {
|
||||
|
||||
@Test
|
||||
void testForOneElement() {
|
||||
int[] a = { 10, 20, 30, 50, 10, 70, 30 };
|
||||
int[] a = {10, 20, 30, 50, 10, 70, 30};
|
||||
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
|
||||
assertTrue(k == 70);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForTwoElements() {
|
||||
int[] a = { 5, 3, 2, 6, 3, 2, 6 };
|
||||
int[] a = {5, 3, 2, 6, 3, 2, 6};
|
||||
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
|
||||
assertTrue(k == 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForThreeElements() {
|
||||
int[] a = { 10, 10, 10, 10, 10, 10, 10 };
|
||||
int[] a = {10, 10, 10, 10, 10, 10, 10};
|
||||
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
|
||||
assertTrue(k == 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForFourElements() {
|
||||
int[] a = { 70, 60, 50, 40, 30, 20 };
|
||||
int[] a = {70, 60, 50, 40, 30, 20};
|
||||
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
|
||||
assertTrue(k == 70);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForFiveElements() {
|
||||
int[] a = { 50 };
|
||||
int[] a = {50};
|
||||
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
|
||||
assertTrue(k == 50);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForSixElements() {
|
||||
int[] a = { 1, 4, 7, 9, 2, 4, 6 };
|
||||
int[] a = {1, 4, 7, 9, 2, 4, 6};
|
||||
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
|
||||
assertTrue(k == 9);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForSevenElements() {
|
||||
int[] a = { -1, -5, -7, -9, -12, -14 };
|
||||
int[] a = {-1, -5, -7, -9, -12, -14};
|
||||
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
|
||||
assertTrue(k == -1);
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,17 +1,16 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CountCharTest {
|
||||
|
||||
@Test
|
||||
void testCountCharacters(){
|
||||
void testCountCharacters() {
|
||||
String input = "12345";
|
||||
int expectedValue = 5;
|
||||
|
||||
assertEquals(expectedValue, CountChar.CountCharacters(input));
|
||||
}
|
||||
|
||||
}
|
@ -9,49 +9,49 @@ public class CountFriendsPairingTest {
|
||||
|
||||
@Test
|
||||
void testForOneElement() {
|
||||
int[] a = { 1, 2, 2 };
|
||||
int[] a = {1, 2, 2};
|
||||
assertTrue(CountFriendsPairing.countFriendsPairing(3, a));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForTwoElements() {
|
||||
int[] a = { 1, 2, 2, 3 };
|
||||
int[] a = {1, 2, 2, 3};
|
||||
assertTrue(CountFriendsPairing.countFriendsPairing(4, a));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForThreeElements() {
|
||||
int[] a = { 1, 2, 2, 3, 3 };
|
||||
int[] a = {1, 2, 2, 3, 3};
|
||||
assertTrue(CountFriendsPairing.countFriendsPairing(5, a));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForFourElements() {
|
||||
int[] a = { 1, 2, 2, 3, 3, 4 };
|
||||
int[] a = {1, 2, 2, 3, 3, 4};
|
||||
assertTrue(CountFriendsPairing.countFriendsPairing(6, a));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForFiveElements() {
|
||||
int[] a = { 1, 2, 2, 3, 3, 4, 4 };
|
||||
int[] a = {1, 2, 2, 3, 3, 4, 4};
|
||||
assertTrue(CountFriendsPairing.countFriendsPairing(7, a));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForSixElements() {
|
||||
int[] a = { 1, 2, 2, 3, 3, 4, 4, 4 };
|
||||
int[] a = {1, 2, 2, 3, 3, 4, 4, 4};
|
||||
assertTrue(CountFriendsPairing.countFriendsPairing(8, a));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForSevenElements() {
|
||||
int[] a = { 1, 2, 2, 3, 3, 4, 4, 4, 5 };
|
||||
int[] a = {1, 2, 2, 3, 3, 4, 4, 4, 5};
|
||||
assertTrue(CountFriendsPairing.countFriendsPairing(9, a));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForEightElements() {
|
||||
int[] a = { 1, 2, 2, 3, 3, 4, 4, 4, 5, 5 };
|
||||
int[] a = {1, 2, 2, 3, 3, 4, 4, 4, 5, 5};
|
||||
assertTrue(CountFriendsPairing.countFriendsPairing(10, a));
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.util.HashMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
class CountWordsTest {
|
||||
@Test
|
||||
public void testWordCount() {
|
||||
|
@ -19,9 +19,9 @@ class FirstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForUseOfOneBlock() {
|
||||
//test1 - no use of one block for two processes
|
||||
sizeOfBlocks = new int[] { 5, 12, 17, 10 };
|
||||
sizeOfProcesses = new int[] { 10, 5, 15, 2 };
|
||||
// test1 - no use of one block for two processes
|
||||
sizeOfBlocks = new int[] {5, 12, 17, 10};
|
||||
sizeOfProcesses = new int[] {10, 5, 15, 2};
|
||||
memAllocation = firstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(1, 0, 2, 1));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -29,9 +29,9 @@ class FirstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForEqualProcecesses() {
|
||||
//test2
|
||||
sizeOfBlocks = new int[] { 5, 12, 17, 10 };
|
||||
sizeOfProcesses = new int[] { 10, 10, 10, 10 };
|
||||
// test2
|
||||
sizeOfBlocks = new int[] {5, 12, 17, 10};
|
||||
sizeOfProcesses = new int[] {10, 10, 10, 10};
|
||||
memAllocation = firstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(1, 2, 3, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -39,9 +39,9 @@ class FirstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForNoEmptyBlockCell() {
|
||||
//test3 for more processes than blocks - no empty space left to none of the blocks
|
||||
sizeOfBlocks = new int[] { 5, 12, 17 };
|
||||
sizeOfProcesses = new int[] { 5, 12, 10, 7 };
|
||||
// test3 for more processes than blocks - no empty space left to none of the blocks
|
||||
sizeOfBlocks = new int[] {5, 12, 17};
|
||||
sizeOfProcesses = new int[] {5, 12, 10, 7};
|
||||
memAllocation = firstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(0, 1, 2, 2));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -49,9 +49,9 @@ class FirstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForSameInputDifferentQuery() {
|
||||
//test4 for more processes than blocks - one element does not fit due to input series
|
||||
sizeOfBlocks = new int[] { 5, 12, 17 };
|
||||
sizeOfProcesses = new int[] { 5, 7, 10, 12 };
|
||||
// test4 for more processes than blocks - one element does not fit due to input series
|
||||
sizeOfBlocks = new int[] {5, 12, 17};
|
||||
sizeOfProcesses = new int[] {5, 7, 10, 12};
|
||||
memAllocation = firstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(0, 1, 2, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -59,9 +59,9 @@ class FirstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForMoreBlocksNoFit() {
|
||||
//test5 for more blocks than processes
|
||||
sizeOfBlocks = new int[] { 5, 4, -1, 3, 6 };
|
||||
sizeOfProcesses = new int[] { 10, 11 };
|
||||
// test5 for more blocks than processes
|
||||
sizeOfBlocks = new int[] {5, 4, -1, 3, 6};
|
||||
sizeOfProcesses = new int[] {10, 11};
|
||||
memAllocation = firstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(-255, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
|
@ -9,49 +9,49 @@ public class KadaneAlogrithmTest {
|
||||
|
||||
@Test
|
||||
void testForOneElement() {
|
||||
int[] a = { -1 };
|
||||
int[] a = {-1};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a, -1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForTwoElements() {
|
||||
int[] a = { -2, 1 };
|
||||
int[] a = {-2, 1};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForThreeElements() {
|
||||
int[] a = { 5, 3, 12 };
|
||||
int[] a = {5, 3, 12};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForFourElements() {
|
||||
int[] a = { -1, -3, -7, -4 };
|
||||
int[] a = {-1, -3, -7, -4};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a, -1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForFiveElements() {
|
||||
int[] a = { 4, 5, 3, 0, 2 };
|
||||
int[] a = {4, 5, 3, 0, 2};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a, 14));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForSixElements() {
|
||||
int[] a = { -43, -45, 47, 12, 87, -13 };
|
||||
int[] a = {-43, -45, 47, 12, 87, -13};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a, 146));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForSevenElements() {
|
||||
int[] a = { 9, 8, 2, 23, 13, 6, 7 };
|
||||
int[] a = {9, 8, 2, 23, 13, 6, 7};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a, 68));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForEightElements() {
|
||||
int[] a = { 9, -5, -5, -2, 4, 5, 0, 1 };
|
||||
int[] a = {9, -5, -5, -2, 4, 5, 0, 1};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a, 10));
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +1,28 @@
|
||||
package com.thealgorithms.others;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
public class LineSweepTest {
|
||||
|
||||
|
||||
@Test
|
||||
void testForOverlap(){
|
||||
int[][]arr = {{0,10},{7,20},{15,24}};
|
||||
void testForOverlap() {
|
||||
int[][] arr = {{0, 10}, {7, 20}, {15, 24}};
|
||||
assertTrue(LineSweep.isOverlap(arr));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForNoOverlap(){
|
||||
int[][]arr = {{0,10},{11,20},{21,24}};
|
||||
void testForNoOverlap() {
|
||||
int[][] arr = {{0, 10}, {11, 20}, {21, 24}};
|
||||
assertFalse(LineSweep.isOverlap(arr));
|
||||
}
|
||||
@Test
|
||||
void testForOverlapWhenEndAEqualsStartBAndViceVersa(){
|
||||
int[][]arr = {{0,10},{10,20},{21,24}};
|
||||
void testForOverlapWhenEndAEqualsStartBAndViceVersa() {
|
||||
int[][] arr = {{0, 10}, {10, 20}, {21, 24}};
|
||||
assertTrue(LineSweep.isOverlap(arr));
|
||||
}
|
||||
@Test
|
||||
void testForMaximumEndPoint(){
|
||||
int[][]arr = {{10,20},{1,100},{14,16},{1,8}};
|
||||
assertEquals(100,LineSweep.FindMaximumEndPoint(arr));
|
||||
void testForMaximumEndPoint() {
|
||||
int[][] arr = {{10, 20}, {1, 100}, {14, 16}, {1, 8}};
|
||||
assertEquals(100, LineSweep.FindMaximumEndPoint(arr));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,49 +9,49 @@ public class LinkListSortTest {
|
||||
|
||||
@Test
|
||||
void testForOneElement() {
|
||||
int[] a = { 56 };
|
||||
int[] a = {56};
|
||||
assertTrue(LinkListSort.isSorted(a, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForTwoElements() {
|
||||
int[] a = { 6, 4 };
|
||||
int[] a = {6, 4};
|
||||
assertTrue(LinkListSort.isSorted(a, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForThreeElements() {
|
||||
int[] a = { 875, 253, 12 };
|
||||
int[] a = {875, 253, 12};
|
||||
assertTrue(LinkListSort.isSorted(a, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForFourElements() {
|
||||
int[] a = { 86, 32, 87, 13 };
|
||||
int[] a = {86, 32, 87, 13};
|
||||
assertTrue(LinkListSort.isSorted(a, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForFiveElements() {
|
||||
int[] a = { 6, 5, 3, 0, 9 };
|
||||
int[] a = {6, 5, 3, 0, 9};
|
||||
assertTrue(LinkListSort.isSorted(a, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForSixElements() {
|
||||
int[] a = { 9, 65, 432, 32, 47, 327 };
|
||||
int[] a = {9, 65, 432, 32, 47, 327};
|
||||
assertTrue(LinkListSort.isSorted(a, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForSevenElements() {
|
||||
int[] a = { 6, 4, 2, 1, 3, 6, 7 };
|
||||
int[] a = {6, 4, 2, 1, 3, 6, 7};
|
||||
assertTrue(LinkListSort.isSorted(a, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForEightElements() {
|
||||
int[] a = { 123, 234, 145, 764, 322, 367, 768, 34 };
|
||||
int[] a = {123, 234, 145, 764, 322, 367, 768, 34};
|
||||
assertTrue(LinkListSort.isSorted(a, 2));
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,13 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import java.util.HashMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class LowestBasePalindromeTest {
|
||||
@ -17,14 +16,18 @@ public class LowestBasePalindromeTest {
|
||||
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>()));
|
||||
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1))));
|
||||
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 1))));
|
||||
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 1))));
|
||||
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 2, 1))));
|
||||
assertTrue(
|
||||
LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 1))));
|
||||
assertTrue(
|
||||
LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 2, 1))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsPalindromicNegative() {
|
||||
assertFalse(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2))));
|
||||
assertFalse(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 1, 1))));
|
||||
assertFalse(
|
||||
LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2))));
|
||||
assertFalse(
|
||||
LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 1, 1))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -45,17 +48,13 @@ public class LowestBasePalindromeTest {
|
||||
@Test
|
||||
public void testIsPalindromicInBaseThrowsExceptionForNegativeNumbers() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> LowestBasePalindrome.isPalindromicInBase(-1, 5)
|
||||
);
|
||||
IllegalArgumentException.class, () -> LowestBasePalindrome.isPalindromicInBase(-1, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsPalindromicInBaseThrowsExceptionForWrongBases() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> LowestBasePalindrome.isPalindromicInBase(10, 1)
|
||||
);
|
||||
IllegalArgumentException.class, () -> LowestBasePalindrome.isPalindromicInBase(10, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -19,9 +19,9 @@ class NextFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForUseOfOneBlock() {
|
||||
//test1 - third process does not fit because of algorithms procedure
|
||||
sizeOfBlocks = new int[] { 5, 12, 17, 10 };
|
||||
sizeOfProcesses = new int[] { 10, 5, 15, 2 };
|
||||
// test1 - third process does not fit because of algorithms procedure
|
||||
sizeOfBlocks = new int[] {5, 12, 17, 10};
|
||||
sizeOfProcesses = new int[] {10, 5, 15, 2};
|
||||
memAllocation = nextFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(1, 2, -255, 2));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -29,9 +29,9 @@ class NextFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForEqualProcecesses() {
|
||||
//test2
|
||||
sizeOfBlocks = new int[] { 5, 12, 17, 10 };
|
||||
sizeOfProcesses = new int[] { 10, 10, 10, 10 };
|
||||
// test2
|
||||
sizeOfBlocks = new int[] {5, 12, 17, 10};
|
||||
sizeOfProcesses = new int[] {10, 10, 10, 10};
|
||||
memAllocation = nextFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(1, 2, 3, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -39,9 +39,9 @@ class NextFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForNoEmptyBlockCell() {
|
||||
//test3 for more processes than blocks - no empty space left to none of the blocks
|
||||
sizeOfBlocks = new int[] { 5, 12, 17 };
|
||||
sizeOfProcesses = new int[] { 5, 12, 10, 7 };
|
||||
// test3 for more processes than blocks - no empty space left to none of the blocks
|
||||
sizeOfBlocks = new int[] {5, 12, 17};
|
||||
sizeOfProcesses = new int[] {5, 12, 10, 7};
|
||||
memAllocation = nextFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(0, 1, 2, 2));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -49,9 +49,9 @@ class NextFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForSameInputDifferentQuery() {
|
||||
//test4 for more processes than blocks - one element does not fit due to input series
|
||||
sizeOfBlocks = new int[] { 5, 12, 17 };
|
||||
sizeOfProcesses = new int[] { 5, 7, 10, 12 };
|
||||
// test4 for more processes than blocks - one element does not fit due to input series
|
||||
sizeOfBlocks = new int[] {5, 12, 17};
|
||||
sizeOfProcesses = new int[] {5, 7, 10, 12};
|
||||
memAllocation = nextFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(0, 1, 2, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -59,9 +59,9 @@ class NextFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForMoreBlocksNoFit() {
|
||||
//test5 for more blocks than processes
|
||||
sizeOfBlocks = new int[] { 5, 4, -1, 3, 6 };
|
||||
sizeOfProcesses = new int[] { 10, 11 };
|
||||
// test5 for more blocks than processes
|
||||
sizeOfBlocks = new int[] {5, 4, -1, 3, 6};
|
||||
sizeOfProcesses = new int[] {10, 11};
|
||||
memAllocation = nextFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(-255, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
|
@ -9,12 +9,8 @@ public class PasswordGenTest {
|
||||
@Test
|
||||
public void failGenerationWithSameMinMaxLengthTest() {
|
||||
int length = 10;
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
PasswordGen.generatePassword(length, length);
|
||||
}
|
||||
);
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> { PasswordGen.generatePassword(length, length); });
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -27,12 +23,8 @@ public class PasswordGenTest {
|
||||
public void failGenerationWithMinLengthSmallerThanMaxLengthTest() {
|
||||
int minLength = 10;
|
||||
int maxLength = 5;
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
PasswordGen.generatePassword(minLength, maxLength);
|
||||
}
|
||||
);
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> { PasswordGen.generatePassword(minLength, maxLength); });
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -1,32 +1,25 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestPrintMatrixInSpiralOrder {
|
||||
@Test
|
||||
public void testOne() {
|
||||
int[][] matrix = {
|
||||
{ 3, 4, 5, 6, 7 },
|
||||
{ 8, 9, 10, 11, 12 },
|
||||
{ 14, 15, 16, 17, 18 },
|
||||
{ 23, 24, 25, 26, 27 },
|
||||
{ 30, 31, 32, 33, 34 }
|
||||
};
|
||||
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18},
|
||||
{23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
|
||||
var printClass = new PrintAMatrixInSpiralOrder();
|
||||
List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
|
||||
List<Integer> list = List.of(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25,
|
||||
24, 15, 16);
|
||||
List<Integer> list = List.of(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9,
|
||||
10, 11, 17, 26, 25, 24, 15, 16);
|
||||
assertIterableEquals(res, list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwo() {
|
||||
int[][] matrix = {
|
||||
{ 2, 2 }
|
||||
};
|
||||
int[][] matrix = {{2, 2}};
|
||||
var printClass = new PrintAMatrixInSpiralOrder();
|
||||
List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
|
||||
List<Integer> list = List.of(2, 2);
|
||||
|
@ -1,44 +1,43 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TwoPointersTest {
|
||||
|
||||
|
||||
@Test
|
||||
void twoPointersFirstTestCase(){
|
||||
int[] arr = {2,6,9,22,121};
|
||||
void twoPointersFirstTestCase() {
|
||||
int[] arr = {2, 6, 9, 22, 121};
|
||||
int key = 28;
|
||||
assertEquals(true, TwoPointers.isPairedSum(arr,key));
|
||||
assertEquals(true, TwoPointers.isPairedSum(arr, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoPointersSecondTestCase(){
|
||||
int[] arr = {-1,-12,12,0,8};
|
||||
void twoPointersSecondTestCase() {
|
||||
int[] arr = {-1, -12, 12, 0, 8};
|
||||
int key = 0;
|
||||
assertEquals(true, TwoPointers.isPairedSum(arr,key));
|
||||
assertEquals(true, TwoPointers.isPairedSum(arr, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoPointersThirdTestCase(){
|
||||
int[] arr = {12,35,12,152,0};
|
||||
void twoPointersThirdTestCase() {
|
||||
int[] arr = {12, 35, 12, 152, 0};
|
||||
int key = 13;
|
||||
assertEquals(false, TwoPointers.isPairedSum(arr,key));
|
||||
assertEquals(false, TwoPointers.isPairedSum(arr, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoPointersFourthTestCase(){
|
||||
int[] arr = {-2,5,-1,52,31};
|
||||
void twoPointersFourthTestCase() {
|
||||
int[] arr = {-2, 5, -1, 52, 31};
|
||||
int key = -3;
|
||||
assertEquals(true, TwoPointers.isPairedSum(arr,key));
|
||||
assertEquals(true, TwoPointers.isPairedSum(arr, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoPointersFiftiethTestCase(){
|
||||
int[] arr = {25,1,0,61,21};
|
||||
void twoPointersFiftiethTestCase() {
|
||||
int[] arr = {25, 1, 0, 61, 21};
|
||||
int key = 12;
|
||||
assertEquals(false, TwoPointers.isPairedSum(arr,key));
|
||||
assertEquals(false, TwoPointers.isPairedSum(arr, key));
|
||||
}
|
||||
}
|
||||
|
@ -19,9 +19,9 @@ class WorstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForUseOfOneBlock() {
|
||||
//test1
|
||||
sizeOfBlocks = new int[] { 5, 12, 17, 10 };
|
||||
sizeOfProcesses = new int[] { 10, 5, 15, 2 };
|
||||
// test1
|
||||
sizeOfBlocks = new int[] {5, 12, 17, 10};
|
||||
sizeOfProcesses = new int[] {10, 5, 15, 2};
|
||||
memAllocation = worstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(2, 1, -255, 3));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -29,9 +29,9 @@ class WorstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForEqualProcecesses() {
|
||||
//test2
|
||||
sizeOfBlocks = new int[] { 5, 12, 17, 10 };
|
||||
sizeOfProcesses = new int[] { 10, 10, 10, 10 };
|
||||
// test2
|
||||
sizeOfBlocks = new int[] {5, 12, 17, 10};
|
||||
sizeOfProcesses = new int[] {10, 10, 10, 10};
|
||||
memAllocation = worstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(2, 1, 3, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -39,9 +39,9 @@ class WorstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForNoEmptyBlockCell() {
|
||||
//test3 - could suits best, bad use of memory allocation due to worstFit algorithm
|
||||
sizeOfBlocks = new int[] { 5, 12, 17 };
|
||||
sizeOfProcesses = new int[] { 5, 12, 10, 7 };
|
||||
// test3 - could suits best, bad use of memory allocation due to worstFit algorithm
|
||||
sizeOfBlocks = new int[] {5, 12, 17};
|
||||
sizeOfProcesses = new int[] {5, 12, 10, 7};
|
||||
memAllocation = worstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(2, 1, 2, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -49,9 +49,9 @@ class WorstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForSameInputDifferentQuery() {
|
||||
//test4 same example different series - same results
|
||||
sizeOfBlocks = new int[] { 5, 12, 17 };
|
||||
sizeOfProcesses = new int[] { 5, 7, 10, 12 };
|
||||
// test4 same example different series - same results
|
||||
sizeOfBlocks = new int[] {5, 12, 17};
|
||||
sizeOfProcesses = new int[] {5, 7, 10, 12};
|
||||
memAllocation = worstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(2, 1, 2, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -59,9 +59,9 @@ class WorstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitForMoreBlocksNoFit() {
|
||||
//test5 for more blocks than processes
|
||||
sizeOfBlocks = new int[] { 5, 4, -1, 3, 6 };
|
||||
sizeOfProcesses = new int[] { 10, 11 };
|
||||
// test5 for more blocks than processes
|
||||
sizeOfBlocks = new int[] {5, 4, -1, 3, 6};
|
||||
sizeOfProcesses = new int[] {10, 11};
|
||||
memAllocation = worstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(-255, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
@ -69,12 +69,11 @@ class WorstFitCPUTest {
|
||||
|
||||
@Test
|
||||
void testFitBadCase() {
|
||||
//test6 for only two process fit
|
||||
sizeOfBlocks = new int[] { 7, 17, 7, 5, 6 };
|
||||
sizeOfProcesses = new int[] { 8, 10, 10, 8, 8, 8 };
|
||||
// test6 for only two process fit
|
||||
sizeOfBlocks = new int[] {7, 17, 7, 5, 6};
|
||||
sizeOfProcesses = new int[] {8, 10, 10, 8, 8, 8};
|
||||
memAllocation = worstFit.fitProcess(sizeOfBlocks, sizeOfProcesses);
|
||||
testMemAllocation =
|
||||
new ArrayList<>(Arrays.asList(1, -255, -255, 1, -255, -255));
|
||||
testMemAllocation = new ArrayList<>(Arrays.asList(1, -255, -255, 1, -255, -255));
|
||||
assertEquals(testMemAllocation, memAllocation);
|
||||
}
|
||||
}
|
||||
|
@ -46,8 +46,8 @@ public class HammingDistanceTest {
|
||||
|
||||
@Test
|
||||
public void checkForLongDataBits() {
|
||||
String senderBits = "10010101101010000100110100", receiverBits =
|
||||
"00110100001011001100110101";
|
||||
String senderBits = "10010101101010000100110100",
|
||||
receiverBits = "00110100001011001100110101";
|
||||
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
|
||||
Assertions.assertThat(answer).isEqualTo(7);
|
||||
}
|
||||
@ -56,23 +56,16 @@ public class HammingDistanceTest {
|
||||
public void mismatchDataBits() {
|
||||
String senderBits = "100010", receiverBits = "00011";
|
||||
|
||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
int answer = hd.getHammingDistanceBetweenBits(
|
||||
senderBits,
|
||||
receiverBits
|
||||
);
|
||||
}
|
||||
);
|
||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class,
|
||||
() -> { int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits); });
|
||||
|
||||
Assertions.assertThat(ex.getMessage()).contains("bits should be same");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkForLongDataBitsSame() {
|
||||
String senderBits = "10010101101010000100110100", receiverBits =
|
||||
"10010101101010000100110100";
|
||||
String senderBits = "10010101101010000100110100",
|
||||
receiverBits = "10010101101010000100110100";
|
||||
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
|
||||
Assertions.assertThat(answer).isEqualTo(0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user