package cyclops.data.base;

import cyclops.control.Option;
import org.junit.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.*;

public class HAMTTest {
    Integer one = 1;
    Integer minusOne = -1;
    Integer two = 2;
    @Test
    public void empty() throws Exception {
        HAMT.Node<Integer, Integer> node = HAMT.<Integer, Integer>empty();
        assertThat(node.size(),equalTo(0));
    }
    @Test
    public void put1() throws Exception {
        HAMT.Node<Integer, Integer> node = HAMT.<Integer, Integer>empty().plus(0,one.hashCode(),one,one);
        assertThat(node.get(0,one.hashCode(),one),equalTo(Option.some(1)));
    }
    @Test
    public void putMany() throws Exception {
        HAMT.Node<Integer, Integer> node = HAMT.<Integer, Integer>empty();
        for(int i=0;i<1026;i++) {

            Integer next = i;
            node = node.plus(0, next.hashCode(), next, next);
            assertThat(node.toString(),node.get(0,next.hashCode(),next),equalTo(Option.some(next)));
            node = node.plus(0,minusOne.hashCode(),minusOne,minusOne);
        }
        System.out.println(node);
        node = node.plus(0,minusOne.hashCode(),minusOne,minusOne);
        assertTrue(node.get(0,minusOne.hashCode(),minusOne).isPresent());
        System.out.println(node);
    }
    @Test
    public void put31() throws Exception {
        HAMT.Node<Integer, Integer> node = HAMT.<Integer, Integer>empty();
        for(int i=0;i<32;i++) {

            Integer next = i;
            node = node.plus(0, next.hashCode(), next, next);
            assertThat(node.toString(),node.get(0,next.hashCode(),next),equalTo(Option.some(next)));
            if(i==10)
                node = node.plus(0,minusOne.hashCode(),minusOne,minusOne);
        }
        System.out.println(node);
        node = node.plus(0,minusOne.hashCode(),minusOne,minusOne);
        assertTrue(node.get(0,minusOne.hashCode(),minusOne).isPresent());
        System.out.println(node);
    }

}
@@ -1998,9 +1998,405 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void problemBitsetNode()%7B%0A        HAMT.Node%3CInteger, Integer%3E%5B%5D nodes = new HAMT.Node%5B2%5D;%0A        nodes%5B0%5D = new HAMT.ValueNode%3C%3E(-1,-1,-1);%0A        nodes%5B1%5D = new HAMT.ValueNode%3C%3E(31,31,31);%0A        new HAMT.BitsetNode%3CInteger,Integer%3E( new Long(Long.parseLong(%2210000000000000000000000000000001%22, 2)).intValue(),%0A                                            2,nodes);%0A    %7D%0A%0A
 %7D

test base for HAMT issue