Add N-bit min/max/mask utilities to deInt32.h.
authorJarkko Pöyry <jpoyry@google.com>
Tue, 21 Apr 2015 03:48:15 +0000 (20:48 -0700)
committerJarkko Pöyry <jpoyry@google.com>
Thu, 23 Apr 2015 22:49:17 +0000 (15:49 -0700)
- Add internal tests.
- Enable int32, stl_util and command line self tests.

Change-Id: I8546c6fa8e629da7c0a068eeffc88614f7ad1b4e

framework/delibs/debase/deInt32.h
framework/delibs/debase/deInt32Test.c
framework/delibs/deutil/deCommandLine.c
framework/delibs/deutil/deCommandLine.h
modules/internal/ditDelibsTests.cpp

index f25daf0..1ec9a37 100644 (file)
@@ -469,18 +469,69 @@ DE_INLINE deBool dePointerEqual (const void* a, const void* b)
  *     \brief  Modulo that generates the same sign as divisor and rounds toward
  *                     negative infinity -- assuming c99 %-operator.
  */
-DE_INLINE deInt32 deInt32ModF(deInt32 n, deInt32 d)
+DE_INLINE deInt32 deInt32ModF (deInt32 n, deInt32 d)
 {
        deInt32 r = n%d;
        if ((r > 0 && d < 0) || (r < 0 && d > 0)) r = r+d;
        return r;
 }
 
-DE_INLINE deBool deInt64InInt32Range(deInt64 x)
+DE_INLINE deBool deInt64InInt32Range (deInt64 x)
 {
        return ((x >= (-1ll<<31)) && (x <= ((1ll<<31)-1)));
 }
 
+
+DE_INLINE deUint32 deBitMask32 (int leastSignificantBitNdx, int numBits)
+{
+       DE_ASSERT(deInRange32(leastSignificantBitNdx, 0, 32));
+       DE_ASSERT(deInRange32(numBits, 0, 32));
+       DE_ASSERT(deInRange32(leastSignificantBitNdx+numBits, 0, 32));
+
+       if (numBits < 32 && leastSignificantBitNdx < 32)
+               return ((1u<<numBits)-1u) << (deUint32)leastSignificantBitNdx;
+       else if (numBits == 0 && leastSignificantBitNdx == 32)
+               return 0u;
+       else
+       {
+               DE_ASSERT(numBits == 32 && leastSignificantBitNdx == 0);
+               return 0xFFFFFFFFu;
+       }
+}
+
+DE_INLINE deUint32 deUintMaxValue32 (int numBits)
+{
+       DE_ASSERT(deInRange32(numBits, 1, 32));
+       if (numBits < 32)
+               return ((1u<<numBits)-1u);
+       else
+               return 0xFFFFFFFFu;
+}
+
+DE_INLINE deInt32 deIntMaxValue32 (int numBits)
+{
+       DE_ASSERT(deInRange32(numBits, 1, 32));
+       if (numBits < 32)
+               return ((deInt32)1 << (numBits - 1)) - 1;
+       else
+       {
+               /* avoid undefined behavior of int overflow when shifting */
+               return 0x7FFFFFFF;
+       }
+}
+
+DE_INLINE deInt32 deIntMinValue32 (int numBits)
+{
+       DE_ASSERT(deInRange32(numBits, 1, 32));
+       if (numBits < 32)
+               return -((deInt32)1 << (numBits - 1));
+       else
+       {
+               /* avoid undefined behavior of int overflow when shifting */
+               return (deInt32)(-0x7FFFFFFF - 1);
+       }
+}
+
 DE_END_EXTERN_C
 
 #endif /* _DEINT32_H */
index b7cd360..eaf20dc 100644 (file)
@@ -130,6 +130,31 @@ void deInt32_selfTest (void)
                        DE_TEST_ASSERT(exp == numBits);
                }
        }
