mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 06:55:02 +08:00
@ -0,0 +1,42 @@
|
||||
package com.thealgorithms.greedyalgorithms;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ActivitySelectionTest {
|
||||
@Test
|
||||
public void testActivitySelection() {
|
||||
int start[] = {1, 3, 0, 5, 8, 5};
|
||||
int end[] = {2, 4, 6, 7, 9, 9};
|
||||
|
||||
ArrayList<Integer> result = ActivitySelection.activitySelection(start, end);
|
||||
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(0, 1, 3, 4));
|
||||
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleActivity() {
|
||||
int start[] = {1};
|
||||
int end[] = {2};
|
||||
|
||||
ArrayList<Integer> result = ActivitySelection.activitySelection(start, end);
|
||||
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(0));
|
||||
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoOverlap() {
|
||||
int start[] = {1, 2, 3};
|
||||
int end[] = {2, 3, 4};
|
||||
|
||||
ArrayList<Integer> result = ActivitySelection.activitySelection(start, end);
|
||||
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(0, 1, 2));
|
||||
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.thealgorithms.greedyalgorithms;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CoinChangeTest {
|
||||
@Test
|
||||
public void testCoinChangeProblemWithValidAmount() {
|
||||
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(500, 50, 20, 20, 1));
|
||||
ArrayList<Integer> coins = CoinChange.coinChangeProblem(591);
|
||||
assertEquals(expected, coins);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCoinChangeProblemWithLargeAmount() {
|
||||
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(2000));
|
||||
ArrayList<Integer> coins = CoinChange.coinChangeProblem(2000);
|
||||
assertEquals(expected, coins);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCoinChangeProblemWithPartialCoins2() {
|
||||
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(500, 50, 20));
|
||||
ArrayList<Integer> coins = CoinChange.coinChangeProblem(570);
|
||||
assertEquals(expected, coins);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCoinChangeProblemWithSmallAmount() {
|
||||
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(2, 1));
|
||||
ArrayList<Integer> coins = CoinChange.coinChangeProblem(3);
|
||||
assertEquals(expected, coins);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCoinChangeProblemWithLargeAmountAndMultipleDenominations() {
|
||||
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(2000, 2000, 2000, 2000, 500, 500, 500, 100, 100, 100, 100, 50, 20, 20, 5, 2, 2));
|
||||
ArrayList<Integer> coins = CoinChange.coinChangeProblem(9999);
|
||||
assertEquals(expected, coins);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCoinChangeProblemWithAllDenominations() {
|
||||
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(2000, 500, 100, 100, 100, 50, 20, 10, 5, 2, 1));
|
||||
ArrayList<Integer> coins = CoinChange.coinChangeProblem(2888);
|
||||
assertEquals(expected, coins);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCoinChangeProblemWithZeroAmount() {
|
||||
ArrayList<Integer> expected = new ArrayList<>();
|
||||
ArrayList<Integer> coins = CoinChange.coinChangeProblem(0);
|
||||
assertEquals(expected, coins);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.thealgorithms.greedyalgorithms;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class FractionalKnapsackTest {
|
||||
|
||||
@Test
|
||||
public void testFractionalKnapsackWithExampleCase() {
|
||||
int weight[] = {10, 20, 30};
|
||||
int value[] = {60, 100, 120};
|
||||
int capacity = 50;
|
||||
assertEquals(240, FractionalKnapsack.fractionalKnapsack(weight, value, capacity));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFractionalKnapsackWithZeroCapacity() {
|
||||
int weight[] = {10, 20, 30};
|
||||
int value[] = {60, 100, 120};
|
||||
int capacity = 0;
|
||||
assertEquals(0, FractionalKnapsack.fractionalKnapsack(weight, value, capacity));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFractionalKnapsackWithEmptyItems() {
|
||||
int weight[] = {};
|
||||
int value[] = {};
|
||||
int capacity = 50;
|
||||
assertEquals(0, FractionalKnapsack.fractionalKnapsack(weight, value, capacity));
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.thealgorithms.greedyalgorithms;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class JobSequencingTest {
|
||||
@Test
|
||||
public void testJobSequencingWithExampleCase() {
|
||||
ArrayList<JobSequencing.Job> jobs = new ArrayList<>();
|
||||
jobs.add(new JobSequencing.Job('a', 2, 100));
|
||||
jobs.add(new JobSequencing.Job('b', 1, 19));
|
||||
jobs.add(new JobSequencing.Job('c', 2, 27));
|
||||
jobs.add(new JobSequencing.Job('d', 1, 25));
|
||||
jobs.add(new JobSequencing.Job('e', 3, 15));
|
||||
Collections.sort(jobs);
|
||||
String jobSequence = JobSequencing.findJobSequence(jobs, jobs.size());
|
||||
|
||||
assertEquals("Job Sequence: c -> a -> e", jobSequence);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user