namespace pv {
namespace util {
-static const QString SIPrefixes[17] = {
- "y", "z",
- "a", "f", "p",
- "n", QChar(0x03BC), "m",
- "",
- "k", "M", "G",
- "T", "P", "E",
- "Z", "Y"};
-const int EmptySIPrefix = 8;
-const int FirstSIPrefixPower = -(EmptySIPrefix * 3);
+static QTextStream& operator<<(QTextStream& stream, SIPrefix prefix)
+{
+ switch (prefix) {
+ case SIPrefix::yocto: return stream << 'y';
+ case SIPrefix::zepto: return stream << 'z';
+ case SIPrefix::atto: return stream << 'a';
+ case SIPrefix::femto: return stream << 'f';
+ case SIPrefix::pico: return stream << 'p';
+ case SIPrefix::nano: return stream << 'n';
+ case SIPrefix::micro: return stream << QChar(0x03BC);
+ case SIPrefix::milli: return stream << 'm';
+ case SIPrefix::kilo: return stream << 'k';
+ case SIPrefix::mega: return stream << 'M';
+ case SIPrefix::giga: return stream << 'G';
+ case SIPrefix::tera: return stream << 'T';
+ case SIPrefix::peta: return stream << 'P';
+ case SIPrefix::exa: return stream << 'E';
+ case SIPrefix::zetta: return stream << 'Z';
+ case SIPrefix::yotta: return stream << 'Y';
+
+ default: return stream;
+ }
+}
+
+int exponent(SIPrefix prefix)
+{
+ return 3 * (static_cast<int>(prefix) - static_cast<int>(SIPrefix::none));
+}
+
+static SIPrefix successor(SIPrefix prefix)
+{
+ assert(prefix != SIPrefix::yotta);
+ return static_cast<SIPrefix>(static_cast<int>(prefix) + 1);
+}
// Insert the timestamp value into the stream in fixed-point notation
// (and honor the precision)
return stream << QString::fromStdString(str);
}
-QString format_si_value(const Timestamp& v, QString unit, int prefix,
+QString format_si_value(const Timestamp& v, QString unit, SIPrefix prefix,
unsigned int precision, bool sign)
{
- if (prefix < 0) {
+ if (prefix == SIPrefix::unspecified) {
// No prefix given, calculate it
if (v.is_zero()) {
- prefix = EmptySIPrefix;
+ prefix = SIPrefix::none;
} else {
- int exp = -FirstSIPrefixPower;
-
- prefix = 0;
- while ((fabs(v) * pow(10.0, exp)) > 999.0 &&
- prefix < (int)(countof(SIPrefixes) - 1)) {
- prefix++;
+ int exp = exponent(SIPrefix::yotta);
+ prefix = SIPrefix::yocto;
+ while ((fabs(v) * pow(Timestamp(10), exp)) > 999 &&
+ prefix < SIPrefix::yotta) {
+ prefix = successor(prefix);
exp -= 3;
}
}
}
- assert(prefix >= 0);
- assert(prefix < (int)countof(SIPrefixes));
+ assert(prefix >= SIPrefix::yocto);
+ assert(prefix <= SIPrefix::yotta);
- const Timestamp multiplier = pow(Timestamp(10),
- (int)- prefix * 3 - FirstSIPrefixPower);
+ const Timestamp multiplier = pow(Timestamp(10), -exponent(prefix));
QString s;
QTextStream ts(&s);
<< qSetRealNumberPrecision(precision)
<< (v * multiplier)
<< ' '
- << SIPrefixes[prefix]
+ << prefix
<< unit;
return s;
}
static QString format_time_with_si(const Timestamp& t, QString unit,
- int prefix, unsigned int precision)
+ SIPrefix prefix, unsigned int precision)
{
// The precision is always given without taking the prefix into account
// so we need to deduct the number of decimals the prefix might imply
- const int prefix_order =
- -(prefix * 3 + pv::util::FirstSIPrefixPower);
+ const int prefix_order = -exponent(prefix);
const unsigned int relative_prec =
- (prefix >= EmptySIPrefix) ? precision :
+ (prefix >= SIPrefix::none) ? precision :
std::max((int)(precision - prefix_order), 0);
return format_si_value(t, unit, prefix, relative_prec);
}
-QString format_time(const Timestamp& t, int prefix, TimeUnit unit,
+QString format_time(const Timestamp& t, SIPrefix prefix, TimeUnit unit,
unsigned int precision)
{
// Make 0 appear as 0, not random +0 or -0
return "0";
// If we have to use samples then we have no alternative formats
- if (unit == Samples)
+ if (unit == TimeUnit::Samples)
return format_time_with_si(t, "sa", prefix, precision);
// View in "normal" range -> medium precision, medium step size
QString format_second(const Timestamp& second)
{
- return format_si_value(second, "s", -1, 0, false);
+ return format_si_value(second, "s", SIPrefix::unspecified, 0, false);
}
} // namespace util
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)
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
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
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()