// spidertours_test.cpp
// Glenn G. Chappell
// 26 Feb 2008
//
// For CS 311 Spring 2008
// Test program for function countSpiderTours
// Used in Assignment 4, Exercise A

// Includes for code to be tested
#include "spidertours.h"  // For function countSpiderTours
#include "spidertours.h"  // Double inclusion test

// Includes for testing package
#include <iostream>     // for std::cout, std::endl
#include <string>       // for std::string
#include <stdexcept>    // for std::runtime_error

// Includes for this test program
#include <sstream>      // for std::ostringstream


// ************************************************************************
// Testing Package:
//     Class Tester - For Tracking Tests
// ************************************************************************


// class Tester
// For extremely simple unit testing.
// Keeps track of number of tests and number of passes.
// Use test (with success/failure parameter) to do a test.
// Get results with numTests, numPassed, numFailed, allPassed.
// Restart testing with reset.
// Invariants:
//     countTests_ == number of tests (calls to test) since last reset.
//     countPasses_ == number of times function test called with true param
//      since last reset.
//     0 <= countPasses_ <= countTests_.
//     tolerance_ >= 0.
class Tester {

// ***** Tester: ctors, dctor, op= *****
public:

    // Default ctor
    // Sets countTests_, countPasses_ to zero, tolerance_ to given value
    // Pre: None.
    // Post:
    //     numTests == 0, countPasses == 0, tolerance_ == abs(theTolerance)
    // Does not throw (No-Throw Guarantee)
    Tester(double theTolerance = 0.0000001)
        :countTests_(0),
         countPasses_(0),
         tolerance_(theTolerance >= 0 ? theTolerance : -theTolerance)
    {}

    // Compiler-generated copy ctor, copy op=, dctor are used

// ***** Tester: general public functions *****
public:

    // test
    // Handles single test, param indicates pass/fail
    // Pre: None.
    // Post:
    //     countTests_ incremented
    //     countPasses_ incremented if (success)
    //     Message indicating test name (if given)
    //      and pass/fail printed to cout
    // Does not throw (No-Throw Guarantee)
    //  - Assuming exceptions have not been turned on for cout.
    void test(bool success,
              const std::string & testName = "")
    {
        ++countTests_;
        if (success) ++countPasses_;

        std::cout << "    ";
        if (testName != "")
        {
            std::cout << "Test: "
                      << testName
                      << " - ";
        }
        std::cout << (success ? "passed" : "********** FAILED **********")
                  << std::endl;
    }

    // ftest
    // Does single floating-point test.
    // Tests passes iff difference of first two values is <= tolerance.
    // Pre: None.
    // Post:
    //     countTests_ incremented
    //     countPasses_ incremented if (abs(val1-val2) <= tolerance_)
    //     Message indicating test name (if given)
    //      and pass/fail printed to cout
    // Does not throw (No-Throw Guarantee)
    void ftest(double val1,
               double val2,
               const std::string & testName = "")
    { test(val1-val2 <= tolerance_ && val2-val1 <= tolerance_, testName); }

    // reset
    // Resets *this to default constructed state
    // Pre: None.
    // Post:
    //     countTests_ == 0, countPasses_ == 0
    // Does not throw (No-Throw Guarantee)
    void reset()
    {
        countTests_ = 0;
        countPasses_ = 0;
    }

    // numTests
    // Returns the number of tests that have been done since last reset 
    // Pre: None.
    // Post:
    //     return == countTests_
    // Does not throw (No-Throw Guarantee)
    int numTests() const
    { return countTests_; }

    // numPassed
    // Returns the number of tests that have passed since last reset
    // Pre: None.
    // Post:
    //     return == countPasses_
    // Does not throw (No-Throw Guarantee)
    int numPassed() const
    { return countPasses_; }

    // numFailed
    // Returns the number of tests that have not passed since last reset
    // Pre: None.
    // Post:
    //     return + countPasses_ == numTests_
    // Does not throw (No-Throw Guarantee)
    int numFailed() const
    { return countTests_ - countPasses_; }

    // allPassed
    // Returns true if all tests since last reset have passed
    // Pre: None.
    // Post:
    //     return == (countPasses_ == countTests_)
    // Does not throw (No-Throw Guarantee)
    bool allPassed() const
    { return countPasses_ == countTests_; }

