]> sigrok.org Git - pulseview.git/blob - test/view/ruler.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / test / view / ruler.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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <boost/version.hpp>
21 #if BOOST_VERSION >= 107100 // 1.71 deprecated the old header location.
22 #include <boost/test/tools/floating_point_comparison.hpp>
23 #else
24 #include <boost/test/floating_point_comparison.hpp>
25 #endif
26 #include <boost/test/unit_test.hpp>
27
28 #include "pv/views/trace/ruler.hpp"
29 #include "test/test.hpp"
30
31 using namespace pv::views::trace;
32
33 namespace {
34         QString format(const pv::util::Timestamp& t)
35         {
36                 return pv::util::format_time_si(t, pv::util::SIPrefix::none, 6);
37         }
38
39         const double e = 0.0001;
40 };
41
42 BOOST_AUTO_TEST_SUITE(RulerTest)
43
44 BOOST_AUTO_TEST_CASE(tick_position_test_0)
45 {
46         const pv::util::Timestamp major_period("0.1");
47         const pv::util::Timestamp offset("0");
48         const double scale(0.001);
49         const int width(500);
50         const unsigned int minor_tick_count(4);
51
52         const TickPositions ts = Ruler::calculate_tick_positions(
53                 major_period, offset, scale, width, minor_tick_count, format);
54
55         BOOST_REQUIRE_EQUAL(ts.major.size(), 6);
56
57         BOOST_CHECK_CLOSE(ts.major[0].first,   0, e);
58         BOOST_CHECK_CLOSE(ts.major[1].first, 100, e);
59         BOOST_CHECK_CLOSE(ts.major[2].first, 200, e);
60         BOOST_CHECK_CLOSE(ts.major[3].first, 300, e);
61         BOOST_CHECK_CLOSE(ts.major[4].first, 400, e);
62         BOOST_CHECK_CLOSE(ts.major[5].first, 500, e);
63
64         BOOST_CHECK_EQUAL(ts.major[0].second,  "0.000000 s");
65         BOOST_CHECK_EQUAL(ts.major[1].second, "+0.100000 s");
66         BOOST_CHECK_EQUAL(ts.major[2].second, "+0.200000 s");
67         BOOST_CHECK_EQUAL(ts.major[3].second, "+0.300000 s");
68         BOOST_CHECK_EQUAL(ts.major[4].second, "+0.400000 s");
69         BOOST_CHECK_EQUAL(ts.major[5].second, "+0.500000 s");
70
71         BOOST_REQUIRE_EQUAL(ts.minor.size(), 16);
72
73         BOOST_CHECK_CLOSE(ts.minor[ 0], -25, e);
74         BOOST_CHECK_CLOSE(ts.minor[ 1],  25, e);
75         BOOST_CHECK_CLOSE(ts.minor[ 2],  50, e);
76         BOOST_CHECK_CLOSE(ts.minor[ 3],  75, e);
77         BOOST_CHECK_CLOSE(ts.minor[ 4], 125, e);
78         BOOST_CHECK_CLOSE(ts.minor[ 5], 150, e);
79         BOOST_CHECK_CLOSE(ts.minor[ 6], 175, e);
80         BOOST_CHECK_CLOSE(ts.minor[ 7], 225, e);
81         BOOST_CHECK_CLOSE(ts.minor[ 8], 250, e);
82         BOOST_CHECK_CLOSE(ts.minor[ 9], 275, e);
83         BOOST_CHECK_CLOSE(ts.minor[10], 325, e);
84         BOOST_CHECK_CLOSE(ts.minor[11], 350, e);
85         BOOST_CHECK_CLOSE(ts.minor[12], 375, e);
86         BOOST_CHECK_CLOSE(ts.minor[13], 425, e);
87         BOOST_CHECK_CLOSE(ts.minor[14], 450, e);
88         BOOST_CHECK_CLOSE(ts.minor[15], 475, e);
89 }
90
91 BOOST_AUTO_TEST_CASE(tick_position_test_1)
92 {
93         const pv::util::Timestamp major_period("0.1");
94         const pv::util::Timestamp offset("-0.463");
95         const double scale(0.001);
96         const int width(500);
97         const unsigned int minor_tick_count(4);
98
99         const TickPositions ts = Ruler::calculate_tick_positions(
100                 major_period, offset, scale, width, minor_tick_count, format);
101
102         BOOST_REQUIRE_EQUAL(ts.major.size(), 5);
103
104         BOOST_CHECK_CLOSE(ts.major[0].first,   63, e);
105         BOOST_CHECK_CLOSE(ts.major[1].first,  163, e);
106         BOOST_CHECK_CLOSE(ts.major[2].first,  263, e);
107         BOOST_CHECK_CLOSE(ts.major[3].first,  363, e);
108         BOOST_CHECK_CLOSE(ts.major[4].first,  463, e);
109
110         BOOST_CHECK_EQUAL(ts.major[0].second, "-0.400000 s");
111         BOOST_CHECK_EQUAL(ts.major[1].second, "-0.300000 s");
112         BOOST_CHECK_EQUAL(ts.major[2].second, "-0.200000 s");
113         BOOST_CHECK_EQUAL(ts.major[3].second, "-0.100000 s");
114         BOOST_CHECK_EQUAL(ts.major[4].second,  "0.000000 s");
115
116         BOOST_REQUIRE_EQUAL(ts.minor.size(), 17);
117         BOOST_CHECK_CLOSE(ts.minor[ 0], -12, e);
118         BOOST_CHECK_CLOSE(ts.minor[ 1],  13, e);
119         BOOST_CHECK_CLOSE(ts.minor[ 2],  38, e);
120         BOOST_CHECK_CLOSE(ts.minor[ 3],  88, e);
121         BOOST_CHECK_CLOSE(ts.minor[ 4], 113, e);
122         BOOST_CHECK_CLOSE(ts.minor[ 5], 138, e);
123         BOOST_CHECK_CLOSE(ts.minor[ 6], 188, e);
124         BOOST_CHECK_CLOSE(ts.minor[ 7], 213, e);
125         BOOST_CHECK_CLOSE(ts.minor[ 8], 238, e);
126         BOOST_CHECK_CLOSE(ts.minor[ 9], 288, e);
127         BOOST_CHECK_CLOSE(ts.minor[10], 313, e);
128         BOOST_CHECK_CLOSE(ts.minor[11], 338, e);
129         BOOST_CHECK_CLOSE(ts.minor[12], 388, e);
130         BOOST_CHECK_CLOSE(ts.minor[13], 413, e);
131         BOOST_CHECK_CLOSE(ts.minor[14], 438, e);
132         BOOST_CHECK_CLOSE(ts.minor[15], 488, e);
133         BOOST_CHECK_CLOSE(ts.minor[16], 513, e);
134 }
135
136 BOOST_AUTO_TEST_CASE(tick_position_test_2)
137 {
138         const pv::util::Timestamp major_period("20");
139         const pv::util::Timestamp offset("8");
140         const double scale(0.129746);
141         const int width(580);
142         const unsigned int minor_tick_count(4);
143
144         const TickPositions ts = Ruler::calculate_tick_positions(
145                 major_period, offset, scale, width, minor_tick_count, format);
146
147         const double mp = 5;
148         const int off = 8;
149
150         BOOST_REQUIRE_EQUAL(ts.major.size(), 4);
151
152         BOOST_CHECK_CLOSE(ts.major[0].first, ( 4 * mp - off) / scale, e);
153         BOOST_CHECK_CLOSE(ts.major[1].first, ( 8 * mp - off) / scale, e);
154         BOOST_CHECK_CLOSE(ts.major[2].first, (12 * mp - off) / scale, e);
155         BOOST_CHECK_CLOSE(ts.major[3].first, (16 * mp - off) / scale, e);
156
157         BOOST_CHECK_EQUAL(ts.major[0].second, "+20.000000 s");
158         BOOST_CHECK_EQUAL(ts.major[1].second, "+40.000000 s");
159         BOOST_CHECK_EQUAL(ts.major[2].second, "+60.000000 s");
160         BOOST_CHECK_EQUAL(ts.major[3].second, "+80.000000 s");
161
162         BOOST_REQUIRE_EQUAL(ts.minor.size(), 13);
163
164         BOOST_CHECK_CLOSE(ts.minor[ 0], ( 1 * mp - off) / scale, e);
165         BOOST_CHECK_CLOSE(ts.minor[ 1], ( 2 * mp - off) / scale, e);
166         BOOST_CHECK_CLOSE(ts.minor[ 2], ( 3 * mp - off) / scale, e);
167         BOOST_CHECK_CLOSE(ts.minor[ 3], ( 5 * mp - off) / scale, e);
168         BOOST_CHECK_CLOSE(ts.minor[ 4], ( 6 * mp - off) / scale, e);
169         BOOST_CHECK_CLOSE(ts.minor[ 5], ( 7 * mp - off) / scale, e);
170         BOOST_CHECK_CLOSE(ts.minor[ 6], ( 9 * mp - off) / scale, e);
171         BOOST_CHECK_CLOSE(ts.minor[ 7], (10 * mp - off) / scale, e);
172         BOOST_CHECK_CLOSE(ts.minor[ 8], (11 * mp - off) / scale, e);
173         BOOST_CHECK_CLOSE(ts.minor[ 9], (13 * mp - off) / scale, e);
174         BOOST_CHECK_CLOSE(ts.minor[10], (14 * mp - off) / scale, e);
175         BOOST_CHECK_CLOSE(ts.minor[11], (15 * mp - off) / scale, e);
176         BOOST_CHECK_CLOSE(ts.minor[12], (17 * mp - off) / scale, e);
177 }
178
179 BOOST_AUTO_TEST_SUITE_END()