/// @file unit_tests/genieRoom.cpp /// @brief unit test function for function that implements genieRoom /// /// @param harnessOutput is a reference to an opened file for harness results /// @param harnessError is a reference to an opened file for harness errors /// @param input is the location of the input file that will be utilized /// this is an optional parameter and if not specified will be NULL /// void testFunction(std::ofstream &harnessOutput, std::ofstream &harnessError, char *input = NULL) { bool proper = false; // update return value type to be of expected type // std::string ret_val; std::ofstream inputFile; // Answers that are expected, only one char expected at a time int ansA[] = {0, 0}; vector <int> ans(ansA, ansA + sizeof(ansA) / sizeof(ansA[0])); int ansB[] = {0, 0}; vector <int> ans2(ansB, ansB + sizeof(ansB) / sizeof(ansB[0])); // Parameters to provide function -- one vector per parameter int parA[] = {1, 12}; vector <int> par(parA, parA + sizeof(parA) / sizeof(parA[0])); int parB[] = {1, 12}; vector <int> par2(parB, parB + sizeof(parB) / sizeof(parB[0])); std::string parC[] = {"Pinhead", "Boo"}; vector <std::string> par3(parC, parC + sizeof(parC) / sizeof(parC[0])); // Create input file if warranted // Single test case's input per line, cin.ignore '\n' utilized after test vector <std::string> input_contents; if (input != NULL) { // input file contents can always go out as strings input_contents.push_back("e"); // extra inputs should change depending on what sort of valid inputs // are to be expected by the program std::string extra = "a b c d e f g h i j k l m n o p q "; // clear the file before writing, write out input and extra per line inputFile.open(input, ios::out | ios::trunc); for (int i = 0; i < input_contents.size(); i++) { inputFile << input_contents.at(i) << " " << extra << std::endl << std::endl; } inputFile.close(); } // go through every test in the unit test (each test has 1 answer) for (int i=0; i < ans.size(); i++) { // section title for output, script should replace the generic name std::string call = "Calling STUDENT_FUNC_NAME("; call = call + "var1, \"" + par3.at(i) + "\", var2) // initial values "; call = call + "of var1 and var2: "; call = call + convertInt(par.at(i)) + ", " + convertInt(par2.at(i)); try { // output the section title to standard output so it precedes // any function output within standard out for this individual test std::cout << call << std::endl; // call to generic function, script should replace the name STUDENT_FUNC_NAME(par.at(i), par3.at(i), par2.at(i)); // determine if the return value is what is expected proper = ans.at(i) == par.at(i) && ans2.at(i) == par2.at(i); if (!proper) { // tab separation utilized to break up title and items that // should exist in a list below the title // output section title to harness output harnessOutput << call; harnessOutput << "\t"; // output what was expected harnessOutput << "Expected Result: "; harnessOutput << "var1 = " << ans.at(i); harnessOutput << ", var2 = " << ans2.at(i); harnessOutput << "\t"; // output what was returned by the function call harnessOutput << "Received: "; harnessOutput << "var1 = " << par.at(i); harnessOutput << ", var2 = " << par2.at(i); // output the input file contents for this test if warranted if (input != NULL) { harnessOutput << "\t"; harnessOutput << "Input File Conents: "; harnessOutput << input_contents.at(i); } harnessOutput << std::endl; } } catch(out_of_range& oor) { // Catch out of range errors, output section title and what threw // the exception separated by a tab, similar to harnessOutput harnessError << call; harnessError << "\t"; harnessError << "Out of Range exception thrown by: "; harnessError << oor.what() << std::endl; } catch(...) { // Catch all other exceptions, output section title and a simple // statement that an exception was thrown harnessError << call; harnessError << "\t"; harnessError << "exception thrown" << std::endl; } // The next test's input will be after the next newline if (input != NULL) { std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } } }
TEST_F(UnifiedVarRenamerTests, WhenUseDebugNamesIsFalseDoNotUseDebugNames) { // Set-up the module. // // int g; // from debug info // int h; // // void test(int p, int m) { // p has name from debug info // int a; // int b; // from debug info // } // ShPtr<Variable> varG(Variable::create("g", IntType::create(32))); module->addGlobalVar(varG); module->addDebugNameForVar(varG, varG->getName()); ShPtr<Variable> varH(Variable::create("h", IntType::create(32))); module->addGlobalVar(varH); ShPtr<Variable> varP(Variable::create("p", IntType::create(32))); testFunc->addParam(varP); module->addDebugNameForVar(varP, varP->getName()); ShPtr<Variable> varM(Variable::create("m", IntType::create(32))); testFunc->addParam(varM); ShPtr<Variable> varA(Variable::create("a", IntType::create(32))); testFunc->addLocalVar(varA); ShPtr<Variable> varB(Variable::create("b", IntType::create(32))); testFunc->addLocalVar(varB); module->addDebugNameForVar(varB, varB->getName()); ShPtr<VarDefStmt> varDefB(VarDefStmt::create(varB)); ShPtr<VarDefStmt> varDefA(VarDefStmt::create(varA, ShPtr<Expression>(), varDefB)); testFunc->setBody(varDefA); // Setup the renamer (do not use debug names). INSTANTIATE_VAR_NAME_GEN_AND_VAR_RENAMER(UnifiedVarRenamer, false); // Do the renaming. varRenamer->renameVars(module); // We expect the following output: // // int g1; // int g2; // // void test(int a1, int a2) { // int v1; // int v2; // } // // Globals: VarSet globalVarsSet(module->getGlobalVars()); ASSERT_EQ(2, globalVarsSet.size()); // We have to sort the variables to ease the checking. VarVector globalVarsVector(globalVarsSet.begin(), globalVarsSet.end()); sortByName(globalVarsVector); ShPtr<Variable> var1(globalVarsVector[0]); EXPECT_EQ("g1", var1->getName()); ShPtr<Variable> var2(globalVarsVector[1]); EXPECT_EQ("g2", var2->getName()); // Parameters: VarVector params(testFunc->getParams()); ASSERT_EQ(2, params.size()); ShPtr<Variable> par1(params.front()); EXPECT_EQ("a1", par1->getName()); ShPtr<Variable> par2(params.back()); EXPECT_EQ("a2", par2->getName()); // Locals: EXPECT_EQ("v1", varDefA->getVar()->getName()); EXPECT_EQ("v2", varDefB->getVar()->getName()); }
/***********************************************************************//** * @brief Test GApplicationPar class **************************************************************************/ void TestGApplication::test_GApplicationPar(void) { // Test void constructor test_try("Void constructor"); try { GApplicationPar par; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test copy constructor test_try("Copy constructor"); try { GApplicationPar par; GApplicationPar par2(par); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test parameter constructor test_try("Parameter constructor"); try { GApplicationPar par("name", "r", "a", "1.0", "0.0", "2.0", "Parameter name"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test integer parameter exceptions GApplicationPar par; par = GApplicationPar("name", "i", "a", "INDEF", "0.0", "2.0", "Parameter name"); test_assert(par.is_undefined(), "Check integer parameter INDEF.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "i", "a", "NONE", "0.0", "2.0", "Parameter name"); test_assert(par.is_undefined(), "Check integer parameter NONE.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "i", "a", "UNDEF", "0.0", "2.0", "Parameter name"); test_assert(par.is_undefined(), "Check integer parameter UNDEF.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "i", "a", "UNDEFINED", "0.0", "2.0", "Parameter name"); test_assert(par.is_undefined(), "Check integer parameter UNDEFINED.", par.value()+" found instead of undefined value."); // Test floating point parameter exceptions par = GApplicationPar("name", "r", "a", "INDEF", "0.0", "2.0", "Parameter name"); test_assert(par.is_undefined(), "Check floating point parameter INDEF.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "r", "a", "NONE", "0.0", "2.0", "Parameter name"); test_assert(par.is_undefined(), "Check floating point parameter NONE.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "r", "a", "UNDEF", "0.0", "2.0", "Parameter name"); test_assert(par.is_undefined(), "Check floating point parameter UNDEF.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "r", "a", "UNDEFINED", "0.0", "2.0", "Parameter name"); test_assert(par.is_undefined(), "Check floating point parameter UNDEFINED.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "r", "a", "INF", "0.0", "2.0", "Parameter name"); test_assert(par.is_notanumber(), "Check floating point parameter INF.", par.value()+" found instead of infinite value."); par = GApplicationPar("name", "r", "a", "INFINITY", "0.0", "2.0", "Parameter name"); test_assert(par.is_notanumber(), "Check floating point parameter INFINITY.", par.value()+" found instead of infinite value."); par = GApplicationPar("name", "r", "a", "NAN", "0.0", "2.0", "Parameter name"); test_assert(par.is_notanumber(), "Check floating point parameter NAN.", par.value()+" found instead of not a number."); // Return return; }
int tee_para( void ) { int r; switch ( tyybinr ) { case 0: /* muutumatu sõna */ return(1); case 1: /* vallatu S 1 */ r = par1(); break; case 2: /* õpik õpiku S 2 */ r = par2(); break; case 3: /* sama, mis case 2: vaher vahtra S 3 */ r = par3(); break; case 4: /* ase aseme S 4 */ r = par4(); break; case 5: /* sama, mis case 4: liige liikme S 5 */ r = par5(); break; case 6: /* mõte mõtte S 6 */ r = par6(); break; case 7: /* sama, mis case 6: hammas hamba S 7 */ r = par7(); break; case 8: /* erineb case 6-st mitmuse tunnuse poolest tütar tütre S 7 */ r = par8(); break; case 9: /* sama, mis case 6: katus katuse S 9 */ r = par9(); break; case 10: /* soolane soolase soolas[t A 10 */ r = par10(); break; case 11: /* harjutus harjutuse S 11 i */ r = par11(); break; case 12: /*oluline olulise olulis[t A 12 i */ r = par12(); break; case 13: /* suur suure suur[te A 13 i */ r = par13(); break; case 14: /* uus uue uu[t uu[te (uusi) A 14 i */ r = par14(); break; case 15: /* käsi käe kä[tt kä[te S 15 */ r = par15(); break; case 16: /* kõne kõnne S 16 */ r = par16(); break; case 17: /* saba sappa S 17 u */ r = par17(); break; case 18: /* sõda sõja sõtta S 18 u */ r = par18(); break; case 19: /* seminar seminari S 19 e */ r = par19(); break; case 20: /* nimi nime (nimme) S 20 */ r = par20(); break; case 21: /* jõgi jõge jõe jõkke S 20 */ r = par21(); break; case 22: /* sepp seppa sepa S 22 i */ r = par22(); break; case 23: /* hein h'eina heina heinte S 23 u */ r = par23(); break; case 24: /* padi patja padja S 24 u */ r = par24(); break; case 25: /* õnnelik õnnelikku õnneliku S 25 e */ r = par25(); break; case 26: /* idee {ee->e[i} S 26 */ r = par26(); break; default: return(0); /* "võimatu" nr */ } if ( !r ) return(0); if ( plural ) { r = eemalda_ainsus(); if ( !r ) return(0); } if ( singular ) { r = eemalda_mitmus(); if ( !r ) return(0); r = eemalda_mitmus(); } return(1); }
/***********************************************************************//** * @brief Test GApplicationPar class **************************************************************************/ void TestGApplication::test_GApplicationPar(void) { // Test void constructor test_try("Void constructor"); try { GApplicationPar par; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test copy constructor test_try("Copy constructor"); try { GApplicationPar par; GApplicationPar par2(par); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test parameter constructor test_try("Parameter constructor"); try { GApplicationPar par("name", "r", "a", "1.0", "0.0", "2.0", "Parameter name"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test integer parameter validity test_try("Integer parameter in valid range"); try { GApplicationPar par("name", "i", "a", "1", "0", "2", "Parameter name"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } test_try("Integer parameter outside valid range"); try { GApplicationPar par("name", "i", "a", "3", "0", "2", "Parameter name"); test_try_failure("Integer parameter outside validity range shall throw" " an exception."); } catch (GException::invalid_value &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } test_try("Integer parameter with valid option"); try { GApplicationPar par("name", "i", "a", "1", "0|1|2", "", "Parameter name"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } test_try("Integer parameter with invalid option"); try { GApplicationPar par("name", "i", "a", "3", "0|1|2", "", "Parameter name"); test_try_failure("Integer parameter outside validity range shall throw" " an exception."); } catch (GException::invalid_value &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test real parameter validity test_try("Real parameter in valid range"); try { GApplicationPar par("name", "r", "a", "1.0", "0.0", "2.0", "Parameter name"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } test_try("Real parameter outside valid range"); try { GApplicationPar par("name", "r", "a", "3.0", "0.0", "2.0", "Parameter name"); test_try_failure("Real parameter outside validity range shall throw" " an exception."); } catch (GException::invalid_value &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } test_try("Real parameter with valid option"); try { GApplicationPar par("name", "r", "a", "1.0", "0.0|1.0|2.0", "", "Parameter name"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } test_try("Real parameter with invalid option"); try { GApplicationPar par("name", "r", "a", "3.0", "0.0|1.0|2.0", "", "Parameter name"); test_try_failure("Real parameter outside validity range shall throw" " an exception."); } catch (GException::invalid_value &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test string parameter validity test_try("String parameter with valid option"); try { GApplicationPar par("name", "s", "a", "WaN", "Obi|Wan|Joda", "", "Parameter name"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } test_try("String parameter with invalid option"); try { GApplicationPar par("name", "s", "a", "Kenobi", "Obi|Wan|Joda", "", "Parameter name"); test_try_failure("String parameter outside validity range shall throw" " an exception."); } catch (GException::invalid_value &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test filename parameter validity test_try("String parameter with valid option"); try { GApplicationPar par("name", "f", "a", "Wan", "Obi|Wan|Joda", "", "Parameter name"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } test_try("String parameter with invalid option"); try { GApplicationPar par("name", "f", "a", "WaN", "Obi|Wan|Joda", "", "Parameter name"); test_try_failure("String parameter outside validity range shall throw" " an exception."); } catch (GException::invalid_value &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } test_try("String parameter with invalid option"); try { GApplicationPar par("name", "f", "a", "Kenobi", "Obi|Wan|Joda", "", "Parameter name"); test_try_failure("String parameter outside validity range shall throw" " an exception."); } catch (GException::invalid_value &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test integer parameter exceptions GApplicationPar par; par = GApplicationPar("name", "i", "a", "INDEF", "", "", "Parameter name"); test_assert(par.is_undefined(), "Check integer parameter INDEF.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "i", "a", "NONE", "", "", "Parameter name"); test_assert(par.is_undefined(), "Check integer parameter NONE.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "i", "a", "UNDEF", "", "", "Parameter name"); test_assert(par.is_undefined(), "Check integer parameter UNDEF.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "i", "a", "UNDEFINED", "", "", "Parameter name"); test_assert(par.is_undefined(), "Check integer parameter UNDEFINED.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "i", "a", "INF", "", "", "Parameter name"); test_assert(par.is_valid(), "Check integer point parameter INF.", par.value()+" found instead of infinite value."); par = GApplicationPar("name", "i", "a", "INFINITY", "", "", "Parameter name"); test_assert(par.is_valid(), "Check integer point parameter INFINITY.", par.value()+" found instead of infinite value."); par = GApplicationPar("name", "i", "a", "NAN", "", "", "Parameter name"); test_assert(par.is_valid(), "Check integer point parameter NAN.", par.value()+" found instead of not a number."); // Test floating point parameter exceptions par = GApplicationPar("name", "r", "a", "INDEF", "", "", "Parameter name"); test_assert(par.is_undefined(), "Check floating point parameter INDEF.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "r", "a", "NONE", "", "", "Parameter name"); test_assert(par.is_undefined(), "Check floating point parameter NONE.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "r", "a", "UNDEF", "", "", "Parameter name"); test_assert(par.is_undefined(), "Check floating point parameter UNDEF.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "r", "a", "UNDEFINED", "", "", "Parameter name"); test_assert(par.is_undefined(), "Check floating point parameter UNDEFINED.", par.value()+" found instead of undefined value."); par = GApplicationPar("name", "r", "a", "INF", "", "", "Parameter name"); test_assert(par.is_notanumber(), "Check floating point parameter INF.", par.value()+" found instead of infinite value."); par = GApplicationPar("name", "r", "a", "INFINITY", "", "", "Parameter name"); test_assert(par.is_notanumber(), "Check floating point parameter INFINITY.", par.value()+" found instead of infinite value."); par = GApplicationPar("name", "r", "a", "NAN", "", "", "Parameter name"); test_assert(par.is_notanumber(), "Check floating point parameter NAN.", par.value()+" found instead of not a number."); // Return return; }