aboutsummaryrefslogtreecommitdiffstats
path: root/testutil/src/test/java/com/yahoo/test/OrderTesterTest.java
blob: 2fa24d65b2b996e7d524d9b307ab5066bfb4c330 (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
74
75
76
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.test;

import org.junit.Test;

/**
 * @author Vegard Sjonfjell
 */
public class OrderTesterTest {
    private class PartialInt implements Comparable<PartialInt>  {
        int value;

        public PartialInt(int value) {
            this.value = value;
        }

        @Override
        public int compareTo(PartialInt other) {
            if (Math.abs(value - other.value) < 3) {
                return 0;
            }
            else if (value > other.value)  {
                return 1;
            }
            else if (value < other.value)  {
                return 1;
            }

            return 0;
        }

        @Override
        public String toString() {
            return Integer.toString(value);
        }
    }

    @Test
    public void testTotalOrderTester() {
        new TotalOrderTester<Integer>()
                .theseObjects(3, 3)
                .areLessThan(4)
                .areLessThan(5)
                .areLessThan(6)
                .testOrdering();
    }

    @Test
    public void testPartialOrderTester() {
        new PartialOrderTester<PartialInt>()
                .theseObjects(new PartialInt(3))
                .areLessThan(new PartialInt(3), new PartialInt(3))
                .areLessThan(new PartialInt(4))
                .areLessThan(new PartialInt(4))
                .areLessThan(new PartialInt(5))
                .testOrdering();
    }

    @Test (expected = AssertionError.class)
    public void testTotalOrderTesterFailsOnIncorrectOrdering() {
        new TotalOrderTester<Integer>()
                .theseObjects(3)
                .areLessThan(2)
                .areLessThan(5)
                .testOrdering();
    }

    @Test (expected = AssertionError.class)
    public void testPartialOrderTesterFailsOnIncorrectOrdering() {
        new PartialOrderTester<PartialInt>()
                .theseObjects(new PartialInt(6))
                .areLessThan(new PartialInt(2))
                .areLessThan(new PartialInt(3))
                .testOrdering();
    }
}