Source of TestBag.java


  1: import java.util.Arrays;
  2: import java.util.NoSuchElementException;

  4: public class TestBag {

  6:     public static void main(String[] args) {
  7:         // test constructor
  8:         Bag<Integer> numbers = new ArrayBag<>(10);

 10:         // test size/isEmpty/getFrequency/contains
 11:         testOthers(numbers);

 13:         // test add
 14:         for (int i = 0; i < 12; ++i) {
 15:             if (numbers.add(i)) {
 16:                 System.out.println("Added " + i);
 17:             } else {
 18:                 System.out.println("Failed to add " + i);
 19:             }
 20:         }

 22:         // test toString
 23:         System.out.println(numbers);

 25:         // test toArray(...)
 26:         testArray(numbers);

 28:         // test size/isEmpty/getFrequency/contains
 29:         numbers.add(6);
 30:         testOthers(numbers);

 32:         // test remove(T)
 33:         for (int i = 0; i < 20; i += 3) {
 34:             if (numbers.remove(i)) {
 35:                 System.out.println("Removed " + i);
 36:             } else {
 37:                 System.out.println("Failed to remove " + i);
 38:             }
 39:         }
 40:         System.out.println(numbers);

 42:         // test size/isEmpty/getFrequency/contains
 43:         testOthers(numbers);

 45:         // test remove()
 46:         try {
 47:             while (true) {
 48:                 System.out.println("Removing " + numbers.remove());
 49:             }
 50:         } catch (NoSuchElementException nsee) {
 51:             System.out.println("No more to remove!");
 52:         }
 53:         System.out.println(numbers);

 55:         // test size/isEmpty/getFrequency/contains
 56:         testOthers(numbers);
 57:     }

 59:     private static void testArray(Bag<Integer> bag) {
 60:         // toArray()
 61:         Object[] numbers = bag.toArray();
 62:         System.out.println(bag + ".toArray() == " + Arrays.toString(numbers));

 64:         // toArray(T[]) -- big array
 65:         Integer[] big = new Integer[20];
 66:         Arrays.fill(big, -1);
 67:         System.out.println("Array 'big' before: " + Arrays.toString(big));
 68:         Integer[] returnedBig = bag.toArray(big);
 69:         System.out.println(bag + ".toArray(big) == " 
 70:                 + Arrays.toString(returnedBig));
 71:         System.out.println("Array 'big' after: " + Arrays.toString(big));

 73:         // toArray(T[]) -- small array
 74:         Integer[] small = new Integer[2];
 75:         Arrays.fill(small, -1);
 76:         System.out.println("Array 'small' before: " + Arrays.toString(small));
 77:         Integer[] returnedSmall = bag.toArray(small);
 78:         System.out.println(bag + ".toArray(small) == " 
 79:                 + Arrays.toString(returnedSmall));
 80:         System.out.println("Array 'small' after: " + Arrays.toString(small));
 81:     }

 83:     private static void testOthers(Bag<Integer> bag) {
 84:         System.out.println(bag + ".size() == " + bag.size());
 85:         System.out.println(bag + ".isEmpty() == " + bag.isEmpty());
 86:         System.out.println(bag + ".contains(6) == " + bag.contains(6));
 87:         System.out.println(bag + ".contains(7) == " + bag.contains(7));
 88:         System.out.println(bag + ".getFrequency(6) == " + bag.getFrequency(6));
 89:         System.out.println(bag + ".getFrequency(7) == " + bag.getFrequency(7));
 90:     }

 92: }