1 //===- RTLinearAllocatorTest.cpp ------------------------------------------===// 2 // 3 // The MCLinker Project 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #include "mcld/Support/Allocators.h" 10 #include <RTLinearAllocatorTest.h> 11 12 using namespace mcld; 13 using namespace mcldtest; 14 15 16 // Constructor can do set-up work for all test here. 17 RTLinearAllocatorTest::RTLinearAllocatorTest() 18 { 19 // create testee. modify it if need 20 m_pTestee = new LinearAllocator<Data, 0>(CHUNK_SIZE); 21 } 22 23 // Destructor can do clean-up work that doesn't throw exceptions here. 24 RTLinearAllocatorTest::~RTLinearAllocatorTest() 25 { 26 delete m_pTestee; 27 } 28 29 // SetUp() will be called immediately before each test. 30 void RTLinearAllocatorTest::SetUp() 31 { 32 } 33 34 // TearDown() will be called immediately after each test. 35 void RTLinearAllocatorTest::TearDown() 36 { 37 } 38 39 //==========================================================================// 40 // Testcases 41 // 42 43 TEST_F(RTLinearAllocatorTest, AllocateN) { 44 Data* pointer = m_pTestee->allocate(10); 45 ASSERT_FALSE(0 == pointer); 46 ASSERT_TRUE(CHUNK_SIZE == m_pTestee->max_size()); 47 ASSERT_FALSE(m_pTestee->empty()); 48 } 49 50 TEST_F(RTLinearAllocatorTest, allocate ) { 51 Data* pointer = m_pTestee->allocate(); 52 ASSERT_FALSE(0 == pointer); 53 ASSERT_TRUE(CHUNK_SIZE == m_pTestee->max_size()); 54 ASSERT_FALSE(m_pTestee->empty()); 55 } 56 57 TEST_F(RTLinearAllocatorTest, allocateOver ) { 58 Data* pointer = m_pTestee->allocate(CHUNK_SIZE+1); 59 ASSERT_TRUE(0 == pointer); 60 ASSERT_TRUE(0 == m_pTestee->max_size()); 61 ASSERT_TRUE(m_pTestee->empty()); 62 } 63 64 TEST_F(RTLinearAllocatorTest, alloc_construct ) { 65 Data* pointer = m_pTestee->allocate(); 66 m_pTestee->construct(pointer); 67 ASSERT_TRUE(1 == pointer->one); 68 ASSERT_TRUE(2 == pointer->two); 69 ASSERT_TRUE(3 == pointer->three); 70 ASSERT_TRUE(4 == pointer->four); 71 } 72 73 TEST_F(RTLinearAllocatorTest, alloc_constructCopy ) { 74 Data* pointer = m_pTestee->allocate(); 75 Data data(7, 7, 7, 7); 76 m_pTestee->construct(pointer, data); 77 78 ASSERT_TRUE(7 == pointer->one); 79 ASSERT_TRUE(7 == pointer->two); 80 ASSERT_TRUE(7 == pointer->three); 81 ASSERT_TRUE(7 == pointer->four); 82 } 83 84 TEST_F(RTLinearAllocatorTest, allocN_construct ) { 85 Data* pointer = m_pTestee->allocate(10); 86 m_pTestee->construct(pointer); 87 ASSERT_TRUE(1 == pointer->one); 88 ASSERT_TRUE(2 == pointer->two); 89 ASSERT_TRUE(3 == pointer->three); 90 ASSERT_TRUE(4 == pointer->four); 91 } 92 93 TEST_F(RTLinearAllocatorTest, allocN_constructCopy ) { 94 Data* pointer = m_pTestee->allocate(10); 95 Data data(7, 7, 7, 7); 96 m_pTestee->construct(pointer, data); 97 98 ASSERT_TRUE(7 == pointer->one); 99 ASSERT_TRUE(7 == pointer->two); 100 ASSERT_TRUE(7 == pointer->three); 101 ASSERT_TRUE(7 == pointer->four); 102 } 103 104 TEST_F(RTLinearAllocatorTest, multi_alloc_ctor_iterate ) { 105 for (int i=0; i<101; ++i) { 106 Data* pointer = m_pTestee->allocate(); 107 m_pTestee->construct(pointer); 108 pointer->one = i; 109 } 110 /** 111 Alloc::iterator data, dEnd = m_pTestee->end(); 112 int counter = 0; 113 for (data=m_pTestee->begin(); data!=dEnd; ++data) { 114 ASSERT_EQ(counter, (*data).one); 115 ++counter; 116 } 117 **/ 118 } 119 120 TEST_F(RTLinearAllocatorTest, multi_allocN_ctor_iterate ) { 121 int counter = 0; 122 for (int i=0; i<10000; ++i) { 123 Data* pointer = m_pTestee->allocate(10); 124 for (int j=0; j<10; ++j) { 125 m_pTestee->construct(pointer); 126 pointer->one = counter; 127 ++pointer; 128 ++counter; 129 } 130 } 131 /** 132 Alloc::iterator data, dEnd = m_pTestee->end(); 133 counter = 0; 134 for (data=m_pTestee->begin(); data!=dEnd; ++data) { 135 ASSERT_EQ(counter, (*data).one); 136 ++counter; 137 } 138 **/ 139 } 140 141