|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 | 4 |
|
5 |
| -import org.junit.jupiter.params.ParameterizedTest; |
6 |
| -import org.junit.jupiter.params.provider.CsvSource; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.List; |
| 7 | +import org.junit.jupiter.api.BeforeAll; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.TestInstance; |
| 10 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 11 | +import util.JsonLoader; |
| 12 | + |
| 13 | +@TestInstance(Lifecycle.PER_CLASS) |
| 14 | +class Euler001Test { |
| 15 | + |
| 16 | + public static class Euler001TestCase { |
| 17 | + public Integer a; |
| 18 | + public Integer b; |
| 19 | + public Integer n; |
| 20 | + public Long expected; |
| 21 | + } |
7 | 22 |
|
| 23 | + private List<Euler001TestCase> testCases; |
8 | 24 |
|
9 |
| -class Euler001Test { |
| 25 | + @BeforeAll |
| 26 | + public void setup() throws IOException { |
| 27 | + String path = String.join("/", |
| 28 | + "hackerrank", |
| 29 | + "projecteuler", |
| 30 | + "euler001.testcases.json"); |
| 31 | + |
| 32 | + this.testCases = JsonLoader.loadJson(path, Euler001TestCase.class); |
| 33 | + } |
| 34 | + |
| 35 | + @Test void euler001() { |
| 36 | + |
| 37 | + for (Euler001TestCase test : testCases) { |
| 38 | + Long solutionFound = Euler001.euler001(test.a, test.b, test.n); |
10 | 39 |
|
11 |
| - @ParameterizedTest |
12 |
| - @CsvSource({ |
13 |
| - "3, 5, 10, 23, Test Case 1", |
14 |
| - "3, 5, 100, 2318, Test Case 2", |
15 |
| - "3, 5, 1000, 233168, Test Case 3" |
16 |
| - }) |
17 |
| - void euler001( |
18 |
| - int a, |
19 |
| - int b, |
20 |
| - int n, |
21 |
| - long answer, |
22 |
| - String testCase) { |
23 |
| - |
24 |
| - Long solutionFound = Euler001.euler001(a, b, n); |
25 |
| - |
26 |
| - String log = String.format("Problem 001 solved: %s. Answer must be %s", |
27 |
| - testCase, |
28 |
| - answer); |
29 |
| - |
30 |
| - assertEquals(answer, solutionFound, log); |
| 40 | + assertEquals(test.expected, solutionFound, |
| 41 | + "%s(%d, %d, %d) => must be: %s".formatted( |
| 42 | + "Euler001.euler001", |
| 43 | + test.a, test.b, test.n, |
| 44 | + test.expected |
| 45 | + ) |
| 46 | + ); |
| 47 | + } |
31 | 48 | }
|
32 | 49 | }
|
0 commit comments