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 // @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_as.hpp"
12 #include "Teuchos_Range1D.hpp"
13 
14 
15 namespace {
16 
17 
18 using Teuchos::Range1D;
19 typedef Teuchos::Ordinal Ordinal;
20 
21 
22 TEUCHOS_UNIT_TEST( Range1D, range_0_0 )
23 {
24  ECHO(Range1D rng(0,0));
25  TEST_EQUALITY_CONST(rng.lbound(), 0);
26  TEST_EQUALITY_CONST(rng.ubound(), 0);;
27  TEST_EQUALITY_CONST(rng.size(), 1);
28  TEST_ASSERT(!rng.full_range());
29  TEST_ASSERT(!rng.in_range(-1));
30  TEST_ASSERT(rng.in_range(0));
31  TEST_ASSERT(!rng.in_range(1));
32  TEST_ASSERT(!rng.in_range(2));
33 }
34 
35 
36 TEUCHOS_UNIT_TEST( Range1D, range_1_2 )
37 {
38  ECHO(Range1D rng(1,2));
39  TEST_EQUALITY_CONST(rng.lbound(), 1);
40  TEST_EQUALITY_CONST(rng.ubound(), 2);;
41  TEST_EQUALITY_CONST(rng.size(), 2);
42  TEST_ASSERT(!rng.full_range());
43  TEST_ASSERT(!rng.in_range(-1));
44  TEST_ASSERT(!rng.in_range(0));
45  TEST_ASSERT(rng.in_range(1));
46  TEST_ASSERT(rng.in_range(2));
47  TEST_ASSERT(!rng.in_range(3));
48 }
49 
50 
51 TEUCHOS_UNIT_TEST( Range1D, range_full )
52 {
53  ECHO(Range1D rng);
54  TEST_EQUALITY_CONST(rng.lbound(), 0);
55  TEST_EQUALITY_CONST(rng.ubound(), std::numeric_limits<Ordinal>::max()-1);
56  TEST_EQUALITY_CONST(rng.size(), std::numeric_limits<Ordinal>::max());
57  TEST_ASSERT(rng.full_range());
58  TEST_ASSERT(!rng.in_range(-1));
59  TEST_ASSERT(rng.in_range(0));
60  TEST_ASSERT(rng.in_range(1));
61  TEST_ASSERT(rng.in_range(2));
62  TEST_ASSERT(rng.in_range(std::numeric_limits<Ordinal>::max()-1));
63  TEST_ASSERT(!rng.in_range(std::numeric_limits<Ordinal>::max()));
64 }
65 
66 
67 TEUCHOS_UNIT_TEST( Range1D, range_invalid )
68 {
69  ECHO(Range1D rng(Range1D::INVALID));
70  TEST_EQUALITY_CONST(rng.lbound(), 0);
71  TEST_EQUALITY_CONST(rng.ubound(), -2);
72  TEST_EQUALITY_CONST(rng.size(), -1);
73  TEST_ASSERT(!rng.full_range());
74  TEST_ASSERT(!rng.in_range(-1));
75  TEST_ASSERT(!rng.in_range(0));
76  TEST_ASSERT(!rng.in_range(1));
77 }
78 
79 
80 TEUCHOS_UNIT_TEST( Range1D, range_0_m1 )
81 {
82  ECHO(Range1D rng(0,-1));
83  TEST_EQUALITY_CONST(rng.lbound(), 0);
84  TEST_EQUALITY_CONST(rng.ubound(), -1);;
85  TEST_EQUALITY_CONST(rng.size(), 0);
86  TEST_ASSERT(!rng.full_range());
87  TEST_ASSERT(!rng.in_range(-1));
88  TEST_ASSERT(!rng.in_range(0));
89  TEST_ASSERT(!rng.in_range(1));
90 }
91 
92 
93 TEUCHOS_UNIT_TEST( Range1D, range_1_0 )
94 {
95  ECHO(Range1D rng(1,0));
96  TEST_EQUALITY_CONST(rng.lbound(), 1);
97  TEST_EQUALITY_CONST(rng.ubound(), 0);;
98  TEST_EQUALITY_CONST(rng.size(), 0);
99  TEST_ASSERT(!rng.full_range());
100  TEST_ASSERT(!rng.in_range(0));
101  TEST_ASSERT(!rng.in_range(1));
102  TEST_ASSERT(!rng.in_range(2));
103 }
104 
105 
106 TEUCHOS_UNIT_TEST( Range1D, range_4_3 )
107 {
108  ECHO(Range1D rng(4,3));
109  TEST_EQUALITY_CONST(rng.lbound(), 4);
110  TEST_EQUALITY_CONST(rng.ubound(), 3);
111  TEST_EQUALITY_CONST(rng.size(), 0);
112  TEST_ASSERT(!rng.full_range());
113  TEST_ASSERT(!rng.in_range(2));
114  TEST_ASSERT(!rng.in_range(3));
115  TEST_ASSERT(!rng.in_range(4));
116 }
117 
118 
119 TEUCHOS_UNIT_TEST( Range1D, equalityOp )
120 {
121 
122  ECHO(Range1D rng1(0,1));
123 
124  TEST_EQUALITY(rng1, rng1);
125 
126  ECHO(Range1D rng2(0,1));
127  TEST_EQUALITY(rng2, rng1);
128 
129  ECHO(Range1D rng3(0,2));
130  TEST_INEQUALITY(rng3, rng1);
131 
132  ECHO(Range1D rng4(1,2));
133  TEST_INEQUALITY(rng3, rng1);
134 
135  ECHO(Range1D rng5 = rng4 - 1);
136  TEST_EQUALITY(rng5, rng1);
137 
138 }
139 
140 
141 TEUCHOS_UNIT_TEST( Range1D, increment )
142 {
143  ECHO(Range1D rng1(4,6));
144  TEST_EQUALITY_CONST(rng1.lbound(), 4);
145  TEST_EQUALITY_CONST(rng1.ubound(), 6);
146 
147  ECHO(rng1 += 3);
148  TEST_EQUALITY_CONST(rng1.lbound(), 7);
149  TEST_EQUALITY_CONST(rng1.ubound(), 9);
150 
151  ECHO(rng1 -= 1);
152  TEST_EQUALITY_CONST(rng1.lbound(), 6);
153  TEST_EQUALITY_CONST(rng1.ubound(), 8);
154 
155  ECHO(rng1 -= 6);
156  TEST_EQUALITY_CONST(rng1.lbound(), 0);
157  TEST_EQUALITY_CONST(rng1.ubound(), 2);
158 
159  ECHO(Range1D rng2 = Range1D(2,3) + 4);
160  TEST_EQUALITY_CONST(rng2.lbound(), 6);
161  TEST_EQUALITY_CONST(rng2.ubound(), 7);
162 
163  ECHO(Range1D rng2b = 4 + Range1D(2,3));
164  TEST_EQUALITY_CONST(rng2b.lbound(), 6);
165  TEST_EQUALITY_CONST(rng2b.ubound(), 7);
166 
167  ECHO(Range1D rng3 = Range1D(4,5) - 2);
168  TEST_EQUALITY_CONST(rng3.lbound(), 2);
169  TEST_EQUALITY_CONST(rng3.ubound(), 3);
170 
171  ECHO(Range1D rng4 = Range1D(4,4) - 4);
172  TEST_EQUALITY_CONST(rng4.lbound(), 0);
173  TEST_EQUALITY_CONST(rng4.ubound(), 0);
174 
175  ECHO(Range1D rng5 = Range1D(4,4) + (-4));
176  TEST_EQUALITY_CONST(rng5.lbound(), 0);
177  TEST_EQUALITY_CONST(rng5.ubound(), 0);
178 }
179 
180 
181 #ifdef TEUCHOS_DEBUG
182 
183 TEUCHOS_UNIT_TEST( Range1D, outOfRange )
184 {
185  TEST_THROW(Range1D(-1,-1), std::out_of_range);
186  TEST_THROW(Range1D(-1,1), std::out_of_range);
187  TEST_THROW(Range1D(2,0), std::out_of_range);
188  TEST_THROW(Range1D(3,0), std::out_of_range);
189  TEST_THROW(Range1D(5,3), std::out_of_range);
190  TEST_THROW(Range1D(0,0)-1, std::out_of_range);
191  TEST_THROW(Range1D(0,0)+(-1), std::out_of_range);
192 }
193 
194 #endif // TEUCHOS_DEBUG
195 
196 // ToDo: Test creating invalid ranges
197 
198 // ToDo: Test invalid lower increment.
199 
200 
201 
202 } // 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.