    // setTolerance
    // Sets tolerance_ to given value
    // Pre: None.
    // Post:
    //     tolerance_ = abs(theTolerance)
    // Does not throw (No-Throw Guarantee)
    void setTolerance(double theTolerance)
    { tolerance_ = (theTolerance >= 0 ? theTolerance : -theTolerance); }

// ***** Tester: data members *****
private:

    int countTests_;    // Number of tests done since last reset
    int countPasses_;   // Number of tests passed since last reset
    double tolerance_;  // Tolerance for floating-point near-equality tests

};  // end class Tester


// ************************************************************************
// Testing Package:
//     Class TypeCheck - Helper Class for Type Checking
// ************************************************************************


// class TypeCheck
// This class exists in order to have static member function check, which
// takes a parameter of a given type, by reference. Objects of type
// TypeCheck<T> cannot be created.
// Usage:
//     TypeCheck<MyType>::check(x)
//     returns true if the type of x is (MyType) or (const MyType),
//     otherwise false.
// Invariants: None.
// Requirements on Types: None.
template<typename T>
class TypeCheck {

private:

    // Uncopyable class. Do not define copy ctor, copy assn.
    TypeCheck(const TypeCheck &);
    TypeCheck<T> & operator=(const TypeCheck &);

    // Compiler-generated dctor is used (but irrelevant).

public:

    // check
    // The function and function template below simulate a single function
    // that takes a single parameter, and returns true iff the parameter has
    // type T or (const T).

    // check (reference-to-const T)
    // Pre: None.
    // Post:
    //     Return is true.
    // Does not throw (No-Throw Guarantee)
    static bool check(const T & param)
    { return true; }

    // check (reference-to-const non-T)
    // Pre: None.
    // Post:
    //     Return is false.
    // Requirements on types: None.
    // Does not throw (No-Throw Guarantee)
    template <typename OtherType>
    static bool check(const OtherType & param)
    { return false; }

};  // End class TypeCheck


// ************************************************************************
// Testing Package:
//     Class Counter - Helper Class for Counting Calls & Objects, Throwing
// ************************************************************************


// class Counter
// Item type for counting ctor, dctor, op= calls, counting existing
//  objects, and possibly throwing on copy.
// If static member copyThrow_ is set, then copy ctor and copy assn throw
//  std::runtime_error. Increments static data member ctorCount_ on default
//  construction and successful copy construction. Increments static data
//  member assnCount_ on successful copy assignment. Increments static
//  data member dctorCount_ on destruction. Increments static data member
//  existing_ on construction, and decrements it on destruction.
// Invariants:
//     Counter::existing_ is number of existing objects of this class.
//     Counter::ctorCount_ is number of successful ctor calls since
//      most recent call to reset, or start of program if reset has never
//      been called.
//     Counter::dctorCount_ is (similarly) number of dctor calls.
//     Counter::assnCount_ is (similarly) number of copy assn calls.
class Counter {

// ***** Counter: Ctors, dctor, op= *****
public:

    // Default ctor
    // Pre: None.
    // Post:
    //     (ctorCount_ has been incremented.)
    //     (existing_ has been incremented.)
    Counter()
    { ++existing_; ++ctorCount_; }

    // Copy ctor
    // Pre: None.
    // Post:
    //     (ctorCount_ has been incremented.)
    //     (existing_ has been incremented.)
    Counter(const Counter & other)
    {
        if (copyThrow_)
            throw std::runtime_error("C");
        ++existing_;
        ++ctorCount_;
    }

    // Copy assignment
    // Pre: None.
    // Post:
    //     Return value is *this.
    //     (assnCount_ has been incremented.)
    Counter & operator=(const Counter & rhs)
    {
        if (copyThrow_)
            throw std::runtime_error("A");
        ++assnCount_;
        return *this;
    }

    // Dctor
    // Pre: None.
    // Post:
    //     (dctorCount_ has been incremented.)
    //     (existing_ has been decremented.)
    ~Counter()
    { --existing_; ++dctorCount_; }

// ***** Counter: Functions dealing with count *****
public:

