package com.jillesvangurp.efficientstring;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import java.util.Set;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.google.common.collect.Sets;

@Test
public class EfficientStringTest {
    
    @BeforeClass
    public void beforeClass() {
        EfficientString.fromString("abc");
        EfficientString.fromString("def");
        EfficientString.fromString("ghi");
        EfficientString.fromString("jkl");
    }

    public void shouldNotCreateNewStrings() {
        assertThat(EfficientString.fromString("abc").index(), is(0));
        assertThat(EfficientString.fromString("def").index(), is(1));
        assertThat(EfficientString.fromString("ghi").index(), is(2));
        assertThat(EfficientString.fromString("jkl").index(), is(3));
    }
    
    public void shouldCreateNewString() {
        assertThat(EfficientString.fromString("foo").index(), greaterThan(3));        
    }
    
    public void shouldShareSameInstance() {
        EfficientString es1 = EfficientString.fromString("imunique");
        EfficientString es2 = EfficientString.fromString("imunique");
        assertThat("should be same instance", es1==es2);
    }
    
    public void shouldBeAbleToRetrieveUsingIndex() {
        EfficientString es = EfficientString.fromString("bar");
        assertThat(EfficientString.get(es.index()).toString(), is("bar"));
    }
    
    public void shouldStoreAndRetrieveManyStrings() {
        Set<EfficientString> created = Sets.newHashSet();
        //exceed the average bucket size to force growth of the buckets
        for(int i=0;i<6*EfficientString.HASH_MODULO;i++) { 
            EfficientString fromString = EfficientString.fromString(""+i);
            assertThat(fromString, notNullValue());
            created.add(fromString);            
        }
        for (EfficientString efficientString : created) {
            assertThat(EfficientString.get(efficientString.index()), is(efficientString));
            assertThat(EfficientString.fromString(efficientString.toString()), is(efficientString));
        }
    }
}

@@ -94,57 +94,8 @@
 at;%0A
-import static org.hamcrest.Matchers.greaterThan;%0A
 impo
@@ -208,51 +208,8 @@
 t;%0A%0A
-import org.testng.annotations.BeforeClass;%0A
 impo
@@ -335,345 +335,140 @@
     
-@BeforeClass%0A    public void beforeClass() %7B%0A        EfficientString.fromString(%22abc%22);%0A        EfficientString.fromString(%22def%22);%0A        EfficientString.fromString(%22ghi%22);%0A        EfficientString.fromString(%22jkl%22);%0A    %7D%0A%0A    public void shouldNotCreateNewStrings() %7B%0A        assertThat(EfficientString.fromString(%22abc%22).index(), 
+public void shouldCreateNewString() %7B%0A        int next = EfficientString.index;%0A        String str = %22foo%22+System.currentTimeMill
 is(
-0)
 );%0A 
@@ -470,35 +470,24 @@
 ();%0A        
-assertThat(
 EfficientStr
@@ -493,63 +493,14 @@
 ring
-.fromString(%22def%22).index(), is(1));%0A        assertThat(
+ es = 
 Effi
@@ -526,29 +526,11 @@
 ing(
-%22ghi%22).index(), is(2)
+str
 );%0A 
@@ -551,111 +551,30 @@
 hat(
-EfficientString.fromString(%22jkl%22).index(), is(3));%0A    %7D%0A    %0A    public void shouldCreateNewString() %7B
+es.index(), is(next));
 %0A   
@@ -609,59 +609,27 @@
 ing.
-fromString(%22foo%22).index(), greaterThan(3));        
+get(next), is(es));
 %0A   

make test more robust