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