2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include <boost/test/unit_test.hpp>
23 #include "../logicdatasnapshot.h"
25 void push_logic(LogicDataSnapshot &s, unsigned int length, uint8_t value)
27 sr_datafeed_logic logic;
29 logic.length = length;
30 logic.data = new uint8_t[length];
31 memset(logic.data, value, length * logic.unitsize);
32 s.append_payload(logic);
33 delete[] (uint8_t*)logic.data;
36 BOOST_AUTO_TEST_CASE(LogicDataSnapshotTest)
38 // Create an empty LogicDataSnapshot object
39 sr_datafeed_logic logic;
44 LogicDataSnapshot s(logic);
46 BOOST_CHECK(s.get_sample_count() == 0);
47 for(int i = 0; i < LogicDataSnapshot::ScaleStepCount; i++)
49 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
50 BOOST_CHECK_EQUAL(m.length, 0);
51 BOOST_CHECK_EQUAL(m.data_length, 0);
52 BOOST_CHECK(m.data == NULL);
55 // Push 8 samples of all zeros
58 BOOST_CHECK(s.get_sample_count() == 8);
60 // There should not be enough samples to have a single mip map sample
61 for(int i = 0; i < LogicDataSnapshot::ScaleStepCount; i++)
63 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
64 BOOST_CHECK_EQUAL(m.length, 0);
65 BOOST_CHECK_EQUAL(m.data_length, 0);
66 BOOST_CHECK(m.data == NULL);
69 // Push 8 samples of 0x11s to bring the total up to 16
70 push_logic(s, 8, 0x11);
72 // There should now be enough data for exactly one sample
73 // in mip map level 0, and that sample should be 0
74 const LogicDataSnapshot::MipMapLevel &m0 = s._mip_map[0];
75 BOOST_CHECK_EQUAL(m0.length, 1);
76 BOOST_CHECK_EQUAL(m0.data_length, LogicDataSnapshot::MipMapDataUnit);
77 BOOST_REQUIRE(m0.data != NULL);
78 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[0], 0x11);
80 // The higher levels should still be empty
81 for(int i = 1; i < LogicDataSnapshot::ScaleStepCount; i++)
83 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
84 BOOST_CHECK_EQUAL(m.length, 0);
85 BOOST_CHECK_EQUAL(m.data_length, 0);
86 BOOST_CHECK(m.data == NULL);
89 // Push 240 samples of all zeros to bring the total up to 256
90 push_logic(s, 240, 0);
92 BOOST_CHECK_EQUAL(m0.length, 16);
93 BOOST_CHECK_EQUAL(m0.data_length, LogicDataSnapshot::MipMapDataUnit);
95 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[1], 0x11);
96 for(int i = 2; i < m0.length; i++)
97 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[i], 0);
99 const LogicDataSnapshot::MipMapLevel &m1 = s._mip_map[1];
100 BOOST_CHECK_EQUAL(m1.length, 1);
101 BOOST_CHECK_EQUAL(m1.data_length, LogicDataSnapshot::MipMapDataUnit);
102 BOOST_REQUIRE(m1.data != NULL);
103 BOOST_CHECK_EQUAL(((uint8_t*)m1.data)[0], 0x11);