1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19 #include <iostream>
20 #include "itkRealTimeInterval.h"
21 #include "itkMacro.h"
22 #include "itkNumericTraits.h"
23
24 #define CHECK_FOR_VALUE(a,b) \
25 { \
26 double eps = 4.0*itk::NumericTraits<double>::epsilon(); \
27 CLANG_PRAGMA_PUSH \
28 CLANG_SUPPRESS_Wfloat_equal \
29 eps = ( b == 0.0 ) ? eps : std::fabs(b*eps); \
30 CLANG_PRAGMA_POP \
31 if( std::fabs( a - b ) > eps ) \
32 { \
33 std::cerr << "Error in "#a << " expected " << b << " but got " << a << std::endl; \
34 return EXIT_FAILURE; \
35 } \
36 }
37
38 #define CHECK_FOR_BOOLEAN( x, expected ) \
39 { \
40 if( (x) != expected ) \
41 { \
42 std::cerr << "Error in "#x << std::endl; \
43 return EXIT_FAILURE; \
44 } \
45 }
46
47
itkRealTimeIntervalTest(int,char * [])48 int itkRealTimeIntervalTest( int, char * [] )
49 {
50 itk::RealTimeInterval interval0;
51
52 double timeInMicroSeconds = interval0.GetTimeInMicroSeconds();
53 double timeInMilliSeconds = interval0.GetTimeInMilliSeconds();
54 double timeInSeconds = interval0.GetTimeInSeconds();
55 double timeInHours = interval0.GetTimeInHours();
56 double timeInDays = interval0.GetTimeInDays();
57
58 CHECK_FOR_VALUE( timeInMicroSeconds, 0.0 );
59 CHECK_FOR_VALUE( timeInMilliSeconds, 0.0 );
60 CHECK_FOR_VALUE( timeInSeconds, 0.0 );
61 CHECK_FOR_VALUE( timeInHours, 0.0 );
62 CHECK_FOR_VALUE( timeInDays, 0.0 );
63
64 itk::RealTimeInterval interval1;
65 itk::RealTimeInterval intervalX = interval0;
66
67 itk::RealTimeInterval oneSecond( 1, 0 );
68 for( unsigned int i=0; i < 1000000L; i++)
69 {
70 intervalX += oneSecond;
71 }
72
73 std::cout << "intervalX = " << intervalX << std::endl;
74
75 itk::RealTimeInterval manySeconds = intervalX - interval0;
76
77 timeInSeconds = manySeconds.GetTimeInSeconds();
78
79 CHECK_FOR_VALUE( timeInSeconds, 1000000.0 );
80
81 itk::RealTimeInterval fiveMicroseconds;
82 fiveMicroseconds.Set( 0, 5 );
83
84 itk::RealTimeInterval interval3 = interval0;
85
86 for( unsigned int i=0; i < 1000000L; i++)
87 {
88 interval3 += fiveMicroseconds;
89 }
90
91 manySeconds = interval3 - interval0;
92
93 timeInSeconds = manySeconds.GetTimeInSeconds();
94
95 CHECK_FOR_VALUE( timeInSeconds, 5.0 );
96
97 for( unsigned int i=0; i < 1000000L; i++)
98 {
99 interval3 -= fiveMicroseconds;
100 }
101
102 manySeconds = interval3 - interval0;
103
104 timeInSeconds = manySeconds.GetTimeInSeconds();
105
106 CHECK_FOR_VALUE( timeInSeconds, 0.0 );
107
108
109 itk::RealTimeInterval timeSpan;
110
111 timeSpan.Set( 19, -5000000L );
112
113 timeInSeconds = timeSpan.GetTimeInSeconds();
114
115 CHECK_FOR_VALUE( timeInSeconds, 14.0 );
116
117 timeSpan.Set( -19, 5000000L );
118
119 timeInSeconds = timeSpan.GetTimeInSeconds();
120
121 CHECK_FOR_VALUE( timeInSeconds, -14.0 );
122
123 timeSpan.Set( -19, -5000000L );
124
125 timeInSeconds = timeSpan.GetTimeInSeconds();
126
127 CHECK_FOR_VALUE( timeInSeconds, -24.0 );
128
129 timeSpan.Set( 19, 5000000L );
130
131 timeInSeconds = timeSpan.GetTimeInSeconds();
132
133 CHECK_FOR_VALUE( timeInSeconds, 24.0 );
134
135
136 itk::RealTimeInterval timeSpan1( 19, 300000L );
137 itk::RealTimeInterval timeSpan2( 13, 500000L );
138
139 itk::RealTimeInterval timeSpan3 = timeSpan1 + timeSpan2;
140
141 timeInSeconds = timeSpan3.GetTimeInSeconds();
142
143 CHECK_FOR_VALUE( timeInSeconds, 32.8 );
144
145 // Test comparison operations
146 itk::RealTimeInterval dt1( 15, 13 );
147 itk::RealTimeInterval dt2( 19, 11 );
148 itk::RealTimeInterval dt3( 15, 25 );
149
150 CHECK_FOR_BOOLEAN( dt1 == dt1, true );
151 CHECK_FOR_BOOLEAN( dt1 != dt2, true );
152 CHECK_FOR_BOOLEAN( dt1 != dt1, false );
153 CHECK_FOR_BOOLEAN( dt2 >= dt1, true );
154 CHECK_FOR_BOOLEAN( dt1 >= dt1, true );
155 CHECK_FOR_BOOLEAN( dt2 > dt1, true );
156 CHECK_FOR_BOOLEAN( dt1 <= dt2, true );
157 CHECK_FOR_BOOLEAN( dt1 <= dt1, true );
158 CHECK_FOR_BOOLEAN( dt1 < dt2, true );
159
160 CHECK_FOR_BOOLEAN( dt3 == dt3, true );
161 CHECK_FOR_BOOLEAN( dt1 != dt3, true );
162 CHECK_FOR_BOOLEAN( dt3 >= dt1, true );
163 CHECK_FOR_BOOLEAN( dt3 > dt1, true );
164 CHECK_FOR_BOOLEAN( dt3 <= dt1, false );
165 CHECK_FOR_BOOLEAN( dt3 < dt1, false );
166 CHECK_FOR_BOOLEAN( dt1 <= dt3, true );
167 CHECK_FOR_BOOLEAN( dt1 < dt3, true );
168 CHECK_FOR_BOOLEAN( dt1 >= dt3, false );
169 CHECK_FOR_BOOLEAN( dt1 > dt3, false );
170
171
172 std::cout << "[PASSED]" << std::endl;
173 return EXIT_SUCCESS;
174 }
175