    // resetCount
    // Pre: None.
    // Post:
    //     ctorCount_ == 0.
    //     dctorCount_ == 0.
    //     assnCount_ == 0.
    //     copyThrow_ == shouldThrow.
    static void reset(bool shouldThrow = false)
    {
        ctorCount_ = 0;
        dctorCount_ = 0;
        assnCount_ = 0;
        copyThrow_ = shouldThrow;
    }

    // getExisting
    // Pre: None.
    // Post:
    //     return == existing_.
    static int getExisting()
    { return existing_; }

    // getCtorCount
    // Pre: None.
    // Post:
    //     return == ctorCount_.
    static int getCtorCount()
    { return ctorCount_; }

    // getDctorCount
    // Pre: None.
    // Post:
    //     return == dctorCount_.
    static int getDctorCount()
    { return dctorCount_; }

    // getAssnCount
    // Pre: None.
    // Post:
    //     return == assnCount_.
    static int getAssnCount()
    { return assnCount_; }

    // setCopyThrow
    // Pre: None.
    // Post:
    //     copyThrow_ == shouldThrow
    static void setCopyThrow(bool shouldThrow)
    { copyThrow_ = shouldThrow; }

// ***** Counter: Data Members *****
private:

    static int existing_;    // # of existing objects
    static int ctorCount_;   // # of successful (non-throwing) ctor calls
    static int dctorCount_;  // # of dctor calls
    static int assnCount_;   // # of successful (non-throwing) copy = calls
    static bool copyThrow_;  // true if copy operations (ctor, =) throw

};  // End class Counter

// Definition of static data member of class Counter
int Counter::existing_ = 0;
int Counter::ctorCount_ = 0;
int Counter::dctorCount_ = 0;
int Counter::assnCount_ = 0;
bool Counter::copyThrow_ = false;


// ************************************************************************
// Utility Functions/Classes for This Testing Program
// ************************************************************************


// check_countSpiderTours_data
// Given an array of "tests". Each test is the parameters for a call to
//  countSpiderTours, along with the correct return value.
//  For each test, calls countSpiderTours with the given parameters and
//  tests whether the return value matches the given value.
// Pre: None.
// Post:
//     Pass/fail status of all tests have been registered with t.
//     Appropriate messages have been printed to cout.
void check_countSpiderTours_data(Tester & t,
                                 const int data[][7])
{
    for (int i = 0; data[i][0] != -1; ++i)
    {
        int size_x = data[i][0];
        int size_y = data[i][1];
        int start_x = data[i][2];
        int start_y = data[i][3];
        int end_x = data[i][4];
        int end_y = data[i][5];
        int correct_answer = data[i][6];

        std::ostringstream oss;
        int returnValue = countSpiderTours(size_x, size_y,
                                        start_x, start_y,
                                        end_x, end_y);
        oss << "("
            << size_x << "," << size_y << ", "
            << start_x << "," << start_y << ", "
            << end_x << "," << end_y << "), "
            << "correct: " << correct_answer
            << ", returned: " << returnValue;
        t.test(returnValue == correct_answer, oss.str());
    }
}


// ************************************************************************
// Test Suite Functions
// ************************************************************************


// test_countSpiderTours_2_or_less
// Test suite for function countSpiderTours,
//  ALL possible boards of x/y size 2 or less.
// Pre: None.
// Post:
//     Pass/fail status of tests have been registered with t.
//     Appropriate messages have been printed to cout.
void test_countSpiderTours_2_or_less(Tester & t)
{
    // Test values
    // Each row is
    //  size_x, size_y, start_x, start_y, end_x, end_y, answer
    const int data[][7] = {
        // size_x, size_y <= 2: all possibilities
        { 2,1, 0,0, 1,0,  1 },
        { 2,1, 1,0, 0,0,  1 },
        { 1,2, 0,1, 0,0,  1 },
        { 1,2, 0,0, 0,1,  1 },
        { 2,2, 0,0, 1,0,  2 },
        { 2,2, 0,0, 1,1,  2 },
        { 2,2, 0,0, 0,1,  2 },
        { 2,2, 1,0, 1,1,  2 },
        { 2,2, 1,0, 0,1,  2 },
        { 2,2, 1,0, 0,0,  2 },
        { 2,2, 1,1, 0,1,  2 },
        { 2,2, 1,1, 0,0,  2 },
        { 2,2, 1,1, 1,0,  2 },
        { 2,2, 0,1, 0,0,  2 },
        { 2,2, 0,1, 1,0,  2 },
        { 2,2, 0,1, 1,1,  2 },

        // End mark
        {-1, 0, 0, 0, 0, 0, 0 }
    };

    std::cout << "Test Suite: function countSpiderTours - x/y sizes 2 or less" << std::endl;
    check_countSpiderTours_data(t, data);
}


