- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathAmicableNumberTest.java
58 lines (47 loc) · 2.14 KB
/
AmicableNumberTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
packagecom.thealgorithms.maths;
importstaticorg.assertj.core.api.Assertions.assertThat;
importjava.util.Set;
importorg.apache.commons.lang3.tuple.Pair;
importorg.junit.jupiter.api.Assertions;
importorg.junit.jupiter.api.Test;
publicclassAmicableNumberTest {
privatestaticfinalStringINVALID_RANGE_EXCEPTION_MESSAGE = "Given range of values is invalid!";
privatestaticfinalStringINVALID_NUMBERS_EXCEPTION_MESSAGE = "Input numbers must be natural!";
@Test
publicvoidtestShouldThrowExceptionWhenInvalidRangeProvided() {
checkInvalidRange(0, 0);
checkInvalidRange(0, 1);
checkInvalidRange(1, 0);
checkInvalidRange(10, -1);
checkInvalidRange(-1, 10);
}
@Test
publicvoidtestShouldThrowExceptionWhenInvalidNumbersProvided() {
checkInvalidNumbers(0, 0);
checkInvalidNumbers(0, 1);
checkInvalidNumbers(1, 0);
}
@Test
publicvoidtestAmicableNumbers() {
assertThat(AmicableNumber.isAmicableNumber(220, 284)).isTrue();
assertThat(AmicableNumber.isAmicableNumber(1184, 1210)).isTrue();
assertThat(AmicableNumber.isAmicableNumber(2620, 2924)).isTrue();
}
@Test
publicvoidtestShouldFindAllAmicableNumbersInRange() {
// given
varexpectedResult = Set.of(Pair.of(220, 284), Pair.of(1184, 1210), Pair.of(2620, 2924));
// when
Set<Pair<Integer, Integer>> result = AmicableNumber.findAllInRange(1, 3000);
// then
Assertions.assertTrue(result.containsAll(expectedResult));
}
privatestaticvoidcheckInvalidRange(intfrom, intto) {
IllegalArgumentExceptionexception = Assertions.assertThrows(IllegalArgumentException.class, () -> AmicableNumber.findAllInRange(from, to));
Assertions.assertEquals(exception.getMessage(), INVALID_RANGE_EXCEPTION_MESSAGE);
}
privatestaticvoidcheckInvalidNumbers(inta, intb) {
IllegalArgumentExceptionexception = Assertions.assertThrows(IllegalArgumentException.class, () -> AmicableNumber.isAmicableNumber(a, b));
Assertions.assertEquals(exception.getMessage(), INVALID_NUMBERS_EXCEPTION_MESSAGE);
}
}