35 using Teuchos::ptrFromRef;
36 using Teuchos::rcpFromPtr;
43 static void share_ptr_to_threads(Ptr<int> shared_ptr,
int theTestValue,
44 Cycle_Index_Tracker & index_tracker) {
46 while (!ThreadTestManager::s_bAllowThreadsToRun) {}
55 bool bCheckStatus = ThreadTestManager::s_bMainThreadSetToNull;
57 index_tracker.trackCycle = cycle;
70 if (*shared_ptr != theTestValue) {
71 index_tracker.scambledMemory = cycle;
76 int * pScramblerInt =
new int;
84 index_tracker.unknownError = cycle;
90 catch(DanglingReferenceError&) {
92 index_tracker.danglingReference = cycle;
128 const int theTestValue = 1454083084;
130 int countDanglingReferences = 0;
131 int scrambledMemoryEvents = 0;
132 int unknownErrors = 0;
133 for (
int testCycle = 0; testCycle < numTests; ++testCycle) {
136 int * pInt =
new int;
138 *pInt = theTestValue;
140 RCP<int> shared_rcp =
rcp(pInt);
142 Ptr<int> shared_ptr = shared_rcp.ptr();
144 ThreadTestManager::s_bAllowThreadsToRun =
false;
146 ThreadTestManager::s_bMainThreadSetToNull =
false;
148 Cycle_Index_Tracker index_tracker[numThreads];
150 std::vector<std::thread> threads;
151 for (
int i = 0; i < numThreads; ++i) {
152 threads.push_back(std::thread(share_ptr_to_threads, shared_ptr,
153 theTestValue, std::ref(index_tracker[i])));
156 ThreadTestManager::s_bAllowThreadsToRun =
true;
158 while( index_tracker[0].trackCycle < 1 ) {}
164 ThreadTestManager::s_bMainThreadSetToNull =
true;
166 for (
unsigned int i = 0; i < threads.size(); ++i) {
170 for (
unsigned int i = 0; i < threads.size(); ++i) {
171 if (index_tracker[i].danglingReference != -1) {
172 ++countDanglingReferences;
174 if (index_tracker[i].scambledMemory != -1 ) {
175 ++scrambledMemoryEvents;
177 if (index_tracker[i].unknownError != -1 ) {
183 convenience_log_progress(testCycle, numTests);
187 int expectedDanglingReferences = numThreads * numTests;
188 if( countDanglingReferences != expectedDanglingReferences) {
189 std::cout << std::endl <<
"Test FAILED because only " <<
190 countDanglingReferences <<
191 " dangling references were detected but expected "
192 << expectedDanglingReferences <<
"." << std::endl;
203 std::cout <<
"Danglers: " << countDanglingReferences <<
" Scrambles: "
204 << scrambledMemoryEvents <<
" ";
209 if (unknownErrors != 0) {
210 std::cout << std::endl <<
"Detected " << unknownErrors <<
211 " dangling references were missed which should have been detected."
215 TEST_ASSERT(countDanglingReferences == expectedDanglingReferences)
223 #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...