Beispiel #1
0
  /**
   * Runs NUM adder threads, each adder adds 1 (unique) seqno. When all adders are done, we should
   * have NUM elements in the table.
   */
  public void testConcurrentAdd() {
    final int NUM = 100;
    final Table<Integer> buf = new Table<Integer>(3, 10, 0);

    CountDownLatch latch = new CountDownLatch(1);
    Adder[] adders = new Adder[NUM];
    for (int i = 0; i < adders.length; i++) {
      adders[i] = new Adder(latch, i + 1, buf);
      adders[i].start();
    }

    System.out.println("starting threads");
    latch.countDown();
    System.out.print("waiting for threads to be done: ");
    for (Adder adder : adders) {
      try {
        adder.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    System.out.println("OK");
    System.out.println("buf = " + buf);
    assert buf.size() == NUM;
  }
  public static void main(String[] args) {
    // Create a new Adder object
    Adder a = new SolutionInheritance2().new Adder();

    // Print the name of the superclass on a new line
    System.out.println("My superclass is: " + a.getClass().getSuperclass().getName());

    // Print the result of 3 calls to Adder's `add(int,int)` method as 3 space-separated integers:
    System.out.print(a.add(10, 32) + " " + a.add(10, 3) + " " + a.add(10, 10) + "\n");
  }
Beispiel #3
0
  public void runLoop() throws IOException {

    while (true) {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      System.out.println("Welcome to supercalc");
      System.out.println("enter command:");
      String line = br.readLine();

      if (line.equals("e")) {
        System.out.println("good bye, have a nice day...");
        break;
      } else {
        String[] args = line.split(" ");
        if (args.length == 3) {
          String opCode = args[0];
          Integer op1 = Integer.parseInt(args[1]);
          Integer op2 = Integer.parseInt(args[2]);

          if (opCode.equals("+")) {
            System.out.println("result: " + adder.add(op1, op2));
          } else if (opCode.equals("-")) {
            System.out.println("result: " + sub.subtract(op1, op2));
          }
        }
      }
    }
  }
Beispiel #4
0
  /**
   * The main method.
   *
   * @param args the arguments
   */
  public static void main(String[] args) {
    String direc = args[0];
    String fileName = args[1];
    try {
      Repository r = new FileRepository(direc);
      Git g = new Git(r);

      Adder adr = new Adder();
      adr.addFile(g, fileName);

      g.close();
    } catch (IOException e) {
      e.printStackTrace();
      return;
    }
  }
Beispiel #5
0
  /**
   * Creates a table and fills it to capacity. Then starts a number of adder threads, each trying to
   * add a seqno, blocking until there is more space. Each adder will block until the remover
   * removes elements, so the adder threads get unblocked and can then add their elements to the
   * buffer.
   */
  public void testConcurrentAddAndRemove() throws Exception {
    final int NUM = 5;
    final Table<Integer> buf = new Table<Integer>(3, 10, 0);
    for (int i = 1; i <= 10; i++) buf.add(i, i); // fill the buffer, add() will block now

    CountDownLatch latch = new CountDownLatch(1);
    Adder[] adders = new Adder[NUM];
    for (int i = 0; i < adders.length; i++) {
      adders[i] = new Adder(latch, i + 11, buf);
      adders[i].start();
    }

    System.out.println("releasing threads");
    latch.countDown();
    System.out.print("waiting for threads to be done: ");

    Thread remover =
        new Thread("Remover") {
          public void run() {
            Util.sleep(2000);
            List<Integer> list = buf.removeMany(true, 5);
            System.out.println("\nremover: removed = " + list);
          }
        };
    remover.start();

    for (Adder adder : adders) {
      try {
        adder.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    remover.join();

    System.out.println("OK");
    System.out.println("buf = " + buf);
    assert buf.size() == 10;
    assertIndices(buf, 5, 5, 15);

    List<Integer> list = buf.removeMany(true, 0);
    System.out.println("removed = " + list);
    assert list.size() == 10;
    for (int i = 6; i <= 15; i++) assert list.contains(i);
    assertIndices(buf, 15, 15, 15);
  }
  public static void main(String[] args) {

    Adder adder1 =
        (x, y) -> {
          return x + y;
        };

    int x1 = adder1.add(40, 2);

    Adder adder2 =
        new Adder() {
          @Override
          public int add(int x, int y) {
            return x + y;
          }
        };

    int x2 = adder2.add(39, 3);

    if (x1 == x2) {
      VMTest.markResult(false);
    }
  }
  ///////////////////////////////////////////
  //   Exercise: class that adds Euro and CHF
  //
  //   Wechselkurs
  //   W�hrung
  //   Zielw�hrung ber�cksichtigen!
  //   done Summands are changeable
  //   Wie kommt der Wechselkurs in die Methode?
  //
  //
  @Test
  public void addCurrency() {

    Adder adder = new Adder();
    ExchangeRateService service = new ExchangeRateService();

    assertEquals(5 + 10 * service.getRate(chf, euro), adder.add(5, euro, 10, chf, euro), 0.00001);
    assertEquals(7 + 10 * service.getRate(chf, euro), adder.add(7, euro, 10, chf, euro), 0.00001);
    assertEquals(7 + 10 * service.getRate(chf, euro), adder.add(10, chf, 7, euro, euro), 0.00001);
    assertEquals(
        (7 + 10 * service.getRate(chf, euro)) * service.getRate(euro, chf),
        adder.add(10, chf, 7, euro, chf),
        0.00001);
    assertEquals(5 + 10, adder.add(5, euro, 10, euro, euro), 0.00001);
    assertEquals(5 + 10, adder.add(5, chf, 10, chf, chf), 0.00001);
    assertEquals(
        5 + 10 * service.getRate(dollar, euro), adder.add(5, euro, 10, dollar, euro), 0.00001);
  }
  /** @param args */
  public static void main(String[] args) {
    Counter counter = new Counter();
    Adder adder1 = new Adder("Adder 1", counter, 1000, 1, 1);
    Adder adder2 = new Adder("Adder 2", counter, 1000, 1, 2);
    Adder adder3 = new Adder("Adder 3", counter, 1000, 1, 3);
    Adder adder4 = new Adder("Adder 4", counter, 1000, 1, 4);
    Adder adder5 = new Adder("Adder 5", counter, 1000, 1, 5);
    Adder adder6 = new Adder("Adder 1", counter, 1000, 1, 6);
    Adder adder7 = new Adder("Adder 2", counter, 1000, 1, 7);
    Adder adder8 = new Adder("Adder 3", counter, 1000, 1, 8);
    Adder adder9 = new Adder("Adder 4", counter, 1000, 1, 9);
    Adder adder10 = new Adder("Adder 5", counter, 1000, 1, 10);

    // use executor
    Executor executor = Executors.newCachedThreadPool();
    executor.execute(adder1);
    executor.execute(adder2);
    executor.execute(adder3);
    executor.execute(adder4);
    executor.execute(adder5);
    executor.execute(adder6);
    executor.execute(adder7);
    executor.execute(adder8);
    executor.execute(adder9);
    executor.execute(adder10);

    while (!adder1.isFinished()
        || !adder2.isFinished()
        || !adder3.isFinished()
        || !adder4.isFinished()
        || !adder5.isFinished()
        || !adder6.isFinished()
        || !adder7.isFinished()
        || !adder8.isFinished()
        || !adder9.isFinished()
        || !adder10.isFinished()) {}
    System.out.println("Adder 1 status: " + adder1.isFinished());
    System.out.println("Adder 2 status: " + adder2.isFinished());
    System.out.println("Adder 3 status: " + adder3.isFinished());
    System.out.println("Adder 4 status: " + adder4.isFinished());
    System.out.println("Adder 5 status: " + adder5.isFinished());
    System.out.println("Adder 6 status: " + adder6.isFinished());
    System.out.println("Adder 7 status: " + adder7.isFinished());
    System.out.println("Adder 8 status: " + adder8.isFinished());
    System.out.println("Adder 9 status: " + adder9.isFinished());
    System.out.println("Adder 10 status: " + adder10.isFinished());

    System.out.println("Adders are Finished and count is " + counter.getCount() + "!");
    System.exit(0);
  }
Beispiel #9
0
 public static void main(String[] argh) {
   Adder X = new Adder();
   System.out.println("My superclass is: " + X.getClass().getSuperclass().getName());
   System.out.print(X.add(10, 32) + " " + X.add(10, 3) + " " + X.add(10, 10) + "\n");
 }
 @当("^加上一个正数(\\d+)$")
 public void 加上一个正数(int arg1) throws Throwable {
   adder.add(arg1);
 }
 @假设("^有一个正数(\\d+)$")
 public void 有一个正数(int arg1) throws Throwable {
   adder = new Adder();
   adder.a = arg1;
 }
 public void testAdd() {
   Adder adder = new Adder();
   int re = adder.add(100, 200);
   assertEquals(300, re);
 }
Beispiel #13
0
  public Any exec(Any a) throws AnyException {
    Transaction t = getTransaction();

    Composite sumRoot = (Composite) EvalExpr.evalFunc(t, a, sumRoot_, Composite.class);

    if (sumRoot == null) nullOperand(sumRoot_);

    Any expression = null;
    Any sum = null;
    Any tmp = null;
    Adder adder = null;

    if (takeAverage_ && sumRoot.entries() == 0) return null;

    Iter i = sumRoot.createIterator();

    Any loop = t.getLoop();

    try {
      while (i.hasNext()) {
        Any child = i.next();

        // Set $loop
        t.setLoop(child);

        // Lazy clone of expression
        if (expression == null) {
          expression = expression_.cloneAny();
        }

        if (sum == null) {
          sum = EvalExpr.evalFunc(t, a, expression);

          if (sum == null)
            throw new NullPointerException("Failed to resolve " + expression_ + "during summation");

          // To save on the creation of temporaries, the first sum
          // item is used as the prototype for the result.  The
          // remaining items, if not the same, must be convertible to it.
          adder = new Adder(sum);
          sum = adder.add(sum);
          tmp = sum.buildNew(null);
          adder.setTmp(tmp);
        } else {
          Any next = EvalExpr.evalFunc(t, a, expression);
          if (next == null)
            throw new NullPointerException("Failed to resolve " + expression_ + "during summation");

          adder.add(next);
        }

        // No point in continuing the iteration if we hit a null
        if (adder.isNull()) break;
      }

      if (adder != null && adder.isNull()) return AnyNull.instance();

      if (takeAverage_ && sum != null) return adder.doAvg(sum, sumRoot.entries());

      return sum == null ? AnyNull.instance() : sum;
    } finally {
      t.setLoop(loop);
    }
  }