Skip to content

Commit

Permalink
Adding score correstness unit tests based on precalculated and random…
Browse files Browse the repository at this point in the history
… values

Signed-off-by: Martin Gaievski <[email protected]>
  • Loading branch information
martin-gaievski committed Aug 2, 2023
1 parent 793f05a commit b2aeaae
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.carrotsearch.randomizedtesting.RandomizedTest;

public class ArithmeticMeanScoreCombinationTechniqueTests extends BaseScoreCombinationTechniqueTests {

Expand All @@ -29,21 +33,47 @@ public void testLogic_whenNotAllScoresPresentAndNoWeights_thenCorrectScores() {
}

public void testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores() {
List<Float> scores = List.of(1.0f, 0.5f, 0.3f);
List<Double> weights = List.of(0.9, 0.2, 0.7);
ScoreCombinationTechnique technique = new ArithmeticMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
float expectedScore = 0.6722f;
testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, scores, expectedScore);
}

public void testRandomValues_whenAllScoresAndWeightsPresent_thenCorrectScores() {
List<Double> weights = IntStream.range(0, RANDOM_SCORES_SIZE)
.mapToObj(i -> RandomizedTest.randomDouble())
.collect(Collectors.toList());
ScoreCombinationTechnique technique = new ArithmeticMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testRandomValues_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
}

public void testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores() {
List<Float> scores = List.of(1.0f, -1.0f, 0.6f);
List<Double> weights = List.of(0.9, 0.2, 0.7);
ScoreCombinationTechnique technique = new ArithmeticMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
float expectedScore = 0.825f;
testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, scores, expectedScore);
}

public void testRandomValues_whenNotAllScoresAndWeightsPresent_thenCorrectScores() {
List<Double> weights = IntStream.range(0, RANDOM_SCORES_SIZE)
.mapToObj(i -> RandomizedTest.randomDouble())
.collect(Collectors.toList());
ScoreCombinationTechnique technique = new ArithmeticMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testRandomValues_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
}

private float arithmeticMean(List<Float> scores, List<Double> weights) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import org.apache.commons.lang.ArrayUtils;
import org.opensearch.test.OpenSearchTestCase;

import com.carrotsearch.randomizedtesting.RandomizedTest;

