Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
time-basics-test.cpp
Go to the documentation of this file.
1/*
2 TimeBasics(Test) - working with Lumiera's internal Time values
3
4 Copyright (C)
5 2008, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
19#include "lib/test/run.hpp"
22#include "lib/random.hpp"
23#include "lib/util.hpp"
24
25#include <boost/lexical_cast.hpp>
26
27using boost::lexical_cast;
28using util::isnil;
29
30
31namespace lib {
32namespace test{
33
34 using time::Time;
35 using time::TimeVar;
36 using time::FSecs;
38
39
40 /****************************************/
43 class TimeBasics_test : public Test
44 {
45 virtual void
46 run (Arg arg)
47 {
48 FSecs refval = isnil(arg)? 1 : lexical_cast<long> (arg[1]);
49
50 Time org(refval);
51
52 checkBasics (org);
53 checkComparisons (org);
55 }
56
57
58 void
59 checkBasics (Time const& ref)
60 {
61 Time zero;
62 Time two (FSecs(2));
63 Time max (Time::MAX);
64 Time min (Time::MIN);
65
66 TimeVar var (ref);
67
68 var += two;
69 var *= 2;
70 CHECK (zero == (var - 2*(ref + two)) );
71
72 var = ref;
73 CHECK (zero == (var - ref));
74 }
75
76
77 void
79 {
80 Time zero;
81 Time max (Time::MAX);
82 Time min (Time::MIN);
83
84 CHECK (zero == Time());
85 CHECK (min < zero);
86 CHECK (max > zero);
87
88 TimeVar var (ref);
89 CHECK ( (var == ref) );
90 CHECK (!(var != ref) );
91 CHECK ( (var >= ref) );
92 CHECK ( (var <= ref) );
93 CHECK (!(var < ref) );
94 CHECK (!(var > ref) );
95
96 var += Time(FSecs(2));
97 CHECK (!(var == ref) );
98 CHECK ( (var != ref) );
99 CHECK ( (var >= ref) );
100 CHECK (!(var <= ref) );
101 CHECK (!(var < ref) );
102 CHECK ( (var > ref) );
103
104 raw_time_64 rat = _raw(var);
105 CHECK (!(rat == ref) );
106 CHECK ( (rat != ref) );
107 CHECK ( (rat >= ref) );
108 CHECK (!(rat <= ref) );
109 CHECK (!(rat < ref) );
110 CHECK ( (rat > ref) );
111
112 CHECK ( (var == rat) );
113 CHECK (!(var != rat) );
114 CHECK ( (var >= rat) );
115 CHECK ( (var <= rat) );
116 CHECK (!(var < rat) );
117 CHECK (!(var > rat) );
118 }
119
120
121 void
123 {
124 Time t1(2008,0);
125 CHECK (t1 == "0:00:02.008"_expect);
126
127 Time t2(2008,88);
128 CHECK (t2 == "0:01:30.008"_expect);
129
130 Time t3(2008,118,58);
131 CHECK (t3 == "1:00:00.008"_expect);
132
133 Time t4(t2 - t3);
134 CHECK (t4 == "-0:58:30.000"_expect);
135
136 CHECK (Time::ZERO == "0:00:00.000"_expect);
137 CHECK (Time::MAX == "85401592:56:01.825"_expect);
138 CHECK (Time::MIN == "-85401592:56:01.825"_expect);
139
140
141 seedRand();
142 int millis = 1 + rani (999);
143 int secs = rani (60);
144 int mins = rani (60);
145 int hours = rani (100);
146
147 Time randTime(millis,secs,mins,hours);
148 CHECK (Time() < randTime);
149
150 const auto TIME_SCALE_sec{lib::time::TimeValue::SCALE };
151 const auto TIME_SCALE_ms {lib::time::TimeValue::SCALE / 1000};
152
153 CHECK (millis == (_raw(randTime) / TIME_SCALE_ms ) % 1000);
154 CHECK (secs == (_raw(randTime) / TIME_SCALE_sec) % 60);
155 CHECK (mins == (_raw(randTime) / TIME_SCALE_sec / 60) % 60);
156 CHECK (hours == _raw(randTime) / TIME_SCALE_sec / 60 / 60);
157 }
158 };
159
160
162 LAUNCHER (TimeBasics_test, "unit common");
163
164
165
166}} // namespace lib::test
void checkBasics(Time const &ref)
virtual void run(Arg arg)
void checkComparisons(Time const &ref)
static const raw_time_64 SCALE
Number of micro ticks (µs) per second as basic time scale.
a mutable time value, behaving like a plain number, allowing copy and re-accessing
Lumiera's internal time value datatype.
static const Time MIN
static const Time ZERO
static const Time MAX
lib::time::Time randTime()
create a random but not insane Time value between 1s ... 10min + 500ms
int64_t raw_time_64
Raw µ-tick time representation used in Lumiera.
boost::rational< int64_t > FSecs
rational representation of fractional seconds
Implementation namespace for support and library code.
int rani(uint bound=_iBOUND())
Definition random.hpp:135
Test runner and basic definitions for tests.
bool isnil(lib::time::Duration const &dur)
Generating (pseudo) random numbers with controlled seed.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
A collection of frequently used helper functions to support unit testing.
a family of time value like entities and their relationships.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...