// test_countSpiderTours_1_by_n
// Test suite for function countSpiderTours,
//  1-by-n boards and n-by-1 boards, with n >= 3.
// Pre: None.
// Post:
//     Pass/fail status of tests have been registered with t.
//     Appropriate messages have been printed to cout.
void test_countSpiderTours_1_by_n(Tester & t)
{
    // Test values
    // Each row is
    //  size_x, size_y, start_x, start_y, end_x, end_y, answer
    const int data[][7] = {
        // 1 x n, various
        { 1,3, 0,0, 0,1,   0 },
        { 1,3, 0,0, 0,2,   1 },
        { 1,3, 0,1, 0,0,   0 },
        { 1,3, 0,1, 0,2,   0 },
        { 1,3, 0,2, 0,0,   1 },
        { 1,3, 0,2, 0,1,   0 },
        { 1,4, 0,0, 0,3,   1 },
        { 1,4, 0,3, 0,0,   1 },
        { 1,4, 0,1, 0,2,   0 },
        { 1,5, 0,0, 0,4,   1 },
        { 1,6, 0,1, 0,5,   0 },
        { 1,8, 0,7, 0,1,   0 },
        { 1,9, 0,8, 0,0,   1 },
        // n x 1, reflections of above
        { 3,1, 0,0, 1,0,   0 },
        { 3,1, 0,0, 2,0,   1 },
        { 3,1, 1,0, 0,0,   0 },
        { 3,1, 1,0, 2,0,   0 },
        { 3,1, 2,0, 0,0,   1 },
        { 3,1, 2,0, 1,0,   0 },
        { 4,1, 0,0, 3,0,   1 },
        { 4,1, 3,0, 0,0,   1 },
        { 4,1, 1,0, 2,0,   0 },
        { 5,1, 0,0, 4,0,   1 },
        { 6,1, 1,0, 5,0,   0 },
        { 8,1, 7,0, 1,0,   0 },
        { 9,1, 8,0, 0,0,   1 },

        // End mark
        {-1, 0, 0, 0, 0, 0, 0 }
    };

    std::cout << "Test Suite: function countSpiderTours - 1-by-n boards" << std::endl;
    check_countSpiderTours_data(t, data);
}


// test_countSpiderTours_2_by_n
// Test suite for function countSpiderTours,
//  2-by-n boards and n-by-2 boards, with n >= 3.
// Pre: None.
// Post:
//     Pass/fail status of tests have been registered with t.
//     Appropriate messages have been printed to cout.
void test_countSpiderTours_2_by_n(Tester & t)
{
    // Test values
    // Each row is
    //  size_x, size_y, start_x, start_y, end_x, end_y, answer
    const int data[][7] = {
        // 2 x n, various
        { 2,3, 0,0, 0,1,   2 },
        { 2,3, 0,0, 0,2,   6 },
        { 2,3, 0,0, 1,0,   4 },
        { 2,3, 0,0, 1,1,   2 },
        { 2,3, 0,0, 1,2,   6 },
        { 2,3, 0,1, 1,1,   0 },
        { 2,3, 1,1, 0,1,   0 },
        { 2,4, 0,0, 1,3,  20 },
        { 2,4, 1,0, 1,3,  20 },
        { 2,4, 0,1, 0,2,   4 },
        { 2,5, 0,0, 1,4,  64 },
        { 2,5, 0,0, 0,4,  64 },
        { 2,5, 0,3, 1,1,   8 },
        { 2,6, 0,1, 1,4,  32 },
        { 2,7, 0,0, 1,2,  64 },
        // n x 2, reflections of above
        { 3,2, 0,0, 1,0,   2 },
        { 3,2, 0,0, 2,0,   6 },
        { 3,2, 0,0, 0,1,   4 },
        { 3,2, 0,0, 1,1,   2 },
        { 3,2, 0,0, 2,1,   6 },
        { 3,2, 1,0, 1,1,   0 },
        { 3,2, 1,1, 1,0,   0 },
        { 4,2, 0,0, 3,1,  20 },
        { 4,2, 0,1, 3,1,  20 },
        { 4,2, 1,0, 2,0,   4 },
        { 5,2, 0,0, 4,1,  64 },
        { 5,2, 0,0, 4,0,  64 },
        { 5,2, 3,0, 1,1,   8 },
        { 6,2, 1,0, 4,1,  32 },
        { 7,2, 0,0, 1,1,  32 },

        // End mark
        {-1, 0, 0, 0, 0, 0, 0 }
    };

    std::cout << "Test Suite: function countSpiderTours - 2-by-n boards" << std::endl;
    check_countSpiderTours_data(t, data);
}


