javaclass

Annotated tests/DispatchTest.java

146:1dc70ce13680
2005-01-23 Paul Boddie Added more tests.
paul@28 1
public class DispatchTest {
paul@28 2
    public int a;
paul@28 3
    public float b;
paul@28 4
paul@28 5
    public DispatchTest() {
paul@28 6
        this.a = 1;
paul@28 7
        this.b = 2;
paul@28 8
    }
paul@28 9
paul@28 10
    public DispatchTest(int a) {
paul@28 11
        this.a = a;
paul@28 12
        this.b = 2;
paul@28 13
    }
paul@28 14
paul@28 15
    public DispatchTest(float b) {
paul@28 16
        this.a = 1;
paul@28 17
        this.b = b;
paul@28 18
    }
paul@28 19
paul@28 20
    public DispatchTest(int a, float b) {
paul@28 21
        this.a = a;
paul@28 22
        this.b = b;
paul@28 23
    }
paul@28 24
paul@28 25
    public void set(int a) {
paul@28 26
        this.a = a;
paul@28 27
    }
paul@28 28
paul@28 29
    public void set(float b) {
paul@28 30
        this.b = b;
paul@28 31
    }
paul@32 32
paul@36 33
    // The ordering of the methods probably should not prevent the most specific
paul@36 34
    // method from being chosen, but this happens with a linear search of
paul@36 35
    // appropriate methods.
paul@36 36
paul@32 37
    public int test(DispatchInterface obj) {
paul@32 38
        return obj.test();
paul@32 39
    }
paul@32 40
paul@32 41
    public int test(DispatchClass1 obj) {
paul@36 42
        return obj.test() + 10;
paul@32 43
    }
paul@32 44
paul@32 45
    public int testTest(DispatchInterface obj) {
paul@32 46
        return test(obj);
paul@32 47
    }
paul@146 48
paul@146 49
    public static void main(String[] args) {
paul@146 50
        DispatchTest test = new DispatchTest();
paul@146 51
        DispatchClass1 dc1 = new DispatchClass1();
paul@146 52
        DispatchClass2 dc2 = new DispatchClass2();
paul@146 53
paul@146 54
        if (test.a == 1 && test.b == 2) {
paul@146 55
            System.out.println("test.a, test.b correct: " + test.a + ", " + test.b);
paul@146 56
        } else {
paul@146 57
            System.err.println("test.a, test.b failed!");
paul@146 58
        }
paul@146 59
        test.set(5);
paul@146 60
        if (test.a == 5 && test.b == 2) {
paul@146 61
            System.out.println("test.a, test.b correct: " + test.a + ", " + test.b);
paul@146 62
        } else {
paul@146 63
            System.err.println("test.a, test.b failed!");
paul@146 64
        }
paul@146 65
        test.set(7.0f);
paul@146 66
        if (test.a == 5 && test.b == 7.0f) {
paul@146 67
            System.out.println("test.a, test.b correct: " + test.a + ", " + test.b);
paul@146 68
        } else {
paul@146 69
            System.err.println("test.a, test.b failed!");
paul@146 70
        }
paul@146 71
        if (test.test(dc1) == 11) {
paul@146 72
            System.out.println("test.test(dc1) correct: " + test.test(dc1));
paul@146 73
        } else {
paul@146 74
            System.err.println("test.test(dc1) failed!");
paul@146 75
        }
paul@146 76
        if (test.test(dc2) == 2) {
paul@146 77
            System.out.println("test.test(dc2) correct: " + test.test(dc2));
paul@146 78
        } else {
paul@146 79
            System.err.println("test.test(dc2) failed!");
paul@146 80
        }
paul@146 81
        // Yes, one might think this could be 11, but the parameter becomes
paul@146 82
        // "more vague" when passed to testTest.
paul@146 83
        if (test.testTest(dc1) == 1) {
paul@146 84
            System.out.println("test.testTest(dc1) correct: " + test.testTest(dc1));
paul@146 85
        } else {
paul@146 86
            System.err.println("test.testTest(dc1) failed!");
paul@146 87
        }
paul@146 88
        if (test.testTest(dc2) == 2) {
paul@146 89
            System.out.println("test.testTest(dc2) correct: " + test.testTest(dc2));
paul@146 90
        } else {
paul@146 91
            System.err.println("test.testTest(dc2) failed!");
paul@146 92
        }
paul@146 93
    }
paul@28 94
}
paul@32 95
paul@32 96
interface DispatchInterface {
paul@32 97
    public int test();
paul@32 98
}
paul@32 99
paul@32 100
class DispatchClass1 implements DispatchInterface {
paul@32 101
    public int test() {
paul@32 102
        return 1;
paul@32 103
    }
paul@32 104
}
paul@32 105
paul@32 106
class DispatchClass2 implements DispatchInterface {
paul@32 107
    public int test() {
paul@32 108
        return 2;
paul@32 109
    }
paul@32 110
}
paul@146 111
paul@146 112
// vim: tabstop=4 expandtab shiftwidth=4