Skip to content

Commit 6ff688d

Browse files
committed
Add tests for Problem 1, 2 and 3
1 parent 56da6ac commit 6ff688d

File tree

7 files changed

+63
-7
lines changed

7 files changed

+63
-7
lines changed

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,16 @@
88
<artifactId>project-euler-java</artifactId>
99
<version>1.0-SNAPSHOT</version>
1010

11-
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.12</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.assertj</groupId>
19+
<artifactId>assertj-core</artifactId>
20+
<version>3.6.2</version>
21+
</dependency>
22+
</dependencies>
1223
</project>

src/main/java/Problem1.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
* Find the sum of all the multiples of 3 or 5 below 1000.
66
*/
77
public class Problem1 {
8-
public static void main(String[] args) {
8+
public int solve() {
99
int result = 0;
1010

1111
for (int i = 3; i < 1000; i++) {
1212
if (i % 3 == 0 || i % 5 == 0) {
1313
result += i;
1414
}
1515
}
16+
return result;
17+
}
18+
19+
public static void main(String[] args) {
20+
int result = new Problem1().solve();
1621
System.out.println("Result: " + result);
1722
}
1823
}

src/main/java/Problem2.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
88
*/
99
public class Problem2 {
10-
public static void main(String[] args) {
10+
public int solve() {
1111
int previous = 1;
1212
int current = 1;
1313
int result = 0;
@@ -20,6 +20,11 @@ public static void main(String[] args) {
2020
previous = current;
2121
current = newCurrent;
2222
}
23+
return result;
24+
}
25+
26+
public static void main(String[] args) {
27+
int result = new Problem2().solve();
2328
System.out.println("Result: " + result);
2429
}
2530
}

src/main/java/Problem3.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* What is the largest prime factor of the number 600851475143 ?
66
*/
77
public class Problem3 {
8-
public static void main(String[] args) {
8+
public int solve() {
99
long num = 600851475143L;
10-
long largest = -1;
10+
int largest = -1;
1111
long copy = num;
12-
for (long i = 2; i < Math.sqrt(num); i++) {
12+
for (int i = 2; i < Math.sqrt(num); i++) {
1313
if (copy == 1L) {
1414
break;
1515
}
@@ -18,6 +18,11 @@ public static void main(String[] args) {
1818
largest = i;
1919
}
2020
}
21-
System.out.println("Result: " + largest);
21+
return largest;
22+
}
23+
24+
public static void main(String[] args) {
25+
int result = new Problem3().solve();
26+
System.out.println("Result: " + result);
2227
}
2328
}

src/test/java/Problem1Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.assertj.core.api.Assertions;
2+
import org.junit.Test;
3+
4+
public class Problem1Test {
5+
@Test
6+
public void checkValidValue() {
7+
int result = new Problem1().solve();
8+
Assertions.assertThat(result).isEqualTo(233168);
9+
}
10+
}

src/test/java/Problem2Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.assertj.core.api.Assertions;
2+
import org.junit.Test;
3+
4+
public class Problem2Test {
5+
@Test
6+
public void checkValidValue() {
7+
int result = new Problem2().solve();
8+
Assertions.assertThat(result).isEqualTo(4613732);
9+
}
10+
}

src/test/java/Problem3Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.assertj.core.api.Assertions;
2+
import org.junit.Test;
3+
4+
public class Problem3Test {
5+
@Test
6+
public void checkValidValue() {
7+
int result = new Problem3().solve();
8+
Assertions.assertThat(result).isEqualTo(6857);
9+
}
10+
}

0 commit comments

Comments
 (0)