Skip to content

Added Equal.notEq(), List.isPrefixOf(), List.isSuffixOf() #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/src/main/java/fj/Equal.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public boolean eq(final A a1, final A a2) {
return f.f(a1).f(a2);
}

/**
* Returns <code>true</code> if the two given arguments are not equal, <code>false</code> otherwise.
*
* @param a1 An object to test for inequality against another.
* @param a2 An object to test for inequality against another.
* @return <code>true</code> if the two given arguments are not equal, <code>false</code> otherwise.
*/
public boolean notEq(final A a1, final A a2) {
return !eq(a1, a2);
}

/**
* First-class equality check.
*
Expand Down
26 changes: 26 additions & 0 deletions core/src/main/java/fj/data/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,32 @@ public boolean allEqual(final Equal<A> eq) {
return isEmpty() || tail().isEmpty() || eq.eq(head(), tail().head()) && tail().allEqual(eq);
}

public final boolean isPrefixOf(final Equal<A> eq, final List<A> xs) {
final Iterator<A> i = iterator();
final Iterator<A> j = xs.iterator();

while (i.hasNext() && j.hasNext()) {
if (!eq.eq(i.next(), j.next())) {
return false;
}
}

return !i.hasNext();
}

public final boolean isSuffixOf(final Equal<A> eq, final List<A> xs) {
final Iterator<A> i = iterator();
final Iterator<A> j = xs.drop(xs.length() - length()).iterator();

while (i.hasNext() && j.hasNext()) {
if (!eq.eq(i.next(), j.next())) {
return false;
}
}

return !i.hasNext();
}

/**
* First-class length.
*
Expand Down
57 changes: 57 additions & 0 deletions core/src/test/java/fj/data/properties/ListProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package fj.data.properties;

import fj.Equal;
import fj.P;
import fj.P2;
import fj.data.List;
import fj.runner.PropertyTestRunner;
import fj.test.Gen;
import fj.test.Property;
import org.junit.runner.RunWith;

import static fj.test.Arbitrary.*;
import static fj.test.Property.implies;
import static fj.test.Property.prop;
import static fj.test.Property.property;
import static fj.Equal.intEqual;

/**
* Created by Zheka Kozlov on 02.06.2015.
*/
@RunWith(PropertyTestRunner.class)
public class ListProperties {

public Property isPrefixOf() {
final Gen<P2<List<Integer>, Integer>> gen = arbList(arbInteger).gen.bind(list ->
Gen.choose(0, list.length()).map(i -> P.p(list, i)));

return property(arbitrary(gen), pair -> prop(pair._1().take(pair._2()).isPrefixOf(intEqual, pair._1())));
}

public Property isSuffixOf() {
final Gen<P2<List<Integer>, Integer>> gen = arbList(arbInteger).gen.bind(list ->
Gen.choose(0, list.length()).map(i -> P.p(list, i)));

return property(arbitrary(gen), pair -> prop(pair._1().drop(pair._2()).isSuffixOf(intEqual, pair._1())));
}

public Property isPrefixOfShorter() {
return property(arbList(arbInteger), arbList(arbInteger), (list1, list2) ->
implies(list1.length() > list2.length(), () -> prop(!list1.isPrefixOf(intEqual, list2))));
}

public Property isSuffixOfShorter() {
return property(arbList(arbInteger), arbList(arbInteger), (list1, list2) ->
implies(list1.length() > list2.length(), () -> prop(!list1.isSuffixOf(intEqual, list2))));
}

public Property isPrefixOfDifferentHeads() {
return property(arbList(arbInteger), arbList(arbInteger), arbInteger, arbInteger, (list1, list2, h1, h2) ->
implies(intEqual.notEq(h1, h2), () -> prop(!list1.cons(h1).isPrefixOf(intEqual, list2.cons(h2)))));
}

public Property isSuffixOfDifferentHeads() {
return property(arbList(arbInteger), arbList(arbInteger), arbInteger, arbInteger, (list1, list2, h1, h2) ->
implies(intEqual.notEq(h1, h2), () -> prop(!list1.snoc(h1).isSuffixOf(intEqual, list2.snoc(h2)))));
}
}