test19.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  test19.cpp -- Test sc_time string/tuple conversions
23
24  Original Author: Philipp A. Hartmann, OFFIS
25
26 *****************************************************************************/
27
28
29#include "systemc.h"
30
31void check_time( const sc_time& t, sc_time_unit tu, const std::string & str )
32{
33    sc_time_tuple tp = t;
34
35    std::cout << t.to_string() << ", value=" << t.value() << std::endl;
36    std::cout << "  ";
37    if( tp.has_value() )
38        std::cout << "t.value=" << tp.value();
39    else
40        std::cout << "t.double=" << tp.to_double();
41    std::cout << ", t.unit="  << tp.unit_symbol()
42              << std::endl;
43
44    sc_assert( tp.has_value() );
45    sc_assert( t.to_string() == str );
46    sc_assert( tp.to_string() == t.to_string() );
47    sc_assert( tp.unit() == tu );
48
49    sc_time u = sc_time::from_string( str.c_str() );
50    sc_assert( t == u );
51    sc_assert( u == tp );
52    sc_assert( tp.unit() == sc_time_tuple(u).unit() );
53
54    u = sc_time( tp.to_double(), tp.unit_symbol() );
55    sc_assert( t == u );
56    sc_assert( u == tp );
57    sc_assert( tp.unit() == sc_time_tuple(u).unit() );
58}
59
60int sc_main( int, char*[] )
61{
62    sc_report_handler::set_actions( SC_ID_SET_TIME_RESOLUTION_, SC_DO_NOTHING );
63    sc_report_handler::set_actions( SC_ID_TIME_CONVERSION_FAILED_, SC_DISPLAY );
64
65    unsigned resolutions[] = { 100, 10, 1 };
66    sc_time_unit resunit   = SC_FS;
67    unsigned* res = resolutions;
68
69    while( true )
70    {
71        std::cout << "\nResolution = " << sc_get_time_resolution() << std::endl;
72
73        check_time( sc_time(  10,   SC_NS),  SC_NS,  "10 ns" );
74        check_time( sc_time( 100,   SC_NS),  SC_NS, "100 ns" );
75        check_time( sc_time(1000,   SC_NS),  SC_US,   "1 us" );
76        check_time( sc_time(   0.1, SC_US),  SC_NS, "100 ns" );
77        check_time( sc_time(   1,   SC_US),  SC_US,   "1 us" );
78        check_time( sc_time(  10,   SC_US),  SC_US,  "10 us" );
79        check_time( sc_time( 100,   SC_US),  SC_US, "100 us" );
80        check_time( sc_time(1000,   SC_US),  SC_MS,   "1 ms" );
81        check_time( sc_time( 100,  SC_SEC), SC_SEC, "100 s" );
82
83        // exit loop before final resolution update
84        if (res == resolutions + (sizeof(resolutions)/sizeof(*resolutions)))
85            break;
86
87        sc_set_time_resolution( *res, resunit );
88        res++;
89    }
90
91    cout << "\nProgram completed" << endl;
92    return 0;
93}
94