Exemplo n.º 1
0
void unittest ()
{
    cout << "\nSTARTING UNIT TEST\n\n";

    ShoppingList* myList = new ShoppingList;

    cout << "* DEFAULT CONSTRUCTOR *\n\n";

    try {
        btassert<bool>(myList->getMaxItems() == 25);
        cout << "Passed TEST 1: ShoppingList::getMaxItems () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 1: ShoppingList::getMaxItems () #\n";
    }

    try {
        btassert<bool>(myList->getItemCount() == 0);
        cout << "Passed TEST 2: ShoppingList::getItemCount () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 2: ShoppingList::getItemCount () #\n";
    }

    try {
        myList->getItem(0);
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY INDEX");
            cout << "Passed TEST 3: ShoppingList::getItem(0) EXCEPTION HANDLING \n";
        } catch (bool b) {
            cout << "# FAILED TEST 3: ShoppingList::getItem(0) EXCEPTION HANDLING #\n";
        }
    } catch(bool b) {
        cout << "# FAILED TEST 3: ShoppingList::getItem(0) EXCEPTION HANDLING #\n";
    }

    try {
        btassert<bool>(myList->addItem("apples") == true);
        cout << "Passed TEST 4: ShoppingList::addItem(\"apples\") \n";
    } catch (bool b) {
        cout << "# FAILED TEST 4: ShoppingList::addItem(\"apples\") #\n";
    }

    try {
        btassert<bool>(myList->getItemCount() == 1);
        cout << "Passed TEST 5: ShoppingList::getItemCount () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 5: ShoppingList::getItemCount () #\n";
    }

    try {
        btassert<bool>(myList->getItem(0) == "apples");
        cout << "Passed TEST 6: ShoppingList::getItem(0) \n";
    } catch (bool b) {
        cout << "# FAILED TEST 6: ShoppingList::getItem(0) #\n";
    }

    myList->getItem(0) = "oranges";
    try {
        btassert<bool>(myList->getItem(0) == "oranges");
        cout << "Passed TEST 7: string& ShoppingList::getItem(0) \n";
    } catch (bool b) {
        cout << "# FAILED TEST 7: string& ShoppingList::getItem(0) #\n";
    }

    try {
        btassert<bool>(myList->removeItem(0) == "oranges");
        cout << "Passed TEST 8: ShoppingList::removeItem(0) \n";
    } catch (bool b) {
        cout << "# FAILED TEST 8: ShoppingList::removeItem(0) #\n";
    }

    try {
        btassert<bool>(myList->getItemCount() == 0);
        cout << "Passed TEST 9: ShoppingList::getItemCount () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 9: ShoppingList::getItemCount () #\n";
    }

    for (unsigned int i=0; i<myList->getMaxItems(); i++)
        myList->addItem("grapes");

    try {
        btassert<bool>(myList->getItemCount() == 25);
        cout << "Passed TEST 10: ShoppingList::getItemCount () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 10: ShoppingList::getItemCount () #\n";
    }

    try {
        myList->getItem(26);
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY INDEX");
            cout << "Passed TEST 11: ShoppingList::getItem(11) EXCEPTION HANDLING \n";
        } catch (bool b) {
            cout << "# FAILED TEST 11: ShoppingList::setItem(11) EXCEPTION HANDLING #\n";
        }
    } catch(bool b) {
        cout << "# FAILED TEST 11: ShoppingList::setItem(11) EXCEPTION HANDLING #\n";
    }

    try {
        btassert<bool>(myList->addItem("bananas") == false);
        cout << "Passed TEST 12: ShoppingList::addItem(\"bananas\") \n";
    } catch (bool b) {
        cout << "# FAILED TEST 12: ShoppingList::getItem(\"bananas\") #\n";
    }

    unsigned int itemsToRemove = myList->getItemCount();
    for (unsigned int i=0; i<itemsToRemove; i++)
        myList->removeItem(0);

    try {
        btassert<bool>(myList->getItemCount() == 0);
        cout << "Passed TEST 13: ShoppingList::getItemCount () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 13: ShoppingList::getItemCount () #\n";
    }

    try {
        myList->getItem(0) = "oranges";
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY INDEX");
            cout << "Passed TEST 14: string& ShoppingList::getItem(0) EXCEPTION HANDLING \n";
        } catch (bool b) {
            cout << "# FAILED TEST 14: string& ShoppingList::setItem(0) EXCEPTION HANDLING #\n";
        }
    } catch(bool b) {
        cout << "# FAILED TEST 14: string& ShoppingList::setItem(0) EXCEPTION HANDLING #\n";
    }

    try {
        btassert<bool>(myList->getMaxItems() == 25);
        cout << "Passed TEST 15: ShoppingList::getMaxItems () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 15: ShoppingList::getMaxItems () #\n";
    }

    delete myList;
    myList = new ShoppingList(1);

    cout << "\n* OVERLOADED CONSTRUCTOR *\n\n";

    try {
        btassert<bool>(myList->getMaxItems() == 1);
        cout << "Passed TEST 16: ShoppingList::getMaxItems () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 16: ShoppingList::getMaxItems () #\n";
    }

    try {
        btassert<bool>(myList->addItem("apples") == true);
        cout << "Passed TEST 17: ShoppingList::addItem(\"apples\") \n";
    } catch (bool b) {
        cout << "# FAILED TEST 17: ShoppingList::addItem(\"apples\") #\n";
    }

    try {
        btassert<bool>(myList->getItem(0) == "apples");
        cout << "Passed TEST 18: ShoppingList::getItem(0) \n";
    } catch (bool b) {
        cout << "# FAILED TEST 18: ShoppingList::getItem(0) #\n";
    }

    try {
        myList->removeItem(1);
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY INDEX");
            cout << "Passed TEST 19: ShoppingList::removeItem(1) EXCEPTION HANDLING \n";
        } catch (bool b) {
            cout << "# FAILED TEST 19: ShoppingList::removeItem(1) EXCEPTION HANDLING #\n";
        }
    } catch(bool b) {
        cout << "# FAILED TEST 19: ShoppingList::removeItem(1) EXCEPTION HANDLING #\n";
    }

    try {
        myList->getItem(1);
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY INDEX");
            cout << "Passed TEST 20: ShoppingList::getItem(1) EXCEPTION HANDLING \n";
        } catch (bool b) {
            cout << "# FAILED TEST 20: ShoppingList::getItem(1) EXCEPTION HANDLING #\n";
        }
    } catch(bool b) {
        cout << "# FAILED TEST 20: ShoppingList::getItem(1) EXCEPTION HANDLING #\n";
    }

    cout << "\n* RESET FUNCTION *\n\n";

    try {
        myList->reset(0);
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY SIZE");
            cout << "Passed TEST 21: ShoppingList::reset(0) EXCEPTION HANDLING \n";
        } catch (bool b) {
            cout << "# FAILED TEST 21: ShoppingList::reset(0) EXCEPTION HANDLING #\n";
        }
    } catch(bool b) {
        cout << "# FAILED TEST 21: ShoppingList::reset(0) EXCEPTION HANDLING #\n";
    }

    try {
        btassert<bool>(myList->getMaxItems() == 1);
        cout << "Passed TEST 22: ShoppingList::getMaxItems () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 22: ShoppingList::getMaxItems () #\n";
    }

    myList->reset(5);

    try {
        btassert<bool>(myList->getMaxItems() == 5);
        cout << "Passed TEST 23: ShoppingList::getMaxItems () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 23: ShoppingList::getMaxItems () #\n";
    }

    try {
        btassert<bool>(myList->getItemCount() == 0);
        cout << "Passed TEST 24: ShoppingList::getItemCount () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 24: ShoppingList::getItemCount () #\n";
    }

    for (unsigned int i=0; i<myList->getMaxItems(); i++)
        myList->addItem("grapes");

    try {
        btassert<bool>(myList->getItemCount() == 5);
        cout << "Passed TEST 25: ShoppingList::getItemCount () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 25: ShoppingList::getItemCount () #\n";
    }

    try {
        myList->getItem(6);
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY INDEX");
            cout << "Passed TEST 26: ShoppingList::getItem(6) EXCEPTION HANDLING \n";
        } catch (bool b) {
            cout << "# FAILED TEST 26: ShoppingList::setItem(6) EXCEPTION HANDLING #\n";
        }
    } catch(bool b) {
        cout << "# FAILED TEST 26: ShoppingList::setItem(6) EXCEPTION HANDLING #\n";
    }

    try {
        btassert<bool>(myList->addItem("bananas") == false);
        cout << "Passed TEST 27: ShoppingList::addItem(\"bananas\") \n";
    } catch (bool b) {
        cout << "# FAILED TEST 27: ShoppingList::getItem(\"bananas\") #\n";
    }

    itemsToRemove = myList->getItemCount();
    for (unsigned int i=0; i<itemsToRemove; i++)
        myList->removeItem(0);

    try {
        btassert<bool>(myList->getItemCount() == 0);
        cout << "Passed TEST 28: ShoppingList::getItemCount () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 28: ShoppingList::getItemCount () #\n";
    }

    try {
        myList->getItem(0) = "oranges";
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY INDEX");
            cout << "Passed TEST 29: string& ShoppingList::getItem(0) EXCEPTION HANDLING \n";
        } catch (bool b) {
            cout << "# FAILED TEST 29: string& ShoppingList::setItem(0) EXCEPTION HANDLING #\n";
        }
    } catch(bool b) {
        cout << "# FAILED TEST 29: string& ShoppingList::setItem(0) EXCEPTION HANDLING #\n";
    }

    try {
        btassert<bool>(myList->getMaxItems() == 5);
        cout << "Passed TEST 30: ShoppingList::getMaxItems () \n";
    } catch (bool b) {
        cout << "# FAILED TEST 30: ShoppingList::getMaxItems () #\n";
    }

    cout << "\nUNIT TEST COMPLETE\n\n";
}
Exemplo n.º 2
0
void unittest () {

    unsigned short utCount = 15;
    unsigned short utPassed = 0;

    cout << "\nSTARTING UNIT TEST\n\n";

    ShoppingList myList;

    try {
        btassert<bool>(myList.getMaxItems() == 10);
        cout << "Passed TEST 1: ShoppingList::getMaxItems () \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 1: ShoppingList::getMaxItems () #\n";
    }

    try {
        btassert<bool>(myList.getItemCount() == 0);
        cout << "Passed TEST 2: ShoppingList::getItemCount () \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 2: ShoppingList::getItemCount () #\n";
    }

    try {
        myList.getItem(0);
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY INDEX");
            cout << "Passed TEST 3: ShoppingList::getItem(0) EXCEPTION HANDLING \n";
            ++utPassed;
        } catch (bool b) {
            cout << "# FAILED TEST 3: ShoppingList::getItem(0) EXCEPTION HANDLING #\n";
        }
    } catch(bool b) {
        cout << "# FAILED TEST 3: ShoppingList::getItem(0) EXCEPTION HANDLING #\n";
    }

    try {
        btassert<bool>(myList.addItem("apples") == true);
        cout << "Passed TEST 4: ShoppingList::addItem(\"apples\") \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 4: ShoppingList::addItem(\"apples\") #\n";
    }

    try {
        btassert<bool>(myList.getItemCount() == 1);
        cout << "Passed TEST 5: ShoppingList::getItemCount () \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 5: ShoppingList::getItemCount () #\n";
    }

    try {
        btassert<bool>(myList.getItem(0) == "apples");
        cout << "Passed TEST 6: ShoppingList::getItem(0) \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 6: ShoppingList::getItem(0) #\n";
    }

    myList.getItem(0) = "oranges";
    try {
        btassert<bool>(myList.getItem(0) == "oranges");
        cout << "Passed TEST 7: string& ShoppingList::getItem(0) \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 7: string& ShoppingList::getItem(0) #\n";
    }

    try {
        btassert<bool>(myList.removeItem(0) == "oranges");
        cout << "Passed TEST 8: ShoppingList::removeItem(0) \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 8: ShoppingList::removeItem(0) #\n";
    }
    
    try {
        btassert<bool>(myList.getItemCount() == 0);
        cout << "Passed TEST 9: ShoppingList::getItemCount () \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 9: ShoppingList::getItemCount () #\n";
    }

    for (unsigned int i=0; i<myList.getMaxItems(); i++)
        myList.addItem("grapes");

    try {
        btassert<bool>(myList.getItemCount() == 10);
        cout << "Passed TEST 10: ShoppingList::getItemCount () \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 10: ShoppingList::getItemCount () #\n";
    }

    try {
        myList.getItem(11);
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY INDEX");
            cout << "Passed TEST 11: ShoppingList::getItem(11) EXCEPTION HANDLING \n";
            ++utPassed;
        } catch (bool b) {
            cout << "# FAILED TEST 11: ShoppingList::setItem(11) EXCEPTION HANDLING #\n";
        }
    } catch (bool b) {
        cout << "# FAILED TEST 11: ShoppingList::setItem(11) EXCEPTION HANDLING #\n";
    }

    try {
        btassert<bool>(myList.addItem("bananas") == false);
        cout << "Passed TEST 12: ShoppingList::addItem(\"bananas\") \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 12: ShoppingList::getItem(\"bananas\") #\n";
    }

    unsigned int itemsToRemove = myList.getItemCount();
    for (unsigned int i=0; i<itemsToRemove; i++)
        myList.removeItem(0);

    try {
        btassert<bool>(myList.getItemCount() == 0);
        cout << "Passed TEST 13: ShoppingList::getItemCount () \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 13: ShoppingList::getItemCount () #\n";
    }

    try {
        myList.getItem(0) = "oranges";
        btassert<bool>(false);
    } catch (ArrayException e) {
        try {
            btassert<bool>(e.message == "INVALID ARRAY INDEX");
            cout << "Passed TEST 14: string& ShoppingList::getItem(0) EXCEPTION HANDLING \n";
            ++utPassed;
        } catch (bool b) {
            cout << "# FAILED TEST 14: string& ShoppingList::setItem(0) EXCEPTION HANDLING #\n";
        }
    } catch (bool b) {
        cout << "# FAILED TEST 14: string& ShoppingList::setItem(0) EXCEPTION HANDLING #\n";
    }

    try {
        btassert<bool>(myList.getMaxItems() == 10);
        cout << "Passed TEST 15: ShoppingList::getMaxItems () \n";
        ++utPassed;
    } catch (bool b) {
        cout << "# FAILED TEST 15: ShoppingList::getMaxItems () #\n";
    }

    cout << "\nUNIT TEST COMPLETE\n\n";
    
    cout << "PASSED " << utPassed << " OF " << utCount << " UNIT TEST";
    if (utCount > 1) {
        cout << "S";
    }
    cout << "\n\n";
}