Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Tuple_MT_UnitTests_Decl.hpp
Go to the documentation of this file.
1/*
2// @HEADER
3// ***********************************************************************
4//
5// Teuchos: Common Tools Package
6// Copyright (2004) Sandia Corporation
7//
8// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9// license for use of this work by or on behalf of the U.S. Government.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39//
40// ***********************************************************************
41// @HEADER
42*/
43
44// These unit tests are used for both a Nightly version and a Basic version
45
47
48#include "Teuchos_Tuple.hpp"
51#include <vector>
52#include <thread>
53
54namespace {
55
56using Teuchos::Tuple;
57using Teuchos::RCP;
58using Teuchos::tuple;
59using Teuchos::rcp;
60
61// convenience Tuple type for testing with abritrary n = 8
62#define TUPLE_SIZE 8 // arbitrary
63typedef Tuple<int, TUPLE_SIZE> TupleClass;
64
65// this class is used to test Tuple in mtTupleMultipleReads below
66class TupleContainingClass
67{
68 public:
69 TupleContainingClass()
70 {
71 // just set some values to match the index which we will check
72 for (int n = 0; n < TUPLE_SIZE; ++n) {
73 myTuple[n] = n;
74 }
75 }
76
77 // this method is called by the threads and returns false if it has any
78 // trouble reading the values. The constructor simply sets the elements
79 // to be equal to the array index and here we verify they read that way.
80 // If something happened strange with the iterators or memory we would
81 // hope to trigger a problem but we did not expect this to happen and
82 // everything seems ok.
83 bool readTheTuple()
84 {
85 int sanityCheckIndex = 0;
86 for (TupleClass::iterator iter = myTuple.begin();
87 iter < myTuple.end(); ++iter) {
88 if (sanityCheckIndex != *iter) {
89 // something happened
90 return false;
91 }
92 ++sanityCheckIndex;
93 }
94 return true;
95 }
96
97 private:
98 TupleClass myTuple; // the Tuple to be tested
99
100};
101
102// utility thread method used by mtTupleMultipleReads below
103static void share_tuple_to_threads(RCP<TupleContainingClass> shared_tuple,
104 std::atomic<int> & countErrors) {
105 while (!ThreadTestManager::s_bAllowThreadsToRun) {}
106 for( int n = 0; n < 1000; ++n) {
107 if (!shared_tuple->readTheTuple()) {
108 ++countErrors;
109 }
110 }
111}
112
113// RCP Thread Safety Unit Test: mtTupleMultipleReads
114//
115// Purpose:
116// Sanity Check: Mirrors the Array test (which would fail without mutex
117// protection) but this oen is ok because the ArrayView it uses does not
118// have any mutable behavior - it is true const.
119//
120// Description:
121// Creates a Tuple, shares it to multiple threads, and reads it with
122// some verification that the reading is not doing anything odd.
123//
124// Solution to the Problem:
125// Sanity Check
126//
127// Demonstration of Problem:
128// Sanity Check
129TEUCHOS_UNIT_TEST( Tuple, mtTupleMultipleReads )
130{
131 const int numThreads = TEUCHOS_THREAD_SAFE_UNIT_TESTS_THREADS_USED;
132 const int numTests = NUM_TESTS_TO_RUN;
133 // threads will increment if errors occur
134 std::atomic<int> countErrors(0);
135 try {
136 for (int testCycle = 0; testCycle < numTests; ++testCycle) {
137 std::vector<std::thread> threads;
138 // note the tuple constructor makes this a type TupleClass
139 // which is Tuple<int,8>
140 RCP<TupleContainingClass> shared_tuple_rcp = rcp(
141 new TupleContainingClass());
142 // create threads all sharing access to the Tuple RCP
143 for (int i = 0; i < numThreads; ++i) {
144 threads.push_back( std::thread(share_tuple_to_threads,
145 shared_tuple_rcp, std::ref(countErrors)) );
146 }
147 ThreadTestManager::s_bAllowThreadsToRun = true; // let the threads run
148 for (unsigned int i = 0; i < threads.size(); ++i) {
149 threads[i].join();
150 }
151 convenience_log_progress(testCycle, numTests); // this is just output
152 }
153 }
154 TEUCHOS_STANDARD_CATCH_STATEMENTS(true, std::cerr, success);
155 TEST_EQUALITY(countErrors, 0 );
156}
157
158} // end namespace
#define NUM_TESTS_TO_RUN
#define TEUCHOS_THREAD_SAFE_UNIT_TESTS_THREADS_USED
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
Unit testing support.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
#define TUPLE_SIZE
Smart reference counting pointer class for automatic garbage collection.
Statically sized simple array (tuple) class.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.