import java.util.Arrays;
import java.util.LinkedList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * Test the implementations of containsAll, addAll, removeAll and retainAll
 * in the ArryBag class.
 *
 * @author Mark Young (A00000000)
 */
public class TestAlls {

    public static void main(String[] args) {
        List<Integer> starters = List.of(10, 20, 10, 30, 42);

        try {
            System.out.println("---- containsAll ----");
            testContains(starters, List.of(20, 42), true, starters);
            testContains(starters, Set.of(10, 40), false, starters);

            System.out.println("---- addAll ----");
            testAdd(starters, Set.of(5, 10, 50), true, 
                    List.of(10, 20, 10, 30, 42, 5, 50, 10));
            testAdd(starters, List.of(), false, starters);

            System.out.println("---- removeAll ----");
            testRemove(starters, Set.of(5, 10, 50), true, List.of(42, 20, 30));
            testRemove(starters, List.of(), false, starters);

            System.out.println("---- retainAll ----");
            testRetain(starters, Set.of(5, 10, 50), true, List.of(10, 10));
            testRetain(starters, List.of(10, 30, 42, 50, 20), false, starters);
        } catch (UnsupportedOperationException ex) {
            System.out.println("Remaining tests skipped "
                    + "due to UnsupportedOperation");
        }
    }

    private static void testContains(
            List<Integer> inBag, 
            Collection<Integer> tests, 
            boolean expected,
            List<Integer> end) {
        // prepare
        Bag<Integer> bag = makeBag(inBag);
        System.out.println("Testing " + bag + ".containsAll(" + tests + ")");
        
        // run test
        boolean result = bag.containsAll(tests);

        // check result
        if (result != expected) {
            System.out.println("FAIL: wrong return value");
        } else {
            System.out.println("OK");
        }

        // check contents
        check(bag, end);
    }

    private static void testAdd(
            List<Integer> inBag, 
            Collection<Integer> tests, 
            boolean expected,
            List<Integer> end) {
        // prepare
        Bag<Integer> bag = makeBag(inBag);
        System.out.println("Testing " + bag + ".addAll(" + tests + ")");
        
        // run test
        boolean result = bag.addAll(tests);

        // check result
        if (result != expected) {
            System.out.println("FAIL: wrong result for "
                    + "addAll(" + tests + ")");
        } else {
            System.out.println("OK");
        }

        // check contents
        check(bag, end);
    }

    private static void testRemove(
            List<Integer> inBag, 
            Collection<Integer> tests, 
            boolean expected,
            List<Integer> end) {
        // prepare
        Bag<Integer> bag = makeBag(inBag);
        System.out.println("Testing " + bag + ".removeAll(" + tests + ")");
        
        // run test
        boolean result = bag.removeAll(tests);

        // check result
        if (result != expected) {
            System.out.println("FAIL: wrong result for "
                    + "removeAll(" + tests + ")");
        } else {
            System.out.println("OK");
        }

        // check contents
        check(bag, end);
    }

    private static void testRetain(
            List<Integer> inBag, 
            Collection<Integer> tests, 
            boolean expected,
            List<Integer> end) {
        // prepare
        Bag<Integer> bag = makeBag(inBag);
        System.out.println("Testing " + bag + ".retainAll(" + tests + ")");
        
        // run test
        boolean result = bag.retainAll(tests);

        // check result
        if (result != expected) {
            System.out.println("FAIL: wrong result for "
                    + "retainAll(" + tests + ")");
        } else {
            System.out.println("OK");
        }

        // check contents
        check(bag, end);
    }

    private static Bag<Integer> makeBag(List<Integer> contents) {
        Bag<Integer> result = new ArrayBag<>();
        for (Integer num : contents) {
            result.add(num);
        }
        return result;
    }

    private static void check(Bag<Integer> bag, List<Integer> expected) {
        boolean ok = true;
        Integer[] arr = bag.toArray(new Integer[0]);
        List<Integer> testing = new LinkedList<>(expected);
        for (Integer value : arr) {
            if (!testing.remove(value)) {
                System.out.println("FAIL: unexpected " + value + " in bag");
                ok = false;
            }
        }
        if (!testing.isEmpty()) {
            System.out.println("FAIL: missing " + testing + " from bag");
            ok = false;
        }
        if (ok) {
            System.out.println("OK");
        }
    }

}
