Unreviewed, suppress warnings on GCC
[WebKit.git] / Tools / TestWebKitAPI / Tests / WTF / CheckedArithmeticOperations.cpp
index 1d93c9c..b8f30c5 100644 (file)
 
 namespace TestWebKitAPI {
 
+class OverflowCrashLogger {
+protected:
+    void overflowed()
+    {
+        m_overflowCount++;
+    }
+    
+    void clearOverflow()
+    {
+        m_overflowCount = 0;
+    }
+    
+    static void crash()
+    {
+        s_didCrash = true;
+    }
+    
+public:
+    void reset()
+    {
+        m_overflowCount = 0;
+        s_didCrash = false;
+    }
+    
+    bool hasOverflowed() const { return m_overflowCount > 0; }
+    int overflowCount() const { return m_overflowCount; }
+
+    bool didCrash() const { return s_didCrash; }
+    
+private:
+    int m_overflowCount { 0 };
+    static bool s_didCrash;
+};
+
+bool OverflowCrashLogger::s_didCrash = false;
+
+template <typename type>
+static void resetOverflow(Checked<type, OverflowCrashLogger>& value)
+{
+    value.reset();
+    value = 100;
+    value *= std::numeric_limits<type>::max();
+}
+
 #define CheckedArithmeticTest(type, Coercer, MixedSignednessTester) \
     TEST(WTF, Checked_##type) \
     { \
         typedef Coercer<type> CoercerType; \
         typedef MixedSignednessTester<type, CoercerType> MixedSignednessTesterType; \
-        CheckedArithmeticTester<type, CoercerType, MixedSignednessTesterType>(); \
+        CheckedArithmeticTester<type, CoercerType, MixedSignednessTesterType>::run(); \
     }
     
-#define coerceLiteral(x) Coerser::coerce(x)
+#define coerceLiteral(x) Coercer::coerce(x)
     
-template <typename type, typename Coerser, typename MixedSignednessTester>
+template <typename type, typename Coercer, typename MixedSignednessTester>
 class CheckedArithmeticTester {
 public:
     static void run()
@@ -121,11 +165,208 @@ public:
         value = 10;
         EXPECT_EQ(true, (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).hasOverflowed());
 
+
+        Checked<type, OverflowCrashLogger> nvalue; // to hold a not overflowed value.
+        Checked<type, OverflowCrashLogger> ovalue; // to hold an overflowed value.
+
+        bool unused;
+        UNUSED_PARAM(unused);
+
+        _value = 75;
+        type _largeValue = 100;
+        type _smallValue = 50;
+
+        value = _smallValue;
+        nvalue = _value;
+        ovalue = _value;
+
+        // Make sure the OverflowCrashLogger is working as expected.
+        EXPECT_EQ(false, (ovalue.hasOverflowed()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), ovalue.hasOverflowed()));
+        EXPECT_EQ(false, (resetOverflow(ovalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (unused = (ovalue == ovalue), ovalue.didCrash()));
+        EXPECT_EQ(false, (resetOverflow(ovalue), ovalue.didCrash()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+        EXPECT_EQ(false, nvalue.didCrash());
+
+        // Test operator== that should not overflow nor crash.
+        EXPECT_EQ(true, (nvalue == nvalue));
+        EXPECT_EQ(true, (nvalue == Checked<type, OverflowCrashLogger>(_value)));
+        EXPECT_EQ(false, (nvalue == value));
+        EXPECT_EQ(true, (nvalue == _value));
+        EXPECT_EQ(false, (nvalue == Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
+        EXPECT_EQ(false, (nvalue == std::numeric_limits<type>::max()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+        EXPECT_EQ(false, nvalue.didCrash());
+
+        // Test operator!= that should not overflow nor crash.
+        EXPECT_EQ(false, (nvalue != nvalue));
+        EXPECT_EQ(false, (nvalue != Checked<type, OverflowCrashLogger>(_value)));
+        EXPECT_EQ(true, (nvalue != value));
+        EXPECT_EQ(false, (nvalue != _value));
+        EXPECT_EQ(true, (nvalue != Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
+        EXPECT_EQ(true, (nvalue != std::numeric_limits<type>::max()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+        EXPECT_EQ(false, nvalue.didCrash());
+
+        // Test operator< that should not overflow nor crash.
+        EXPECT_EQ(false, (nvalue < nvalue));
+        EXPECT_EQ(false, (nvalue < value));
+        EXPECT_EQ(true, (nvalue < Checked<type, OverflowCrashLogger>(_largeValue)));
+        EXPECT_EQ(false, (nvalue < Checked<type, OverflowCrashLogger>(_value)));
+        EXPECT_EQ(false, (nvalue < Checked<type, OverflowCrashLogger>(_smallValue)));
+        EXPECT_EQ(true, (nvalue < _largeValue));
+        EXPECT_EQ(false, (nvalue < _value));
+        EXPECT_EQ(false, (nvalue < _smallValue));
+        EXPECT_EQ(true, (nvalue < Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
+        EXPECT_EQ(true, (nvalue < std::numeric_limits<type>::max()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+        EXPECT_EQ(false, nvalue.didCrash());
+
+        // Test operator<= that should not overflow nor crash.
+        EXPECT_EQ(true, (nvalue <= nvalue));
+        EXPECT_EQ(false, (nvalue <= value));
+        EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(_largeValue)));
+        EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(_value)));
+        EXPECT_EQ(false, (nvalue <= Checked<type, OverflowCrashLogger>(_smallValue)));
+        EXPECT_EQ(true, (nvalue <= _largeValue));
+        EXPECT_EQ(true, (nvalue <= _value));
+        EXPECT_EQ(false, (nvalue <= _smallValue));
+        EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
+        EXPECT_EQ(true, (nvalue <= std::numeric_limits<type>::max()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+        EXPECT_EQ(false, nvalue.didCrash());
+
+        // Test operator> that should not overflow nor crash.
+        EXPECT_EQ(false, (nvalue > nvalue));
+        EXPECT_EQ(true, (nvalue > value));
+        EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(_largeValue)));
+        EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(_value)));
+        EXPECT_EQ(true, (nvalue > Checked<type, OverflowCrashLogger>(_smallValue)));
+        EXPECT_EQ(false, (nvalue > _largeValue));
+        EXPECT_EQ(false, (nvalue > _value));
+        EXPECT_EQ(true, (nvalue > _smallValue));
+        EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
+        EXPECT_EQ(false, (nvalue > std::numeric_limits<type>::max()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+        EXPECT_EQ(false, nvalue.didCrash());
+
+        // Test operator>= that should not overflow nor crash.
+        EXPECT_EQ(true, (nvalue >= nvalue));
+        EXPECT_EQ(true, (nvalue >= value));
+        EXPECT_EQ(false, (nvalue >= Checked<type, OverflowCrashLogger>(_largeValue)));
+        EXPECT_EQ(true, (nvalue >= Checked<type, OverflowCrashLogger>(_value)));
+        EXPECT_EQ(true, (nvalue >= Checked<type, OverflowCrashLogger>(_smallValue)));
+        EXPECT_EQ(false, (nvalue >= _largeValue));
+        EXPECT_EQ(true, (nvalue >= _value));
+        EXPECT_EQ(true, (nvalue >= _smallValue));
+        EXPECT_EQ(false, (nvalue >= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
+        EXPECT_EQ(false, (nvalue >= std::numeric_limits<type>::max()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+        EXPECT_EQ(false, nvalue.didCrash());
+
+        // Test operator== with an overflowed value.
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == ovalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == _value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == _value * std::numeric_limits<type>::max()), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == std::numeric_limits<type>::max()), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == nvalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue == ovalue), ovalue.didCrash()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+
+        // Test operator!= with an overflowed value.
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != ovalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != _value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != _value * std::numeric_limits<type>::max()), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != std::numeric_limits<type>::max()), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != nvalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue != ovalue), ovalue.didCrash()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+
+        // Test operator< with an overflowed value.
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < ovalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _largeValue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _smallValue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < std::numeric_limits<type>::max()), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < nvalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue < ovalue), ovalue.didCrash()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+
+        // Test operator<= with an overflowed value.
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= ovalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _largeValue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _smallValue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= std::numeric_limits<type>::max()), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= nvalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue <= ovalue), ovalue.didCrash()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+
+        // Test operator> with an overflowed value.
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > ovalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _largeValue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _smallValue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > std::numeric_limits<type>::max()), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > nvalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue > ovalue), ovalue.didCrash()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+
+        // Test operator>= with an overflowed value.
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= ovalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _largeValue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _value), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _smallValue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= std::numeric_limits<type>::max()), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= nvalue), ovalue.didCrash()));
+        EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue >= ovalue), ovalue.didCrash()));
+
+        EXPECT_EQ(false, nvalue.hasOverflowed());
+
         MixedSignednessTester::run();
     }
 };
 
