]> sigrok.org Git - pulseview.git/blame - test/util.cpp
DecodeSignal: Reset and re-use existing decoder sessions
[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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
f52be90d
JS
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include "pv/util.hpp"
c677193d 23#include "test/test.hpp"
f52be90d
JS
24
25using namespace pv::util;
26using ts = pv::util::Timestamp;
27
6f925ba9
UH
28using std::bind;
29
f52be90d
JS
30namespace {
31 QChar mu = QChar(0x03BC);
d001f416
JS
32
33 pv::util::SIPrefix unspecified = pv::util::SIPrefix::unspecified;
34 pv::util::SIPrefix yocto = pv::util::SIPrefix::yocto;
3ccf0f7f 35 pv::util::SIPrefix nano = pv::util::SIPrefix::nano;
5d6ae8a2 36/* pv::util::SIPrefix micro = pv::util::SIPrefix::micro; // Not currently used */
d001f416
JS
37 pv::util::SIPrefix milli = pv::util::SIPrefix::milli;
38 pv::util::SIPrefix none = pv::util::SIPrefix::none;
39 pv::util::SIPrefix kilo = pv::util::SIPrefix::kilo;
40 pv::util::SIPrefix yotta = pv::util::SIPrefix::yotta;
41
5d6ae8a2 42/* pv::util::TimeUnit Time = pv::util::TimeUnit::Time; // Not currently used */
870ea3db 43} // namespace
f52be90d 44
f52be90d
JS
45BOOST_AUTO_TEST_SUITE(UtilTest)
46
d001f416
JS
47BOOST_AUTO_TEST_CASE(exponent_test)
48{
49 BOOST_CHECK_EQUAL(exponent(SIPrefix::yocto), -24);
50 BOOST_CHECK_EQUAL(exponent(SIPrefix::zepto), -21);
51 BOOST_CHECK_EQUAL(exponent(SIPrefix::atto), -18);
52 BOOST_CHECK_EQUAL(exponent(SIPrefix::femto), -15);
53 BOOST_CHECK_EQUAL(exponent(SIPrefix::pico), -12);
54 BOOST_CHECK_EQUAL(exponent(SIPrefix::nano), -9);
55 BOOST_CHECK_EQUAL(exponent(SIPrefix::micro), -6);
56 BOOST_CHECK_EQUAL(exponent(SIPrefix::milli), -3);
57 BOOST_CHECK_EQUAL(exponent(SIPrefix::none), 0);
58 BOOST_CHECK_EQUAL(exponent(SIPrefix::kilo), 3);
59 BOOST_CHECK_EQUAL(exponent(SIPrefix::mega), 6);
60 BOOST_CHECK_EQUAL(exponent(SIPrefix::giga), 9);
61 BOOST_CHECK_EQUAL(exponent(SIPrefix::tera), 12);
62 BOOST_CHECK_EQUAL(exponent(SIPrefix::peta), 15);
63 BOOST_CHECK_EQUAL(exponent(SIPrefix::exa), 18);
64 BOOST_CHECK_EQUAL(exponent(SIPrefix::zetta), 21);
65 BOOST_CHECK_EQUAL(exponent(SIPrefix::yotta), 24);
66}
67
3ccf0f7f 68BOOST_AUTO_TEST_CASE(format_time_si_test)
f52be90d
JS
69{
70 // check prefix calculation
71
3ccf0f7f
JS
72 BOOST_CHECK_EQUAL(format_time_si(ts("0")), "0 s");
73
74 BOOST_CHECK_EQUAL(format_time_si(ts("1e-24")), "+1 ys");
75 BOOST_CHECK_EQUAL(format_time_si(ts("1e-23")), "+10 ys");
76 BOOST_CHECK_EQUAL(format_time_si(ts("1e-22")), "+100 ys");
77 BOOST_CHECK_EQUAL(format_time_si(ts("1e-21")), "+1 zs");
78 BOOST_CHECK_EQUAL(format_time_si(ts("1e-20")), "+10 zs");
79 BOOST_CHECK_EQUAL(format_time_si(ts("1e-19")), "+100 zs");
80 BOOST_CHECK_EQUAL(format_time_si(ts("1e-18")), "+1 as");
81 BOOST_CHECK_EQUAL(format_time_si(ts("1e-17")), "+10 as");
82 BOOST_CHECK_EQUAL(format_time_si(ts("1e-16")), "+100 as");
83 BOOST_CHECK_EQUAL(format_time_si(ts("1e-15")), "+1 fs");
84 BOOST_CHECK_EQUAL(format_time_si(ts("1e-14")), "+10 fs");
85 BOOST_CHECK_EQUAL(format_time_si(ts("1e-13")), "+100 fs");
86 BOOST_CHECK_EQUAL(format_time_si(ts("1e-12")), "+1 ps");
87 BOOST_CHECK_EQUAL(format_time_si(ts("1e-11")), "+10 ps");
88 BOOST_CHECK_EQUAL(format_time_si(ts("1e-10")), "+100 ps");
89 BOOST_CHECK_EQUAL(format_time_si(ts("1e-9")), "+1 ns");
90 BOOST_CHECK_EQUAL(format_time_si(ts("1e-8")), "+10 ns");
91 BOOST_CHECK_EQUAL(format_time_si(ts("1e-7")), "+100 ns");
92 BOOST_CHECK_EQUAL(format_time_si(ts("1e-6")), QString("+1 ") + mu + "s");
93 BOOST_CHECK_EQUAL(format_time_si(ts("1e-5")), QString("+10 ") + mu + "s");
94 BOOST_CHECK_EQUAL(format_time_si(ts("1e-4")), QString("+100 ") + mu + "s");
95 BOOST_CHECK_EQUAL(format_time_si(ts("1e-3")), "+1 ms");
96 BOOST_CHECK_EQUAL(format_time_si(ts("1e-2")), "+10 ms");
97 BOOST_CHECK_EQUAL(format_time_si(ts("1e-1")), "+100 ms");
98 BOOST_CHECK_EQUAL(format_time_si(ts("1e0")), "+1 s");
99 BOOST_CHECK_EQUAL(format_time_si(ts("1e1")), "+10 s");
100 BOOST_CHECK_EQUAL(format_time_si(ts("1e2")), "+100 s");
101 BOOST_CHECK_EQUAL(format_time_si(ts("1e3")), "+1 ks");
102 BOOST_CHECK_EQUAL(format_time_si(ts("1e4")), "+10 ks");
103 BOOST_CHECK_EQUAL(format_time_si(ts("1e5")), "+100 ks");
104 BOOST_CHECK_EQUAL(format_time_si(ts("1e6")), "+1 Ms");
105 BOOST_CHECK_EQUAL(format_time_si(ts("1e7")), "+10 Ms");
106 BOOST_CHECK_EQUAL(format_time_si(ts("1e8")), "+100 Ms");
107 BOOST_CHECK_EQUAL(format_time_si(ts("1e9")), "+1 Gs");
108 BOOST_CHECK_EQUAL(format_time_si(ts("1e10")), "+10 Gs");
109 BOOST_CHECK_EQUAL(format_time_si(ts("1e11")), "+100 Gs");
110 BOOST_CHECK_EQUAL(format_time_si(ts("1e12")), "+1 Ts");
111 BOOST_CHECK_EQUAL(format_time_si(ts("1e13")), "+10 Ts");
112 BOOST_CHECK_EQUAL(format_time_si(ts("1e14")), "+100 Ts");
113 BOOST_CHECK_EQUAL(format_time_si(ts("1e15")), "+1 Ps");
114 BOOST_CHECK_EQUAL(format_time_si(ts("1e16")), "+10 Ps");
115 BOOST_CHECK_EQUAL(format_time_si(ts("1e17")), "+100 Ps");
116 BOOST_CHECK_EQUAL(format_time_si(ts("1e18")), "+1 Es");
117 BOOST_CHECK_EQUAL(format_time_si(ts("1e19")), "+10 Es");
118 BOOST_CHECK_EQUAL(format_time_si(ts("1e20")), "+100 Es");
119 BOOST_CHECK_EQUAL(format_time_si(ts("1e21")), "+1 Zs");
120 BOOST_CHECK_EQUAL(format_time_si(ts("1e22")), "+10 Zs");
121 BOOST_CHECK_EQUAL(format_time_si(ts("1e23")), "+100 Zs");
122 BOOST_CHECK_EQUAL(format_time_si(ts("1e24")), "+1 Ys");
123 BOOST_CHECK_EQUAL(format_time_si(ts("1e25")), "+10 Ys");
124 BOOST_CHECK_EQUAL(format_time_si(ts("1e26")), "+100 Ys");
125 BOOST_CHECK_EQUAL(format_time_si(ts("1e27")), "+1000 Ys");
126
127 BOOST_CHECK_EQUAL(format_time_si(ts("1234")), "+1 ks");
128 BOOST_CHECK_EQUAL(format_time_si(ts("1234"), kilo, 3), "+1.234 ks");
129 BOOST_CHECK_EQUAL(format_time_si(ts("1234.5678")), "+1 ks");
130
131 // check prefix
132
133 BOOST_CHECK_EQUAL(format_time_si(ts("1e-24"), yocto), "+1 ys");
134 BOOST_CHECK_EQUAL(format_time_si(ts("1e-21"), yocto), "+1000 ys");
135 BOOST_CHECK_EQUAL(format_time_si(ts("0"), yocto), "0 ys");
136
137 BOOST_CHECK_EQUAL(format_time_si(ts("1e-4"), milli), "+0 ms");
138 BOOST_CHECK_EQUAL(format_time_si(ts("1e-4"), milli, 1), "+0.1 ms");
139 BOOST_CHECK_EQUAL(format_time_si(ts("1000"), milli), "+1000000 ms");
140 BOOST_CHECK_EQUAL(format_time_si(ts("0"), milli), "0 ms");
141
142 BOOST_CHECK_EQUAL(format_time_si(ts("1e-1"), none), "+0 s");
143 BOOST_CHECK_EQUAL(format_time_si(ts("1e-1"), none, 1), "+0.1 s");
144 BOOST_CHECK_EQUAL(format_time_si(ts("1e-1"), none, 2), "+0.10 s");
145 BOOST_CHECK_EQUAL(format_time_si(ts("1"), none), "+1 s");
146 BOOST_CHECK_EQUAL(format_time_si(ts("1e1"), none), "+10 s");
147
148 BOOST_CHECK_EQUAL(format_time_si(ts("1e23"), yotta), "+0 Ys");
149 BOOST_CHECK_EQUAL(format_time_si(ts("1e23"), yotta, 1), "+0.1 Ys");
150 BOOST_CHECK_EQUAL(format_time_si(ts("1e27"), yotta), "+1000 Ys");
151 BOOST_CHECK_EQUAL(format_time_si(ts("0"), yotta), "0 Ys");
152
153 // check precision, rounding
154
155 BOOST_CHECK_EQUAL(format_time_si(ts("1.2345678")), "+1 s");
156 BOOST_CHECK_EQUAL(format_time_si(ts("1.4")), "+1 s");
157 BOOST_CHECK_EQUAL(format_time_si(ts("1.5")), "+2 s");
158 BOOST_CHECK_EQUAL(format_time_si(ts("1.9")), "+2 s");
159 BOOST_CHECK_EQUAL(format_time_si(ts("1.2345678"), unspecified, 2), "+1.23 s");
160 BOOST_CHECK_EQUAL(format_time_si(ts("1.2345678"), unspecified, 3), "+1.235 s");
161 BOOST_CHECK_EQUAL(format_time_si(ts("1.2345678"), milli, 3), "+1234.568 ms");
162 BOOST_CHECK_EQUAL(format_time_si(ts("1.2345678"), milli, 0), "+1235 ms");
163 BOOST_CHECK_EQUAL(format_time_si(ts("1.2"), unspecified, 3), "+1.200 s");
164
165 // check unit and sign
166
167 BOOST_CHECK_EQUAL(format_time_si(ts("-1"), none, 0, "V", true), "-1 V");
168 BOOST_CHECK_EQUAL(format_time_si(ts("-1"), none, 0, "V", false), "-1 V");
169 BOOST_CHECK_EQUAL(format_time_si(ts("1"), none, 0, "V", true), "+1 V");
170 BOOST_CHECK_EQUAL(format_time_si(ts("1"), none, 0, "V", false), "1 V");
f52be90d
JS
171}
172
3ccf0f7f 173BOOST_AUTO_TEST_CASE(format_time_si_adjusted_test)
af95045e 174{
3ccf0f7f
JS
175 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts("-1.5"), milli), "-1500 ms");
176 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts("-1.0"), milli), "-1000 ms");
177 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts("-0.2"), milli), "-200 ms");
178 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts("-0.1"), milli), "-100 ms");
179 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "0.0"), milli), "0 ms");
180 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "0.1"), milli), "+100 ms");
181 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "0.2"), milli), "+200 ms");
182 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "0.3"), milli), "+300 ms");
183 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "0.4"), milli), "+400 ms");
184 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "0.5"), milli), "+500 ms");
185 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "0.6"), milli), "+600 ms");
186 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "0.7"), milli), "+700 ms");
187 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "0.8"), milli), "+800 ms");
188 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "0.9"), milli), "+900 ms");
189 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.0"), milli), "+1000 ms");
190 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.1"), milli), "+1100 ms");
191 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.2"), milli), "+1200 ms");
192 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.3"), milli), "+1300 ms");
193 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.4"), milli), "+1400 ms");
194 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.5"), milli), "+1500 ms");
195
196 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.5"), milli, 6), "+1500.000 ms");
197 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.5"), nano, 6), "+1500000000 ns");
198 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.5"), nano, 8), "+1500000000 ns");
199 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.5"), nano, 9), "+1500000000 ns");
200 BOOST_CHECK_EQUAL(format_time_si_adjusted(ts( "1.5"), nano, 10), "+1500000000.0 ns");
201}
202
203BOOST_AUTO_TEST_CASE(format_time_minutes_test)
204{
205 using namespace std::placeholders;
206
6f925ba9 207 auto fmt = bind(format_time_minutes, _1, _2, true);
3ccf0f7f
JS
208
209 BOOST_CHECK_EQUAL(fmt(ts( 0), 0), "+0:00");
210 BOOST_CHECK_EQUAL(fmt(ts( 1), 0), "+0:01");
211 BOOST_CHECK_EQUAL(fmt(ts( 59), 0), "+0:59");
212 BOOST_CHECK_EQUAL(fmt(ts( 60), 0), "+1:00");
213 BOOST_CHECK_EQUAL(fmt(ts( -1), 0), "-0:01");
214 BOOST_CHECK_EQUAL(fmt(ts( -59), 0), "-0:59");
215 BOOST_CHECK_EQUAL(fmt(ts( -60), 0), "-1:00");
216 BOOST_CHECK_EQUAL(fmt(ts( 100), 0), "+1:40");
217 BOOST_CHECK_EQUAL(fmt(ts( -100), 0), "-1:40");
218 BOOST_CHECK_EQUAL(fmt(ts( 4000), 0), "+1:06:40");
219 BOOST_CHECK_EQUAL(fmt(ts(-4000), 0), "-1:06:40");
220 BOOST_CHECK_EQUAL(fmt(ts(12000), 0), "+3:20:00");
221 BOOST_CHECK_EQUAL(fmt(ts(15000), 0), "+4:10:00");
222 BOOST_CHECK_EQUAL(fmt(ts(20000), 0), "+5:33:20");
223 BOOST_CHECK_EQUAL(fmt(ts(25000), 0), "+6:56:40");
224
225 BOOST_CHECK_EQUAL(fmt(ts("10641906.007008009"), 0), "+123:04:05:06");
226 BOOST_CHECK_EQUAL(fmt(ts("10641906.007008009"), 1), "+123:04:05:06.0");
227 BOOST_CHECK_EQUAL(fmt(ts("10641906.007008009"), 2), "+123:04:05:06.01");
228 BOOST_CHECK_EQUAL(fmt(ts("10641906.007008009"), 3), "+123:04:05:06.007");
229 BOOST_CHECK_EQUAL(fmt(ts("10641906.007008009"), 4), "+123:04:05:06.007 0");
230 BOOST_CHECK_EQUAL(fmt(ts("10641906.007008009"), 5), "+123:04:05:06.007 01");
231 BOOST_CHECK_EQUAL(fmt(ts("10641906.007008009"), 6), "+123:04:05:06.007 008");
232 BOOST_CHECK_EQUAL(fmt(ts("10641906.007008009"), 7), "+123:04:05:06.007 008 0");
233 BOOST_CHECK_EQUAL(fmt(ts("10641906.007008009"), 8), "+123:04:05:06.007 008 01");
234 BOOST_CHECK_EQUAL(fmt(ts("10641906.007008009"), 9), "+123:04:05:06.007 008 009");
235
236 BOOST_CHECK_EQUAL(format_time_minutes(ts( 0), 0, false), "0:00");
237 BOOST_CHECK_EQUAL(format_time_minutes(ts( 100), 0, false), "1:40");
238 BOOST_CHECK_EQUAL(format_time_minutes(ts(-100), 0, false), "-1:40");
af95045e
JS
239}
240
f52be90d 241BOOST_AUTO_TEST_SUITE_END()