+
+       DE_TEST_ASSERT(deBitMask32(0, 0) == 0);
+       DE_TEST_ASSERT(deBitMask32(8, 0) == 0);
+       DE_TEST_ASSERT(deBitMask32(16, 0) == 0);
+       DE_TEST_ASSERT(deBitMask32(31, 0) == 0);
+       DE_TEST_ASSERT(deBitMask32(32, 0) == 0);
+
+       DE_TEST_ASSERT(deBitMask32(0, 2) == 3);
+       DE_TEST_ASSERT(deBitMask32(0, 32) == 0xFFFFFFFFu);
+
+       DE_TEST_ASSERT(deBitMask32(16, 16) == 0xFFFF0000u);
+       DE_TEST_ASSERT(deBitMask32(31, 1) == 0x80000000u);
+       DE_TEST_ASSERT(deBitMask32(8, 4) == 0xF00u);
+
+       DE_TEST_ASSERT(deUintMaxValue32(1) == 1);
+       DE_TEST_ASSERT(deUintMaxValue32(2) == 3);
+       DE_TEST_ASSERT(deUintMaxValue32(32) == 0xFFFFFFFFu);
+
+       DE_TEST_ASSERT(deIntMaxValue32(1) == 0);
+       DE_TEST_ASSERT(deIntMaxValue32(2) == 1);
+       DE_TEST_ASSERT(deIntMaxValue32(32) == 0x7FFFFFFF);
+
+       DE_TEST_ASSERT(deIntMinValue32(1) == -1);
+       DE_TEST_ASSERT(deIntMinValue32(2) == -2);
+       DE_TEST_ASSERT(deIntMinValue32(32) == -0x7FFFFFFF - 1);
 }
 
 DE_END_EXTERN_C
index 8f2dc8f..44f61f4 100644 (file)
@@ -143,7 +143,6 @@ void deCommandLine_destroy (deCommandLine* cmdLine)
        deFree(cmdLine);
 }
 
-#if defined(DE_DEBUG)
 static void testParse (const char* cmdLine, const char* const* refArgs, int numArgs)
 {
        deCommandLine*  parsedCmdLine   = deCommandLine_parse(cmdLine);
@@ -216,4 +215,3 @@ void deCommandLine_selfTest (void)
                testParse(cmdLine, ref, DE_LENGTH_OF_ARRAY(ref));
        }
 }
-#endif
index f171a74..b607e05 100644 (file)
@@ -37,9 +37,7 @@ typedef struct deCommandLine_s
 deCommandLine* deCommandLine_parse             (const char* cmdLine);
 void                   deCommandLine_destroy   (deCommandLine* cmdLine);
 
-#if defined(DE_DEBUG)
 void                   deCommandLine_selfTest  (void);
-#endif
 
 DE_END_EXTERN_C
 
index df47341..f2bc06c 100644 (file)
 
 // deutil
 #include "deTimerTest.h"
+#include "deCommandLine.h"
+
+// debase
+#include "deInt32.h"
 
 // decpp
 #include "deBlockBuffer.hpp"
@@ -53,6 +57,7 @@
 #include "deArrayBuffer.hpp"
 #include "deStringUtil.hpp"
 #include "deSpinBarrier.hpp"
+#include "deSTLUtil.hpp"
 
 namespace dit
 {
@@ -135,7 +140,22 @@ public:
 
        void init (void)
        {
-               addChild(new SelfCheckCase(m_testCtx, "timer",  "deTimer_selfTest()",   deTimer_selfTest));
+               addChild(new SelfCheckCase(m_testCtx, "timer",                  "deTimer_selfTest()",           deTimer_selfTest));
+               addChild(new SelfCheckCase(m_testCtx, "command_line",   "deCommandLine_selfTest()",     deCommandLine_selfTest));
+       }
+};
+
+class DebaseTests : public tcu::TestCaseGroup
+{
+public:
+       DebaseTests (tcu::TestContext& testCtx)
+               : tcu::TestCaseGroup(testCtx, "debase", "debase self-tests")
+       {
+       }
+
+       void init (void)
+       {
+               addChild(new SelfCheckCase(m_testCtx, "int32",  "deInt32_selfTest()",   deInt32_selfTest));
        }
 };
 
@@ -161,6 +181,7 @@ public:
                addChild(new SelfCheckCase(m_testCtx, "array_buffer",                           "de::ArrayBuffer_selfTest()",                   de::ArrayBuffer_selfTest));
                addChild(new SelfCheckCase(m_testCtx, "string_util",                            "de::StringUtil_selfTest()",                    de::StringUtil_selfTest));
                addChild(new SelfCheckCase(m_testCtx, "spin_barrier",                           "de::SpinBarrier_selfTest()",                   de::SpinBarrier_selfTest));
+               addChild(new SelfCheckCase(m_testCtx, "stl_util",                                       "de::STLUtil_selfTest()",                               de::STLUtil_selfTest));
        }
 };
 
@@ -178,6 +199,7 @@ void DelibsTests::init (void)
        addChild(new DepoolTests        (m_testCtx));
        addChild(new DethreadTests      (m_testCtx));
        addChild(new DeutilTests        (m_testCtx));
+       addChild(new DebaseTests        (m_testCtx));
        addChild(new DecppTests         (m_testCtx));
 }