// test_countSpiderTours_medium_sized
// Test suite for function countSpiderTours,
//  various medium-sized boards.
// Pre: None.
// Post:
//     Pass/fail status of tests have been registered with t.
//     Appropriate messages have been printed to cout.
void test_countSpiderTours_medium_sized(Tester & t)
{
    // Test values
    // Each row is
    //  size_x, size_y, start_x, start_y, end_x, end_y, answer
    const int data[][7] = {
        // various medium-sized boards (both dims > 2, # squares < 16)
        {  3,3, 0,0,  1,2,    8 },
        {  3,4, 1,1,  2,1,   22 },
        {  4,3, 3,0,  0,2,  226 },
        {  3,5, 2,2,  1,1,  180 },
        {  5,3, 4,1,  0,0,  459 },
        { 1,15, 0,0,  0,2,    0 },
        { 15,1, 0,0, 14,0,    1 },

        // End mark
        {-1, 0, 0, 0, 0, 0, 0 }
    };

    std::cout << "Test Suite: function countSpiderTours - medium-sized boards" << std::endl;
    check_countSpiderTours_data(t, data);
}


// test_countSpiderTours_large
// Test suite for function countSpiderTours,
//  various large boards.
// Pre: None.
// Post:
//     Pass/fail status of tests have been registered with t.
//     Appropriate messages have been printed to cout.
void test_countSpiderTours_large(Tester & t)
{
    // Test values
    // Each row is
    //  size_x, size_y, start_x, start_y, end_x, end_y, answer
    const int data[][7] = {
        // various large boards (16 <= # squares < 20)
        {  2,8, 0,0,  1,2,   128 },
        {  8,2, 2,1,  1,1,    64 },
        {  2,9, 1,7,  1,6,   128 },
        {  9,2, 6,0,  1,1,   640 },
        {  3,6, 0,5,  1,2,  3224 },
        {  6,3, 4,1,  1,2,  1101 },
        {  4,4, 2,1,  0,0,  1455 },
        { 1,19, 0,0, 0,18,     1 },

        // End mark
        {-1, 0, 0, 0, 0, 0, 0 }
    };

    std::cout << "Test Suite: function countSpiderTours - large boards" << std::endl;
    check_countSpiderTours_data(t, data);
}


// test_countSpiderTours
// Test suite for function countSpiderTours
// Uses other test-suite functions
// Pre: None.
// Post:
//     Pass/fail status of tests have been registered with t.
//     Appropriate messages have been printed to cout.
void test_countSpiderTours(Tester & t)
{
    // Do all the test suites
    std::cout << "TEST SUITES FOR FUNCTION countSpiderTours" << std::endl;
    test_countSpiderTours_2_or_less(t);
    test_countSpiderTours_1_by_n(t);
    test_countSpiderTours_2_by_n(t);
    test_countSpiderTours_medium_sized(t);
    test_countSpiderTours_large(t);
}


// ************************************************************************
// Main program
// ************************************************************************


// main
// Runs function countSpiderTours test suite, prints results to cout.
int main()
{
    Tester t;
    test_countSpiderTours(t);

    std::cout << std::endl;
    if (t.allPassed())
    {
        std::cout << "All tests successful" 
                  << std::endl;
    }
    else
    {
        std::cout << "Tests ********** UNSUCCESSFUL **********"
                  << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

