From: Joel Holdsworth Date: Sat, 15 Sep 2012 12:31:14 +0000 (+0100) Subject: Added tests, and corrected behaviour of LogicDataSnapshot::pow2_ceil X-Git-Tag: pulseview-0.1.0~301 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=8743e7cba1f05ea0dde3dd566f28e6082ed055d0;p=pulseview.git Added tests, and corrected behaviour of LogicDataSnapshot::pow2_ceil --- diff --git a/logicdatasnapshot.cpp b/logicdatasnapshot.cpp index e751b9d1..5301abf5 100644 --- a/logicdatasnapshot.cpp +++ b/logicdatasnapshot.cpp @@ -323,7 +323,8 @@ void LogicDataSnapshot::get_subsampled_edges( get_sample(end) & sig_mask)); } -int64_t LogicDataSnapshot::pow2_ceil(int64_t x, int power) +int64_t LogicDataSnapshot::pow2_ceil(int64_t x, unsigned int power) { - return ((x >> power) + 1) << power; + const int64_t p = 1 << power; + return ((x < 0) ? x : (x + p - 1)) / p * p; } diff --git a/logicdatasnapshot.h b/logicdatasnapshot.h index 630aad43..de0f4732 100644 --- a/logicdatasnapshot.h +++ b/logicdatasnapshot.h @@ -24,6 +24,7 @@ #include namespace LogicDataSnapshotTest { + class Pow2; class Basic; class LargeData; } @@ -79,12 +80,13 @@ public: private: - static inline int64_t pow2_ceil(int64_t x, int power); + static int64_t pow2_ceil(int64_t x, unsigned int power); private: struct MipMapLevel _mip_map[ScaleStepCount]; uint64_t _last_append_sample; + friend class LogicDataSnapshotTest::Pow2; friend class LogicDataSnapshotTest::Basic; friend class LogicDataSnapshotTest::LargeData; }; diff --git a/test/logicdatasnapshot.cpp b/test/logicdatasnapshot.cpp index 6e1f2636..66232cd6 100644 --- a/test/logicdatasnapshot.cpp +++ b/test/logicdatasnapshot.cpp @@ -18,6 +18,9 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#define __STDC_LIMIT_MACROS +#include + #include #include "../logicdatasnapshot.h" @@ -37,6 +40,28 @@ void push_logic(LogicDataSnapshot &s, unsigned int length, uint8_t value) delete[] (uint8_t*)logic.data; } +BOOST_AUTO_TEST_CASE(Pow2) +{ + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(-2, 0), -2); + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(-1, 0), -1); + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(0, 0), 0); + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(1, 0), 1); + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(2, 0), 2); + + BOOST_CHECK_EQUAL( + LogicDataSnapshot::pow2_ceil(INT64_MIN, 0), INT64_MIN); + BOOST_CHECK_EQUAL( + LogicDataSnapshot::pow2_ceil(INT64_MAX, 0), INT64_MAX); + + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(-3, 1), -2); + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(-2, 1), -2); + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(-1, 1), 0); + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(0, 1), 0); + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(1, 1), 2); + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(2, 1), 2); + BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(3, 1), 4); +} + BOOST_AUTO_TEST_CASE(Basic) { // Create an empty LogicDataSnapshot object