Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vector_UnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_Array.hpp"
12 #include "Teuchos_getConst.hpp"
13 #include "Teuchos_as.hpp"
14 
15 
16 namespace {
17 
18 
19 int n = 4;
20 
21 
23 {
25  "n", &n, "Number of elements in the vectors" );
26 }
27 
28 
29 template<class T>
30 std::vector<T> generatevector(const int n_in)
31 {
32  using Teuchos::as;
33  std::vector<T> a(n_in);
34  for( int i = 0; i < n_in; ++i )
35  a[i] = as<T>(i); // tests non-const operator[](i)
36  return a;
37 }
38 
39 
40 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, defaultConstruct, T )
41 {
42  using std::vector;
43  using Teuchos::as;
44  vector<T> a2;
45  TEST_EQUALITY_CONST( as<int>(a2.size()), 0 );
46  TEST_EQUALITY_CONST( a2.empty(), true );
47 }
48 
49 
50 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, sizedConstruct, T )
51 {
52  using std::vector;
53  using Teuchos::as;
54  using Teuchos::getConst;
55  typedef typename std::vector<T>::size_type size_type;
56  vector<T> a(n);
57  TEST_EQUALITY_CONST( a.empty(), false );
58  TEST_EQUALITY( as<int>(a.size()), n );
59  TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
60  TEST_COMPARE( as<int>(a.capacity()), >=, n );
61  TEST_COMPARE_CONST( as<int>(a.capacity()), >=, n );
62 }
63 
64 
65 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, operatorBracket, T )
66 {
67  using std::vector;
68  using Teuchos::as;
69  out << "\nTest that a[i] == i ... ";
70  vector<T> a = generatevector<T>(n);;
71  bool local_success = true;
72  for( int i = 0; i < n; ++i ) {
73  TEST_ARRAY_ELE_EQUALITY( a, i, as<T>(i) );
74  }
75  if (local_success) out << "passed\n";
76  else success = false;
77 }
78 
79 
80 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, constAt, T )
81 {
82  using std::vector;
83  using Teuchos::as;
84  out << "\nTest that a.at(i) == i ...\n";
85  vector<T> a = generatevector<T>(n);;
86  bool local_success = true;
87  for( int i = 0; i < n; ++i ) {
88  TEUCHOS_TEST_EQUALITY( a.at(i), as<T>(i), out, local_success );
89  }
90  if (local_success) out << "passed\n";
91  else success = false;
92 }
93 
94 
95 //
96 // Instantiations
97 //
98 
99 
100 #define UNIT_TEST_GROUP( T ) \
101  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, defaultConstruct, T ) \
102  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, sizedConstruct, T ) \
103  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, operatorBracket, T ) \
104  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, constAt, T )
105 
106 
107 UNIT_TEST_GROUP(int)
108 UNIT_TEST_GROUP(float)
109 UNIT_TEST_GROUP(double)
110 
111 
112 } // namespace
#define UNIT_TEST_GROUP(T)
static CommandLineProcessor & getCLP()
Return the CLP to add options to.
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(TEST_GROUP, TEST_NAME, TYPE)
Macro for defining a templated unit test with one template parameter.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_COMPARE(v1, comp, v2)
Assert that v1 comp v2 (where comp = &#39;==&#39;, &#39;&gt;=&quot;, &quot;!=", etc).
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Templated array class derived from the STL std::vector.
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
Test that two values are equal.
#define TEST_ARRAY_ELE_EQUALITY(a, i, val)
Assert that a[i] == val.
#define TEST_COMPARE_CONST(v1, comp, v2)
Assert that v1 comp v2 (where comp = &#39;==&#39;, &#39;&gt;=&quot;, &quot;!=", etc) where the second object v2 is printed as ...
Definition of Teuchos::as, for conversions between types.
TEUCHOS_STATIC_SETUP()