-template <typename type, typename Coerser>
+template <typename type, typename Coercer>
 class AllowMixedSignednessTest {
 public:
     static void run()
@@ -164,7 +405,7 @@ public:
     }
 };
 
-template <typename type, typename Coerser>
+template <typename type, typename Coercer>
 class IgnoreMixedSignednessTest {
 public:
     static void run() { }
@@ -187,4 +428,62 @@ CheckedArithmeticTest(uint32_t, CoerceLiteralToUnsigned, AllowMixedSignednessTes
 CheckedArithmeticTest(int64_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
 CheckedArithmeticTest(uint64_t, CoerceLiteralToUnsigned, IgnoreMixedSignednessTest)
 
+TEST(CheckedArithmeticTest, IsInBounds)
+{
+    // bigger precision, signed, signed
+    EXPECT_TRUE(WTF::isInBounds<int32_t>(std::numeric_limits<int16_t>::max()));
+    EXPECT_TRUE(WTF::isInBounds<int32_t>(std::numeric_limits<int16_t>::min()));
+
+    // bigger precision, unsigned, signed
+    EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<int32_t>::max()));
+    EXPECT_FALSE(WTF::isInBounds<uint32_t>(std::numeric_limits<int16_t>::min()));
+
+    EXPECT_FALSE(WTF::isInBounds<uint32_t>((int32_t)-1));
+    EXPECT_FALSE(WTF::isInBounds<uint16_t>((int32_t)-1));
+    EXPECT_FALSE(WTF::isInBounds<unsigned long>((int)-1));
+
+    EXPECT_TRUE(WTF::isInBounds<uint32_t>((int32_t)1));
+    EXPECT_TRUE(WTF::isInBounds<uint32_t>((int16_t)1));
+    EXPECT_TRUE(WTF::isInBounds<unsigned>((int)1));
+
+    EXPECT_TRUE(WTF::isInBounds<uint32_t>((int32_t)0));
+    EXPECT_TRUE(WTF::isInBounds<uint16_t>((int32_t)0));
+    EXPECT_TRUE(WTF::isInBounds<uint32_t>((int16_t)0));
+    EXPECT_TRUE(WTF::isInBounds<unsigned>((int)0));
+
+    EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<int32_t>::max()));
+    EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<int16_t>::max()));
+    EXPECT_TRUE(WTF::isInBounds<unsigned>(std::numeric_limits<int>::max()));
+
+    // bigger precision, signed, unsigned
+    EXPECT_TRUE(WTF::isInBounds<int32_t>(std::numeric_limits<uint16_t>::max()));
+    EXPECT_FALSE(WTF::isInBounds<int32_t>(std::numeric_limits<uint32_t>::max()));
+    EXPECT_TRUE(WTF::isInBounds<int32_t>((uint32_t)0));
+
+    // bigger precision, unsigned, unsigned
+    EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<uint16_t>::max()));
+    EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<uint16_t>::min()));
+
+    // lower precision, signed signed
+    EXPECT_FALSE(WTF::isInBounds<int16_t>(std::numeric_limits<int32_t>::max()));
+    EXPECT_FALSE(WTF::isInBounds<int16_t>(std::numeric_limits<int32_t>::min()));
+    EXPECT_TRUE(WTF::isInBounds<int16_t>((int32_t)-1));
+    EXPECT_TRUE(WTF::isInBounds<int16_t>((int32_t)0));
+    EXPECT_TRUE(WTF::isInBounds<int16_t>((int32_t)1));
+    // lower precision, unsigned, signed
+    EXPECT_FALSE(WTF::isInBounds<uint16_t>(std::numeric_limits<int32_t>::max()));
+    EXPECT_FALSE(WTF::isInBounds<uint16_t>(std::numeric_limits<int32_t>::min()));
+    EXPECT_FALSE(WTF::isInBounds<uint16_t>((int32_t)-1));
+    EXPECT_TRUE(WTF::isInBounds<uint16_t>((int32_t)0));
+    EXPECT_TRUE(WTF::isInBounds<uint16_t>((int32_t)1));
+    // lower precision, signed, unsigned
+    EXPECT_FALSE(WTF::isInBounds<int16_t>(std::numeric_limits<uint32_t>::max()));
+    EXPECT_TRUE(WTF::isInBounds<int16_t>((uint32_t)0));
+    EXPECT_TRUE(WTF::isInBounds<int16_t>((uint32_t)1));
+    // lower precision, unsigned, unsigned
+    EXPECT_FALSE(WTF::isInBounds<uint16_t>(std::numeric_limits<uint32_t>::max()));
+    EXPECT_TRUE(WTF::isInBounds<uint16_t>((uint32_t)0));
+    EXPECT_TRUE(WTF::isInBounds<uint16_t>((uint32_t)1));
+}
+
 } // namespace TestWebKitAPI