69 using Teuchos::ptrFromRef;
70 using Teuchos::rcpFromPtr;
77 static void share_ptr_to_threads(Ptr<int> shared_ptr,
int theTestValue,
78 Cycle_Index_Tracker & index_tracker) {
80 while (!ThreadTestManager::s_bAllowThreadsToRun) {}
89 bool bCheckStatus = ThreadTestManager::s_bMainThreadSetToNull;
91 index_tracker.trackCycle = cycle;
104 if (*shared_ptr != theTestValue) {
105 index_tracker.scambledMemory = cycle;
110 int * pScramblerInt =
new int;
112 delete pScramblerInt;
118 index_tracker.unknownError = cycle;
124 catch(DanglingReferenceError) {
126 index_tracker.danglingReference = cycle;
162 const int theTestValue = 1454083084;
164 int countDanglingReferences = 0;
165 int scrambledMemoryEvents = 0;
166 int unknownErrors = 0;
167 for (
int testCycle = 0; testCycle < numTests; ++testCycle) {
170 int * pInt =
new int;
172 *pInt = theTestValue;
174 RCP<int> shared_rcp =
rcp(pInt);
176 Ptr<int> shared_ptr = shared_rcp.ptr();
178 ThreadTestManager::s_bAllowThreadsToRun =
false;
180 ThreadTestManager::s_bMainThreadSetToNull =
false;
182 Cycle_Index_Tracker index_tracker[numThreads];
184 std::vector<std::thread> threads;
185 for (
int i = 0; i < numThreads; ++i) {
186 threads.push_back(std::thread(share_ptr_to_threads, shared_ptr,
187 theTestValue, std::ref(index_tracker[i])));
190 ThreadTestManager::s_bAllowThreadsToRun =
true;
192 while( index_tracker[0].trackCycle < 1 ) {}
198 ThreadTestManager::s_bMainThreadSetToNull =
true;
200 for (
unsigned int i = 0; i < threads.size(); ++i) {
204 for (
unsigned int i = 0; i < threads.size(); ++i) {
205 if (index_tracker[i].danglingReference != -1) {
206 ++countDanglingReferences;
208 if (index_tracker[i].scambledMemory != -1 ) {
209 ++scrambledMemoryEvents;
211 if (index_tracker[i].unknownError != -1 ) {
217 convenience_log_progress(testCycle, numTests);
221 int expectedDanglingReferences = numThreads * numTests;
222 if( countDanglingReferences != expectedDanglingReferences) {
223 std::cout << std::endl <<
"Test FAILED because only " <<
224 countDanglingReferences <<
225 " dangling references were detected but expected "
226 << expectedDanglingReferences <<
"." << std::endl;
237 std::cout <<
"Danglers: " << countDanglingReferences <<
" Scrambles: "
238 << scrambledMemoryEvents <<
" ";
243 if (unknownErrors != 0) {
244 std::cout << std::endl <<
"Detected " << unknownErrors <<
245 " dangling references were missed which should have been detected."
249 TEST_ASSERT(countDanglingReferences == expectedDanglingReferences)
257 #endif // TEUCHOS_DEBUG
Dangling reference error exception class.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
#define TEST_ASSERT(v1)
Assert the given statement is true.
#define TEUCHOS_THREAD_SAFE_UNIT_TESTS_THREADS_USED
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Smart reference counting pointer class for automatic garbage collection.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...