Advertisement
Guest User

Untitled

a guest
Oct 5th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package test;
  2.  
  3. import org.openjdk.jcstress.annotations.*;
  4. import org.openjdk.jcstress.infra.results.I_Result;
  5.  
  6. import java.math.BigInteger;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.concurrent.ArrayBlockingQueue;
  10. import java.util.concurrent.BlockingQueue;
  11. import java.util.stream.Stream;
  12.  
  13. import static org.openjdk.jcstress.annotations.Expect.ACCEPTABLE;
  14. import static org.openjdk.jcstress.annotations.Expect.FORBIDDEN;
  15.  
  16.  
  17. public class OrderedTest {
  18.     @JCStressTest()
  19.     @Outcome(id = {"1"}, expect = ACCEPTABLE, desc = "OK")
  20.     @Outcome(id = {"2"}, expect = FORBIDDEN, desc = "Repetition")
  21.     @Outcome(id = {"3"}, expect = FORBIDDEN, desc = "Not fibonacci")
  22.     @State
  23.     public static class Test1 {
  24.         private static final int SIZE = 4;
  25.         private final BlockingQueue<BigInteger> queue = new ArrayBlockingQueue<>(SIZE);
  26.         private final Fibonacci fibonacci = new Fibonacci();
  27.  
  28.         @Actor
  29.         public void a1() {
  30.             queue.add(fibonacci.next());
  31.         }
  32.  
  33.         @Actor
  34.         public void a2() {
  35.             queue.add(fibonacci.next());
  36.         }
  37.  
  38.         @Actor
  39.         public void a3() {
  40.             queue.add(fibonacci.next());
  41.         }
  42.  
  43.         @Actor
  44.         public void a4() {
  45.             queue.add(fibonacci.next());
  46.         }
  47.  
  48.         @Arbiter
  49.         public void check(I_Result state) {
  50.             List<BigInteger> vals = new ArrayList<>(Stream.of( 1, 2, 3, 5, 8, 13, 21, 34, 55)
  51.                     .map(BigInteger::valueOf)
  52.                     .toList());
  53.             List<BigInteger> removed = new ArrayList<>(vals);
  54.             try {
  55.                 for (int i = 0; i < SIZE; ++i) {
  56.                     var val = queue.take();
  57.                     if (!vals.contains(val)) {
  58.                         state.r1 = 3;
  59.                         return;
  60.                     }
  61.                     if (!removed.remove(val)) {
  62.                         state.r1 = 2;
  63.                         return;
  64.                     }
  65.                 }
  66.                 state.r1 = 1;
  67.             } catch (InterruptedException e) {
  68.                 Thread.currentThread().interrupt();
  69.             }
  70.         }
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement