From 84f0f216b4643601e4b8760d190b087bbce98bd4 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Sat, 6 Jun 2015 16:40:04 +1000 Subject: Lots of work on the stateful checking stuff: it's a fair bit nicer now --- .../zancanaro/javacheck/state/queue/MyQueue.java | 38 ++++++++++++++ .../javacheck/state/queue/NewQueueCommand.java | 30 ----------- .../javacheck/state/queue/PopQueueCommand.java | 58 --------------------- .../javacheck/state/queue/PushQueueCommand.java | 60 ---------------------- .../javacheck/state/queue/QueueState.java | 8 ++- .../zancanaro/javacheck/state/queue/QueueTest.java | 22 ++++---- .../javacheck/state/queue/commands/Clear.java | 29 +++++++++++ .../javacheck/state/queue/commands/Iterator.java | 31 +++++++++++ .../javacheck/state/queue/commands/New.java | 27 ++++++++++ .../javacheck/state/queue/commands/Offer.java | 41 +++++++++++++++ .../javacheck/state/queue/commands/Peek.java | 28 ++++++++++ .../javacheck/state/queue/commands/Poll.java | 37 +++++++++++++ .../javacheck/state/queue/commands/Size.java | 27 ++++++++++ 13 files changed, 277 insertions(+), 159 deletions(-) create mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/MyQueue.java delete mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/NewQueueCommand.java delete mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/PopQueueCommand.java delete mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/PushQueueCommand.java create mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Clear.java create mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Iterator.java create mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/commands/New.java create mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Offer.java create mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Peek.java create mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Poll.java create mode 100644 src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Size.java (limited to 'src/test/java/au/id/zancanaro/javacheck') diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/MyQueue.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/MyQueue.java new file mode 100644 index 0000000..a820532 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/MyQueue.java @@ -0,0 +1,38 @@ +package au.id.zancanaro.javacheck.state.queue; + +import java.util.AbstractQueue; +import java.util.Iterator; +import java.util.LinkedList; + +public class MyQueue extends AbstractQueue { + public final LinkedList list; + + public MyQueue() { + this.list = new LinkedList<>(); + } + + @Override + public Iterator iterator() { + return list.iterator(); + } + + @Override + public int size() { + return list.size(); + } + + @Override + public boolean offer(T t) { + return list.offer(t); + } + + @Override + public T poll() { + return list.poll(); + } + + @Override + public T peek() { + return list.peek(); + } +} diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/NewQueueCommand.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/NewQueueCommand.java deleted file mode 100644 index d2815dc..0000000 --- a/src/test/java/au/id/zancanaro/javacheck/state/queue/NewQueueCommand.java +++ /dev/null @@ -1,30 +0,0 @@ -package au.id.zancanaro.javacheck.state.queue; - -import au.id.zancanaro.javacheck.state.Command; -import au.id.zancanaro.javacheck.state.CommandValue; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.Queue; - -public class NewQueueCommand extends Command, Void, Queue> { - @Override - public boolean preCondition(QueueState state, Void args) { - return state == null; - } - - @Override - public Queue runCommand(Void args) { - return new LinkedList<>(); - } - - @Override - public QueueState nextState(QueueState state, Void args, CommandValue> result) { - return new QueueState<>(result, new ArrayList<>()); - } - - @Override - public String toString() { - return "new"; - } -} diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/PopQueueCommand.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/PopQueueCommand.java deleted file mode 100644 index f8a018c..0000000 --- a/src/test/java/au/id/zancanaro/javacheck/state/queue/PopQueueCommand.java +++ /dev/null @@ -1,58 +0,0 @@ -package au.id.zancanaro.javacheck.state.queue; - -import au.id.zancanaro.javacheck.Generator; -import au.id.zancanaro.javacheck.state.Command; -import au.id.zancanaro.javacheck.state.CommandValue; - -import java.util.ArrayList; -import java.util.List; -import java.util.Queue; - -import static au.id.zancanaro.javacheck.Generator.pure; - -public class PopQueueCommand extends Command, PopQueueCommand.ArgsType, T> { - @Override - public Generator> argsGenerator(QueueState tQueueState) { - return pure(tQueueState.getConcreteQueue()).map(x -> new ArgsType<>(x)); - } - - @Override - public boolean preCondition(QueueState state, ArgsType args) { - return state != null && !state.getAbstractQueue().isEmpty(); - } - - @Override - public T runCommand(ArgsType args) { - return args.queue.get().poll(); - } - - @Override - public QueueState nextState(QueueState state, ArgsType args, CommandValue result) { - List newState = new ArrayList<>(state.getAbstractQueue()); - newState.remove(0); - return new QueueState<>(state.getConcreteQueue(), newState); - } - - @Override - public boolean postCondition(QueueState oldState, QueueState newState, ArgsType args, T result) { - return oldState.getAbstractQueue().get(0) == result; - } - - @Override - public String toString() { - return "pop"; - } - - static class ArgsType { - public final CommandValue> queue; - - public ArgsType(CommandValue> queue) { - this.queue = queue; - } - - @Override - public String toString() { - return "[" + queue + "]"; - } - } -} diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/PushQueueCommand.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/PushQueueCommand.java deleted file mode 100644 index b198eb5..0000000 --- a/src/test/java/au/id/zancanaro/javacheck/state/queue/PushQueueCommand.java +++ /dev/null @@ -1,60 +0,0 @@ -package au.id.zancanaro.javacheck.state.queue; - -import au.id.zancanaro.javacheck.Generator; -import au.id.zancanaro.javacheck.state.Command; -import au.id.zancanaro.javacheck.state.CommandValue; - -import java.util.ArrayList; -import java.util.List; -import java.util.Queue; - -public class PushQueueCommand extends Command, PushQueueCommand.ArgsType, Void> { - public final Generator generator; - - public PushQueueCommand(Generator argGen) { - this.generator = argGen; - } - - @Override - public Generator> argsGenerator(QueueState state) { - return generator.map(i -> new ArgsType<>(state.getConcreteQueue(), i)); - } - - @Override - public Void runCommand(ArgsType args) { - args.queue.get().offer(args.pushValue); - return null; - } - - @Override - public QueueState nextState(QueueState state, ArgsType args, CommandValue result) { - List newState = new ArrayList<>(state.getAbstractQueue()); - newState.add(args.pushValue); - return new QueueState<>(state.getConcreteQueue(), newState); - } - - @Override - public boolean preCondition(QueueState state, ArgsType args) { - return state != null; - } - - @Override - public String toString() { - return "push"; - } - - static class ArgsType { - public final CommandValue> queue; - public final T pushValue; - - public ArgsType(CommandValue> queue, T pushValue) { - this.queue = queue; - this.pushValue = pushValue; - } - - @Override - public String toString() { - return "[" + queue + ", " + pushValue + "]"; - } - } -} diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/QueueState.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/QueueState.java index e4f65f8..6cd6cf8 100644 --- a/src/test/java/au/id/zancanaro/javacheck/state/queue/QueueState.java +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/QueueState.java @@ -14,8 +14,12 @@ public class QueueState { this.abstractQueue = abstractQueue; } - public CommandValue> getConcreteQueue() { - return concreteQueue; + public QueueState withAbstractQueue(List abstractQueue) { + return new QueueState<>(this.concreteQueue, abstractQueue); + } + + public Queue getConcreteQueue() { + return concreteQueue.get(); } public List getAbstractQueue() { diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/QueueTest.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/QueueTest.java index 083a8ad..9672fca 100644 --- a/src/test/java/au/id/zancanaro/javacheck/state/queue/QueueTest.java +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/QueueTest.java @@ -3,29 +3,33 @@ package au.id.zancanaro.javacheck.state.queue; import au.id.zancanaro.javacheck.Generator; import au.id.zancanaro.javacheck.annotations.DataSource; import au.id.zancanaro.javacheck.annotations.Property; -import au.id.zancanaro.javacheck.annotations.Seed; import au.id.zancanaro.javacheck.junit.Properties; import au.id.zancanaro.javacheck.state.*; +import au.id.zancanaro.javacheck.state.queue.commands.*; import org.junit.runner.RunWith; -import java.util.*; +import java.util.LinkedList; import static au.id.zancanaro.javacheck.Generator.pure; +import static au.id.zancanaro.javacheck.Generators.elements; import static au.id.zancanaro.javacheck.Generators.integer; -import static au.id.zancanaro.javacheck.Generators.oneOf; import static org.junit.Assert.assertFalse; @RunWith(Properties.class) public class QueueTest { @DataSource public static Generator>> commandGenerator = - new CommandListGenerator<>(state -> + new CommandListGenerator<>(null, state -> state == null ? - pure(new NewQueueCommand<>()) : - oneOf( - pure(new PopQueueCommand<>()), - pure(new PushQueueCommand<>(integer())) - )); + pure(new New<>()) : + elements( + new New<>(), + new Poll<>(), + new Peek<>(), + new Offer<>(integer()), + new Size<>(), + new Clear<>(), + new Iterator<>())); @Property public void test(CommandList> commands) throws Throwable { diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Clear.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Clear.java new file mode 100644 index 0000000..2e88796 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Clear.java @@ -0,0 +1,29 @@ +package au.id.zancanaro.javacheck.state.queue.commands; + +import au.id.zancanaro.javacheck.Generator; +import au.id.zancanaro.javacheck.state.Command; +import au.id.zancanaro.javacheck.state.CommandValue; +import au.id.zancanaro.javacheck.state.queue.QueueState; + +import java.util.ArrayList; +import java.util.Queue; + +import static org.junit.Assert.assertEquals; + +public class Clear extends Command, Void, Void> { + @Override + public boolean preCondition(QueueState state, Void args) { + return state != null; + } + + @Override + public Void runCommand(QueueState state, Void args) { + state.getConcreteQueue().clear(); + return null; + } + + @Override + public QueueState nextState(QueueState state, Void args, CommandValue result) { + return state.withAbstractQueue(new ArrayList<>()); + } +} diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Iterator.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Iterator.java new file mode 100644 index 0000000..361abd6 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Iterator.java @@ -0,0 +1,31 @@ +package au.id.zancanaro.javacheck.state.queue.commands; + +import au.id.zancanaro.javacheck.state.Command; +import au.id.zancanaro.javacheck.state.queue.QueueState; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class Iterator extends Command, Void, java.util.Iterator> { + @Override + public boolean preCondition(QueueState state, Void args) { + return state != null; + } + + @Override + public java.util.Iterator runCommand(QueueState state, Void args) { + return state.getConcreteQueue().iterator(); + } + + @Override + public void postCondition(QueueState oldState, QueueState newState, Void args, java.util.Iterator result) throws Throwable { + java.util.Iterator abstractIterator = oldState.getAbstractQueue().iterator(); + while (abstractIterator.hasNext() && result.hasNext()) { + assertEquals(abstractIterator.next(), result.next()); + } + assertFalse("The abstract iterator is 'longer' than the concrete iterator", + abstractIterator.hasNext()); + assertFalse("The concrete iterator is 'longer' than the abstract iterator", + result.hasNext()); + } +} diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/New.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/New.java new file mode 100644 index 0000000..114e7ba --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/New.java @@ -0,0 +1,27 @@ +package au.id.zancanaro.javacheck.state.queue.commands; + +import au.id.zancanaro.javacheck.state.Command; +import au.id.zancanaro.javacheck.state.CommandValue; +import au.id.zancanaro.javacheck.state.queue.MyQueue; +import au.id.zancanaro.javacheck.state.queue.QueueState; +import org.junit.Assume; + +import java.util.ArrayList; +import java.util.Queue; + +public class New extends Command, Void, Queue> { + @Override + public boolean preCondition(QueueState state, Void args) { + return state == null; + } + + @Override + public Queue runCommand(QueueState state, Void args) { + return new MyQueue<>(); + } + + @Override + public QueueState nextState(QueueState state, Void args, CommandValue> result) { + return new QueueState<>(result, new ArrayList<>()); + } +} diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Offer.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Offer.java new file mode 100644 index 0000000..301efeb --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Offer.java @@ -0,0 +1,41 @@ +package au.id.zancanaro.javacheck.state.queue.commands; + +import au.id.zancanaro.javacheck.Generator; +import au.id.zancanaro.javacheck.state.Command; +import au.id.zancanaro.javacheck.state.CommandValue; +import au.id.zancanaro.javacheck.state.queue.QueueState; + +import java.util.ArrayList; +import java.util.List; +import java.util.Queue; + +public class Offer extends Command, T, Void> { + public final Generator generator; + + public Offer(Generator argGen) { + this.generator = argGen; + } + + @Override + public Generator argsGenerator(QueueState state) { + return generator; + } + + @Override + public boolean preCondition(QueueState state, T args) { + return state != null; + } + + @Override + public Void runCommand(QueueState state, T args) { + state.getConcreteQueue().offer(args); + return null; + } + + @Override + public QueueState nextState(QueueState state, T args, CommandValue result) { + List newState = new ArrayList<>(state.getAbstractQueue()); + newState.add(args); + return state.withAbstractQueue(newState); + } +} diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Peek.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Peek.java new file mode 100644 index 0000000..5bb71a9 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Peek.java @@ -0,0 +1,28 @@ +package au.id.zancanaro.javacheck.state.queue.commands; + +import au.id.zancanaro.javacheck.Generator; +import au.id.zancanaro.javacheck.state.Command; +import au.id.zancanaro.javacheck.state.CommandValue; +import au.id.zancanaro.javacheck.state.queue.QueueState; + +import java.util.Queue; + +import static au.id.zancanaro.javacheck.Generator.pure; +import static org.junit.Assert.assertEquals; + +public class Peek extends Command, Void, T> { + @Override + public boolean preCondition(QueueState state, Void args) { + return state != null && !state.getAbstractQueue().isEmpty(); + } + + @Override + public T runCommand(QueueState state, Void args) { + return state.getConcreteQueue().peek(); + } + + @Override + public void postCondition(QueueState oldState, QueueState newState, Void args, T result) { + assertEquals(oldState.getAbstractQueue().get(0), result); + } +} diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Poll.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Poll.java new file mode 100644 index 0000000..b5e0d98 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Poll.java @@ -0,0 +1,37 @@ +package au.id.zancanaro.javacheck.state.queue.commands; + +import au.id.zancanaro.javacheck.Generator; +import au.id.zancanaro.javacheck.state.Command; +import au.id.zancanaro.javacheck.state.CommandValue; +import au.id.zancanaro.javacheck.state.queue.QueueState; + +import java.util.ArrayList; +import java.util.List; +import java.util.Queue; + +import static au.id.zancanaro.javacheck.Generator.pure; +import static org.junit.Assert.assertEquals; + +public class Poll extends Command, Void, T> { + @Override + public boolean preCondition(QueueState state, Void args) { + return state != null && !state.getAbstractQueue().isEmpty(); + } + + @Override + public T runCommand(QueueState state, Void args) { + return state.getConcreteQueue().poll(); + } + + @Override + public QueueState nextState(QueueState state, Void args, CommandValue result) { + List newState = new ArrayList<>(state.getAbstractQueue()); + newState.remove(0); + return state.withAbstractQueue(newState); + } + + @Override + public void postCondition(QueueState oldState, QueueState newState, Void args, T result) { + assertEquals(oldState.getAbstractQueue().get(0), result); + } +} diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Size.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Size.java new file mode 100644 index 0000000..ba9d196 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Size.java @@ -0,0 +1,27 @@ +package au.id.zancanaro.javacheck.state.queue.commands; + +import au.id.zancanaro.javacheck.Generator; +import au.id.zancanaro.javacheck.state.Command; +import au.id.zancanaro.javacheck.state.CommandValue; +import au.id.zancanaro.javacheck.state.queue.QueueState; + +import java.util.Queue; + +import static org.junit.Assert.assertEquals; + +public class Size extends Command, Void, Integer> { + @Override + public boolean preCondition(QueueState state, Void args) { + return state != null; + } + + @Override + public Integer runCommand(QueueState state, Void args) { + return state.getConcreteQueue().size(); + } + + @Override + public void postCondition(QueueState oldState, QueueState newState, Void args, Integer result) throws Throwable { + assertEquals((Object) oldState.getAbstractQueue().size(), result); + } +} -- cgit v1.2.3