• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "gtest/gtest.h"
2 #include "memory_align.h"
3 
4 using namespace WelsCommon;
5 
6 //Tests of WelsGetCacheLineSize Begin
TEST(MemoryAlignTest,GetCacheLineSize_LoopWithin16K)7 TEST (MemoryAlignTest, GetCacheLineSize_LoopWithin16K) {
8   const unsigned int kuiTestBoundary16K = 16 * 1024;
9   unsigned int uiTargetAlign = 1;
10   while (uiTargetAlign < kuiTestBoundary16K) {
11     CMemoryAlign cTestMa (uiTargetAlign);
12     ASSERT_EQ ((uiTargetAlign & 0x0F) ? 16 : uiTargetAlign, cTestMa.WelsGetCacheLineSize());
13     ++ uiTargetAlign;
14   }
15 }
16 
TEST(MemoryAlignTest,GetCacheLineSize_Zero)17 TEST (MemoryAlignTest, GetCacheLineSize_Zero) {
18   CMemoryAlign cTestMa (0);
19   const uint32_t kuiSixteen = 16;
20   ASSERT_EQ (kuiSixteen, cTestMa.WelsGetCacheLineSize());
21 }
TEST(MemoryAlignTest,GetCacheLineSize_MaxUINT)22 TEST (MemoryAlignTest, GetCacheLineSize_MaxUINT) {
23   CMemoryAlign cTestMa (0xFFFFFFFF);
24   const uint32_t kuiSixteen = 16;
25   ASSERT_EQ (kuiSixteen, cTestMa.WelsGetCacheLineSize());
26 }
27 //Tests of WelsGetCacheLineSize End
28 //Tests of WelsMallocAndFree Begin
TEST(MemoryAlignTest,WelsMallocAndFreeOnceFunctionVerify)29 TEST (MemoryAlignTest, WelsMallocAndFreeOnceFunctionVerify) {
30   const uint32_t kuiTargetAlignSize[4] = {32, 16, 64, 8};
31   const uint32_t kuiZero = 0;
32   for (int i = 0; i < 4; i++) {
33     const uint32_t kuiTestAlignSize = kuiTargetAlignSize[i];
34     const uint32_t kuiTestDataSize  = abs (rand());
35 
36     CMemoryAlign cTestMa (kuiTestAlignSize);
37     const uint32_t uiSize = kuiTestDataSize;
38     const char strUnitTestTag[100] = "pUnitTestData";
39     const uint32_t kuiUsedCacheLineSize = ((kuiTestAlignSize == 0)
40                                            || (kuiTestAlignSize & 0x0F)) ? (16) : (kuiTestAlignSize);
41     const uint32_t kuiExtraAlignSize    = kuiUsedCacheLineSize - 1;
42     const uint32_t kuiExpectedSize      = sizeof (void**) + sizeof (int32_t) + kuiExtraAlignSize + uiSize;
43     uint8_t* pUnitTestData = static_cast<uint8_t*> (cTestMa.WelsMalloc (uiSize, strUnitTestTag));
44     if (pUnitTestData != NULL) {
45       ASSERT_TRUE ((((uintptr_t) (pUnitTestData)) & kuiExtraAlignSize) == 0);
46       EXPECT_EQ (kuiExpectedSize, cTestMa.WelsGetMemoryUsage());
47       cTestMa.WelsFree (pUnitTestData, strUnitTestTag);
48       EXPECT_EQ (kuiZero, cTestMa.WelsGetMemoryUsage());
49     } else {
50       EXPECT_EQ (NULL, pUnitTestData);
51       EXPECT_EQ (kuiZero, cTestMa.WelsGetMemoryUsage());
52       cTestMa.WelsFree (pUnitTestData, strUnitTestTag);
53       EXPECT_EQ (kuiZero, cTestMa.WelsGetMemoryUsage());
54     }
55   }
56 }
57