Example #1
0
 private String privilegesAsString(Set<Privilege> privileges) {
   SortedSet<Privilege> sortedPrivileges =
       new TreeSet<Privilege>(
           new Comparator<Privilege>() {
             @Override
             public int compare(Privilege p1, Privilege p2) {
               return p1.ordinal() - p2.ordinal();
             }
           });
   sortedPrivileges.addAll(privileges);
   return sortedPrivileges.toString().replaceAll(" ", "");
 }
Example #2
0
  public static void main(String[] args) {

    UnsortedSet<Integer> s1 = new UnsortedSet<Integer>();
    for (int i = 1; i <= 10; i++) {
      s1.add(i);
    }

    UnsortedSet<Integer> s2 = new UnsortedSet<Integer>();
    for (int i = 5; i < 15; i++) {
      s2.add(i);
    }

    // Test 1: size method
    System.out.print("Test 1, UnsortedSet  size method: ");
    if (s1.size() == 10) System.out.println("passed");
    else System.out.println("failed");

    // Test 2: add method
    System.out.print("Test 2, UnsortedSet  add method: ");
    s1.add(11);
    if (s1.contains(11)) {
      System.out.println("passed");
    } else {
      System.out.println("failed");
    }

    // Test 3: toString method
    System.out.print("Test 3, UnsortedSet toString method: ");
    if (s1.toString().equals("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)")) {
      System.out.println("passed");
    } else {
      System.out.println("failed");
    }

    // Test 4: add all method
    System.out.print("Test 4, UnsortedSet  add all method: ");
    s1.addAll(s2);
    if (s1.toString().equals("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)")) {
      System.out.println("passed");
    } else {
      System.out.println("failed");
    }

    // Test 5: remove method
    System.out.print("Test 5, UnsortedSet remove method: ");
    for (int i = 11; i < 15; i++) {
      s1.remove(i);
    }
    if (s1.toString().equals("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)")) {
      System.out.println("passed");
    } else {
      System.out.println("failed");
    }

    // Test 6: clear method
    System.out.print("Test 6, UnsortedSet clear method: ");
    s1.clear();
    if (s1.size() == 0) {
      System.out.println("passed");
    } else {
      System.out.println("failed");
    }

    for (int i = 1; i <= 10; i++) {
      s1.add(i);
    }

    // Test 7: contains method
    System.out.print("Test 7, UnsortedSet contains method: ");
    if (!s1.contains(999)) {
      System.out.println("passed");
    } else {
      System.out.println("failed");
    }

    // Test 8: contains all method
    System.out.print("Test 8, UnsortedSet contains all method: ");
    if (!s1.containsAll(s2)) System.out.println("passed");
    else System.out.println("failed");

    // Test 9: union method
    System.out.print("Test 9, UnsortedSet union method: ");
    ISet<Integer> s3 = s1.union(s2);
    if (s3.toString().equals("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)"))
      System.out.println("passed");
    else System.out.println("failed");

    // Test 10: difference method
    System.out.print("Test 10, UnsortedSet difference method: ");
    s3 = s1.difference(s2);
    if (s3.toString().equals("(1, 2, 3, 4)")) System.out.println("passed");
    else System.out.println("failed");

    // Test 11: intersection method
    System.out.print("Test 11, UnsortedSet intersection method: ");
    s3 = s2.intersection(s1);
    if (s3.toString().equals("(5, 6, 7, 8, 9, 10)")) System.out.println("passed");
    else System.out.println("failed");

    // The following test cases are for sorted set

    // Test 12: Create a sorted set with unsorted set
    UnsortedSet<Integer> list1 = new UnsortedSet<Integer>();
    list1.add(9);
    list1.add(3);
    list1.add(1);
    list1.add(5);
    SortedSet<Integer> list2 = new SortedSet<Integer>(list1);
    System.out.print("Test 12, SortedSet constructor method: ");
    if (list2.toString().equals("(1, 3, 5, 9)")) System.out.println("passed");
    else System.out.println("failed");

    // Test 13: add method
    System.out.print("Test 13, SortedSet add method: ");
    list2.add(7);
    if (list2.toString().equals("(1, 3, 5, 7, 9)")) System.out.println("passed");
    else System.out.println("failed");

    // Test 14: add all method
    System.out.print("Test 14, SortedSet add all method: ");
    list1.clear();
    list1.add(1);
    list1.add(3);
    list1.add(6);
    list1.add(4);
    list2.addAll(list1);
    if (list2.toString().equals("(1, 3, 4, 5, 6, 7, 9)")) System.out.println("passed");
    else System.out.println("failed");

    // Test 15: remove method
    System.out.print("Test 15, SortedSet  remove method: ");
    list2.remove(4);
    list2.remove(6);
    if (list2.toString().equals("(1, 3, 5, 7, 9)")) System.out.println("passed");
    else System.out.println("failed");

    // Test 16: clear and size method
    System.out.print("Test 16, SortedSet clear and size method: ");
    list2.clear();
    if (list2.size() == 0) System.out.println("passed");
    else System.out.println("failed");

    // Test 17: contains method
    System.out.print("Test 17, SortedSet contains method: ");
    list2.add(4);
    list2.add(2);
    list2.add(3);
    list2.add(23);
    list2.add(9999);
    if (list2.contains(9999)) System.out.println("passed");
    else System.out.println("failed");

    // Test 18: contains all method
    System.out.print("Test 18, SortedSet contains all method: ");
    if (!list2.containsAll(list1)) System.out.println("passed");
    else System.out.println("failed");

    // Test 19: union method
    System.out.print("Test 19, SortedSet union method: ");
    ISet<Integer> resultList = list2.union(list1);
    if (resultList.toString().equals("(1, 2, 3, 4, 6, 23, 9999)")) System.out.println("passed");
    else System.out.println("failed");

    // Test 20: difference method
    System.out.print("Test 20, SortedSet difference method: ");
    resultList = list1.difference(list2);
    if (resultList.toString().equals("(1, 6)")) System.out.println("passed");
    else System.out.println("failed");

    // Test 21: intersection method
    System.out.print("Test 21, SortedSet intersection method: ");
    resultList = list2.intersection(list1);
    if (resultList.toString().equals("(3, 4)")) System.out.println("passed");
    else System.out.println("failed");

    // Test 22: minimum method
    System.out.print("Test 22, SortedSet min method: ");
    if (list2.min() == 2) System.out.println("passed");
    else System.out.println(false);

    // Test 23: maximum method
    System.out.print("Test 23, SortedSet max method: ");
    if (list2.max() == 9999) System.out.println("passed");
    else System.out.println("failed");

    // Test 24: SortedSet size method
    System.out.print("Test 24, SortedSet size method: ");
    if (list2.size() == 5) System.out.println("passed");
    else System.out.println("failed");

    // Test 25: UnsortedSet size method
    System.out.print("Test 25, UnsortedSet size method");
    if (list1.size() == 4) System.out.println("passed");
    else System.out.println("failed");

    //         CS314 Students. Uncomment this section when ready to
    //         run your experiments
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
      System.out.println("Unable to change look and feel");
    }
    Scanner sc = new Scanner(System.in);
    String response = "";
    do {
      largeTest();
      System.out.print("Another file? Enter y to do another file: ");
      response = sc.next();
    } while (response != null
        && response.length() > 0
        && response.substring(0, 1).equalsIgnoreCase("y"));
  }