|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 | 4 |
|
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; |
9 | 5 | import java.io.IOException;
|
10 | 6 | import java.util.Arrays;
|
11 | 7 | import java.util.List;
|
12 | 8 | import org.junit.jupiter.api.BeforeAll;
|
13 | 9 | import org.junit.jupiter.api.Test;
|
14 | 10 | import org.junit.jupiter.api.TestInstance;
|
15 | 11 | import org.junit.jupiter.api.TestInstance.Lifecycle;
|
16 |
| - |
| 12 | +import util.JsonLoader; |
17 | 13 |
|
18 | 14 | @TestInstance(Lifecycle.PER_CLASS)
|
19 | 15 | class ArraysLeftRotationTest {
|
20 | 16 |
|
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; |
22 | 23 |
|
23 | 24 | @BeforeAll
|
24 | 25 | public void setup() throws IOException {
|
25 |
| - ObjectMapper objectMapper = new ObjectMapper(); |
26 | 26 |
|
27 | 27 | String path = String.join("/", "hackerrank",
|
28 | 28 | "interview_preparation_kit",
|
29 | 29 | "arrays",
|
30 | 30 | "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 { |
41 | 31 |
|
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 | + } |
48 | 34 |
|
49 |
| - int[] expected = mapper.readValue(testCase.get("expected").toString(), int[].class); |
50 |
| - List<Integer> texpected = Arrays.stream(expected).boxed().toList(); |
| 35 | + @Test void testRotLeftOne() { |
51 | 36 |
|
| 37 | + for (ArraysLeftRotationTestCase test : this.testCases) { |
| 38 | + List<Integer> solutionFound = ArraysLeftRotation.rotLeftOne(test.input); |
52 | 39 |
|
53 |
| - assertEquals(texpected, solutionFound, |
| 40 | + assertEquals(test.expected, solutionFound, |
54 | 41 | String.format("%s(%s) answer must be: %s",
|
55 | 42 | "CompareTriplets.compareTriplets",
|
56 |
| - testCase.get("input"), |
57 |
| - testCase.get("expected")) |
| 43 | + test.input, |
| 44 | + test.expected) |
58 | 45 | );
|
59 | 46 | }
|
60 | 47 | }
|
|
0 commit comments