Home | History | Annotate | Download | only in util
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 1996-2016, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  */
      9 package com.ibm.icu.dev.test.util;
     10 
     11 import java.util.Arrays;
     12 import java.util.Collection;
     13 import java.util.Set;
     14 import java.util.TreeSet;
     15 
     16 import org.junit.Test;
     17 import org.junit.runner.RunWith;
     18 import org.junit.runners.JUnit4;
     19 
     20 import com.ibm.icu.dev.test.TestFmwk;
     21 import com.ibm.icu.dev.util.CollectionUtilities;
     22 
     23 @RunWith(JUnit4.class)
     24 public class TestUtilities extends TestFmwk {
     25     @Test
     26     public void TestCollectionUtilitySpeed() {
     27         TreeSet ts1 = new TreeSet();
     28         TreeSet ts2 = new TreeSet();
     29         int size = 1000;
     30         int iterations = 1000;
     31         String prefix =  "abc";
     32         String postfix = "nop";
     33         for (int i = 0; i < size; ++i) {
     34             ts1.add(prefix + String.valueOf(i) + postfix);
     35             ts2.add(prefix + String.valueOf(i) + postfix);
     36         }
     37         // warm up
     38         CollectionUtilities.containsAll(ts1, ts2);
     39         ts1.containsAll(ts2);
     40 
     41         timeAndCompare(ts1, ts2, iterations, true, .75);
     42         // now different sets
     43         ts1.add("Able");
     44         timeAndCompare(ts1, ts2, iterations, true, .75);
     45         timeAndCompare(ts2, ts1, iterations*100, false, 1.05);
     46     }
     47 
     48     private void timeAndCompare(TreeSet ts1, TreeSet ts2, int iterations, boolean expected, double factorOfStandard) {
     49         double utilityTimeSorted = timeUtilityContainsAll(iterations, ts1, ts2, expected)/(double)iterations;
     50         double standardTimeSorted = timeStandardContainsAll(iterations, ts1, ts2, expected)/(double)iterations;
     51 
     52         if (utilityTimeSorted < standardTimeSorted*factorOfStandard) {
     53             logln("Sorted: Utility time (" + utilityTimeSorted + ") << Standard duration (" + standardTimeSorted + "); " + 100*(utilityTimeSorted/standardTimeSorted) + "%");
     54         } else {
     55             /*errln*/logln("Sorted: Utility time (" + utilityTimeSorted + ") !<< Standard duration (" + standardTimeSorted + "); " + 100*(utilityTimeSorted/standardTimeSorted) + "%");
     56         }
     57     }
     58 
     59     private long timeStandardContainsAll(int iterations, Set hs1, Set hs2, boolean expected) {
     60         long standardTime;
     61         {
     62             long start, end;
     63             boolean temp = false;
     64 
     65             start = System.currentTimeMillis();
     66             for (int i = 0; i < iterations; ++i) {
     67                 temp = hs1.containsAll(hs2);
     68                 if (temp != expected) {
     69                     errln("Bad result");
     70                 }
     71             }
     72             end = System.currentTimeMillis();
     73             standardTime = end - start;
     74         }
     75         return standardTime;
     76     }
     77 
     78     private long timeUtilityContainsAll(int iterations, Set hs1, Set hs2, boolean expected) {
     79         long utilityTime;
     80         {
     81             long start, end;
     82             boolean temp = false;
     83             start = System.currentTimeMillis();
     84             for (int i = 0; i < iterations; ++i) {
     85                 temp = CollectionUtilities.containsAll(hs1, hs2);
     86                 if (temp != expected) {
     87                     errln("Bad result");
     88                 }
     89             }
     90             end = System.currentTimeMillis();
     91             utilityTime = end - start;
     92         }
     93         return utilityTime;
     94     }
     95 
     96     @Test
     97     public void TestCollectionUtilities() {
     98         String[][] test = {{"a", "c", "e", "g", "h", "z"}, {"b", "d", "f", "h", "w"}, { "a", "b" }, { "a", "d" }, {"d"}, {}}; //
     99         int resultMask = 0;
    100         for (int i = 0; i < test.length; ++i) {
    101             Collection a = new TreeSet(Arrays.asList(test[i]));
    102             for (int j = 0; j < test.length; ++j) {
    103                 Collection b = new TreeSet(Arrays.asList(test[j]));
    104                 int relation = CollectionUtilities.getContainmentRelation(a, b);
    105                 resultMask |= (1 << relation);
    106                 switch (relation) {
    107                 case CollectionUtilities.ALL_EMPTY:
    108                     checkContainment(a.size() == 0 && b.size() == 0, a, relation, b);
    109                     break;
    110                 case CollectionUtilities.NOT_A_SUPERSET_B:
    111                     checkContainment(a.size() == 0 && b.size() != 0, a, relation, b);
    112                     break;
    113                 case CollectionUtilities.NOT_A_DISJOINT_B:
    114                     checkContainment(a.equals(b) && a.size() != 0, a, relation, b);
    115                     break;
    116                 case CollectionUtilities.NOT_A_SUBSET_B:
    117                     checkContainment(a.size() != 0 && b.size() == 0, a, relation, b);
    118                     break;
    119                 case CollectionUtilities.A_PROPER_SUBSET_OF_B:
    120                     checkContainment(b.containsAll(a) && !a.equals(b), a, relation, b);
    121                     break;
    122                 case CollectionUtilities.NOT_A_EQUALS_B:
    123                     checkContainment(!CollectionUtilities.containsSome(a, b) && a.size() != 0 && b.size() != 0, a, relation, b);
    124                     break;
    125                 case CollectionUtilities.A_PROPER_SUPERSET_B:
    126                     checkContainment(a.containsAll(b) && !a.equals(b), a, relation, b);
    127                 break;
    128                 case CollectionUtilities.A_PROPER_OVERLAPS_B:
    129                     checkContainment(!b.containsAll(a) && !a.containsAll(b) && CollectionUtilities.containsSome(a, b), a, relation, b);
    130                 break;
    131                 }
    132             }
    133         }
    134         if (resultMask != 0xFF) {
    135             String missing = "";
    136             for (int i = 0; i < 8; ++i) {
    137                 if ((resultMask & (1 << i)) == 0) {
    138                     if (missing.length() != 0) missing += ", ";
    139                     missing += RelationName[i];
    140                 }
    141             }
    142             errln("Not all ContainmentRelations checked: " + missing);
    143         }
    144     }
    145 
    146     static final String[] RelationName = {"ALL_EMPTY",
    147             "NOT_A_SUPERSET_B",
    148             "NOT_A_DISJOINT_B",
    149             "NOT_A_SUBSET_B",
    150             "A_PROPER_SUBSET_OF_B",
    151             "A_PROPER_DISJOINT_B",
    152             "A_PROPER_SUPERSET_B",
    153             "A_PROPER_OVERLAPS_B"};
    154 
    155     /**
    156      *
    157      */
    158     private void checkContainment(boolean c, Collection a, int relation, Collection b) {
    159         if (!c) {
    160             errln("Fails relation: " + a + " \t" + RelationName[relation] + " \t" + b);
    161         }
    162     }
    163 }
    164