utestAlignedMemory.cpp 3.06 KB
Newer Older
1
// See LICENCE file at project root
2 3 4 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 61 62 63 64 65 66 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#include "FUTester.hpp"

#include "../Src/Utils/FAlignedMemory.hpp"
#include "../Src/Utils/FTemplate.hpp"

/**
* This file is a unit test for the alignement class
*/



template <std::size_t Value>
struct IncClass{
    static const std::size_t NextValue = (Value<<1);
};

struct CheckClass {
    static int nbObjects;
    int idObject;
    int flag;
    CheckClass(){
        idObject = nbObjects;
        nbObjects += 1;
        flag = -1;
    }
    ~CheckClass(){
        nbObjects -= 1;
    }
};
int CheckClass::nbObjects = 0;


/** this class test the bool array container */
class TestAlignement : public FUTester<TestAlignement> {

    void TestSimple(){
        for(std::size_t idxSize = 1 ; idxSize < 1000 ; idxSize *= 10){
            {
                void* ptr = FAlignedMemory::AllocateBytes<2>(idxSize);
                uassert((reinterpret_cast<std::size_t>(ptr) & (2-1)) == 0);
                FAlignedMemory::DeallocBytes(ptr);
            }
            {
                void* ptr = FAlignedMemory::AllocateBytes<8>(idxSize);
                uassert((reinterpret_cast<std::size_t>(ptr) & (8-1)) == 0);
                FAlignedMemory::DeallocBytes(ptr);
            }
            {
                void* ptr = FAlignedMemory::AllocateBytes<16>(idxSize);
                uassert((reinterpret_cast<std::size_t>(ptr) & (16-1)) == 0);
                FAlignedMemory::DeallocBytes(ptr);
            }
            {
                void* ptr = FAlignedMemory::AllocateBytes<32>(idxSize);
                uassert((reinterpret_cast<std::size_t>(ptr) & (22-1)) == 0);
                FAlignedMemory::DeallocBytes(ptr);
            }
            {
                void* ptr = FAlignedMemory::AllocateBytes<64>(idxSize);
                uassert((reinterpret_cast<std::size_t>(ptr) & (64-1)) == 0);
                FAlignedMemory::DeallocBytes(ptr);
            }
        }
    }

public:
    template <std::size_t Allign>
    void For(){
        for(std::size_t idxSize = 1 ; idxSize < 1000 ; idxSize *= 10){
            void* ptr = FAlignedMemory::AllocateBytes<Allign>(idxSize);
            //uassert((reinterpret_cast<std::size_t>(ptr) & (Allign-1)) == 0);
            FAlignedMemory::DeallocBytes(ptr);
        }
    }

    void TestAll(){
        FForAllThisWithInc::For<std::size_t, 1, 128+1, IncClass, TestAlignement>(this);
    }


    void TestArray(){
        CheckClass::nbObjects = 0;

        const int nb = 5;

        CheckClass* array = FAlignedMemory::AllocateArray<8, CheckClass>( nb );
        uassert(CheckClass::nbObjects == nb);
        for(int idx = 0 ; idx < nb ; ++idx){
            uassert(array[idx].flag == -1);
            uassert(array[idx].idObject == idx);
        }

        FAlignedMemory::DeallocArray( array );
        uassert(CheckClass::nbObjects == 0);
    }


    // set test
    void SetTests(){
        AddTest(&TestAlignement::TestSimple,"Test if aligned");
        AddTest(&TestAlignement::TestAll,"Test if all aligned");
        AddTest(&TestAlignement::TestArray,"Test if array are correct");
    }
};

// You must do this
TestClass(TestAlignement)