Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Range1D_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_as.hpp"
46 #include "Teuchos_Range1D.hpp"
47 
48 
49 namespace {
50 
51 
52 using Teuchos::Range1D;
53 typedef Teuchos::Ordinal Ordinal;
54 
55 
56 TEUCHOS_UNIT_TEST( Range1D, range_0_0 )
57 {
58  ECHO(Range1D rng(0,0));
59  TEST_EQUALITY_CONST(rng.lbound(), 0);
60  TEST_EQUALITY_CONST(rng.ubound(), 0);;
61  TEST_EQUALITY_CONST(rng.size(), 1);
62  TEST_ASSERT(!rng.full_range());
63  TEST_ASSERT(!rng.in_range(-1));
64  TEST_ASSERT(rng.in_range(0));
65  TEST_ASSERT(!rng.in_range(1));
66  TEST_ASSERT(!rng.in_range(2));
67 }
68 
69 
70 TEUCHOS_UNIT_TEST( Range1D, range_1_2 )
71 {
72  ECHO(Range1D rng(1,2));
73  TEST_EQUALITY_CONST(rng.lbound(), 1);
74  TEST_EQUALITY_CONST(rng.ubound(), 2);;
75  TEST_EQUALITY_CONST(rng.size(), 2);
76  TEST_ASSERT(!rng.full_range());
77  TEST_ASSERT(!rng.in_range(-1));
78  TEST_ASSERT(!rng.in_range(0));
79  TEST_ASSERT(rng.in_range(1));
80  TEST_ASSERT(rng.in_range(2));
81  TEST_ASSERT(!rng.in_range(3));
82 }
83 
84 
85 TEUCHOS_UNIT_TEST( Range1D, range_full )
86 {
87  ECHO(Range1D rng);
88  TEST_EQUALITY_CONST(rng.lbound(), 0);
89  TEST_EQUALITY_CONST(rng.ubound(), std::numeric_limits<Ordinal>::max()-1);
90  TEST_EQUALITY_CONST(rng.size(), std::numeric_limits<Ordinal>::max());
91  TEST_ASSERT(rng.full_range());
92  TEST_ASSERT(!rng.in_range(-1));
93  TEST_ASSERT(rng.in_range(0));
94  TEST_ASSERT(rng.in_range(1));
95  TEST_ASSERT(rng.in_range(2));
96  TEST_ASSERT(rng.in_range(std::numeric_limits<Ordinal>::max()-1));
97  TEST_ASSERT(!rng.in_range(std::numeric_limits<Ordinal>::max()));
98 }
99 
100 
101 TEUCHOS_UNIT_TEST( Range1D, range_invalid )
102 {
103  ECHO(Range1D rng(Range1D::INVALID));
104  TEST_EQUALITY_CONST(rng.lbound(), 0);
105  TEST_EQUALITY_CONST(rng.ubound(), -2);
106  TEST_EQUALITY_CONST(rng.size(), -1);
107  TEST_ASSERT(!rng.full_range());
108  TEST_ASSERT(!rng.in_range(-1));
109  TEST_ASSERT(!rng.in_range(0));
110  TEST_ASSERT(!rng.in_range(1));
111 }
112 
113 
114 TEUCHOS_UNIT_TEST( Range1D, range_0_m1 )
115 {
116  ECHO(Range1D rng(0,-1));
117  TEST_EQUALITY_CONST(rng.lbound(), 0);
118  TEST_EQUALITY_CONST(rng.ubound(), -1);;
119  TEST_EQUALITY_CONST(rng.size(), 0);
120  TEST_ASSERT(!rng.full_range());
121  TEST_ASSERT(!rng.in_range(-1));
122  TEST_ASSERT(!rng.in_range(0));
123  TEST_ASSERT(!rng.in_range(1));
124 }
125 
126 
127 TEUCHOS_UNIT_TEST( Range1D, range_1_0 )
128 {
129  ECHO(Range1D rng(1,0));
130  TEST_EQUALITY_CONST(rng.lbound(), 1);
131  TEST_EQUALITY_CONST(rng.ubound(), 0);;
132  TEST_EQUALITY_CONST(rng.size(), 0);
133  TEST_ASSERT(!rng.full_range());
134  TEST_ASSERT(!rng.in_range(0));
135  TEST_ASSERT(!rng.in_range(1));
136  TEST_ASSERT(!rng.in_range(2));
137 }
138 
139 
140 TEUCHOS_UNIT_TEST( Range1D, range_4_3 )
141 {
142  ECHO(Range1D rng(4,3));
143  TEST_EQUALITY_CONST(rng.lbound(), 4);
144  TEST_EQUALITY_CONST(rng.ubound(), 3);
145  TEST_EQUALITY_CONST(rng.size(), 0);
146  TEST_ASSERT(!rng.full_range());
147  TEST_ASSERT(!rng.in_range(2));
148  TEST_ASSERT(!rng.in_range(3));
149  TEST_ASSERT(!rng.in_range(4));
150 }
151 
152 
153 TEUCHOS_UNIT_TEST( Range1D, equalityOp )
154 {
155 
156  ECHO(Range1D rng1(0,1));
157 
158  TEST_EQUALITY(rng1, rng1);
159 
160  ECHO(Range1D rng2(0,1));
161  TEST_EQUALITY(rng2, rng1);
162 
163  ECHO(Range1D rng3(0,2));
164  TEST_INEQUALITY(rng3, rng1);
165 
166  ECHO(Range1D rng4(1,2));
167  TEST_INEQUALITY(rng3, rng1);
168 
169  ECHO(Range1D rng5 = rng4 - 1);
170  TEST_EQUALITY(rng5, rng1);
171 
172 }
173 
174 
175 TEUCHOS_UNIT_TEST( Range1D, increment )
176 {
177  ECHO(Range1D rng1(4,6));
178  TEST_EQUALITY_CONST(rng1.lbound(), 4);
179  TEST_EQUALITY_CONST(rng1.ubound(), 6);
180 
181  ECHO(rng1 += 3);
182  TEST_EQUALITY_CONST(rng1.lbound(), 7);
183  TEST_EQUALITY_CONST(rng1.ubound(), 9);
184 
185  ECHO(rng1 -= 1);
186  TEST_EQUALITY_CONST(rng1.lbound(), 6);
187  TEST_EQUALITY_CONST(rng1.ubound(), 8);
188 
189  ECHO(rng1 -= 6);
190  TEST_EQUALITY_CONST(rng1.lbound(), 0);
191  TEST_EQUALITY_CONST(rng1.ubound(), 2);
192 
193  ECHO(Range1D rng2 = Range1D(2,3) + 4);
194  TEST_EQUALITY_CONST(rng2.lbound(), 6);
195  TEST_EQUALITY_CONST(rng2.ubound(), 7);
196 
197  ECHO(Range1D rng2b = 4 + Range1D(2,3));
198  TEST_EQUALITY_CONST(rng2b.lbound(), 6);
199  TEST_EQUALITY_CONST(rng2b.ubound(), 7);
200 
201  ECHO(Range1D rng3 = Range1D(4,5) - 2);
202  TEST_EQUALITY_CONST(rng3.lbound(), 2);
203  TEST_EQUALITY_CONST(rng3.ubound(), 3);
204 
205  ECHO(Range1D rng4 = Range1D(4,4) - 4);
206  TEST_EQUALITY_CONST(rng4.lbound(), 0);
207  TEST_EQUALITY_CONST(rng4.ubound(), 0);
208 
209  ECHO(Range1D rng5 = Range1D(4,4) + (-4));
210  TEST_EQUALITY_CONST(rng5.lbound(), 0);
211  TEST_EQUALITY_CONST(rng5.ubound(), 0);
212 }
213 
214 
215 #ifdef TEUCHOS_DEBUG
216 
217 TEUCHOS_UNIT_TEST( Range1D, outOfRange )
218 {
219  TEST_THROW(Range1D(-1,-1), std::out_of_range);
220  TEST_THROW(Range1D(-1,1), std::out_of_range);
221  TEST_THROW(Range1D(2,0), std::out_of_range);
222  TEST_THROW(Range1D(3,0), std::out_of_range);
223  TEST_THROW(Range1D(5,3), std::out_of_range);
224  TEST_THROW(Range1D(0,0)-1, std::out_of_range);
225  TEST_THROW(Range1D(0,0)+(-1), std::out_of_range);
226 }
227 
228 #endif // TEUCHOS_DEBUG
229 
230 // ToDo: Test creating invalid ranges
231 
232 // ToDo: Test invalid lower increment.
233 
234 
235 
236 } // namespace
#define TEST_INEQUALITY(v1, v2)
Assert the inequality of v1 and v2.
#define TEST_ASSERT(v1)
Assert the given statement is true.
Subregion Index Range Class.
#define ECHO(statement)
Echo the given statement before it is executed.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Definition of Teuchos::as, for conversions between types.