utestListBlockAllocator.cpp 2.26 KB
Newer Older
1
// See LICENCE file at project root
2
#include "FUTester.hpp"
COULAUD Olivier's avatar
COULAUD Olivier committed
3

BRAMAS Berenger's avatar
BRAMAS Berenger committed
4
#include "Containers/FBlockAllocator.hpp"
COULAUD Olivier's avatar
COULAUD Olivier committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

#include <cstring>

/**
* This file is a unit test for the List block allocator
*/

/**
* This class is simply used to count alloc dealloc
*/
static const int SizeArray = 50;
class TestObject{
public:
    static int counter;
    static int dealloced;

    int array[SizeArray];

    TestObject(){
        memset(array, 0, SizeArray * sizeof(int));
        ++counter;
    }
    TestObject(const TestObject&){
        ++counter;
    }
    ~TestObject(){
        ++dealloced;
    }
};

int TestObject::counter(0);
int TestObject::dealloced(0);


/** this class test the list container */
class TestBlock : public FUTester<TestBlock> {
    // Called before each test : simply set counter to 0
    void PreTest(){
        TestObject::counter = 0;
        TestObject::dealloced = 0;
    }

    // test copy
    void TestBlockFunction(){
        FListBlockAllocator<TestObject, 10> alloc;

        const int NbAlloc = 2;
        TestObject* ptr[NbAlloc];
        for(int idx = 0 ; idx < NbAlloc ; ++idx){
            TestObject* dl1 = alloc.newObject();
            TestObject* dl2 = alloc.newObject();
            alloc.deleteObject(dl1);
            ptr[idx] = alloc.newObject();
            alloc.deleteObject(dl2);
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
61 62 63 64 65 66
        // Just put on volatile to avoid loop unrolling (which leads to a bug with my gcc!)
        volatile int volSizeArray = SizeArray;
        volatile int volNbAlloc = NbAlloc;

        for(int idx = 0 ; idx < volNbAlloc ; ++idx){
            for(int idxval = 0 ; idxval < volSizeArray ; ++idxval){
COULAUD Olivier's avatar
COULAUD Olivier committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
                ptr[idx]->array[idxval] += (idxval * idx);
            }
        }

        for(int idx = 0 ; idx < NbAlloc ; ++idx){
            for(int idxval = 0 ; idxval < SizeArray ; ++idxval){
                uassert(ptr[idx]->array[idxval] == (idxval * idx));
            }
        }

        for(int idx = 0 ; idx < NbAlloc ; ++idx){
            alloc.deleteObject(ptr[idx]);
        }

        uassert(TestObject::counter == (3*NbAlloc));
        uassert(TestObject::counter == TestObject::dealloced);
    }

    // set test
    void SetTests(){
            AddTest(&TestBlock::TestBlockFunction,"Test Allocate Deallocate");
    }
};

// You must do this
TestClass(TestBlock)