callgraph/src/test/java/org/netbeans/lib/callgraph/util/SmallStringTest.java
author Tim Boudreau <tboudreau@netbeans.org>
Sat, 03 Sep 2016 02:41:36 -0400
changeset 18374 04a79821e760
parent 18373 3527a32a19f0
child 18400 c87c223efe6a
permissions -rw-r--r--
Eliminate duplicates in graph files
     1 package org.netbeans.lib.callgraph.util;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Arrays;
     5 import java.util.Collections;
     6 import java.util.HashSet;
     7 import java.util.List;
     8 import java.util.Random;
     9 import java.util.Set;
    10 import org.junit.Test;
    11 import static org.junit.Assert.*;
    12 import org.netbeans.lib.callgraph.util.EightBitStrings.Concatenation;
    13 
    14 /**
    15  *
    16  * @author tim
    17  */
    18 public class SmallStringTest {
    19 
    20     @Test
    21     public void testCreate() {
    22         EightBitStrings strings = new EightBitStrings(false);
    23         CharSequence testOne = strings.create("A first test one");
    24         CharSequence testTwo = strings.create("A second test one");
    25         assertNotEquals(testOne, testTwo);
    26 
    27         CharSequence hello = strings.create("Hello world");
    28         CharSequence hello2 = strings.create("Hello world");
    29         assertEquals(hello, hello2);
    30         assertSame(hello, hello2);
    31         assertEquals(hello.hashCode(), "Hello world".hashCode());
    32         assertEquals("Hello world", hello.toString());
    33         assertEquals("Hello world".length(), hello.length());
    34 
    35         CharSequence worlds = strings.create("Hello worlds");
    36         assertNotEquals(hello2, worlds);
    37 
    38         assertEquals(hello, "Hello world");
    39 //        assertEquals("Hello world", hello);
    40     }
    41 
    42     @Test
    43     public void testInterning() {
    44         EightBitStrings strings = new EightBitStrings(false);
    45         List<String> all = new ArrayList<>(500);
    46         List<CharSequence> seqs = new ArrayList<>();
    47         for (int i = 0; i < 500; i++) {
    48             String s = randomString(5 + r.nextInt(20));
    49             all.add(s);
    50             seqs.add(strings.create(s));
    51         }
    52         int size = strings.internTableSize();
    53         Set<CharSequence> xseqs = new HashSet<>(seqs);
    54         int oldSize = xseqs.size();
    55         for (String again : all) {
    56             CharSequence ss = strings.create(again);
    57             xseqs.add(ss);
    58             assertEquals(size, strings.internTableSize());
    59             assertEquals(oldSize, xseqs.size());
    60         }
    61     }
    62 
    63     @Test
    64     public void testConcatenations() {
    65         EightBitStrings strings = new EightBitStrings(false);
    66         CharSequence concat = strings.concat("Hello ", "there ", "how ", "are ", "you?");
    67         CharSequence concat2 = strings.concat("Hello ", "there ", "how ", "are ", "you?");
    68         assertEquals("Hello there how are you?", concat.toString());
    69         assertEquals("Hello there how are you?".hashCode(), concat.toString().hashCode());
    70         assertEquals(concat, concat2);
    71     }
    72 
    73     @Test
    74     public void testSorting() {
    75         EightBitStrings strings = new EightBitStrings(false);
    76         ComparableCharSequence a = strings.concat(strings.create("a"), strings.create("b"), strings.create("c"), strings.create("d"));
    77         ComparableCharSequence b = strings.concat(strings.create("a"), strings.create("b"), strings.create("cd"));
    78         ComparableCharSequence c = strings.concat(strings.create("ab"), strings.create("cd"));
    79         ComparableCharSequence d = strings.concat(strings.create("a"), strings.create("bcd"));
    80         ComparableCharSequence e = strings.concat(strings.create("abcd"));
    81         assertEquals(4, a.length());
    82         assertEquals(4, b.length());
    83         assertEquals(4, c.length());
    84         assertEquals(4, d.length());
    85         assertEquals(4, e.length());
    86         for (ComparableCharSequence c1 : new ComparableCharSequence[]{a, b, c, d, e}) {
    87             for (ComparableCharSequence c2 : new ComparableCharSequence[]{a, b, c, d, e}) {
    88                 assertEquals(c1, c2);
    89                 assertEquals(0, c1.compareTo(c2));
    90             }
    91         }
    92 
    93         ComparableCharSequence cs = strings.concatQuoted(Arrays.asList(strings.create("a"), strings.create("b"), strings.create("c"), strings.create("d")));
    94         assertEquals("\"a\" \"b\" \"c\" \"d\"", cs.toString());
    95 
    96         strings = new EightBitStrings(true);
    97         a = strings.concat("a", "b", "c", "d");
    98         b = strings.concat("a", "b", "cd");
    99         c = strings.concat("ab", "cd");
   100         d = strings.concat("a", "bcd");
   101         e = strings.concat("abcd");
   102         assertEquals(4, a.length());
   103         assertEquals(4, b.length());
   104         assertEquals(4, c.length());
   105         assertEquals(4, d.length());
   106         assertEquals(4, e.length());
   107         for (ComparableCharSequence c1 : new ComparableCharSequence[]{a, b, c, d, e}) {
   108             for (ComparableCharSequence c2 : new ComparableCharSequence[]{a, b, c, d, e}) {
   109                 assertEquals(c1, c2);
   110                 assertEquals(0, c1.compareTo(c2));
   111             }
   112         }
   113 
   114         strings = new EightBitStrings(false);
   115         a = strings.concat(strings.create("abc"), strings.create("def"));
   116         b = strings.concat("bcd", strings.create("efg"));
   117         c = strings.concat("cde", strings.create("fgh"));
   118         d = strings.create("defghi");
   119         e = strings.create("efghij");
   120         List<ComparableCharSequence> l = Arrays.asList(e, c, d, a, b);
   121         Collections.sort(l);
   122         assertEquals(l, Arrays.asList(a, b, c, d, e));
   123     }
   124     
   125     List<String> stringsOf(List<CharSequence> cs) {
   126         List<String> result = new ArrayList<>();
   127         for (CharSequence c : cs) {
   128             if (c == null) {
   129                 break;
   130             }
   131             result.add(c.toString());
   132         }
   133         return result;
   134     }
   135     
   136     @Test
   137     public void testAggressive() {
   138         EightBitStrings strings = new EightBitStrings(false, true);
   139         ComparableCharSequence c = strings.concat("com.foo.", "bar.baz.", "Hey$You");
   140         assertEquals("com.foo.bar.baz.Hey$You", c.toString());
   141         ComparableCharSequence c2 = strings.concat("com.foo.", "bar.whoodle.", "Hey$You");
   142         List<String> interned = stringsOf(strings.dumpInternTable());
   143         for (String cs : interned) {
   144             if (cs == null) {
   145                 break;
   146             }
   147             System.out.println("  " + cs);
   148         }
   149         assertTrue(interned.contains("com"));
   150         assertTrue(interned.contains("foo"));
   151         assertTrue(interned.contains("bar"));
   152         assertTrue(interned.contains("baz"));
   153         assertTrue(interned.contains("Hey"));
   154         assertTrue(interned.contains("You"));
   155         assertTrue(interned.contains("whoodle"));
   156         assertTrue(interned.contains("."));
   157         assertTrue(interned.contains("$"));
   158         assertTrue(interned.contains("\""));
   159     }
   160 
   161     @Test
   162     public void testConcatQuoted() {
   163         EightBitStrings strings = new EightBitStrings(false);
   164         List<Object> l = Arrays.asList(strings.create("hey"), false, 23, strings.create("bar"), "baz");
   165         CharSequence cc = strings.concatQuoted(l);
   166         assertEquals("\"hey\" false 23 \"bar\" \"baz\"", cc.toString());
   167     }
   168     
   169     private static String randomString(int len) {
   170         char[] c = new char[len];
   171         for (int i = 0; i < c.length; i++) {
   172             c[i] = randomChar();
   173         }
   174         return new String(c);
   175     }
   176 
   177     private static char randomChar() {
   178         return alpha[r.nextInt(alpha.length)];
   179     }
   180 
   181     static final Random r = new Random(320392);
   182     private static final char[] alpha = "abcdefghijklmnopqrstuvwxyz".toCharArray();
   183 
   184 }