示例#1
0
 private static void main(String[] args) {
   RandomizedQueue<String> rq = new RandomizedQueue<String>();
   rq.enqueue("hello");
   rq.enqueue(", ");
   rq.enqueue("world");
   rq.enqueue("!");
   System.out.println(rq);
   System.out.printf("size: %d\n", rq.size());
   System.out.printf("sample %s\n", rq.sample());
   System.out.printf("sample %s\n", rq.sample());
   System.out.printf("dequeue %s\n", rq.dequeue());
   System.out.printf("dequeue %s\n", rq.dequeue());
   System.out.println(rq);
   System.out.printf("size: %d\n", rq.size());
 }
示例#2
0
  /**
   * Unit testing
   *
   * @param args the program arguments
   */
  public static void main(String[] args) {
    RandomizedQueue<String> queue = new RandomizedQueue<>();

    queue.enqueue("rieck");
    queue.enqueue("andrew");
    queue.enqueue("thomas");

    assert (queue.size() == 3);
    assert (!queue.isEmpty());

    for (String t : queue) {
      StdOut.println(t);
    }

    queue.sample();

    queue.dequeue();
    queue.dequeue();
    queue.dequeue();

    assert (queue.isEmpty());

    queue.enqueue("lily");
    queue.enqueue("caleb");
    queue.enqueue("aaron");

    assert (queue.size() == 3);
    assert (!queue.isEmpty());

    int i = 0;
    for (String t : queue) {
      StdOut.println(t);
      for (String u : queue) {
        StdOut.println("   " + u);
        i++;
      }
    }

    assert (i == 9);
  }
  public static void main(String[] args) {
    // Test case: NullPointer when adding null to RandomizedQueue.
    RandomizedQueue<String> rq = new RandomizedQueue<String>();
    try {
      String str = null;
      rq.enqueue(str);
      StdOut.println("Test should throw java.lang.NullPointerException: " + "FAIL!");
    } catch (java.lang.NullPointerException e) {
      StdOut.println("Test should throw java.lang.NullPointerException: " + "Successfull!");
    }

    // Test case: Throw java.util.NoSuchElementException when randomizedQueue
    //            is empty and someone tries to remove an item.
    rq = new RandomizedQueue<String>();
    try {
      rq.dequeue();
      StdOut.println("Test should throw " + "java.util.NoSuchElementException: FAIL!");
    } catch (java.util.NoSuchElementException e) {
      StdOut.println("Test should throw " + "java.util.NoSuchElementException: Successfull!");
    }

    // Test case: Add some stuff and show samples.
    rq = new RandomizedQueue<String>();
    // Make  0,1,2,3,4,5,6,7
    rq.enqueue("7");
    rq.enqueue("6");
    rq.enqueue("5");
    rq.enqueue("4");
    rq.enqueue("3");
    rq.enqueue("2");
    rq.enqueue("1");
    rq.enqueue("0");

    // Sample should not remove entries.
    for (int i = 0; i < 10; i++) {
      StdOut.println(rq.sample());
    }
    StdOut.println("Next case.");
    // dequeue should remove entries in random order.
    for (int i = 0; i < 8; i++) {
      String item = rq.dequeue();
      StdOut.println(item);
    }

    try {
      rq.dequeue();
      StdOut.println("Test should throw " + "java.util.NoSuchElementException: FAIL!");
    } catch (java.util.NoSuchElementException e) {
      StdOut.println("Test should throw java.util." + "NoSuchElementException: Successfull!");
    }

    for (int i = 0; i < 2; i++) {
      // Make  0,1,2,3,4,5,6,7
      rq.enqueue("7");
      rq.enqueue("6");
      rq.enqueue("5");
      rq.enqueue("4");
      rq.enqueue("3");
      rq.enqueue("2");
      rq.enqueue("1");
      rq.enqueue("0");

      for (String s : rq) StdOut.println(s);
      StdOut.println();
    }
  }