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