Synthia
Generic and flexible data structure generator
Assert.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 ca.uqac.lif.synthia.test;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
26 import ca.uqac.lif.synthia.Picker;
29 
30 /**
31  * @ingroup Examples
32  * @author sylvain
33  *
34  * @param <T>
35  */
36 public class Assert<T>
37 {
38  protected static final int MAX_STARTS = 100;
39 
40  protected static final int MAX_CYCLES = 100;
41 
42  protected static final int MAX_TRIES = 10;
43 
45 
46  protected List<T> m_shrunk;
47 
49 
50  public Assert(Shrinkable<T> input, Picker<Float> decision)
51  {
52  super();
53  m_input = input;
54  m_shrunk = new ArrayList<T>();
55  m_decision = decision;
56  }
57 
58  public Assert(Shrinkable<T> input)
59  {
60  this(input, RandomFloat.instance);
61  }
62 
63  public List<T> getIterations()
64  {
65  return m_shrunk;
66  }
67 
68  public T getInitial()
69  {
70  if (m_shrunk.isEmpty())
71  {
72  return null;
73  }
74  return m_shrunk.get(0);
75  }
76 
77  public T getShrunk()
78  {
79  if (m_shrunk.isEmpty())
80  {
81  return null;
82  }
83  return m_shrunk.get(m_shrunk.size() - 1);
84  }
85 
86  @SuppressWarnings("unchecked")
87  public boolean check()
88  {
89  T best = null;
90  for (int start_cnt = 0; start_cnt < MAX_STARTS; start_cnt++)
91  {
92  List<T> shrunk = new ArrayList<T>();
93  T o = null;
94  boolean found = false;
95  try
96  {
97  for (int i = 0; i < MAX_TRIES; i++)
98  {
99  o = m_input.pick();
100  if (!evaluate(o))
101  {
102  found = true;
103  break;
104  }
105  }
106  shrunk.add(o);
107  }
108  catch (GiveUpException e)
109  {
110  continue;
111  }
112  catch (NoMoreElementException e)
113  {
114  continue;
115  }
116  if (!found)
117  {
118  continue;
119  }
120  Shrinkable<T> p = m_input.shrink(o, m_decision, 1);
121  try
122  {
123  for (int i = 0; i < MAX_CYCLES; i++)
124  {
125  boolean new_found = false;
126  for (float magnitude = 0.25f; !new_found && magnitude <= 1; magnitude += 0.25f)
127  {
128  p = p.shrink(o, m_decision, magnitude);
129  int j;
130  for (j = 0; j < MAX_TRIES; j++)
131  {
132  o = p.pick();
133  if (!evaluate(o))
134  {
135  shrunk.add(o);
136  new_found = true;
137  break;
138  }
139  }
140  }
141  }
142  }
143  catch (NoMoreElementException e)
144  {
145  // Nothing to do
146  }
147  if (!(o instanceof Comparable))
148  {
149  // No point in trying to find "best" input
150  m_shrunk = shrunk;
151  break;
152  }
153  if (!shrunk.isEmpty())
154  {
155  Comparable<T> new_o = (Comparable<T>) shrunk.get(shrunk.size() - 1);
156  if (best == null || new_o.compareTo(best) < 0)
157  {
158  // Found a "smaller" input
159  m_shrunk = shrunk;
160  best = shrunk.get(shrunk.size() - 1);
161  }
162  }
163  }
164  return m_shrunk.isEmpty();
165  }
166 
167  protected boolean evaluate(T o)
168  {
169  return true;
170  }
171 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.test.Assert.m_input
Shrinkable< T > m_input
Definition: Assert.java:44
ca.uqac.lif.synthia.test.Assert.m_shrunk
List< T > m_shrunk
Definition: Assert.java:46
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.test.Assert.check
boolean check()
Definition: Assert.java:87
ca.uqac.lif.synthia.Shrinkable.shrink
Shrinkable< T > shrink(T o, Picker< Float > d, float m)
Shrinks a picker.
ca.uqac.lif.synthia.random.RandomFloat.instance
static final transient RandomFloat instance
A public static instance of RandomFloat.
Definition: RandomFloat.java:45
ca.uqac.lif.synthia.test.Assert.MAX_CYCLES
static final int MAX_CYCLES
Definition: Assert.java:40
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.evaluate
boolean evaluate(T o)
Definition: Assert.java:167
ca.uqac.lif.synthia.test.Assert.MAX_TRIES
static final int MAX_TRIES
Definition: Assert.java:42
ca.uqac.lif.synthia.test.Assert.getInitial
T getInitial()
Definition: Assert.java:68
ca.uqac.lif.synthia.test.Assert.Assert
Assert(Shrinkable< T > input, Picker< Float > decision)
Definition: Assert.java:50
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
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.NoMoreElementException
An exception to throw when a picker can't pick an other element.
Definition: NoMoreElementException.java:25
ca.uqac.lif.synthia.test.Assert.m_decision
Picker< Float > m_decision
Definition: Assert.java:48
ca.uqac.lif.synthia.GiveUpException
An exception to throw to prevent the pick method of a PickIf picker from falling into an infinite loo...
Definition: GiveUpException.java:28
ca.uqac.lif.synthia.test.Assert.MAX_STARTS
static final int MAX_STARTS
Definition: Assert.java:38
ca.uqac.lif.synthia.test.Assert.Assert
Assert(Shrinkable< T > input)
Definition: Assert.java:58