Synthia
Generic and flexible data structure generator
Prime.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package examples.quickcheck;
20 
23 import ca.uqac.lif.synthia.test.Assert;
24 
25 /**
26  * Simple illustration of the shrinking process on randomly generated integers.
27  * @ingroup Examples
28  * @author Sylvain Hallé
29  *
30  */
31 public class Prime
32 {
33  public static void main(String[] args)
34  {
35  Assert<Integer> a = new Assert<Integer>(new RandomInteger(0, Integer.MAX_VALUE), new RandomFloat()) {
36  protected boolean evaluate(Integer x) {
37  return !isPrime(x);
38  }
39  };
40  if (!a.check())
41  {
42  System.out.println("Assertion is false");
43  System.out.println(a.getInitial() + " is prime");
44  System.out.println(a.getIterations().size() + " shrinking steps");
45  System.out.println(a.getShrunk() + " is also prime");
46  }
47  }
48 
49  /**
50  * Checks if an integer is prime.
51  * @param n The integer
52  * @return {@code true} if the integer is prime, {@code false} otherwise
53  */
54  public static boolean isPrime(int n)
55  {
56  if (n < 2)
57  {
58  return false;
59  }
60  for (int x = 2; x < n / x; x++)
61  {
62  if (n % x == 0)
63  {
64  return false;
65  }
66  }
67  return true;
68  }
69 }
ca.uqac.lif.synthia.test.Assert.check
boolean check()
Definition: Assert.java:87
examples.quickcheck.Prime.isPrime
static boolean isPrime(int n)
Checks if an integer is prime.
Definition: Prime.java:54
ca.uqac
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.test.Assert.getIterations
List< T > getIterations()
Definition: Assert.java:63
ca.uqac.lif.synthia.test.Assert.getInitial
T getInitial()
Definition: Assert.java:68
ca.uqac.lif
ca.uqac.lif.synthia.test.Assert.getShrunk
T getShrunk()
Definition: Assert.java:77
ca.uqac.lif.synthia.test.Assert
Definition: Assert.java:36
ca.uqac.lif.synthia.random.RandomInteger
Picks an integer uniformly in an interval.
Definition: RandomInteger.java:31
ca
examples.quickcheck.Prime.main
static void main(String[] args)
Definition: Prime.java:33
ca.uqac.lif.synthia.test
Classes that enable Synthia to operate as a fuzz testing tool.
Definition: Action.java:19
examples.quickcheck.Prime
Simple illustration of the shrinking process on randomly generated integers.
Definition: Prime.java:31
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30