Skip to content

Commit aee98ef

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Arrays: Left Rotation. Test cases loaded from JSON using new utility.
1 parent fd7d9d4 commit aee98ef

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotationTest.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,46 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import com.fasterxml.jackson.core.JsonProcessingException;
6-
import com.fasterxml.jackson.databind.JsonNode;
7-
import com.fasterxml.jackson.databind.ObjectMapper;
8-
import java.io.File;
95
import java.io.IOException;
106
import java.util.Arrays;
117
import java.util.List;
128
import org.junit.jupiter.api.BeforeAll;
139
import org.junit.jupiter.api.Test;
1410
import org.junit.jupiter.api.TestInstance;
1511
import org.junit.jupiter.api.TestInstance.Lifecycle;
16-
12+
import util.JsonLoader;
1713

1814
@TestInstance(Lifecycle.PER_CLASS)
1915
class ArraysLeftRotationTest {
2016

21-
public JsonNode testCases;
17+
public static class ArraysLeftRotationTestCase {
18+
public List<Integer> input;
19+
public List<Integer> expected;
20+
}
21+
22+
List<ArraysLeftRotationTestCase> testCases;
2223

2324
@BeforeAll
2425
public void setup() throws IOException {
25-
ObjectMapper objectMapper = new ObjectMapper();
2626

2727
String path = String.join("/", "hackerrank",
2828
"interview_preparation_kit",
2929
"arrays",
3030
"ctci_array_left_rotation.testcases.json");
31-
File file = new File(
32-
this.getClass()
33-
.getClassLoader()
34-
.getResource(path)
35-
.getFile()
36-
);
37-
this.testCases = objectMapper.readTree(file);
38-
}
39-
40-
@Test void testRotLeftOne() throws JsonProcessingException {
4131

42-
ObjectMapper mapper = new ObjectMapper();
43-
44-
for (JsonNode testCase : this.testCases) {
45-
int[] input = mapper.readValue(testCase.get("input").toString(), int[].class);
46-
List<Integer> tlist = Arrays.stream(input).boxed().toList();
47-
List<Integer> solutionFound = ArraysLeftRotation.rotLeftOne(tlist);
32+
this.testCases = JsonLoader.loadJson(path, ArraysLeftRotationTestCase.class);
33+
}
4834

49-
int[] expected = mapper.readValue(testCase.get("expected").toString(), int[].class);
50-
List<Integer> texpected = Arrays.stream(expected).boxed().toList();
35+
@Test void testRotLeftOne() {
5136

37+
for (ArraysLeftRotationTestCase test : this.testCases) {
38+
List<Integer> solutionFound = ArraysLeftRotation.rotLeftOne(test.input);
5239

53-
assertEquals(texpected, solutionFound,
40+
assertEquals(test.expected, solutionFound,
5441
String.format("%s(%s) answer must be: %s",
5542
"CompareTriplets.compareTriplets",
56-
testCase.get("input"),
57-
testCase.get("expected"))
43+
test.input,
44+
test.expected)
5845
);
5946
}
6047
}

0 commit comments

Comments
 (0)