-
Notifications
You must be signed in to change notification settings - Fork 59
/
AbstractComparableTest.java
60 lines (50 loc) · 1.86 KB
/
AbstractComparableTest.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
59
60
package edu.hm.hafner.util;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
/**
* Verifies that comparable objects comply with the contract in {@link Comparable#compareTo(Object)}.
*
* @author Ullrich Hafner
* @param <T> the type of the subject under test
*/
public abstract class AbstractComparableTest<T extends Comparable<T>> {
/**
* Verifies that a negative integer, zero, or a positive integer as this object is less than, equal to, or greater
* than the specified object.
*/
@Test
@SuppressWarnings({"SelfComparison", "EqualsWithItself"})
void shouldBeNegativeIfThisIsSmaller() {
T smaller = createSmallerSut();
T greater = createGreaterSut();
assertThat(smaller.compareTo(greater)).isNegative();
assertThat(greater.compareTo(smaller)).isPositive();
assertThat(smaller.compareTo(smaller)).isZero();
assertThat(greater.compareTo(greater)).isZero();
}
/**
* Verifies that {@code sgn(x.compareTo(y)) == -sgn(y.compareTo(x))} for all {@code x} and {@code y}.
*/
@Test
void shouldBeSymmetric() {
T left = createSmallerSut();
T right = createGreaterSut();
int leftCompareToResult = left.compareTo(right);
int rightCompareToResult = right.compareTo(left);
assertThat(Integer.signum(leftCompareToResult)).isEqualTo(-Integer.signum(rightCompareToResult));
}
/**
* Creates a subject under test. The SUT must be smaller than the SUT of the opposite method {@link
* #createGreaterSut()}.
*
* @return the SUT
*/
protected abstract T createSmallerSut();
/**
* Creates a subject under test. The SUT must be greater than the SUT of the opposite method {@link
* #createSmallerSut()}.
*
* @return the SUT
*/
protected abstract T createGreaterSut();
}