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