Package fj.test

Examples of fj.test.Property


    // bzzt!
    return i == i;
  }

  public static void main(final String[] args) {
    final Property p = property(arbInteger, new F<Integer, Property>() {
      public Property f(final Integer a) {
        return prop(isPositive(a) == (a != 0 && !isNegative(a)));
      }
    });
    summary.println(p.check()); // Falsified after 0 passed tests with argument: 0
  }
View Full Code Here


      }
    })));

    // Finally the property.
    // if m1 equals m2, then this implies that m1's hashCode is equal to m2's hashCode.
    final Property p = property(arbMyClass, arbMyClass, new F2<MyClass, MyClass, Property>() {
      public Property f(final MyClass m1, final MyClass m2) {
        return bool(m1.equals(m2)).implies(m1.hashCode() == m2.hashCode());
      }
    });
    // at least 100 from 10,000 should satisfy the premise (m1.equals(m2)) 
    summary.println(p.check(100, 10000, 0, 100)); // OK, passed 100 tests (4776 discarded).
  }
View Full Code Here

3) Appending two StringBuilders, x and y, then reversing, is the same as reversing the second
   StringBuilder (y) and appending the reverse of the first StringBuilder (x).
*/
public final class StringBuilderReverse {
  public static void main(final String[] args) {
    final Property p1 = property(arbStringBuilder, new F<StringBuilder, Property>() {
      public Property f(final StringBuilder sb) {
        return prop(stringBuilderEqual.eq(new StringBuilder(sb), sb.reverse().reverse()));
      }
    });

    final Property p2 = property(arbCharacter, new F<Character, Property>() {
      public Property f(final Character c) {
        return prop(stringBuilderEqual.eq(new StringBuilder().append(c), new StringBuilder().append(c).reverse()));
      }
    });
   
    final Property p3 = property(arbStringBuilder, arbStringBuilder, new F2<StringBuilder, StringBuilder, Property>() {
      public Property f(final StringBuilder x, final StringBuilder y) {
        // copy the string builders before performing updates on x and y.
        final StringBuilder xx = new StringBuilder(x);
        final StringBuilder yy = new StringBuilder(y);
        return prop(stringBuilderEqual.eq(x.append(y).reverse(), yy.reverse().append(xx.reverse())));
View Full Code Here

integer value may overflow during the addition resulting in a negative value. This property is
tested with shrinking; note the reasonably small counter-example.
*/
public final class IntegerOverflow {
  public static void main(final String[] args) {
    final Property p = property(arbIntegerBoundaries, arbIntegerBoundaries, shrinkInteger, shrinkInteger,
        new F2<Integer, Integer, Property>() {
      public Property f(final Integer a, final Integer b) {
        return bool(a > 0 && b > 0).implies(a + b > 0);
      }
    });
    summary.println(p.check()); // Falsified after 4 passed tests with arguments: [8,2147483647]
  }
View Full Code Here

For any two linked lists, the size after calling addAll on one with the other is equivalent to
adding the size of each.
*/
public final class JavaLinkedList {
  public static void main(final String[] args) {
    final Property p = property(arbLinkedList(arbInteger), arbLinkedList(arbInteger),
        new F2<LinkedList<Integer>, LinkedList<Integer>, Property>() {
      public Property f(final LinkedList<Integer> x, final LinkedList<Integer> y) {
        final LinkedList<Integer> xy = new LinkedList<Integer>(x);
        xy.addAll(y);
        return prop(xy.size() == x.size() + y.size());
      }
    });
    summary.println(p.check()); // OK, passed 100 tests.
  }
View Full Code Here

For any Option (o) and any function (f), then calling flatMap on o with a
function that puts its result in Some is equivalent to calling map on o with f.
*/
public final class OptionMonadFunctorLaw {
  public static void main(final String[] args) {
    final Property unitMap = property(arbOption(arbInteger), Arbitrary.<Integer, String>arbFInvariant(arbString), new F2<Option<Integer>, F<Integer, String>, Property>() {
      public Property f(final Option<Integer> o, final F<Integer, String> f) {
        return prop(optionEqual(stringEqual).eq(o.bind(andThen(f, Option.<String>some_())), o.map(f)));
      }
    });
    summary.println(unitMap.minSuccessful(500)); // OK, passed 500 tests.    
  }
View Full Code Here

Checks that adding any two integers, a + b, is equivalent to b + a.
This is the commutative property of addition.
*/
public final class AdditionCommutes {
  public static void main(final String[] args) {
    final Property p = property(arbInteger, arbInteger, new F2<Integer, Integer, Property>() {
      public Property f(final Integer a, final Integer b) {
        return prop(a + b == b + a);
      }
    });
    summary.println(p.check()); // OK, passed 100 tests.
  }
View Full Code Here

*/
public final class AdditionCommutesParallel {
  public static void main(final String[] args) {
    final ExecutorService pool = Executors.newFixedThreadPool(8);
    final Strategy<Property> s = Strategy.executorStrategy(pool);
    final Property p = propertyP(arbInteger, arbInteger, new F2<Integer, Integer, P1<Property>>() {
      public P1<Property> f(final Integer a, final Integer b) {
        return s.par(new P1<Property>() {
          public Property _1() {
            return prop(a + b == b + a);
          }
        });
      }
    });
    summary.println(p.check(1000000, 5000000, 0, 100)); // OK, passed 1000000 tests.
    pool.shutdown();
  }
View Full Code Here

*/
public class MemoisationTest {

    @Test
    public void test1() {
        final Property p = property(arbInteger, a -> {
            P1<Integer> t = P.p(a).memo();
            return prop(t._1() == t._1());
        });
        summary.println(p.check());
    }
View Full Code Here

        summary.println(p.check());
    }

    @Test
    public void test2() {
        final Property p = property(arbInteger, arbInteger, (a, b) -> {
            P2<Integer, Integer> t = P.p(a, b).memo();
            return prop(t._1() == t._1() && t._2() == t._2());
        });
        summary.println(p.check());
    }
View Full Code Here

TOP

Related Classes of fj.test.Property

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.