1 import gov.nasa.jpf.jvm.Verify; 20 21 22 25 interface FundConstants { 26 static final int noOfStocks = 3; 27 static final int noOfManagers = 2; static final int testDuration = 20; 29 static final int randomTransfers = 2; static final int initialBalance = 1000; static final int totalMoneyInUniverse = noOfStocks * initialBalance; 32 } 33 34 37 class FundManager implements Runnable , FundConstants { 38 boolean trading = false; 39 40 public boolean isTrading () { 41 return trading; 42 } 43 44 public void run () { 45 int next = 0; 46 47 while (true) { 48 trading = true; 49 Stocks.transfer(TestFundManagers.transfers[next++]); 50 trading = false; 51 52 if (next == randomTransfers) { 53 next = 0; 54 } 55 } 56 } 57 } 58 59 62 class Stocks implements FundConstants { 63 static int[] balances = new int[noOfStocks]; 64 65 static void checkSystem () { 66 int actual = 0; 67 68 for (int n = 0; n < noOfStocks; n++) { 69 actual += balances[n]; 70 } 71 72 assert (actual == totalMoneyInUniverse); 73 } 74 75 static void init () { 76 for (int n = 0; n < noOfStocks; n++) { 77 balances[n] = initialBalance; 78 } 79 } 80 81 static void transfer (Transfer t) { 82 balances[t.fundFrom] -= t.amount; 83 balances[t.fundTo] += t.amount; 84 } 85 } 86 87 90 public class TestFundManagers implements FundConstants { 91 public static Transfer[] transfers = new Transfer[randomTransfers]; 92 93 static { 94 for (int n = 0; n < randomTransfers; n++) { 95 transfers[n] = new Transfer(); 96 } 97 } 98 99 public static void main (String [] args) { 100 Stocks.init(); 101 102 java.lang.Thread [] threads = new java.lang.Thread [noOfManagers]; 103 FundManager[] mgrs = new FundManager[noOfManagers]; 104 105 for (int n = 0; n < noOfManagers; n++) { 106 mgrs[n] = new FundManager(); 107 threads[n] = new java.lang.Thread (mgrs[n]); 108 threads[n].start(); 109 } 110 111 while (true) { 112 Verify.beginAtomic(); 113 114 boolean doCheck = true; 115 116 for (int n = 0; n < noOfManagers; n++) { 117 doCheck = doCheck && !mgrs[n].isTrading(); 118 } 119 120 if (doCheck) { 121 Stocks.checkSystem(); 122 } 123 124 Verify.endAtomic(); 125 } 126 } 127 } 128 129 132 class Transfer implements FundConstants { 133 public final int fundFrom; 134 public final int fundTo; 135 public final int amount; 136 137 public Transfer () { 138 fundFrom = Verify.random(noOfStocks - 1); 139 fundTo = Verify.random(noOfStocks - 1); 140 amount = 100; 141 } 142 } | Popular Tags |