]> sigrok.org Git - pulseview.git/blame - test/util.cpp
Ruler: Fix tick mark calculation
[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"
24
25using namespace pv::util;
26using ts = pv::util::Timestamp;
27
28namespace {
29 QChar mu = QChar(0x03BC);
d001f416
JS
30
31 pv::util::SIPrefix unspecified = pv::util::SIPrefix::unspecified;
32 pv::util::SIPrefix yocto = pv::util::SIPrefix::yocto;
33 pv::util::SIPrefix micro = pv::util::SIPrefix::micro;
34 pv::util::SIPrefix milli = pv::util::SIPrefix::milli;
35 pv::util::SIPrefix none = pv::util::SIPrefix::none;
36 pv::util::SIPrefix kilo = pv::util::SIPrefix::kilo;
37 pv::util::SIPrefix yotta = pv::util::SIPrefix::yotta;
38
39 pv::util::TimeUnit Time = pv::util::TimeUnit::Time;
f52be90d
JS
40}
41
42std::ostream& operator<<(std::ostream& stream, const QString& str)
43{
44 return stream << str.toUtf8().data();
45}
46
47BOOST_AUTO_TEST_SUITE(UtilTest)
48
d001f416
JS
49BOOST_AUTO_TEST_CASE(exponent_test)
50{
51 BOOST_CHECK_EQUAL(exponent(SIPrefix::yocto), -24);
52 BOOST_CHECK_EQUAL(exponent(SIPrefix::zepto), -21);
53 BOOST_CHECK_EQUAL(exponent(SIPrefix::atto), -18);
54 BOOST_CHECK_EQUAL(exponent(SIPrefix::femto), -15);
55 BOOST_CHECK_EQUAL(exponent(SIPrefix::pico), -12);
56 BOOST_CHECK_EQUAL(exponent(SIPrefix::nano), -9);
57 BOOST_CHECK_EQUAL(exponent(SIPrefix::micro), -6);
58 BOOST_CHECK_EQUAL(exponent(SIPrefix::milli), -3);
59 BOOST_CHECK_EQUAL(exponent(SIPrefix::none), 0);
60 BOOST_CHECK_EQUAL(exponent(SIPrefix::kilo), 3);
61 BOOST_CHECK_EQUAL(exponent(SIPrefix::mega), 6);
62 BOOST_CHECK_EQUAL(exponent(SIPrefix::giga), 9);
63 BOOST_CHECK_EQUAL(exponent(SIPrefix::tera), 12);
64 BOOST_CHECK_EQUAL(exponent(SIPrefix::peta), 15);
65 BOOST_CHECK_EQUAL(exponent(SIPrefix::exa), 18);
66 BOOST_CHECK_EQUAL(exponent(SIPrefix::zetta), 21);
67 BOOST_CHECK_EQUAL(exponent(SIPrefix::yotta), 24);
68}
69
f52be90d
JS
70BOOST_AUTO_TEST_CASE(format_si_value_test)
71{
72 // check prefix calculation
73
74 BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V"), "0 V");
75
76 BOOST_CHECK_EQUAL(format_si_value(ts("1e-24"), "V"), "+1 yV");
77 BOOST_CHECK_EQUAL(format_si_value(ts("1e-23"), "V"), "+10 yV");
78 BOOST_CHECK_EQUAL(format_si_value(ts("1e-22"), "V"), "+100 yV");
79 BOOST_CHECK_EQUAL(format_si_value(ts("1e-21"), "V"), "+1 zV");
80 BOOST_CHECK_EQUAL(format_si_value(ts("1e-20"), "V"), "+10 zV");
81 BOOST_CHECK_EQUAL(format_si_value(ts("1e-19"), "V"), "+100 zV");
82 BOOST_CHECK_EQUAL(format_si_value(ts("1e-18"), "V"), "+1 aV");
83 BOOST_CHECK_EQUAL(format_si_value(ts("1e-17"), "V"), "+10 aV");
84 BOOST_CHECK_EQUAL(format_si_value(ts("1e-16"), "V"), "+100 aV");
85 BOOST_CHECK_EQUAL(format_si_value(ts("1e-15"), "V"), "+1 fV");
86 BOOST_CHECK_EQUAL(format_si_value(ts("1e-14"), "V"), "+10 fV");
87 BOOST_CHECK_EQUAL(format_si_value(ts("1e-13"), "V"), "+100 fV");
88 BOOST_CHECK_EQUAL(format_si_value(ts("1e-12"), "V"), "+1 pV");
89 BOOST_CHECK_EQUAL(format_si_value(ts("1e-11"), "V"), "+10 pV");
90 BOOST_CHECK_EQUAL(format_si_value(ts("1e-10"), "V"), "+100 pV");
91 BOOST_CHECK_EQUAL(format_si_value(ts("1e-9"), "V"), "+1 nV");
92 BOOST_CHECK_EQUAL(format_si_value(ts("1e-8"), "V"), "+10 nV");
93 BOOST_CHECK_EQUAL(format_si_value(ts("1e-7"), "V"), "+100 nV");
94 BOOST_CHECK_EQUAL(format_si_value(ts("1e-6"), "V"), QString("+1 ") + mu + "V");
95 BOOST_CHECK_EQUAL(format_si_value(ts("1e-5"), "V"), QString("+10 ") + mu + "V");
96 BOOST_CHECK_EQUAL(format_si_value(ts("1e-4"), "V"), QString("+100 ") + mu + "V");
97 BOOST_CHECK_EQUAL(format_si_value(ts("1e-3"), "V"), "+1 mV");
98 BOOST_CHECK_EQUAL(format_si_value(ts("1e-2"), "V"), "+10 mV");
99 BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V"), "+100 mV");
100 BOOST_CHECK_EQUAL(format_si_value(ts("1e0"), "V"), "+1 V");
101 BOOST_CHECK_EQUAL(format_si_value(ts("1e1"), "V"), "+10 V");
102 BOOST_CHECK_EQUAL(format_si_value(ts("1e2"), "V"), "+100 V");
103 BOOST_CHECK_EQUAL(format_si_value(ts("1e3"), "V"), "+1 kV");
104 BOOST_CHECK_EQUAL(format_si_value(ts("1e4"), "V"), "+10 kV");
105 BOOST_CHECK_EQUAL(format_si_value(ts("1e5"), "V"), "+100 kV");
106 BOOST_CHECK_EQUAL(format_si_value(ts("1e6"), "V"), "+1 MV");
107 BOOST_CHECK_EQUAL(format_si_value(ts("1e7"), "V"), "+10 MV");
108 BOOST_CHECK_EQUAL(format_si_value(ts("1e8"), "V"), "+100 MV");
109 BOOST_CHECK_EQUAL(format_si_value(ts("1e9"), "V"), "+1 GV");
110 BOOST_CHECK_EQUAL(format_si_value(ts("1e10"), "V"), "+10 GV");
111 BOOST_CHECK_EQUAL(format_si_value(ts("1e11"), "V"), "+100 GV");
112 BOOST_CHECK_EQUAL(format_si_value(ts("1e12"), "V"), "+1 TV");
113 BOOST_CHECK_EQUAL(format_si_value(ts("1e13"), "V"), "+10 TV");
114 BOOST_CHECK_EQUAL(format_si_value(ts("1e14"), "V"), "+100 TV");
115 BOOST_CHECK_EQUAL(format_si_value(ts("1e15"), "V"), "+1 PV");
116 BOOST_CHECK_EQUAL(format_si_value(ts("1e16"), "V"), "+10 PV");
117 BOOST_CHECK_EQUAL(format_si_value(ts("1e17"), "V"), "+100 PV");
118 BOOST_CHECK_EQUAL(format_si_value(ts("1e18"), "V"), "+1 EV");
119 BOOST_CHECK_EQUAL(format_si_value(ts("1e19"), "V"), "+10 EV");
120 BOOST_CHECK_EQUAL(format_si_value(ts("1e20"), "V"), "+100 EV");
121 BOOST_CHECK_EQUAL(format_si_value(ts("1e21"), "V"), "+1 ZV");
122 BOOST_CHECK_EQUAL(format_si_value(ts("1e22"), "V"), "+10 ZV");
123 BOOST_CHECK_EQUAL(format_si_value(ts("1e23"), "V"), "+100 ZV");
124 BOOST_CHECK_EQUAL(format_si_value(ts("1e24"), "V"), "+1 YV");
125 BOOST_CHECK_EQUAL(format_si_value(ts("1e25"), "V"), "+10 YV");
126 BOOST_CHECK_EQUAL(format_si_value(ts("1e26"), "V"), "+100 YV");
127 BOOST_CHECK_EQUAL(format_si_value(ts("1e27"), "V"), "+1000 YV");
128
129 BOOST_CHECK_EQUAL(format_si_value(ts("1234"), "V"), "+1 kV");
d001f416 130 BOOST_CHECK_EQUAL(format_si_value(ts("1234"), "V", kilo, 3), "+1.234 kV");
f52be90d
JS
131 BOOST_CHECK_EQUAL(format_si_value(ts("1234.5678"), "V"), "+1 kV");
132
133 // check if a given prefix is honored
134
d001f416
JS
135 BOOST_CHECK_EQUAL(format_si_value(ts("1e-24"), "V", yocto), "+1 yV");
136 BOOST_CHECK_EQUAL(format_si_value(ts("1e-21"), "V", yocto), "+1000 yV");
137 BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", yocto), "0 yV");
f52be90d 138
d001f416
JS
139 BOOST_CHECK_EQUAL(format_si_value(ts("1e-4"), "V", milli), "+0 mV");
140 BOOST_CHECK_EQUAL(format_si_value(ts("1e-4"), "V", milli, 1), "+0.1 mV");
141 BOOST_CHECK_EQUAL(format_si_value(ts("1000"), "V", milli), "+1000000 mV");
142 BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", milli), "0 mV");
f52be90d 143
d001f416
JS
144 BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", none), "+0 V");
145 BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", none, 1), "+0.1 V");
146 BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", none, 2), "+0.10 V");
147 BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", none), "+1 V");
148 BOOST_CHECK_EQUAL(format_si_value(ts("1e1"), "V", none), "+10 V");
f52be90d 149
d001f416
JS
150 BOOST_CHECK_EQUAL(format_si_value(ts("1e23"), "V", yotta), "+0 YV");
151 BOOST_CHECK_EQUAL(format_si_value(ts("1e23"), "V", yotta, 1), "+0.1 YV");
152 BOOST_CHECK_EQUAL(format_si_value(ts("1e27"), "V", yotta), "+1000 YV");
153 BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", yotta), "0 YV");
f52be90d
JS
154
155 // check precision
156
157 BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V"), "+1 V");
158 BOOST_CHECK_EQUAL(format_si_value(ts("1.4"), "V"), "+1 V");
159 BOOST_CHECK_EQUAL(format_si_value(ts("1.5"), "V"), "+2 V");
160 BOOST_CHECK_EQUAL(format_si_value(ts("1.9"), "V"), "+2 V");
d001f416
JS
161 BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", unspecified, 2), "+1.23 V");
162 BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", unspecified, 3), "+1.235 V");
163 BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", milli, 3), "+1234.568 mV");
164 BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", milli, 0), "+1235 mV");
165 BOOST_CHECK_EQUAL(format_si_value(ts("1.2"), "V", unspecified, 3), "+1.200 V");
f52be90d
JS
166
167 // check sign
168
d001f416
JS
169 BOOST_CHECK_EQUAL(format_si_value(ts("-1"), "V", none, 0, true), "-1 V");
170 BOOST_CHECK_EQUAL(format_si_value(ts("-1"), "V", none, 0, false), "-1 V");
171 BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", none, 0, true), "+1 V");
172 BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", none, 0, false), "1 V");
f52be90d
JS
173}
174
af95045e
JS
175BOOST_AUTO_TEST_CASE(format_time_test)
176{
d001f416
JS
177 BOOST_CHECK_EQUAL(format_time(ts("-0.00005"), micro, Time, 5), QString("-50 ") + mu + "s");
178 BOOST_CHECK_EQUAL(format_time(ts( "0.00005"), micro, Time, 5), QString("+50 ") + mu + "s");
af95045e
JS
179 BOOST_CHECK_EQUAL(format_time(ts( "1")), "+1 s");
180 BOOST_CHECK_EQUAL(format_time(ts("-1")), "-1 s");
d001f416
JS
181 BOOST_CHECK_EQUAL(format_time(ts( "100")), "+1:40");
182 BOOST_CHECK_EQUAL(format_time(ts("-100")), "-1:40");
183 BOOST_CHECK_EQUAL(format_time(ts( "4000")), "+1:06:40");
184 BOOST_CHECK_EQUAL(format_time(ts("-4000")), "-1:06:40");
185 BOOST_CHECK_EQUAL(format_time(ts("12000"), kilo, Time, 0), "+3:20:00");
186 BOOST_CHECK_EQUAL(format_time(ts("15000"), kilo, Time, 0), "+4:10:00");
187 BOOST_CHECK_EQUAL(format_time(ts("20000"), kilo, Time, 0), "+5:33:20");
188 BOOST_CHECK_EQUAL(format_time(ts("25000"), kilo, Time, 0), "+6:56:40");
189
190 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 0), "+123:04:05:06");
191 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 1), "+123:04:05:06.0");
192 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 2), "+123:04:05:06.00");
193 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 3), "+123:04:05:06.007");
194 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 4), "+123:04:05:06.007 0");
195 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 5), "+123:04:05:06.007 00");
196 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 6), "+123:04:05:06.007 008");
197 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 7), "+123:04:05:06.007 008 0");
198 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 8), "+123:04:05:06.007 008 00");
199 BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 9), "+123:04:05:06.007 008 009");
200
201 BOOST_CHECK_EQUAL(format_time(ts("-1.5"), milli), "-1500 ms");
202 BOOST_CHECK_EQUAL(format_time(ts("-1.0"), milli), "-1000 ms");
203 BOOST_CHECK_EQUAL(format_time(ts("-0.2")), "-200 ms");
204 BOOST_CHECK_EQUAL(format_time(ts("-0.1")), "-100 ms");
205 BOOST_CHECK_EQUAL(format_time(ts("0.0")), "0");
206 BOOST_CHECK_EQUAL(format_time(ts("0.1")), "+100 ms");
207 BOOST_CHECK_EQUAL(format_time(ts("0.2")), "+200 ms");
208 BOOST_CHECK_EQUAL(format_time(ts("0.3")), "+300 ms");
209 BOOST_CHECK_EQUAL(format_time(ts("0.4")), "+400 ms");
210 BOOST_CHECK_EQUAL(format_time(ts("0.5")), "+500 ms");
211 BOOST_CHECK_EQUAL(format_time(ts("0.6")), "+600 ms");
212 BOOST_CHECK_EQUAL(format_time(ts("0.7")), "+700 ms");
213 BOOST_CHECK_EQUAL(format_time(ts("0.8")), "+800 ms");
214 BOOST_CHECK_EQUAL(format_time(ts("0.9")), "+900 ms");
215 BOOST_CHECK_EQUAL(format_time(ts("1.0"), milli), "+1000 ms");
216 BOOST_CHECK_EQUAL(format_time(ts("1.1"), milli), "+1100 ms");
217 BOOST_CHECK_EQUAL(format_time(ts("1.2"), milli), "+1200 ms");
218 BOOST_CHECK_EQUAL(format_time(ts("1.3"), milli), "+1300 ms");
219 BOOST_CHECK_EQUAL(format_time(ts("1.4"), milli), "+1400 ms");
220 BOOST_CHECK_EQUAL(format_time(ts("1.5"), milli), "+1500 ms");
af95045e
JS
221}
222
f52be90d 223BOOST_AUTO_TEST_SUITE_END()