@NoArgsConstructor
public class BaseScoreCombinationTechniqueTests extends OpenSearchTestCase {

protected BiFunction<List<Float>, List<Double>, Float> expectedScoreFunction;
protected static final int RANDOM_SCORES_SIZE = 100;

private static final float DELTA_FOR_ASSERTION = 0.0001f;

Expand All @@ -37,21 +40,57 @@ public void testLogic_whenNotAllScoresPresentAndNoWeights_thenCorrectScores(fina

public void testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores(
final ScoreCombinationTechnique technique,
List<Double> weights
List<Float> scores,
float expectedScore
) {
float[] scores = { 1.0f, 0.5f, 0.3f };
float[] scoresArray = new float[scores.size()];
for (int i = 0; i < scoresArray.length; i++) {
scoresArray[i] = scores.get(i);
}
float actualScore = technique.combine(scoresArray);
assertEquals(expectedScore, actualScore, DELTA_FOR_ASSERTION);
}

public void testRandomValues_whenAllScoresAndWeightsPresent_thenCorrectScores(
final ScoreCombinationTechnique technique,
final List<Double> weights
) {
float[] scores = new float[weights.size()];
for (int i = 0; i < RANDOM_SCORES_SIZE; i++) {
scores[i] = randomScore();
}
float actualScore = technique.combine(scores);
float expectedScore = expectedScoreFunction.apply(Arrays.asList(ArrayUtils.toObject(scores)), weights);
assertEquals(expectedScore, actualScore, DELTA_FOR_ASSERTION);
}

public void testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores(
final ScoreCombinationTechnique technique,
List<Double> weights
List<Float> scores,
float expectedScore
) {
float[] scores = { 1.0f, -1.0f, 0.6f };
float[] scoresArray = new float[scores.size()];
for (int i = 0; i < scoresArray.length; i++) {
scoresArray[i] = scores.get(i);
}
float actualScore = technique.combine(scoresArray);
assertEquals(expectedScore, actualScore, DELTA_FOR_ASSERTION);
}

public void testRandomValues_whenNotAllScoresAndWeightsPresent_thenCorrectScores(
final ScoreCombinationTechnique technique,
final List<Double> weights
) {
float[] scores = new float[weights.size()];
for (int i = 0; i < RANDOM_SCORES_SIZE; i++) {
scores[i] = randomScore();
}
float actualScore = technique.combine(scores);
float expectedScore = expectedScoreFunction.apply(Arrays.asList(ArrayUtils.toObject(scores)), weights);
assertEquals(expectedScore, actualScore, DELTA_FOR_ASSERTION);
}

private float randomScore() {
return RandomizedTest.randomBoolean() ? -1.0f : RandomizedTest.randomFloat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.carrotsearch.randomizedtesting.RandomizedTest;

public class GeometricMeanScoreCombinationTechniqueTests extends BaseScoreCombinationTechniqueTests {

Expand All @@ -29,21 +33,47 @@ public void testLogic_whenNotAllScoresPresentAndNoWeights_thenCorrectScores() {
}

public void testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores() {
List<Float> scores = List.of(1.0f, 0.5f, 0.3f);
List<Double> weights = List.of(0.9, 0.2, 0.7);
ScoreCombinationTechnique technique = new GeometricMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
float expectedScore = 0.5797f;
testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, scores, expectedScore);
}

public void testRandomValues_whenAllScoresAndWeightsPresent_thenCorrectScores() {
List<Double> weights = IntStream.range(0, RANDOM_SCORES_SIZE)
.mapToObj(i -> RandomizedTest.randomDouble())
.collect(Collectors.toList());
ScoreCombinationTechnique technique = new GeometricMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testRandomValues_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
}

public void testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores() {
List<Float> scores = List.of(1.0f, -1.0f, 0.6f);
List<Double> weights = List.of(0.9, 0.2, 0.7);
ScoreCombinationTechnique technique = new GeometricMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
float expectedScore = 0.7997f;
testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, scores, expectedScore);
}

public void testRandomValues_whenNotAllScoresAndWeightsPresent_thenCorrectScores() {
List<Double> weights = IntStream.range(0, RANDOM_SCORES_SIZE)
.mapToObj(i -> RandomizedTest.randomDouble())
.collect(Collectors.toList());
ScoreCombinationTechnique technique = new GeometricMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testRandomValues_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
}

private float geometricMean(List<Float> scores, List<Double> weights) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.carrotsearch.randomizedtesting.RandomizedTest;

public class HarmonicMeanScoreCombinationTechniqueTests extends BaseScoreCombinationTechniqueTests {

Expand All @@ -29,21 +33,47 @@ public void testLogic_whenNotAllScoresPresentAndNoWeights_thenCorrectScores() {
}

public void testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores() {
List<Float> scores = List.of(1.0f, 0.5f, 0.3f);
List<Double> weights = List.of(0.9, 0.2, 0.7);
ScoreCombinationTechnique technique = new HarmonicMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
float expecteScore = 0.4954f;
testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, scores, expecteScore);
}

public void testRandomValues_whenAllScoresAndWeightsPresent_thenCorrectScores() {
List<Double> weights = IntStream.range(0, RANDOM_SCORES_SIZE)
.mapToObj(i -> RandomizedTest.randomDouble())
.collect(Collectors.toList());
ScoreCombinationTechnique technique = new HarmonicMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testRandomValues_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
}

public void testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores() {
List<Float> scores = List.of(1.0f, -1.0f, 0.6f);
List<Double> weights = List.of(0.9, 0.2, 0.7);
ScoreCombinationTechnique technique = new HarmonicMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
float expectedScore = 0.7741f;
testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, scores, expectedScore);
}

public void testRandomValues_whenNotAllScoresAndWeightsPresent_thenCorrectScores() {
List<Double> weights = IntStream.range(0, RANDOM_SCORES_SIZE)
.mapToObj(i -> RandomizedTest.randomDouble())
.collect(Collectors.toList());
ScoreCombinationTechnique technique = new HarmonicMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testRandomValues_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
}

private float harmonicMean(List<Float> scores, List<Double> weights) {
Expand Down

0 comments on commit b2aeaae

Please sign in to comment.