refactor: improve code and test coverage for MapReduce example (#6348)

refactor: improve code and test coverage for MapReduce example
This commit is contained in:
Oleksandr Klymenko
2025-07-06 23:19:50 +02:00
committed by GitHub
parent 350f149657
commit 4b6006c876
2 changed files with 30 additions and 39 deletions

View File

@@ -2,22 +2,14 @@ package com.thealgorithms.misc;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class MapReduceTest {
@Test
public void testMapReduceWithSingleWordSentence() {
String oneWordSentence = "Hactober";
String result = MapReduce.mapreduce(oneWordSentence);
assertEquals("Hactober: 1", result);
}
@Test
public void testMapReduceWithMultipleWordSentence() {
String multipleWordSentence = "I Love Love HactoberFest";
String result = MapReduce.mapreduce(multipleWordSentence);
assertEquals("I: 1,Love: 2,HactoberFest: 1", result);
@ParameterizedTest
@CsvSource({"'hello world', 'hello: 1,world: 1'", "'one one two', 'one: 2,two: 1'", "'a a a a', 'a: 4'", "' spaced out ', 'spaced: 1,out: 1'"})
void testCountWordFrequencies(String input, String expected) {
String result = MapReduce.countWordFrequencies(input);
assertEquals(expected, result);
}
}