]> sigrok.org Git - pulseview.git/blob - test/view/ruler.cpp
5cef38d5ce20211c1ca8e329ca8e05f0a62629a1
[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/test/floating_point_comparison.hpp>
21 #include <boost/test/unit_test.hpp>
22
23 #include "pv/views/trace/ruler.hpp"
24 #include "test/test.hpp"
25
26 using namespace pv::views::trace;
27
28 namespace {
29         QString format(const pv::util::Timestamp& t)
30         {
31                 return pv::util::format_time_si(t, pv::util::SIPrefix::none, 6);
32         }
33
34         const double e = 0.0001;
35 };
36
37 BOOST_AUTO_TEST_SUITE(RulerTest)
38
39 BOOST_AUTO_TEST_CASE(tick_position_test_0)
40 {
41         const pv::util::Timestamp major_period("0.1");
42         const pv::util::Timestamp offset("0");
43         const double scale(0.001);
44         const int width(500);
45         const unsigned int minor_tick_count(4);
46
47         const TickPositions ts = Ruler::calculate_tick_positions(
48                 major_period, offset, scale, width, minor_tick_count, 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         const unsigned int minor_tick_count(4);
93
94         const TickPositions ts = Ruler::calculate_tick_positions(
95                 major_period, offset, scale, width, minor_tick_count, format);
96
97         BOOST_REQUIRE_EQUAL(ts.major.size(), 5);
98
99         BOOST_CHECK_CLOSE(ts.major[0].first,   63, e);
100         BOOST_CHECK_CLOSE(ts.major[1].first,  163, e);
101         BOOST_CHECK_CLOSE(ts.major[2].first,  263, e);
102         BOOST_CHECK_CLOSE(ts.major[3].first,  363, e);
103         BOOST_CHECK_CLOSE(ts.major[4].first,  463, e);
104
105         BOOST_CHECK_EQUAL(ts.major[0].second, "-0.400000 s");
106         BOOST_CHECK_EQUAL(ts.major[1].second, "-0.300000 s");
107         BOOST_CHECK_EQUAL(ts.major[2].second, "-0.200000 s");
108         BOOST_CHECK_EQUAL(ts.major[3].second, "-0.100000 s");
109         BOOST_CHECK_EQUAL(ts.major[4].second,  "0.000000 s");
110
111         BOOST_REQUIRE_EQUAL(ts.minor.size(), 17);
112         BOOST_CHECK_CLOSE(ts.minor[ 0], -12, e);
113         BOOST_CHECK_CLOSE(ts.minor[ 1],  13, e);
114         BOOST_CHECK_CLOSE(ts.minor[ 2],  38, e);
115         BOOST_CHECK_CLOSE(ts.minor[ 3],  88, e);
116         BOOST_CHECK_CLOSE(ts.minor[ 4], 113, e);
117         BOOST_CHECK_CLOSE(ts.minor[ 5], 138, e);
118         BOOST_CHECK_CLOSE(ts.minor[ 6], 188, e);
119         BOOST_CHECK_CLOSE(ts.minor[ 7], 213, e);
120         BOOST_CHECK_CLOSE(ts.minor[ 8], 238, e);
121         BOOST_CHECK_CLOSE(ts.minor[ 9], 288, e);
122         BOOST_CHECK_CLOSE(ts.minor[10], 313, e);
123         BOOST_CHECK_CLOSE(ts.minor[11], 338, e);
124         BOOST_CHECK_CLOSE(ts.minor[12], 388, e);
125         BOOST_CHECK_CLOSE(ts.minor[13], 413, e);
126         BOOST_CHECK_CLOSE(ts.minor[14], 438, e);
127         BOOST_CHECK_CLOSE(ts.minor[15], 488, e);
128         BOOST_CHECK_CLOSE(ts.minor[16], 513, e);
129 }
130
131 BOOST_AUTO_TEST_CASE(tick_position_test_2)
132 {
133         const pv::util::Timestamp major_period("20");
134         const pv::util::Timestamp offset("8");
135         const double scale(0.129746);
136         const int width(580);
137         const unsigned int minor_tick_count(4);
138
139         const TickPositions ts = Ruler::calculate_tick_positions(
140                 major_period, offset, scale, width, minor_tick_count, format);
141
142         const double mp = 5;
143         const int off = 8;
144
145         BOOST_REQUIRE_EQUAL(ts.major.size(), 4);
146
147         BOOST_CHECK_CLOSE(ts.major[0].first, ( 4 * mp - off) / scale, e);
148         BOOST_CHECK_CLOSE(ts.major[1].first, ( 8 * mp - off) / scale, e);
149         BOOST_CHECK_CLOSE(ts.major[2].first, (12 * mp - off) / scale, e);
150         BOOST_CHECK_CLOSE(ts.major[3].first, (16 * mp - off) / scale, e);
151
152         BOOST_CHECK_EQUAL(ts.major[0].second, "+20.000000 s");
153         BOOST_CHECK_EQUAL(ts.major[1].second, "+40.000000 s");
154         BOOST_CHECK_EQUAL(ts.major[2].second, "+60.000000 s");
155         BOOST_CHECK_EQUAL(ts.major[3].second, "+80.000000 s");
156
157         BOOST_REQUIRE_EQUAL(ts.minor.size(), 13);
158
159         BOOST_CHECK_CLOSE(ts.minor[ 0], ( 1 * mp - off) / scale, e);
160         BOOST_CHECK_CLOSE(ts.minor[ 1], ( 2 * mp - off) / scale, e);
161         BOOST_CHECK_CLOSE(ts.minor[ 2], ( 3 * mp - off) / scale, e);
162         BOOST_CHECK_CLOSE(ts.minor[ 3], ( 5 * mp - off) / scale, e);
163         BOOST_CHECK_CLOSE(ts.minor[ 4], ( 6 * mp - off) / scale, e);
164         BOOST_CHECK_CLOSE(ts.minor[ 5], ( 7 * mp - off) / scale, e);
165         BOOST_CHECK_CLOSE(ts.minor[ 6], ( 9 * mp - off) / scale, e);
166         BOOST_CHECK_CLOSE(ts.minor[ 7], (10 * mp - off) / scale, e);
167         BOOST_CHECK_CLOSE(ts.minor[ 8], (11 * mp - off) / scale, e);
168         BOOST_CHECK_CLOSE(ts.minor[ 9], (13 * mp - off) / scale, e);
169         BOOST_CHECK_CLOSE(ts.minor[10], (14 * mp - off) / scale, e);
170         BOOST_CHECK_CLOSE(ts.minor[11], (15 * mp - off) / scale, e);
171         BOOST_CHECK_CLOSE(ts.minor[12], (17 * mp - off) / scale, e);
172 }
173
174 BOOST_AUTO_TEST_SUITE_END()