test01.cpp revision 12855:588919e0e4aa
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  test01.cpp --
23
24  Original Author: Martin Janssen, Synopsys, Inc., 2002-02-15
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38// test of sc_time's methods
39
40#include "systemc.h"
41
42void
43test_print()
44{
45    cout << "test_print" << endl;
46
47    sc_time t1;
48    cout << t1 << endl;
49
50    uint64 v = 1230;
51    sc_time t2 = sc_time::from_value( v );
52    cout << t2 << endl;
53
54    v *= 10000;
55    sc_time t3 = sc_time::from_value( v );
56    cout << t3 << endl;
57
58    v *= 100;
59    sc_time t4 = sc_time::from_value( v );
60    cout << t4 << endl;
61
62    v *= 10000;
63    sc_time t5 = sc_time::from_value( v );
64    cout << t5 << endl;
65
66    v *= 100;
67    sc_time t6 = sc_time::from_value( v );
68    cout << t6 << endl;
69
70    v *= 10000;
71    sc_time t7 = sc_time::from_value( v );
72    cout << t7 << endl;
73}
74
75void
76test_constructors()
77{
78    cout << "test_constructors" << endl;
79
80    sc_time t1;
81    cout << t1 << endl;
82
83    sc_time t2a( 0, SC_SEC );
84    cout << t2a << endl;
85
86    sc_time t2b( 1.2345, SC_NS );
87    cout << t2b << endl;
88    sc_time t2c( 1.2341, SC_NS );
89    cout << t2c << endl;
90
91    sc_time t2d( 1, SC_FS );
92    cout << t2d << endl;
93
94    sc_time t2e( -1.2345, SC_NS );
95    cout << t2e << endl;
96    sc_time t2f( -1.2341, SC_NS );
97    cout << t2f << endl;
98
99    char v1 = 1;
100    signed char v2 = 2;
101    unsigned char v3 = 3;
102    short v4 = 4;
103    unsigned short v5 = 5;
104    int v6 = 6;
105    unsigned int v7 = 7;
106    long v8 = 8;
107    unsigned long v9 = 9;
108    float v10 = 10;
109    double v11 = 11;
110
111    sc_time t2g( v1, SC_NS );
112    cout << t2g << endl;
113    sc_time t2h( v2, SC_NS );
114    cout << t2h << endl;
115    sc_time t2i( v3, SC_NS );
116    cout << t2i << endl;
117    sc_time t2j( v4, SC_NS );
118    cout << t2j << endl;
119    sc_time t2k( v5, SC_NS );
120    cout << t2k << endl;
121    sc_time t2l( v6, SC_NS );
122    cout << t2l << endl;
123    sc_time t2m( v7, SC_NS );
124    cout << t2m << endl;
125    sc_time t2n( v8, SC_NS );
126    cout << t2n << endl;
127    sc_time t2o( v9, SC_NS );
128    cout << t2o << endl;
129    sc_time t2p( v10, SC_NS );
130    cout << t2p << endl;
131    sc_time t2q( v11, SC_NS );
132    cout << t2q << endl;
133
134    sc_time t3a( 0, SC_SEC );
135    cout << t3a << endl;
136
137    sc_time t3b( 1.2341, true );
138    cout << t3b << endl;
139    sc_time t3c( 1.2345, true );
140    cout << t3c << endl;
141    sc_time t3d( -1.2341, true );
142    cout << t3d << endl;
143    sc_time t3e( -1.2345, true );
144    cout << t3e << endl;
145
146    sc_time t3f( 1.2345, false );
147    cout << t3f << endl;
148    sc_time t3g( 1.5432, false );
149    cout << t3g << endl;
150    sc_time t3h( -1.2345, false );
151    cout << t3h << endl;
152    sc_time t3i( -1.5432, false );
153    cout << t3i << endl;
154
155#if !defined( _MSC_VER )
156    sc_time t4a( 0ull, true );
157    cout << t4a << endl;
158    sc_time t4b( 25ull, true );
159    cout << t4b << endl;
160    sc_time t4c( 25ull, false );
161    cout << t4c << endl;
162#else
163    sc_time t4a( 0ui64, true );
164    cout << t4a << endl;
165    sc_time t4b( 25ui64, true );
166    cout << t4b << endl;
167    sc_time t4c( 25ui64, false );
168    cout << t4c << endl;
169#endif
170
171    sc_time t5( t4c );
172    cout << t5 << endl;
173}
174
175void
176test_assignment()
177{
178    cout << "test_assignment" << endl;
179
180    sc_time t1;
181
182    sc_time t2;
183    t1 = t2;
184    cout << t1 << endl;
185
186    sc_time t3( 1.2345, SC_NS );
187    t1 = t3;
188    cout << t1 << endl;
189
190    sc_time t4( -1.5432, SC_NS );
191    t1 = t4;
192    cout << t1 << endl;
193}
194
195void
196test_conversion()
197{
198    cout << "test_conversion" << endl;
199
200    sc_time t1;
201    cout << t1.value() << endl;
202    cout << t1.to_double() << endl;
203    cout << t1 << endl;
204    cout << t1.to_seconds() << endl;
205
206    sc_time t2( 1.2345, SC_US );
207    cout << t2.value() << endl;
208    cout << t2.to_double() << endl;
209    cout << t2 << endl;
210    cout << t2.to_seconds() << endl;
211
212    sc_time t3( -1.5432, SC_NS );
213    cout << t3.value() << endl;
214    cout << t3.to_double() << endl;
215    cout << t3 << endl;
216    cout << t3.to_seconds() << endl;
217}
218
219void
220test_relational()
221{
222    cout << "test_relational" << endl;
223
224    sc_time t1;
225    sc_time t2( 1, SC_FS );
226    sc_time t3( 1.2345, SC_NS );
227    sc_time t4( 1.2341, SC_NS );
228    sc_time t5( -1.5432, SC_NS );
229
230    cout << ( t1 == t2 ) << endl;
231    cout << ( t1 != t2 ) << endl;
232    cout << ( t1 <  t2 ) << endl;
233    cout << ( t1 <= t2 ) << endl;
234    cout << ( t1 >  t2 ) << endl;
235    cout << ( t1 >= t2 ) << endl;
236
237    cout << ( t3 == t4 ) << endl;
238    cout << ( t3 != t4 ) << endl;
239    cout << ( t3 <  t4 ) << endl;
240    cout << ( t3 <= t4 ) << endl;
241    cout << ( t3 >  t4 ) << endl;
242    cout << ( t3 >= t4 ) << endl;
243
244    cout << ( t1 == t5 ) << endl;
245    cout << ( t1 != t5 ) << endl;
246    cout << ( t1 <  t5 ) << endl;
247    cout << ( t1 <= t5 ) << endl;
248    cout << ( t1 >  t5 ) << endl;
249    cout << ( t1 >= t5 ) << endl;
250}
251
252void
253test_arithmetic()
254{
255    cout << "test_arithmetic" << endl;
256
257    sc_time t1;
258    sc_time t2( 1, SC_FS );
259    sc_time t3( 1.2345, SC_NS );
260    sc_time t4( 1.2341, SC_NS );
261    sc_time t5( -1.5432, SC_NS );
262
263    cout << ( t1 + t2 ) << endl;
264    cout << ( t1 + t3 ) << endl;
265    cout << ( t1 + t5 ) << endl;
266    cout << ( t3 + t2 ) << endl;
267    cout << ( t3 + t4 ) << endl;
268    cout << ( t3 + t5 ) << endl;
269    cout << ( t5 + t5 ) << endl;
270
271    cout << ( t1 - t2 ) << endl;
272    cout << ( t1 - t3 ) << endl;
273    cout << ( t1 - t5 ) << endl;
274    cout << ( t3 - t4 ) << endl;
275    cout << ( t3 - t5 ) << endl;
276    cout << ( t5 - t5 ) << endl;
277
278    cout << ( t1 * 1.2345 ) << endl;
279    cout << ( 1.2345 * t3 ) << endl;
280    cout << ( t4 * 2 ) << endl;
281
282    cout << ( t1 / 1.2345 ) << endl;
283    cout << ( t4 / 2 ) << endl;
284    cout << ( t3 / t4 ) << endl;
285
286    cout << ( t2 += t3 ) << endl;
287    cout << ( t2 -= t4 ) << endl;
288    cout << ( t2 *= 1.2345 ) << endl;
289    cout << ( t2 /= 2 ) << endl;
290}
291
292void
293test_SC_ZERO_TIME()
294{
295    cout << "test_SC_ZERO_TIME" << endl;
296
297    cout << SC_ZERO_TIME << endl;
298}
299
300int
301sc_main( int, char*[] )
302{
303#if defined(_MSC_VER) && _MSC_VER < 1900
304     _set_output_format(_TWO_DIGIT_EXPONENT);
305#endif
306    test_print();
307    test_constructors();
308    test_assignment();
309    test_conversion();
310    test_relational();
311    test_arithmetic();
312    test_SC_ZERO_TIME();
313
314    return 0;
315}
316