]> sigrok.org Git - pulseview.git/blobdiff - test/util.cpp
Use typesafe enum classes in pv::util
[pulseview.git] / test / util.cpp
index ce9b64d27f324cf3f6cc561fe76b2db3c90dc1cc..70550ae660c34115b6bc334ef0a01fdc4b20a36d 100644 (file)
@@ -27,6 +27,16 @@ using ts = pv::util::Timestamp;
 
 namespace {
        QChar mu = QChar(0x03BC);
+
+       pv::util::SIPrefix unspecified = pv::util::SIPrefix::unspecified;
+       pv::util::SIPrefix yocto       = pv::util::SIPrefix::yocto;
+       pv::util::SIPrefix micro       = pv::util::SIPrefix::micro;
+       pv::util::SIPrefix milli       = pv::util::SIPrefix::milli;
+       pv::util::SIPrefix none        = pv::util::SIPrefix::none;
+       pv::util::SIPrefix kilo        = pv::util::SIPrefix::kilo;
+       pv::util::SIPrefix yotta       = pv::util::SIPrefix::yotta;
+
+       pv::util::TimeUnit Time = pv::util::TimeUnit::Time;
 }
 
 std::ostream& operator<<(std::ostream& stream, const QString& str)
@@ -36,6 +46,27 @@ std::ostream& operator<<(std::ostream& stream, const QString& str)
 
 BOOST_AUTO_TEST_SUITE(UtilTest)
 
+BOOST_AUTO_TEST_CASE(exponent_test)
+{
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::yocto), -24);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::zepto), -21);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::atto),  -18);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::femto), -15);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::pico),  -12);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::nano),   -9);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::micro),  -6);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::milli),  -3);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::none),    0);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::kilo),    3);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::mega),    6);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::giga),    9);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::tera),   12);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::peta),   15);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::exa),    18);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::zetta),  21);
+       BOOST_CHECK_EQUAL(exponent(SIPrefix::yotta),  24);
+}
+
 BOOST_AUTO_TEST_CASE(format_si_value_test)
 {
        // check prefix calculation
@@ -96,30 +127,30 @@ BOOST_AUTO_TEST_CASE(format_si_value_test)
        BOOST_CHECK_EQUAL(format_si_value(ts("1e27"), "V"), "+1000 YV");
 
        BOOST_CHECK_EQUAL(format_si_value(ts("1234"), "V"),           "+1 kV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1234"), "V", 9, 3), "+1.234 kV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1234"), "V", kilo, 3), "+1.234 kV");
        BOOST_CHECK_EQUAL(format_si_value(ts("1234.5678"), "V"),      "+1 kV");
 
        // check if a given prefix is honored
 
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e-24"), "V", 0),    "+1 yV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e-21"), "V", 0), "+1000 yV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", 0),         "0 yV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e-24"), "V", yocto),    "+1 yV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e-21"), "V", yocto), "+1000 yV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", yocto),         "0 yV");
 
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e-4"), "V", 7),       "+0 mV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e-4"), "V", 7, 1),  "+0.1 mV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1000"), "V", 7), "+1000000 mV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", 7),           "0 mV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e-4"), "V", milli),       "+0 mV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e-4"), "V", milli, 1),  "+0.1 mV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1000"), "V", milli), "+1000000 mV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", milli),           "0 mV");
 
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", 8),       "+0 V");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", 8, 1),  "+0.1 V");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", 8, 2), "+0.10 V");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", 8),          "+1 V");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e1"), "V", 8),       "+10 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", none),       "+0 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", none, 1),  "+0.1 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e-1"), "V", none, 2), "+0.10 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", none),          "+1 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e1"), "V", none),       "+10 V");
 
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e23"), "V", 16),       "+0 YV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e23"), "V", 16, 1),  "+0.1 YV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1e27"), "V", 16),    "+1000 YV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", 16),           "0 YV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e23"), "V", yotta),       "+0 YV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e23"), "V", yotta, 1),  "+0.1 YV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1e27"), "V", yotta),    "+1000 YV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("0"), "V", yotta),           "0 YV");
 
        // check precision
 
@@ -127,66 +158,66 @@ BOOST_AUTO_TEST_CASE(format_si_value_test)
        BOOST_CHECK_EQUAL(format_si_value(ts("1.4"), "V"),                      "+1 V");
        BOOST_CHECK_EQUAL(format_si_value(ts("1.5"), "V"),                      "+2 V");
        BOOST_CHECK_EQUAL(format_si_value(ts("1.9"), "V"),                      "+2 V");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", -1, 2),      "+1.23 V");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", -1, 3),     "+1.235 V");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", 7, 3),  "+1234.568 mV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", 7, 0),      "+1235 mV");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1.2"), "V", -1, 3),           "+1.200 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", unspecified, 2),      "+1.23 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", unspecified, 3),     "+1.235 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", milli, 3),       "+1234.568 mV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1.2345678"), "V", milli, 0),           "+1235 mV");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1.2"), "V", unspecified, 3),           "+1.200 V");
 
        // check sign
 
