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