|
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 | +import java.io.IOException; |
5 | 10 | import java.util.Arrays;
|
6 | 11 | import java.util.List;
|
7 | 12 | import org.junit.jupiter.api.BeforeAll;
|
|
11 | 16 |
|
12 | 17 |
|
13 | 18 | @TestInstance(Lifecycle.PER_CLASS)
|
14 |
| - class ArraysLeftRotationTest { |
| 19 | +class ArraysLeftRotationTest { |
15 | 20 |
|
16 |
| - public class ArraysLeftRotationTestCase { |
17 |
| - public List<Integer> input; |
18 |
| - public List<Integer> expected; |
| 21 | + public JsonNode testCases; |
19 | 22 |
|
20 |
| - public ArraysLeftRotationTestCase(List<Integer> input, List<Integer> expected) { |
21 |
| - this.input = input; |
22 |
| - this.expected = expected; |
23 |
| - } |
| 23 | + @BeforeAll |
| 24 | + public void setup() throws IOException { |
| 25 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 26 | + |
| 27 | + String path = String.join("/", "hackerrank", |
| 28 | + "interview_preparation_kit", |
| 29 | + "arrays", |
| 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); |
24 | 38 | }
|
25 | 39 |
|
26 |
| - public class ArraysLeftRotationsTestCase { |
27 |
| - public List<Integer> input; |
28 |
| - public Integer d; |
29 |
| - public List<Integer> expected; |
30 |
| - |
31 |
| - public ArraysLeftRotationsTestCase(List<Integer> input, Integer d, List<Integer> expected) { |
32 |
| - this.input = input; |
33 |
| - this.d = d; |
34 |
| - this.expected = expected; |
35 |
| - } |
36 |
| - } |
| 40 | + @Test void testRotLeftOne() throws JsonProcessingException { |
37 | 41 |
|
38 |
| - public List<ArraysLeftRotationTestCase> testCases; |
39 |
| - public List<ArraysLeftRotationsTestCase> testRotationsCases; |
| 42 | + ObjectMapper mapper = new ObjectMapper(); |
40 | 43 |
|
41 |
| - @BeforeAll |
42 |
| - public void setup() { |
43 |
| - this.testCases = Arrays.asList( |
44 |
| - new ArraysLeftRotationTestCase(Arrays.asList(1, 2, 3, 4, 5), Arrays.asList(2, 3, 4, 5, 1)), |
45 |
| - new ArraysLeftRotationTestCase(Arrays.asList(2, 3, 4, 5, 1), Arrays.asList(3, 4, 5, 1, 2)), |
46 |
| - new ArraysLeftRotationTestCase(Arrays.asList(3, 4, 5, 1, 2), Arrays.asList(4, 5, 1, 2, 3)), |
47 |
| - new ArraysLeftRotationTestCase(Arrays.asList(4, 5, 1, 2, 3), Arrays.asList(5, 1, 2, 3, 4)), |
48 |
| - new ArraysLeftRotationTestCase(Arrays.asList(5, 1, 2, 3, 4), Arrays.asList(1, 2, 3, 4, 5)) |
49 |
| - ); |
| 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); |
50 | 48 |
|
51 |
| - this.testRotationsCases = Arrays.asList( |
52 |
| - new ArraysLeftRotationsTestCase( |
53 |
| - Arrays.asList(1, 2, 3, 4, 5), |
54 |
| - 4, |
55 |
| - Arrays.asList(5, 1, 2, 3, 4)) |
56 |
| - ); |
57 |
| - } |
| 49 | + int[] expected = mapper.readValue(testCase.get("expected").toString(), int[].class); |
| 50 | + List<Integer> texpected = Arrays.stream(expected).boxed().toList(); |
58 | 51 |
|
59 |
| - @Test void testRotLeftOne() { |
60 |
| - for (ArraysLeftRotationTestCase testCase : this.testCases) { |
61 |
| - List<Integer> solutionFound = ArraysLeftRotation.rotLeftOne(testCase.input); |
62 | 52 |
|
63 |
| - assertEquals(testCase.expected, solutionFound, |
| 53 | + assertEquals(texpected, solutionFound, |
64 | 54 | String.format("%s(%s) answer must be: %s",
|
65 | 55 | "CompareTriplets.compareTriplets",
|
66 |
| - testCase.input.toString(), |
67 |
| - testCase.expected.toString()) |
| 56 | + testCase.get("input"), |
| 57 | + testCase.get("expected")) |
68 | 58 | );
|
69 | 59 | }
|
70 | 60 | }
|
71 | 61 |
|
72 | 62 | @Test void testRotLeft() {
|
73 |
| - for (ArraysLeftRotationsTestCase testCase : this.testRotationsCases) { |
74 |
| - List<Integer> solutionFound = ArraysLeftRotation.rotLeft(testCase.input, testCase.d); |
75 |
| - |
76 |
| - assertEquals(testCase.expected, solutionFound, |
77 |
| - String.format("%s(%s, %d) answer must be: %s", |
78 |
| - "CompareTriplets.compareTriplets", |
79 |
| - testCase.input.toString(), |
80 |
| - testCase.d, |
81 |
| - testCase.expected.toString()) |
82 |
| - ); |
83 |
| - } |
| 63 | + List<Integer> input = Arrays.asList(1, 2, 3, 4, 5); |
| 64 | + List<Integer> expected = Arrays.asList(5, 1, 2, 3, 4); |
| 65 | + Integer d = 4; |
| 66 | + |
| 67 | + List<Integer> solutionFound = ArraysLeftRotation.rotLeft(input, d); |
| 68 | + |
| 69 | + assertEquals(expected, solutionFound, |
| 70 | + String.format("%s(%s, %d) answer must be: %s", |
| 71 | + "CompareTriplets.compareTriplets", |
| 72 | + input.toString(), |
| 73 | + d, |
| 74 | + expected.toString()) |
| 75 | + ); |
84 | 76 | }
|
85 |
| - |
86 | 77 | }
|
0 commit comments