blob: 2c47c868cb1cab0e45b405add1431386e3708b89 (
plain) (
blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* OrderTester is a an abstract helper class in the spirit of EqualsTester that
* tests an objects total or partial ordering with respect to T#compareTo.
*
* @author Vegard Sjonfjell
* @see com.yahoo.test.TotalOrderTester
* @see com.yahoo.test.PartialOrderTester
*
*/
public abstract class OrderTester<T extends Comparable<T>> {
private ArrayList<List<T>> groups = new ArrayList<>();
abstract protected void lessTest(T a, T b);
abstract protected void greaterTest(T a, T b);
abstract protected void equalTest(T a, T b);
@SafeVarargs
private final OrderTester<T> addGroup(T... group) {
groups.add(Arrays.asList(group));
return this;
}
/**
* Add group of objects being "less" (wrt. compareTo) than all the objects which follow.
* @param group group of objects
* @return the {@link OrderTester} instance, for method chaining
*/
@SafeVarargs
public final OrderTester<T> theseObjects(T... group) {
return addGroup(group);
}
/**
* Add group of objects being "less" (wrt. compareTo) than all the objects which follow.
* @param group group of objects
* @return the {@link OrderTester} instance, for method chaining
*/
@SafeVarargs
public final OrderTester<T> areLessThan(T... group) {
return addGroup(group);
}
/**
* Test the ordering defined with {@link OrderTester#theseObjects} and {@link OrderTester#areLessThan}
* with respect to T#compareTo and the {@link OrderTester} subclass (e.g. {@link com.yahoo.test.TotalOrderTester}).
*/
public void testOrdering() {
for (int i = 0; i < groups.size(); i++) {
for (T item : groups.get(i)) {
for (T otherItem : groups.get(i)) {
equalTest(item, otherItem);
}
}
for (int j = i+1; j < groups.size(); j++) {
for (T lessItem : groups.get(i)) {
for (T greaterItem : groups.get(j)) {
lessTest(lessItem, greaterItem);
greaterTest(greaterItem, lessItem);
}
}
}
}
}
}
|