TestSqVM.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00027 
00028 #include "../include/TestSqVM.h"
00029 
00030 namespace ParCompMarkTest
00031 {
00032 
00033   void TestSqVM::setUp()
00034   {
00035         std::string currentLogFile = "logs/TestSqVM.log";
00036 
00037         if(Logger::getInstance() && Logger::getInstance()->getLogFileName() != currentLogFile)
00038          Logger::destroyInstance();
00039 
00040         if(!FileSystemManager::getInstance())
00041         {
00042          FileSystemManager::createInstance();
00043          FileSystemManager::getInstance()->setAppDirectory("./");
00044          FileSystemManager::getInstance()->initialize();
00045         }
00046 
00047         if(!Logger::getInstance())
00048         {
00049          Logger::createInstance();
00050          Logger::getInstance()->setLogFileName(currentLogFile);
00051          Logger::getInstance()->initialize();
00052         }
00053   }
00054 
00055  /*----------------------------------------------------------------------*/
00056 
00057   void TestSqVM::tearDown()
00058   {
00059   }
00060 
00061  /*----------------------------------------------------------------------*/
00062 
00063   //
00064   // Constructor & destructor tests
00065   //
00066 
00067   void TestSqVM::test_constructor_cstd__string()
00068   {
00069 
00070         /*
00071          * You have to verify the following:
00072          * 
00073          * Create Squirrel virtual machine.
00074          */
00075 
00076         SqVM::Pointer sqvm;
00077         CPPUNIT_ASSERT_NO_THROW(sqvm = new SqVM("TestVM"));
00078   }
00079 
00080  /*----------------------------------------------------------------------*/
00081 
00082   void TestSqVM::test_destructor()
00083   {
00084         // Tested at constructor
00085   }
00086 
00087  /*----------------------------------------------------------------------*/
00088 
00089   //
00090   // Class method tests
00091   //
00092 
00093   void TestSqVM::test_printFunction___HSQUIRRELVM_cSQChar_p_()
00094   {
00095 
00096         /*
00097          * You have to verify the following:
00098          * 
00099          * The print function of the virtual machine.
00100          * This function is used by the built-in function '::print()' to output text.
00101          * 
00102          */
00103 
00104         // No need to test
00105   }
00106 
00107  /*----------------------------------------------------------------------*/
00108 
00109   //
00110   // Method tests
00111   //
00112 
00113   void TestSqVM::test_runScriptFromFile_cstd__string_cstd__string_cstd__list___std__string___cbool()
00114   {
00115 
00116         /*
00117          * You have to verify the following:
00118          * 
00119          * Execute script loaded from file. Giving SqVM::NOMAINMETHOD as mainMethod indicates that no main method.
00120          */
00121 
00122         // Create VM
00123         SqVM::Pointer sqvm(new SqVM("TestVM"));
00124         Logger::getInstance()->setConsoleLogLevel(Logger::DEBUG);
00125 
00126         for(int i = 0; i < 10; i++)
00127         {
00128 
00129          // Test with a proper script with main method
00130          CPPUNIT_ASSERT_NO_THROW(sqvm->runScriptFromFile("scripts/helloWorld.nut"));
00131 
00132          // Test with a proper script without main method
00133          /*
00134           * CPPUNIT_ASSERT_NO_THROW(sqvm->runScriptFromFile("scripts/hello.nut", SqVM::NOMAINMETHOD));
00135           * 
00136           * // Test with a script containing syntax error
00137           * CPPUNIT_ASSERT_THROW(sqvm->runScriptFromFile("scripts/syntaxError.nut"), ParCompMark::Exception);
00138           */
00139 
00140         }
00141   }
00142 
00143  /*----------------------------------------------------------------------*/
00144 
00145   void TestSqVM::test_runScriptFromString_cstd__string_cstd__string_cstd__string_cstd__list___std__string___cbool()
00146   {
00147 
00148         /*
00149          * You have to verify the following:
00150          * 
00151          * Execute script from the given string. 
00152          * If the scriptName is not empty the VM store this script into the class level store for better performance.
00153          * Giving SqVM::NOMAINMETHOD as mainMethod indicates that no main method.
00154          */
00155 
00156         // Create VM
00157         SqVM::Pointer sqvm(new SqVM("TestVM"));
00158         Logger::getInstance()->setConsoleLogLevel(Logger::DEBUG);
00159 
00160         // Test with a proper script without main method and name
00161         CPPUNIT_ASSERT_NO_THROW(sqvm->runScriptFromString("print(\"Hello World!\\n\");", "", SqVM::NOMAINMETHOD));
00162 
00163         // Test with a proper script with main method and without name
00164         CPPUNIT_ASSERT_NO_THROW(sqvm->
00165                                 runScriptFromString("function main() { print(\"Hello World!\\n\"); }", "", "main"));
00166 
00167         // Test with a proper script with main method and name
00168         CPPUNIT_ASSERT_NO_THROW(sqvm->
00169                                 runScriptFromString
00170                                 ("function main() { print(\"Hello World! This is my dynamic script. I will call it later, too.\\n\"); }",
00171                                  "myScript", "main"));
00172 
00173         // Test with a script has errors
00174         CPPUNIT_ASSERT_THROW(sqvm->runScriptFromString("sdhfusid huisfh usifh suf", "", SqVM::NOMAINMETHOD),
00175                           ParCompMark::Exception);
00176 
00177         // Recall the script
00178         CPPUNIT_ASSERT_NO_THROW(sqvm->runScriptFromString("", "myScript", "main"));
00179   }
00180 
00181  /*----------------------------------------------------------------------*/
00182 
00183   void TestSqVM::test_runScriptByName_cstd__string_cstd__list___std__string__()
00184   {
00185 
00186         /*
00187          * You have to verify the following:
00188          * 
00189          * Execute a previously stored script.
00190          */
00191 
00192         // Create VM
00193         SqVM::Pointer sqvm(new SqVM("TestVM"));
00194         Logger::getInstance()->setConsoleLogLevel(Logger::DEBUG);
00195 
00196         // Test with a proper script with main method
00197         Logger::getInstance()->log(Logger::DEBUG, "Calling `scripts/helloWorld.nut\' for 1st.");
00198         CPPUNIT_ASSERT_NO_THROW(sqvm->runScriptFromFile("scripts/helloWorld.nut"));
00199 
00200         // Test with a proper script with main method and name
00201         Logger::getInstance()->log(Logger::DEBUG, "Calling `myScript\' for 1st.");
00202         CPPUNIT_ASSERT_NO_THROW(sqvm->
00203                                 runScriptFromString
00204                                 ("function dynamicMain() { print(\"Hello World! This is my dynamic script. I will call it later, too.\\n\"); }",
00205                                  "myScript", "dynamicMain"));
00206 
00207         Logger::getInstance()->log(Logger::DEBUG, "Calling `scripts/helloWorld.nut\' again");
00208         CPPUNIT_ASSERT_NO_THROW(sqvm->runScriptByName("scripts/helloWorld.nut"));
00209         Logger::getInstance()->log(Logger::DEBUG, "Calling `myScript\' again");
00210         CPPUNIT_ASSERT_NO_THROW(sqvm->runScriptByName("myScript"));
00211   }
00212 
00213  /*----------------------------------------------------------------------*/
00214 
00215   void TestSqVM::test_finalize()
00216   {
00217 
00218         /*
00219          * You have to verify the following:
00220          * 
00221          * Finalize virtual machine.
00222          */
00223 
00224         SqVM::Pointer sqvm1(new SqVM("TestVM1"));
00225         SqVM::Pointer sqvm2(new SqVM("TestVM2"));
00226 
00227         CPPUNIT_ASSERT_NO_THROW(sqvm1->activate());
00228         CPPUNIT_ASSERT_NO_THROW(sqvm1->deactivate());
00229         CPPUNIT_ASSERT_NO_THROW(sqvm2->activate());
00230         CPPUNIT_ASSERT_NO_THROW(sqvm2->deactivate());
00231         CPPUNIT_ASSERT_NO_THROW(sqvm1->activate());
00232         CPPUNIT_ASSERT_NO_THROW(sqvm1->deactivate());
00233   }
00234 
00235  /*----------------------------------------------------------------------*/
00236 
00237   void TestSqVM::test_activate()
00238   {
00239 
00240         /*
00241          * You have to verify the following:
00242          * 
00243          * Activate this VM. This virtual machine will be selected to operate.
00244          */
00245 
00246         // Tested at activate method
00247   }
00248 
00249  /*----------------------------------------------------------------------*/
00250 
00251   void TestSqVM::test_deactivate()
00252   {
00253 
00254         /*
00255          * You have to verify the following:
00256          * 
00257          * Deactivate this VM. This virtual machine will go to sleep and let other VMs to be activated.
00258          */
00259 
00260         // Tested at activate method
00261   }
00262 
00263  /*----------------------------------------------------------------------*/
00264 
00265   void TestSqVM::test_initialize()
00266   {
00267 
00268         /*
00269          * You have to verify the following:
00270          * 
00271          * Initialize virtual machine.
00272          */
00273 
00274         // Tested at activate method
00275   }
00276 
00277  /*----------------------------------------------------------------------*/
00278 
00279   void TestSqVM::test_createScript_cstd__string_cbool_cstd__string_cbool()
00280   {
00281 
00282         /*
00283          * You have to verify the following:
00284          * 
00285          * Create script object with the specified name and entry point.
00286          * The dynamic flag is also set, and the returned script object is locked by default.
00287          * 
00288          */
00289 
00290         CPPUNIT_FAIL("Implement this test!");
00291   }
00292 
00293  /*----------------------------------------------------------------------*/
00294 
00295   void TestSqVM::test_findScript_cstd__string()
00296   {
00297 
00298         /*
00299          * You have to verify the following:
00300          * 
00301          * Finds a prevously added script.
00302          */
00303 
00304         CPPUNIT_FAIL("Implement this test!");
00305   }
00306 
00307  /*----------------------------------------------------------------------*/
00308 
00309   void TestSqVM::test_findOrAddScript_cstd__string_cbool_cstd__string_cbool()
00310   {
00311 
00312         /*
00313          * You have to verify the following:
00314          * 
00315          * If the script exists with the given name then returns, if not then adds it to the internal class level container and return it.
00316          */
00317 
00318         SqVM::Pointer sqvm(new SqVM("TestVM"));
00319         SqVM::Script::Pointer script0, script1;
00320 
00321         // Test attributes
00322         CPPUNIT_ASSERT_NO_THROW(script0 = sqvm->findOrAddScript("test0.nut"));
00323         CPPUNIT_ASSERT(script0->compiled == false);
00324         CPPUNIT_ASSERT(script0->name == "test0.nut");
00325         // TODO: not locked, why?
00326         // CPPUNIT_ASSERT(script0.getLocked());
00327 
00328         // Test caching
00329         CPPUNIT_ASSERT_NO_THROW(script1 = sqvm->findOrAddScript("test0.nut"));
00330         CPPUNIT_ASSERT(script0 == script1);
00331 
00332         // Test another script
00333         CPPUNIT_ASSERT_NO_THROW(script1 = sqvm->findOrAddScript("test1.nut"));
00334         CPPUNIT_ASSERT(script0 != script1);
00335 
00336         // Unlock scripts
00337         CPPUNIT_ASSERT_NO_THROW(script0.unlock());
00338         CPPUNIT_ASSERT_NO_THROW(script1.unlock());
00339   }
00340 
00341  /*----------------------------------------------------------------------*/
00342 
00343   void TestSqVM::test_compileAndExecuteScript_SqVM__Script__Pointer()
00344   {
00345 
00346         /*
00347          * You have to verify the following:
00348          * 
00349          * Compile and execute the script.
00350          */
00351 
00352         CPPUNIT_FAIL("Implement this test!");
00353   }
00354 
00355  /*----------------------------------------------------------------------*/
00356 
00357   void TestSqVM::test_setParameters_SqVM__Script__Pointer_cstd__list___std__string__()
00358   {
00359 
00360         /*
00361          * You have to verify the following:
00362          * 
00363          * Set parameters of a script.
00364          */
00365 
00366         CPPUNIT_FAIL("Implement this test!");
00367   }
00368 
00369  /*----------------------------------------------------------------------*/
00370 
00371   // To register the suite we add:
00373   CPPUNIT_TEST_SUITE_REGISTRATION(TestSqVM);
00374 }