51 lines
1.5 KiB
Java
Executable File
51 lines
1.5 KiB
Java
Executable File
package testing;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
import java.util.*;
|
|
|
|
import grading.Grade;
|
|
import grading.WeightedTotalStrategy;
|
|
import grading.SizeException;
|
|
|
|
class WeightedTotalStrategyTest
|
|
{
|
|
private static final String HOMEWORK_KEY = "HW";
|
|
private static final String MIDTERM_KEY = "Midterm";
|
|
private static final String FINAL_KEY = "Final";
|
|
private static final String TOTAL_KEY = "Total";
|
|
|
|
@Test
|
|
void calculateTest()
|
|
{
|
|
List<Grade> grades = new ArrayList<Grade>();
|
|
grades.add(new Grade(HOMEWORK_KEY, 40.0));
|
|
grades.add(new Grade(MIDTERM_KEY, (Double) null));
|
|
grades.add(new Grade(FINAL_KEY, 100.0));
|
|
|
|
Map<String, Double> weights = new HashMap<String, Double>();
|
|
weights.put(MIDTERM_KEY, 2.0);
|
|
weights.put(FINAL_KEY, 3.0);
|
|
|
|
WeightedTotalStrategy defaultStrategy = new WeightedTotalStrategy();
|
|
|
|
assertThrows(SizeException.class,
|
|
() -> defaultStrategy.calculate(TOTAL_KEY, (List<Grade>) null));
|
|
assertThrows(SizeException.class,
|
|
() -> defaultStrategy.calculate(TOTAL_KEY, new ArrayList<Grade>()));
|
|
|
|
Grade total = defaultStrategy.calculate(TOTAL_KEY, grades);
|
|
|
|
assertEquals(TOTAL_KEY, total.getKey());
|
|
assertEquals(140.0, total.getValue());
|
|
|
|
WeightedTotalStrategy strategy = new WeightedTotalStrategy(weights);
|
|
|
|
Grade weightedTotal = strategy.calculate(TOTAL_KEY, grades);
|
|
|
|
assertEquals(TOTAL_KEY, weightedTotal.getKey());
|
|
assertEquals(340.0, weightedTotal.getValue());
|
|
}
|
|
}
|