01: /*
02: *******************************************************************************
03: * Copyright (C) 2002-2004, International Business Machines Corporation and *
04: * others. All Rights Reserved. *
05: *******************************************************************************
06: */
07:
08: package com.ibm.icu.dev.test.collator;
09:
10: import java.util.*;
11:
12: public class TestComparator {
13:
14: // test the symmetry and transitivity
15: public void test(Comparator comp, int count) {
16: Object c = null;
17: Object b = newObject(c);
18: Object a = newObject(b);
19: int compab = comp.compare(a, b);
20: while (--count >= 0) {
21: // rotate old values
22: c = b;
23: b = a;
24: int compbc = compab;
25:
26: // allocate new and get comparisons
27: a = newObject(b);
28: compab = comp.compare(a, b);
29: int compba = comp.compare(b, a);
30: int compac = comp.compare(a, c);
31:
32: // check symmetry
33: if (compab != -compba) {
34: log("Symmetry Failure", new Object[] { a, b });
35: }
36:
37: // check transitivity
38: check(a, b, c, compab, compbc, compac);
39: check(a, c, b, compab, -compbc, compab);
40: check(b, a, c, -compab, compac, compbc);
41: check(b, c, a, compbc, -compac, -compab);
42: check(c, a, b, -compac, compab, -compbc);
43: check(c, b, a, -compbc, -compab, -compac);
44: }
45: }
46:
47: private void check(Object a, Object b, Object c, int compab,
48: int compbc, int compac) {
49: if (compab <= 0 && compbc <= 0 && !(compac <= 0)) {
50: log("Transitivity Failure", new Object[] { a, b, c });
51: }
52: }
53:
54: public Object newObject(Object c) {
55: // return a new object
56: return "";
57: }
58:
59: public String format(Object c) {
60: // return a new object
61: return c.toString();
62: }
63:
64: public void log(String title, Object[] arguments) {
65: String result = title + ": [";
66: for (int i = 0; i < arguments.length; ++i) {
67: if (i != 0)
68: result += ", ";
69: result += format(arguments[i]);
70: }
71: result += "]";
72: throw new RuntimeException(result);
73: }
74: }
|