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