Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import org.openjdk.jcstress.annotations.*;
- import org.openjdk.jcstress.infra.results.I_Result;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- import java.util.stream.Stream;
- import static org.openjdk.jcstress.annotations.Expect.ACCEPTABLE;
- import static org.openjdk.jcstress.annotations.Expect.FORBIDDEN;
- public class OrderedTest {
- @JCStressTest()
- @Outcome(id = {"1"}, expect = ACCEPTABLE, desc = "OK")
- @Outcome(id = {"2"}, expect = FORBIDDEN, desc = "Repetition")
- @Outcome(id = {"3"}, expect = FORBIDDEN, desc = "Not fibonacci")
- @State
- public static class Test1 {
- private static final int SIZE = 4;
- private final BlockingQueue<BigInteger> queue = new ArrayBlockingQueue<>(SIZE);
- private final Fibonacci fibonacci = new Fibonacci();
- @Actor
- public void a1() {
- queue.add(fibonacci.next());
- }
- @Actor
- public void a2() {
- queue.add(fibonacci.next());
- }
- @Actor
- public void a3() {
- queue.add(fibonacci.next());
- }
- @Actor
- public void a4() {
- queue.add(fibonacci.next());
- }
- @Arbiter
- public void check(I_Result state) {
- List<BigInteger> vals = new ArrayList<>(Stream.of( 1, 2, 3, 5, 8, 13, 21, 34, 55)
- .map(BigInteger::valueOf)
- .toList());
- List<BigInteger> removed = new ArrayList<>(vals);
- try {
- for (int i = 0; i < SIZE; ++i) {
- var val = queue.take();
- if (!vals.contains(val)) {
- state.r1 = 3;
- return;
- }
- if (!removed.remove(val)) {
- state.r1 = 2;
- return;
- }
- }
- state.r1 = 1;
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement