/* Copyright (c) 2003-2006 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ /** * NdbPortLibTest.cpp * Test the functionality of portlib * TODO - Add tests for NdbMem */ #include #include "NdbOut.hpp" #include "NdbThread.h" #include "NdbMutex.h" #include "NdbCondition.h" #include "NdbSleep.h" #include "NdbTick.h" #include "NdbEnv.h" #include "NdbHost.h" #include "NdbMain.h" int TestHasFailed; int verbose = 0; static void fail(const char* test, const char* cause) { TestHasFailed = 1; ndbout << test << " failed, " << cause << endl; } // test 1 variables and funcs extern "C" void* thread1func(void* arg) { int arg1; int returnvalue = 8; arg1 = *(int*)arg; ndbout << "thread1: thread1func called with arg = " << arg1 << endl; // delay(1000); if (arg1 != 7) fail("TEST1", "Wrong arg"); return returnvalue; } // test 2 variables and funcs NdbMutex* test2mutex; extern "C" void* test2func(void* arg) { int arg1; arg1 = *(int*)arg; ndbout << "thread" << arg1 << " started in test2func" << endl; if (NdbMutex_Lock(test2mutex) != 0) fail("TEST2", "Failed to lock mutex"); ndbout << "thread" << arg1 << ", test2func " << endl; if (NdbMutex_Unlock(test2mutex) != 0) fail("TEST2", "Failed to unlock mutex"); int returnvalue = arg1; return returnvalue; } // test 3 and 7 variables and funcs NdbMutex* testmutex; NdbCondition* testcond; int testthreadsdone; extern "C" void* testfunc(void* arg) { int tmpVar; int threadno; int result; threadno = *(int*)arg; ndbout << "Thread" << threadno << " started in testfunc" << endl; do { if ((threadno % 2) == 0) result = NdbSleep_SecSleep(1); else result = NdbSleep_MilliSleep(100); if (result != 0) fail("TEST3", "Wrong result from sleep function"); if (NdbMutex_Lock(testmutex) != 0) fail("TEST3", "Wrong result from NdbMutex_Lock function"); ndbout << "thread" << threadno << ", testfunc " << endl; testthreadsdone++; tmpVar = testthreadsdone; if (NdbCondition_Signal(testcond) != 0) fail("TEST3", "Wrong result from NdbCondition_Signal function"); if (NdbMutex_Unlock(testmutex) != 0) fail("TEST3", "Wrong result from NdbMutex_Unlock function"); } while(tmpVar<100); return 0; } extern "C" void* testTryLockfunc(void* arg) { int tmpVar = 0; int threadno; int result; threadno = *(int*)arg; ndbout << "Thread" << threadno << " started" << endl; do { if ((threadno % 2) == 0) result = NdbSleep_SecSleep(1); else result = NdbSleep_MilliSleep(100); if (result != 0) fail("TEST3", "Wrong result from sleep function"); if (NdbMutex_Trylock(testmutex) == 0){ ndbout << "thread" << threadno << ", testTryLockfunc locked" << endl; testthreadsdone++; tmpVar = testthreadsdone; if (NdbCondition_Signal(testcond) != 0) fail("TEST3", "Wrong result from NdbCondition_Signal function"); if (NdbMutex_Unlock(testmutex) != 0) fail("TEST3", "Wrong result from NdbMutex_Unlock function"); } } while(tmpVar<100); return 0; } void testMicros(int count); Uint64 time_diff(Uint64 s1, Uint64 s2, Uint32 m1, Uint32 m2); NDB_COMMAND(PortLibTest, "portlibtest", "portlibtest", "Test the portable function layer", 4096){ ndbout << "= TESTING ARGUMENT PASSING ============" << endl; ndbout << "ARGC: " << argc << endl; for(int i = 1; i < argc; i++){ ndbout << " ARGV"<= m1) diff += (m2 - m1); else { diff += m2; diff -= m1; } // if(0) // ndbout("(s1,m1) = (%d, %d) (s2,m2) = (%d, %d) -> diff = %d\n", // (Uint32)s1,m1,(Uint32)s2,m2, (Uint32)diff); return diff; }; void testMicros(int count){ Uint32 avg = 0; Uint32 sum2 = 0; for(int i = 0; i (r*1000)){ avg += (m - (r*1000)); sum2 += (m - (r*1000)) * (m - (r*1000)); } else { avg += ((r*1000) - m); sum2 += ((r*1000) - m) * ((r*1000) - m); } #if 0 m /= 1000; if(m > r && ((m - r) > 10)){ ndbout << "Difference to big: " << (m - r) << " - Test failed" << endl; TestHasFailed = 1; } if(m < r && ((r - m) > 10)){ ndbout << "Difference to big: " << (r - m) << " - Test failed" << endl; TestHasFailed = 1; } #endif } Uint32 dev = (avg * avg - sum2) / count; dev /= count; avg /= count; Uint32 t = 0; while((t*t)