/* * Copyright (C) 2014 Pavel Kirienko */ #include #include #include struct NonConvertible { }; struct ConvertibleToBool { const bool value; ConvertibleToBool(bool value) : value(value) { } operator bool() const { return value; } bool operator!() const { return !value; } }; struct NonDefaultConstructible { NonDefaultConstructible(int) { } }; TEST(Util, CoerceOrFallback) { using uavcan::coerceOrFallback; ASSERT_FALSE(coerceOrFallback(NonConvertible())); ASSERT_TRUE(coerceOrFallback(NonConvertible(), true)); ASSERT_EQ(0, coerceOrFallback(NonConvertible())); ASSERT_EQ(9000, coerceOrFallback(NonConvertible(), 9000)); ASSERT_TRUE(coerceOrFallback(ConvertibleToBool(true))); ASSERT_TRUE(coerceOrFallback(ConvertibleToBool(true), false)); ASSERT_FALSE(coerceOrFallback(ConvertibleToBool(false))); ASSERT_FALSE(coerceOrFallback(ConvertibleToBool(false), true)); ASSERT_EQ(1, coerceOrFallback(ConvertibleToBool(true))); ASSERT_EQ(0, coerceOrFallback(ConvertibleToBool(false), -100)); //coerceOrFallback(ConvertibleToBool(true)); // Will fail to compile coerceOrFallback(NonConvertible(), NonDefaultConstructible(64)); } TEST(Util, FloatClassification) { // NAN ASSERT_TRUE(uavcan::isNaN(std::numeric_limits::quiet_NaN())); ASSERT_FALSE(uavcan::isNaN(std::numeric_limits::infinity())); ASSERT_FALSE(uavcan::isNaN(std::numeric_limits::infinity())); ASSERT_FALSE(uavcan::isNaN(123.456)); // INF ASSERT_TRUE(uavcan::isInfinity(std::numeric_limits::infinity())); ASSERT_TRUE(uavcan::isInfinity(-std::numeric_limits::infinity())); ASSERT_FALSE(uavcan::isInfinity(std::numeric_limits::quiet_NaN())); ASSERT_FALSE(uavcan::isInfinity(-0.1L)); // Signbit ASSERT_FALSE(uavcan::getSignBit(12)); ASSERT_TRUE(uavcan::getSignBit(-std::numeric_limits::infinity())); ASSERT_FALSE(uavcan::getSignBit(std::numeric_limits::infinity())); ASSERT_TRUE(uavcan::getSignBit(-0.0)); // Negative zero }