void testGraphicsState() { TSLineGraphicsState_t *tmpState; assertEquals(0, m_graphicsState.size(), "Test initial graphics state size"); addGraphicsState(4, 10, TS_COLOR_MAX, TS_COLOR_MAX, 1, TS_GM_OP_SET, false); addGraphicsState(8, 12, TS_COLOR_YELLOW, TS_COLOR_MAX, 1, TS_GM_OP_ADD, false); addGraphicsState(30, 10, TS_COLOR_MAX, TS_COLOR_CYAN, 4, TS_GM_OP_ADD, false); addGraphicsState(1, 14, TS_COLOR_GREEN, TS_COLOR_BLUE_BRIGHT, 1, TS_GM_OP_REMOVE, false); addGraphicsState(40, 13, TS_COLOR_MAX, TS_COLOR_MAX, 6, TS_GM_OP_SET, false); assertEquals(5, m_graphicsState.size(), "Test graphics state size"); Logger::getInstance()->error("Testing state 0"); testState(0, 4, 10, TS_COLOR_WHITE_BRIGHT, TS_COLOR_BLACK, 1); Logger::getInstance()->error("Testing state 1"); testState(1, 30, 10, TS_COLOR_WHITE_BRIGHT, TS_COLOR_CYAN, 5); Logger::getInstance()->error("Testing state 2"); testState(2, 8, 12, TS_COLOR_YELLOW, TS_COLOR_BLACK, 1); Logger::getInstance()->error("Testing state 3"); testState(3, 40, 13, TS_COLOR_YELLOW, TS_COLOR_BLACK, 6); Logger::getInstance()->error("Testing state 4"); testState(4, 1, 14, TS_COLOR_GREEN, TS_COLOR_BLUE_BRIGHT, 0); addGraphicsState(40, 13, TS_COLOR_CYAN, TS_COLOR_MAX, 1, TS_GM_OP_ADD, false); assertEquals(5, m_graphicsState.size(), "Test graphics state size (1)"); Logger::getInstance()->error("Testing state 3 (1)"); testState(3, 40, 13, TS_COLOR_CYAN, TS_COLOR_BLACK, 7); addGraphicsState(50, 12, TS_COLOR_GREEN, TS_COLOR_RED_BRIGHT, 8, TS_GM_OP_SET, true); assertEquals(4, m_graphicsState.size(), "Test graphics state size (2)"); Logger::getInstance()->error("Testing state 0 (1)"); testState(0, 4, 10, TS_COLOR_WHITE_BRIGHT, TS_COLOR_BLACK, 1); Logger::getInstance()->error("Testing state 1 (1)"); testState(1, 30, 10, TS_COLOR_WHITE_BRIGHT, TS_COLOR_CYAN, 5); Logger::getInstance()->error("Testing state 2 (1)"); testState(2, 8, 12, TS_COLOR_YELLOW, TS_COLOR_BLACK, 1); Logger::getInstance()->error("Testing state 3 (1)"); testState(3, 50, 12, TS_COLOR_GREEN, TS_COLOR_RED_BRIGHT, 8); removeGraphicsState(20, 10, 10, 12, NULL); assertEquals(2, m_graphicsState.size(), "Test graphics state size (3)"); Logger::getInstance()->error("Testing state 0 (2)"); testState(0, 4, 10, TS_COLOR_WHITE_BRIGHT, TS_COLOR_BLACK, 1); Logger::getInstance()->error("Testing state 1 (2)"); testState(1, 50, 12, TS_COLOR_GREEN, TS_COLOR_RED_BRIGHT, 8); removeGraphicsState(20, 9, 1, 10, NULL); assertEquals(2, m_graphicsState.size(), "Test graphics state size (4)"); removeGraphicsState(21, 13, 1, 14, NULL); assertEquals(2, m_graphicsState.size(), "Test graphics state size (5)"); removeGraphicsState(20, 9, 1, 11, NULL); assertEquals(1, m_graphicsState.size(), "Test graphics state size (6)"); Logger::getInstance()->error("Testing state 0 (3)"); testState(1, 50, 12, TS_COLOR_GREEN, TS_COLOR_RED_BRIGHT, 8); removeGraphicsState(1, 8, 12, 13, NULL); assertEquals(0, m_graphicsState.size(), "Test graphics state size (7)"); }
int main() { testState(); }
/******************************************************************* Submits all relevant run information to the run table *******************************************************************/ bool commitSimulation( Simulation* s ) { // do nothing if database is not configured if( DB::connectionInfo.dbname == "" ){ return true; } // set up thread-dependent db connection QSqlDatabase* db; dbLock.lock(); if( !dbStore.contains(QThread::currentThreadId()) ){ db = new QSqlDatabase(); *db = QSqlDatabase::addDatabase("QMYSQL", QString::number(QThread::currentThreadId())); db->setHostName(DB::connectionInfo.host); db->setDatabaseName(DB::connectionInfo.dbname); db->setUserName(DB::connectionInfo.user); db->setPassword(DB::connectionInfo.password); dbStore.insert(QThread::currentThreadId(), db); // make sure the database exists and the credentials are good if( !db->open() ){ qCritical() << "Could not open database"; return false; } } else { db = dbStore[QThread::currentThreadId()]; } dbLock.unlock(); // prepare queries QSqlQuery testState(*db), addState(*db), addSim(*db), updateSim(*db); QSqlQuery addLoop(*db), incrementLoop(*db), termLoopFromState(*db); testState.prepare( "SELECT COUNT(state_def) AS count, loop_id, sim_id FROM states " "WHERE state_def=:id;"); addState.prepare( "INSERT INTO states (state_def, next_state, stepnum, sim_id, loop_id) " "VALUES (:statedef, :nextstate, :stepnum, :simid, :loopid);"); addSim.prepare( "INSERT INTO simulations (sim_id, length) VALUES (:simid, :length);"); updateSim.prepare( "UPDATE simulations SET final_state=:final, term_loop_id=:termloopid " "WHERE sim_id=:id;"); addLoop.prepare( "INSERT INTO loops (loop_id, length, instance_cnt) " "VALUES (:loopid, :length, 0);"); incrementLoop.prepare( "UPDATE loops SET instance_cnt=instance_cnt+1 WHERE loop_id=:id;"); termLoopFromState.prepare( "SELECT simulations.term_loop_id AS termloop FROM states JOIN simulations " "ON states.sim_id=simulations.sim_id WHERE states.state_def=:id;"); State* i; State* initial = s->getInitialState(); ull loopState = 0; bool loopFlag = false; // make sure the simulation has been run if( initial == NULL || initial->next == NULL ){ qCritical() << "Cannot commit an incomplete simulation."; return false; } // lock simulation //QMutexLocker locker( &DB::simLock ); // start transaction db->transaction(); // loop over states for( i = initial; i!=NULL; i = i->next ) { // check if current state is in the db testState.bindValue( ":id", QVariant(i->pack()) ); if( !testState.exec() ){ qCritical() << "Cannot query state table!" << endl << testState.lastError().text(); db->rollback(); return false; } testState.first(); // if state IS NOT in db if( testState.record().value("count").toInt() == 0 ) { // initial state is fresh if( i == initial ) { // commit new simulation entry addSim.bindValue(":simid", QVariant(initial->pack())); addSim.bindValue(":length", QVariant(s->getTotalLength())); if( !addSim.exec() ){ qCritical() << "Cannot add new simulation to database!" << endl << addSim.lastError().text(); db->rollback(); return false; } } // state is the start of the loop if( *i == *(s->getLoopState()) ) { // set the loop id flag for future commits loopFlag = true; loopState = i->pack(); // create new entry in the loop table addLoop.bindValue(":id", QVariant(loopState)); addLoop.bindValue(":length", QVariant(s->getLoopLength())); if( !addLoop.exec() ){ qCritical() << "Cannot add new loop to database!" << endl << addLoop.lastError().text(); db->rollback(); return false; } } // commit state to db addState.bindValue(":statedef", QVariant(i->pack())); addState.bindValue(":nextstate", QVariant(i->next->pack())); addState.bindValue(":stepnum", QVariant(i->stepNum)); addState.bindValue(":simid", QVariant(initial->pack())); addState.bindValue(":loopid", loopFlag ? QVariant(loopState) : QVariant() ); if( !addState.exec() ){ qCritical() << "Cannot add new state to database!" << endl << addState.lastError().text(); db->rollback(); return false; } } // state IS in db else { // initial state has already been checked if( i == initial ) { // our work here is done db->rollback(); return true; } // get the db state's loop id ull termLoop = 0; if( testState.record().value("loop_id").isNull() ) { // join a non-loop state termLoopFromState.bindValue(":id", QVariant(i->pack())); if( !termLoopFromState.exec() ){ qCritical() << "Cannot retrieve terminal loop from state!" << endl << termLoopFromState.lastError().text(); db->rollback(); return false; } termLoop = termLoopFromState.record().value("termloop").toLongLong(); } else { // join a loop state termLoop = testState.record().value("loop_id").toLongLong(); } // update simulation table with merger point and term loop id updateSim.bindValue(":final", QVariant(i->pack())); updateSim.bindValue(":termloopid", QVariant(termLoop)); updateSim.bindValue(":id", QVariant(initial->pack())); if( !updateSim.exec() ){ qCritical() << "Cannot update sim with terminal info!" << endl << updateSim.lastError().text(); db->rollback(); return false; } // increment term loop instance count incrementLoop.bindValue(":id", termLoop); if( !incrementLoop.exec() ){ qCritical() << "Cannot increment loop instance count!" << endl << incrementLoop.lastError().text(); db->rollback(); return false; } // default case: break, complete the transaction, return true // includes self-looping state case break; } // end state presence branch } // end state loop // commit transaction db->commit(); // unlock simulation //locker.unlock(); return true; }