Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmcclean committed Dec 16, 2018
1 parent 4b56077 commit 7d81aca
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import java.util.Iterator;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.function.Supplier;


public interface PersistentMap<K,V> extends Iterable<Tuple2<K,V>> {

PersistentMap<K,V> put(K key, V value);
Expand Down Expand Up @@ -37,20 +39,22 @@ default ReactiveSeq<Tuple2<K,V>> stream(){
return ReactiveSeq.fromIterable(this);
}

default boolean allMatch(Predicate<? super Tuple2<K,V>> c){
return !stream().filterNot(c)
.findFirst()
.isPresent();
}
default boolean equalTo(PersistentMap<K,V> map){
if(size()!=map.size())
return false;
Iterator<Tuple2<K,V>> iterator = iterator();
while(iterator.hasNext()){
Tuple2<K, V> t2 = iterator.next();
if(!Objects.equals(map.getOrElse(t2._1(),null),t2._2())){
return false;
}

}
return true;
return allMatch(map::contains);

}


default boolean contains(Tuple2<K, V> t) {
return get(t._1()).filter(v-> Objects.equals(v,t._2())).isPresent();
}
default MapView<K,V> mapView(){
return new MapView.Impl<>(this);
}
Expand Down
4 changes: 2 additions & 2 deletions cyclops/src/main/java/cyclops/data/HashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public HashMap<K, V> remove(K key) {
public HashMap<K, V> removeAll(K... keys) {
HAMT.Node<K,V> cur = map;
for(K key : keys){
cur = map.minus(0,key.hashCode(),key);
cur = cur.minus(0,key.hashCode(),key);
}
return new HashMap<>(cur);
}
Expand Down Expand Up @@ -211,7 +211,7 @@ public HashMap<K, V> putAll(PersistentMap<? extends K, ? extends V> map) {
public HashMap<K, V> removeAllKeys(Iterable<? extends K> keys) {
HashMap<K,V> res = this;
for(K e : keys){
res = this.remove(e);
res = res.remove(e);
}
return res;
}
Expand Down
6 changes: 5 additions & 1 deletion cyclops/src/main/java/cyclops/data/ImmutableMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public interface ImmutableMap<K,V> extends Iterable<Tuple2<K,V>>,
OnEmptySwitch<Tuple2<K, V>,ImmutableMap<K, V>> {


@Override
default boolean allMatch(final Predicate<? super Tuple2<K, V>> c) {
return Folds.super.allMatch(c);
}

ImmutableMap<K,V> put(K key, V value);
ImmutableMap<K,V> put(Tuple2<K, V> keyAndValue);
Expand Down Expand Up @@ -180,7 +184,7 @@ default ImmutableMap<K,V> notNull(){

@Override
default ImmutableMap<K,V> peek(Consumer<? super V> c) {
return (HashMap<K,V>)Transformable.super.peek(c);
return (ImmutableMap<K,V>)Transformable.super.peek(c);
}


Expand Down
9 changes: 6 additions & 3 deletions cyclops/src/main/java/cyclops/data/TreeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public TreeMap<K, V> remove(K key) {
public TreeMap<K, V> removeAll(K... keys) {
RedBlackTree.Tree<K,V> cur = map;
for(K key : keys){
cur = map.minus(key);
cur = cur.minus(key);
}
return new TreeMap<>(cur, comparator);
}
Expand Down Expand Up @@ -327,10 +327,13 @@ public boolean equals(Object o) {
return equalTo(m);
}
return false;

}

@Override


@Override
public int hashCode() {
return Objects.hash(map);
return map.stream().foldLeft(0,(acc,t2)-> acc+t2.hashCode());
}
}
4 changes: 2 additions & 2 deletions cyclops/src/main/java/cyclops/data/TrieMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public TrieMap<K, V> remove(K key) {
public TrieMap<K, V> removeAll(K[] keys) {
HashedPatriciaTrie.Node<K,V> cur = map;
for(K key : keys){
cur = map.minus(key.hashCode(),key);
cur = cur.minus(key.hashCode(),key);
}
return new TrieMap<>(cur);
}
Expand Down Expand Up @@ -196,6 +196,6 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(map);
return map.streamNaturalOrder().foldLeft(0,(acc,t2)-> acc+t2.hashCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.oath.cyclops.matching.Deconstruct.Deconstruct1;
import com.oath.cyclops.matching.Deconstruct.Deconstruct2;
import com.oath.cyclops.matching.Sealed4;
import cyclops.companion.Comparators;
import cyclops.control.Option;

import cyclops.data.LazySeq;
Expand Down Expand Up @@ -55,6 +56,9 @@ interface Node<K, V> extends Sealed4<EmptyNode<K,V>,SingleNode<K,V>,CollisionNod

Node<K, V> minus(int hash, K key);
ReactiveSeq<Tuple2<K,V>> stream();
default ReactiveSeq<Tuple2<K, V>> streamNaturalOrder(){
return stream();
}

}

Expand Down Expand Up @@ -308,6 +312,10 @@ public <R> R fold(Function<? super EmptyNode<K, V>, ? extends R> fn1, Function<?
public Tuple1<LazySeq<Tuple2<K, V>>> unapply() {
return Tuple.tuple(bucket);
}
@Override
public ReactiveSeq<Tuple2<K, V>> streamNaturalOrder() {
return stream().sorted(Comparators.naturalOrderIdentityComparator());
}
}

static final class ArrayNode<K, V> implements Node<K, V>, Deconstruct1<Node<K,V>[]> {
Expand Down
1 change: 1 addition & 0 deletions cyclops/src/main/java/cyclops/data/base/RedBlackTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ else if (compRes==0)

@Override
public Tree<K, V> plus(K key, V value) {

int compRes = comp.compare(this.key,key);
if (compRes>0) {
return balance(isBlack, left.plus(key, value), right, this.key, this.value);
Expand Down
14 changes: 14 additions & 0 deletions cyclops/src/test/java/cyclops/data/ImmutableTreeMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.stream.Stream;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;


Expand Down Expand Up @@ -48,4 +49,17 @@ public void add50000Entries(){
putAndCompare(map);
}

@Test
public void insertionOrder() {
ImmutableMap<Integer, Integer> map1 = empty();
ImmutableMap<Integer, Integer> map2 = empty();
for (int i = 0; i <= 1000; i++) {
map1 = map1.put(i, i);
map2 = map2.put(1000 - i, 1000 - i);
}
assertEquals(map1,map2);
assertEquals(map1.hashCode(), map2.hashCode());

}

}
23 changes: 1 addition & 22 deletions cyclops/src/test/java/cyclops/data/base/HAMTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,28 +225,7 @@ public void replace12() throws Exception {

}



@Test //@Ignore
public void problemBitsetNode(){
Node<Integer, Integer>[] nodes = new Node[2];
nodes[0] = new HAMT.ValueNode<>(-1,-1,-1);
nodes[1] = new HAMT.ValueNode<>(31,31,31);
BitsetNode<Integer,Integer> node = new BitsetNode<Integer,Integer>( new Long(Long.parseLong("10000000000000000000000000000001", 2)).intValue(),
2,nodes);

System.out.println("index "+ node.bitpos(thirtyOne.hashCode(), 0));
System.out.println("index "+ node.bitpos(thirtyOne.hashCode(), 5));
System.out.println("index "+ node.bitpos(thirtyOne.hashCode(), 10));

System.out.println("index "+ node.bitpos(minusOne.hashCode(), 0));
System.out.println("index "+ node.bitpos(minusOne.hashCode(), 5));
System.out.println("index "+ node.bitpos(minusOne.hashCode(), 10));

assertTrue(node.get(10,minusOne.hashCode(),minusOne).isPresent());
assertTrue(node.get(10,thirtyOne.hashCode(),thirtyOne).isPresent());
}





Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void insertionOrder() {
}
@AllArgsConstructor
@ToString
static class Collider{
static class Collider implements Comparable<Collider>{
int id;
int hash;

Expand All @@ -60,6 +60,11 @@ public boolean equals(Object o){
}
return false;
}

@Override
public int compareTo(Collider o) {
return id-o.id;
}
}

@Test
Expand Down

0 comments on commit 7d81aca

Please sign in to comment.