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