Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
vector_UnitTests.cpp
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
45#include "Teuchos_Array.hpp"
46#include "Teuchos_getConst.hpp"
47#include "Teuchos_as.hpp"
48
49
50namespace {
51
52
53int n = 4;
54
55
57{
59 "n", &n, "Number of elements in the vectors" );
60}
61
62
63template<class T>
64std::vector<T> generatevector(const int n_in)
65{
66 using Teuchos::as;
67 std::vector<T> a(n_in);
68 for( int i = 0; i < n_in; ++i )
69 a[i] = as<T>(i); // tests non-const operator[](i)
70 return a;
71}
72
73
74TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, defaultConstruct, T )
75{
76 using std::vector;
77 using Teuchos::as;
78 vector<T> a2;
79 TEST_EQUALITY_CONST( as<int>(a2.size()), 0 );
80 TEST_EQUALITY_CONST( a2.empty(), true );
81}
82
83
84TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, sizedConstruct, T )
85{
86 using std::vector;
87 using Teuchos::as;
89 typedef typename std::vector<T>::size_type size_type;
90 vector<T> a(n);
91 TEST_EQUALITY_CONST( a.empty(), false );
92 TEST_EQUALITY( as<int>(a.size()), n );
93 TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
94 TEST_COMPARE( as<int>(a.capacity()), >=, n );
95 TEST_COMPARE_CONST( as<int>(a.capacity()), >=, n );
96}
97
98
99TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, operatorBracket, T )
100{
101 using std::vector;
102 using Teuchos::as;
103 out << "\nTest that a[i] == i ... ";
104 vector<T> a = generatevector<T>(n);;
105 bool local_success = true;
106 for( int i = 0; i < n; ++i ) {
107 TEST_ARRAY_ELE_EQUALITY( a, i, as<T>(i) );
108 }
109 if (local_success) out << "passed\n";
110 else success = false;
111}
112
113
114TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, constAt, T )
115{
116 using std::vector;
117 using Teuchos::as;
118 out << "\nTest that a.at(i) == i ...\n";
119 vector<T> a = generatevector<T>(n);;
120 bool local_success = true;
121 for( int i = 0; i < n; ++i ) {
122 TEUCHOS_TEST_EQUALITY( a.at(i), as<T>(i), out, local_success );
123 }
124 if (local_success) out << "passed\n";
125 else success = false;
126}
127
128
129//
130// Instantiations
131//
132
133
134#define UNIT_TEST_GROUP( T ) \
135 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, defaultConstruct, T ) \
136 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, sizedConstruct, T ) \
137 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, operatorBracket, T ) \
138 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, constAt, T )
139
140
142UNIT_TEST_GROUP(float)
143UNIT_TEST_GROUP(double)
144
145
146} // namespace
Templated array class derived from the STL std::vector.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_COMPARE_CONST(v1, comp, v2)
Assert that v1 comp v2 (where comp = '==', '>=", "!=", etc) where the second object v2 is printed as ...
#define TEST_ARRAY_ELE_EQUALITY(a, i, val)
Assert that a[i] == val.
#define TEST_COMPARE(v1, comp, v2)
Assert that v1 comp v2 (where comp = '==', '>=", "!=", etc).
#define TEUCHOS_STATIC_SETUP()
Run setup code statically in a translation unit.
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
Test that two values are equal.
Unit testing support.
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(TEST_GROUP, TEST_NAME, TYPE)
Macro for defining a templated unit test with one template parameter.
Definition of Teuchos::as, for conversions between types.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
static CommandLineProcessor & getCLP()
Return the CLP to add options to.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
#define UNIT_TEST_GROUP(T)