shithub: openh264

Download patch

ref: a922155c9ac62416ba4dc318b2b2b62ab75ff179
parent: 1477f36de2278026dfcfdc4f11a6c81abc033d88
parent: 53a570556dddc6f65d7b02a08ab91465b736dc0d
author: ruil2 <[email protected]>
date: Mon Mar 10 13:25:41 EDT 2014

Merge pull request #466 from sijchen/add_memalign_test

Add memalign unit test

--- a/codec/encoder/core/inc/memory_align.h
+++ b/codec/encoder/core/inc/memory_align.h
@@ -50,9 +50,7 @@
 void* WelsMalloc (const uint32_t kuiSize, const char* kpTag);
 void WelsFree (void* pPointer, const char* kpTag);
 const uint32_t WelsGetCacheLineSize() const;
-#if defined(MEMORY_MONITOR)
 const uint32_t WelsGetMemoryUsage() const;
-#endif//MEMORY_MONITOR
 
  private:
 // private copy & assign constructors adding to fix klocwork scan issues
--- a/codec/encoder/core/src/memory_align.cpp
+++ b/codec/encoder/core/src/memory_align.cpp
@@ -139,10 +139,8 @@
   return m_nCacheLineSize;
 }
 
-#if defined(MEMORY_MONITOR)
 const uint32_t CMemoryAlign::WelsGetMemoryUsage() const {
   return m_nMemoryUsageInBytes;
 }
-#endif//MEMORY_MONITOR
 
 } // end of namespace WelsSVCEnc
--- /dev/null
+++ b/test/EncUT_MemoryAlloc.cpp
@@ -1,0 +1,62 @@
+#include "../gtest/include/gtest/gtest.h"
+#include <string.h>		// use memset/memcmp
+#include "../codec/encoder/core/inc/memory_align.h"
+
+using namespace WelsSVCEnc;
+
+//Tests of WelsGetCacheLineSize Begin
+TEST(MemoryAlignTest, GetCacheLineSize_LoopWithin16K)
+{
+  const unsigned int kuiTestBoundary16K = 16 * 1024;
+	unsigned int uiTargetAlign = 1;
+	while (uiTargetAlign < kuiTestBoundary16K) {
+		CMemoryAlign cTestMa(uiTargetAlign);
+    ASSERT_EQ(  (uiTargetAlign & 0x0F)?16:uiTargetAlign, cTestMa.WelsGetCacheLineSize() );
+		++ uiTargetAlign;
+	}
+}
+
+TEST(MemoryAlignTest, GetCacheLineSize_Zero)
+{
+  CMemoryAlign cTestMa(0);
+  ASSERT_EQ(  16, cTestMa.WelsGetCacheLineSize() );
+}
+TEST(MemoryAlignTest, GetCacheLineSize_MaxUINT)
+{
+	CMemoryAlign cTestMa(0xFFFFFFFF);
+	ASSERT_EQ( 16, cTestMa.WelsGetCacheLineSize() );
+}
+//Tests of WelsGetCacheLineSize End
+//Tests of WelsMallocAndFree Begin
+TEST(MemoryAlignTest, WelsMallocAndFreeOnceFunctionVerify)
+{
+  const uint32_t kuiTargetAlignSize[4] = {32, 16, 64, 8};
+  srand((uint32_t)time(NULL));
+
+  for (int i=0; i<4; i++) {
+    const uint32_t kuiTestAlignSize	= kuiTargetAlignSize[i];
+    const uint32_t kuiTestDataSize		= abs(rand());
+
+    CMemoryAlign cTestMa(kuiTestAlignSize);
+    const uint32_t uiSize = kuiTestDataSize;
+    const char strUnitTestTag[100] = "pUnitTestData";
+    const uint32_t kuiUsedCacheLineSize	= ((kuiTestAlignSize == 0) || (kuiTestAlignSize & 0x0F)) ? (16) : (kuiTestAlignSize);
+    const uint32_t kuiExtraAlignSize	= kuiUsedCacheLineSize-1;
+    const uint32_t kuiExpectedSize	= sizeof( void ** ) + sizeof( int32_t ) + kuiExtraAlignSize + uiSize;
+    uint8_t *pUnitTestData = static_cast<uint8_t *>(cTestMa.WelsMalloc(uiSize, strUnitTestTag));
+    if ( pUnitTestData != NULL ) {
+      ASSERT_TRUE( (((int64_t)(static_cast<void *>(pUnitTestData))) & kuiExtraAlignSize) == 0 );
+      EXPECT_EQ( kuiExpectedSize, cTestMa.WelsGetMemoryUsage() );
+      cTestMa.WelsFree( pUnitTestData, strUnitTestTag );
+      EXPECT_EQ( 0, cTestMa.WelsGetMemoryUsage() );
+    }
+    else {
+      EXPECT_EQ( NULL, pUnitTestData );
+      EXPECT_EQ( 0, cTestMa.WelsGetMemoryUsage() );
+      cTestMa.WelsFree( pUnitTestData, strUnitTestTag );
+      EXPECT_EQ( 0, cTestMa.WelsGetMemoryUsage() );
+    }
+  }
+}
+
+
--- a/test/targets.mk
+++ b/test/targets.mk
@@ -7,6 +7,7 @@
 	$(CODEC_UNITTEST_SRCDIR)/decoder_test.cpp\
 	$(CODEC_UNITTEST_SRCDIR)/encoder_test.cpp\
 	$(CODEC_UNITTEST_SRCDIR)/simple_test.cpp\
+	$(CODEC_UNITTEST_SRCDIR)/EncUT_MemoryAlloc.cpp\
 
 CODEC_UNITTEST_OBJS += $(CODEC_UNITTEST_CPP_SRCS:.cpp=.o)