]> sigrok.org Git - pulseview.git/blame - test/util.cpp
View: Change the type of tick_period to pv::util::Timestamp
[pulseview.git] / test / util.cpp
CommitLineData
f52be90d
JS
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2015 Jens Steinhauser <jens.steinhauser@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <boost/test/unit_test.hpp>
22
23#include "pv/util.hpp"
c677193d 24#include "test/test.hpp"
f52be90d
JS
25
26using namespace pv::util;
27using ts = pv::util::Timestamp;
28
29namespace {
30 QChar mu = QChar(0x03BC);
d001f416
JS
31
32 pv::util::SIPrefix unspecified = pv::util::SIPrefix::unspecified;
33 pv::util::SIPrefix yocto = pv::util::SIPrefix::yocto;
34 pv::util::SIPrefix micro = pv::util::SIPrefix::micro;
35 pv::util::SIPrefix milli = pv::util::SIPrefix::milli;
36 pv::util::SIPrefix none = pv::util::SIPrefix::none;
37 pv::util::SIPrefix kilo = pv::util::SIPrefix::kilo;
38 pv::util::SIPrefix yotta = pv::util::SIPrefix::yotta;
39
40 pv::util::TimeUnit Time = pv::util::TimeUnit::Time;
f52be90d
JS
41}
42
f52be90d
JS
43BOOST_AUTO_TEST_SUITE(UtilTest)
44
d001f416
JS
45BOOST_AUTO_TEST_CASE(exponent_test)
46{
47 BOOST_CHECK_EQUAL(exponent(SIPrefix::yocto), -24);
48 BOOST_CHECK_EQUAL(exponent(SIPrefix::zepto), -21);
49 BOOST_CHECK_EQUAL(exponent(SIPrefix::atto), -18);
50 BOOST_CHECK_EQUAL(exponent(SIPrefix::femto), -15);
51 BOOST_CHECK_EQUAL(exponent(SIPrefix::pico), -12);
52 BOOST_CHECK_EQUAL(exponent(SIPrefix::nano), -9);
53 BOOST_CHECK_EQUAL(exponent(SIPrefix::micro), -6);
54 BOOST_CHECK_EQUAL(exponent(SIPrefix::milli), -3);
55 BOOST_CHECK_EQUAL(exponent(SIPrefix::none), 0);
56 BOOST_CHECK_EQUAL(exponent(SIPrefix::kilo), 3);
57 BOOST_CHECK_EQUAL(exponent(SIPrefix::mega), 6);
58 BOOST_CHECK_EQUAL(exponent(SIPrefix::giga), 9);
59 BOOST_CHECK_EQUAL(exponent(SIPrefix::tera), 12);
60 BOOST_CHECK_EQUAL(exponent(SIPrefix::peta), 15);
61 BOOST_CHECK_EQUAL(exponent(SIPrefix::exa), 18);
62 BOOST_CHECK_EQUAL(exponent(SIPrefix::zetta), 21);
63 BOOST_CHECK_EQUAL(exponent(SIPrefix::yotta), 24);
64}
65
f52be90d
JS
66BOOST_AUTO_TEST_CASE(format_si_value_test)
67{
68 // check prefix calculation
69
70 BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V"), "0 V");
71
72 BOOST_CHECK_EQUAL(format_si_value(ts("1e-24"), "V"), "+1 yV");
73 BOOST_CHECK_EQUAL(format_si_value(ts("1e-23"), "V"), "+10 yV");
74 BOOST_CHECK_EQUAL(format_si_value(ts("1e-22"), "V"), "+100 yV");
75 BOOST_CHECK_EQUAL(format_si_value(ts("1e-21"), "V"), "+1 zV");
76 BOOST_CHECK_EQUAL(format_si_value(ts("1e-20"), "V"), "+10 zV");
77 BOOST_CHECK_EQUAL(format_si_value(ts("1e-19"), "V"), "+100 zV");
78 BOOST_CHECK_EQUAL(format_si_value(ts("1e-18"), "V"), "+1 aV");
79 BOOST_CHECK_EQUAL(format_si_value(ts("1e-17"), "V"), "+10 aV");
80 BOOST_CHECK_EQUAL(format_si_value(ts("1e-16"), "V"), "+100 aV");
81 BOOST_CHECK_EQUAL(format_si_value(ts("1e-15"), "V"), "+1 fV");
82 BOOST_CHECK_EQUAL(format_si_value(ts("1e-14"), "V"), "+10 fV");
83 BOOST_CHECK_EQUAL(format_si_value(ts("1e-13"), "V"), "+100 fV");
84 BOOST_CHECK_EQUAL(format_si_value(ts("1e-12"), "V"), "+1 pV");
85 BOOST_CHECK_EQUAL(format_si_value(ts("1e-11"), "V"), "+10 pV");
86 BOOST_CHECK_EQUAL(format_si_value(ts("1e-10"), "V"), "+100 pV");
87 BOOST_CHECK_EQUAL(format_si_value(ts("1e-9"), "V"), "+1 nV");
88 BOOST_CHECK_EQUAL(format_si_value(ts("1e-8"), "V"), "+10 nV");
89 BOOST_CHECK_EQUAL(format_si_value(ts("1e-7"), "V"), "+100 nV");
90 BOOST_CHECK_EQUAL(format_si_value(ts("1e-6"), "V"), QString("+1 ") + mu + "V");
91 BOOST_CHECK_EQUAL(format_si_value(ts("1e-5"), "V"), QString("+10 ") + mu + "V");
92 BOOST_CHECK_EQUAL(format_si_value(ts("1e-4"), "V"), QString("+100 ") + mu + "V");
93 BOOST_CHECK_EQUAL(format_si_value(ts("1e-3"), "V"), "+1 mV");
94 BOOST_CHECK_EQUAL(format_si_value(ts("1e-2"), "V"), "+10 mV");
95 BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V"), "+100 mV");
96 BOOST_CHECK_EQUAL(format_si_value(ts("1e0"), "V"), "+1 V");
97 BOOST_CHECK_EQUAL(format_si_value(ts("1e1"), "V"), "+10 V");
98 BOOST_CHECK_EQUAL(format_si_value(ts("1e2"), "V"), "+100 V");
99 BOOST_CHECK_EQUAL(format_si_value(ts("1e3"), "V"), "+1 kV");
100 BOOST_CHECK_EQUAL(format_si_value(ts("1e4"), "V"), "+10 kV");
101 BOOST_CHECK_EQUAL(format_si_value(ts("1e5"), "V"), "+100 kV");
102 BOOST_CHECK_EQUAL(format_si_value(ts("1e6"), "V"), "+1 MV");
103 BOOST_CHECK_EQUAL(format_si_value(ts("1e7"), "V"), "+10 MV");
104 BOOST_CHECK_EQUAL(format_si_value(ts("1e8"), "V"), "+100 MV");
105 BOOST_CHECK_EQUAL(format_si_value(ts("1e9"), "V"), "+1 GV");
106 BOOST_CHECK_EQUAL(format_si_value(ts("1e10"), "V"), "+10 GV");
107 BOOST_CHECK_EQUAL(format_si_value(ts("1e11"), "V"), "+100 GV");
108 BOOST_CHECK_EQUAL(format_si_value(ts("1e12"), "V"), "+1 TV");
109 BOOST_CHECK_EQUAL(format_si_value(ts("1e13"), "V"), "+10 TV");
110 BOOST_CHECK_EQUAL(format_si_value(ts("1e14"), "V"), "+100 TV");
111 BOOST_CHECK_EQUAL(format_si_value(ts("1e15"), "V"), "+1 PV");
112 BOOST_CHECK_EQUAL(format_si_value(ts("1e16"), "V"), "+10 PV");
113 BOOST_CHECK_EQUAL(format_si_value(ts("1e17"), "V"), "+100 PV");
114 BOOST_CHECK_EQUAL(format_si_value(ts("1e18"), "V"), "+1 EV");
115 BOOST_CHECK_EQUAL(format_si_value(ts("1e19"), "V"), "+10 EV");
116 BOOST_CHECK_EQUAL(format_si_value(ts("1e20"), "V"), "+100 EV");
117 BOOST_CHECK_EQUAL(format_si_value(ts("1e21"), "V"), "+1 ZV");
118 BOOST_CHECK_EQUAL(format_si_value(ts("1e22"), "V"), "+10 ZV");
119 BOOST_CHECK_EQUAL(format_si_value(ts("1e23"), "V"), "+100 ZV");
120 BOOST_CHECK_EQUAL(format_si_value(ts("1e24"), "V"), "+1 YV");
121 BOOST_CHECK_EQUAL(format_si_value(ts("1e25"), "V"), "+10 YV");
122 BOOST_CHECK_EQUAL(format_si_value(ts("1e26"), "V"), "+100 YV");
123 BOOST_CHECK_EQUAL(format_si_value(ts("1e27"), "V"), "+1000 YV");
124
125 BOOST_CHECK_EQUAL(format_si_value(ts("1234"), "V"), "+1 kV");
d001f416 126 BOOST_CHECK_EQUAL(format_si_value(ts("1234"), "V", kilo, 3), "+1.234 kV");
f52be90d
JS
127 BOOST_CHECK_EQUAL(format_si_value(ts("1234.5678"), "V"), "+1 kV");
128
129 // check if a given prefix is honored
130
d001f416
JS
131 BOOST_CHECK_EQUAL(format_si_value(ts("1e-24"), "V", yocto), "+1 yV");
132 BOOST_CHECK_EQUAL(format_si_value(ts("1e-21"), "V", yocto), "+1000 yV");
133 BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", yocto), "0 yV");
f52be90d 134
d001f416
JS
135 BOOST_CHECK_EQUAL(format_si_value(ts("1e-4"), "V", milli), "+0 mV");
136 BOOST_CHECK_EQUAL(format_si_value(ts("1e-4"), "V", milli, 1), "+0.1 mV");
137 BOOST_CHECK_EQUAL(format_si_value(ts("1000"), "V", milli), "+1000000 mV");
138 BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", milli), "0 mV");
f52be90d 139
d001f416
JS
140 BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", none), "+0 V");
141 BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", none, 1), "+0.1 V");
142 BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", none, 2), "+0.10 V");
143 BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", none), "+1 V");
144 BOOST_CHECK_EQUAL(format_si_value(ts("1e1"), "V", none), "+10 V");
f52be90d 145
d001f416
JS
146 BOOST_CHECK_EQUAL(format_si_value(ts("1e23"), "V", yotta), "+0 YV");
147 BOOST_CHECK_EQUAL(format_si_value(ts("1e23"), "V", yotta, 1), "+0.1 YV");
148 BOOST_CHECK_EQUAL(format_si_value(ts("1e27"), "V", yotta), "+1000 YV");
149 BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", yotta), "0 YV");
f52be90d
JS
150
151 // check precision
152
153 BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V"), "+1 V");
154 BOOST_CHECK_EQUAL(format_si_value(ts("1.4"), "V"), "+1 V");
155 BOOST_CHECK_EQUAL(format_si_value(ts("1.5"), "V"), "+2 V");
156 BOOST_CHECK_EQUAL(format_si_value(ts("1.9"), "V"), "+2 V");
d001f416
JS
157 BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", unspecified, 2), "+1.23 V");
158 BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", unspecified, 3), "+1.235 V");
159 BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", milli, 3), "+1234.568 mV");
160 BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", milli, 0), "+1235 mV");
161 BOOST_CHECK_EQUAL(format_si_value(ts("1.2"), "V", unspecified, 3), "+1.200 V");
f52be90d
JS
162
163 // check sign
164
d001f416
JS
165 BOOST_CHECK_EQUAL(format_si_value(ts("-1"), "V", none, 0, true), "-1 V");
166 BOOST_CHECK_EQUAL(format_si_value(ts("-1"), "V", none, 0, false), "-1 V");
167 BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", none, 0, true), "+1 V");
168 BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", none, 0, false), "1 V");
f52be90d
JS
169}
170
af95045e
JS
171BOOST_AUTO_TEST_CASE(format_time_test)
172{
d001f416
JS
173 BOOST_CHECK_EQUAL(format_time(ts("-0.00005"), micro, Time, 5), QString("-50 ") + mu + "s");
174 BOOST_CHECK_EQUAL(format_time(ts( "0.00005"), micro, Time, 5), QString("+50 ") + mu + "s");
af95045e
JS
175 BOOST_CHECK_EQUAL(format_time(ts( "1")), "+1 s");
176 BOOST_CHECK_EQUAL(format_time(ts("-1")), "-1 s");
d001f416
JS
177 BOOST_CHECK_EQUAL(format_time(ts( "100")), "+1:40");
178 BOOST_CHECK_EQUAL(format_time(ts("-100")), "-1:40");
179 BOOST_CHECK_EQUAL(format_time(ts( "4000")), "+1:06:40");
180 BOOST_CHECK_EQUAL(format_time(ts("-4000")), "-1:06:40");
181 BOOST_CHECK_EQUAL(format_time(ts("12000"), kilo, Time, 0), "+3:20:00");
182 BOOST_CHECK_EQUAL(format_time(ts("15000"), kilo, Time, 0), "+4:10:00");
183 BOOST_CHECK_EQUAL(format_time(ts("20000"), kilo, Time, 0), "+5:33:20");
184 BOOST_CHECK_EQUAL(format_time(ts("25000"), kilo, Time, 0), "+6:56:40");
185
186 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 0), "+123:04:05:06");
187 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 1), "+123:04:05:06.0");
188 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 2), "+123:04:05:06.00");
189 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 3), "+123:04:05:06.007");
190 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 4), "+123:04:05:06.007 0");
191 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 5), "+123:04:05:06.007 00");
192 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 6), "+123:04:05:06.007 008");
193 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 7), "+123:04:05:06.007 008 0");
194 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 8), "+123:04:05:06.007 008 00");
195 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 9), "+123:04:05:06.007 008 009");
196
197 BOOST_CHECK_EQUAL(format_time(ts("-1.5"), milli), "-1500 ms");
198 BOOST_CHECK_EQUAL(format_time(ts("-1.0"), milli), "-1000 ms");
199 BOOST_CHECK_EQUAL(format_time(ts("-0.2")), "-200 ms");
200 BOOST_CHECK_EQUAL(format_time(ts("-0.1")), "-100 ms");
201 BOOST_CHECK_EQUAL(format_time(ts("0.0")), "0");
202 BOOST_CHECK_EQUAL(format_time(ts("0.1")), "+100 ms");
203 BOOST_CHECK_EQUAL(format_time(ts("0.2")), "+200 ms");
204 BOOST_CHECK_EQUAL(format_time(ts("0.3")), "+300 ms");
205 BOOST_CHECK_EQUAL(format_time(ts("0.4")), "+400 ms");
206 BOOST_CHECK_EQUAL(format_time(ts("0.5")), "+500 ms");
207 BOOST_CHECK_EQUAL(format_time(ts("0.6")), "+600 ms");
208 BOOST_CHECK_EQUAL(format_time(ts("0.7")), "+700 ms");
209 BOOST_CHECK_EQUAL(format_time(ts("0.8")), "+800 ms");
210 BOOST_CHECK_EQUAL(format_time(ts("0.9")), "+900 ms");
211 BOOST_CHECK_EQUAL(format_time(ts("1.0"), milli), "+1000 ms");
212 BOOST_CHECK_EQUAL(format_time(ts("1.1"), milli), "+1100 ms");
213 BOOST_CHECK_EQUAL(format_time(ts("1.2"), milli), "+1200 ms");
214 BOOST_CHECK_EQUAL(format_time(ts("1.3"), milli), "+1300 ms");
215 BOOST_CHECK_EQUAL(format_time(ts("1.4"), milli), "+1400 ms");
216 BOOST_CHECK_EQUAL(format_time(ts("1.5"), milli), "+1500 ms");
af95045e
JS
217}
218
f52be90d 219BOOST_AUTO_TEST_SUITE_END()