-       BOOST_CHECK_EQUAL(format_si_value(ts("-1"), "V", 8, 0, true),  "-1 V");
-       BOOST_CHECK_EQUAL(format_si_value(ts("-1"), "V", 8, 0, false), "-1 V");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", 8, 0, true),   "+1 V");
-       BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", 8, 0, false),   "1 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("-1"), "V", none, 0, true),  "-1 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("-1"), "V", none, 0, false), "-1 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", none, 0, true),   "+1 V");
+       BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", none, 0, false),   "1 V");
 }
 
 BOOST_AUTO_TEST_CASE(format_time_test)
 {
-       BOOST_CHECK_EQUAL(format_time(ts("-0.00005"), 6, Time, 5), QString("-50 ") + mu + "s");
-       BOOST_CHECK_EQUAL(format_time(ts( "0.00005"), 6, Time, 5), QString("+50 ") + mu + "s");
+       BOOST_CHECK_EQUAL(format_time(ts("-0.00005"), micro, Time, 5), QString("-50 ") + mu + "s");
+       BOOST_CHECK_EQUAL(format_time(ts( "0.00005"), micro, Time, 5), QString("+50 ") + mu + "s");
        BOOST_CHECK_EQUAL(format_time(ts( "1")), "+1 s");
        BOOST_CHECK_EQUAL(format_time(ts("-1")), "-1 s");
-       BOOST_CHECK_EQUAL(format_time(ts( "100")),                 "+1:40");
-       BOOST_CHECK_EQUAL(format_time(ts("-100")),                 "-1:40");
-       BOOST_CHECK_EQUAL(format_time(ts( "4000")),             "+1:06:40");
-       BOOST_CHECK_EQUAL(format_time(ts("-4000")),             "-1:06:40");
-       BOOST_CHECK_EQUAL(format_time(ts("12000"), 9, Time, 0), "+3:20:00");
-       BOOST_CHECK_EQUAL(format_time(ts("15000"), 9, Time, 0), "+4:10:00");
-       BOOST_CHECK_EQUAL(format_time(ts("20000"), 9, Time, 0), "+5:33:20");
-       BOOST_CHECK_EQUAL(format_time(ts("25000"), 9, Time, 0), "+6:56:40");
-
-       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 0), "+123:04:05:06");
-       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 1), "+123:04:05:06.0");
-       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 2), "+123:04:05:06.00");
-       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 3), "+123:04:05:06.007");
-       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 4), "+123:04:05:06.007 0");
-       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 5), "+123:04:05:06.007 00");
-       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 6), "+123:04:05:06.007 008");
-       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 7), "+123:04:05:06.007 008 0");
-       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 8), "+123:04:05:06.007 008 00");
-       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 9), "+123:04:05:06.007 008 009");
-
-       BOOST_CHECK_EQUAL(format_time(ts("-1.5"), 7), "-1500 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("-1.0"), 7), "-1000 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("-0.2")),     "-200 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("-0.1")),     "-100 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("0.0")),         "0");
-       BOOST_CHECK_EQUAL(format_time(ts("0.1")),      "+100 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("0.2")),      "+200 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("0.3")),      "+300 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("0.4")),      "+400 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("0.5")),      "+500 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("0.6")),      "+600 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("0.7")),      "+700 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("0.8")),      "+800 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("0.9")),      "+900 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("1.0"), 7),  "+1000 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("1.1"), 7),  "+1100 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("1.2"), 7),  "+1200 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("1.3"), 7),  "+1300 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("1.4"), 7),  "+1400 ms");
-       BOOST_CHECK_EQUAL(format_time(ts("1.5"), 7),  "+1500 ms");
+       BOOST_CHECK_EQUAL(format_time(ts( "100")),                    "+1:40");
+       BOOST_CHECK_EQUAL(format_time(ts("-100")),                    "-1:40");
+       BOOST_CHECK_EQUAL(format_time(ts( "4000")),                "+1:06:40");
+       BOOST_CHECK_EQUAL(format_time(ts("-4000")),                "-1:06:40");
+       BOOST_CHECK_EQUAL(format_time(ts("12000"), kilo, Time, 0), "+3:20:00");
+       BOOST_CHECK_EQUAL(format_time(ts("15000"), kilo, Time, 0), "+4:10:00");
+       BOOST_CHECK_EQUAL(format_time(ts("20000"), kilo, Time, 0), "+5:33:20");
+       BOOST_CHECK_EQUAL(format_time(ts("25000"), kilo, Time, 0), "+6:56:40");
+
+       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 0), "+123:04:05:06");
+       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 1), "+123:04:05:06.0");
+       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 2), "+123:04:05:06.00");
+       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 3), "+123:04:05:06.007");
+       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 4), "+123:04:05:06.007 0");
+       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 5), "+123:04:05:06.007 00");
+       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 6), "+123:04:05:06.007 008");
+       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 7), "+123:04:05:06.007 008 0");
+       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 8), "+123:04:05:06.007 008 00");
+       BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), none, Time, 9), "+123:04:05:06.007 008 009");
+
+       BOOST_CHECK_EQUAL(format_time(ts("-1.5"), milli), "-1500 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("-1.0"), milli), "-1000 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("-0.2")),         "-200 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("-0.1")),         "-100 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("0.0")),             "0");
+       BOOST_CHECK_EQUAL(format_time(ts("0.1")),          "+100 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("0.2")),          "+200 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("0.3")),          "+300 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("0.4")),          "+400 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("0.5")),          "+500 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("0.6")),          "+600 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("0.7")),          "+700 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("0.8")),          "+800 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("0.9")),          "+900 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("1.0"), milli),  "+1000 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("1.1"), milli),  "+1100 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("1.2"), milli),  "+1200 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("1.3"), milli),  "+1300 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("1.4"), milli),  "+1400 ms");
+       BOOST_CHECK_EQUAL(format_time(ts("1.5"), milli),  "+1500 ms");
 }
 
 BOOST_AUTO_TEST_SUITE_END()