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