utestBuffer.cpp 3.49 KB
Newer Older
1
// See LICENCE file at project root
2 3 4
#
#include <cstddef>

5 6
#include "FUTester.hpp"

BRAMAS Berenger's avatar
BRAMAS Berenger committed
7 8
#include "Containers/FBufferReader.hpp"
#include "Containers/FBufferWriter.hpp"
9 10 11 12 13 14 15 16 17 18



/** this class test the buffers container */
class TestBuffer : public FUTester<TestBuffer> {

        // test size
        void TestWriteRead(){
            FBufferWriter writer;

19
            const int BytesTested = static_cast<int>(sizeof(int)+sizeof(char)+sizeof(double)+sizeof(float));
20 21 22 23 24
            const int NbTest = 5;
            for(int idxWrite = 0 ; idxWrite < NbTest ; ++idxWrite){
                writer << idxWrite << char(idxWrite) << double(idxWrite) << float(idxWrite);
            }

25 26
            uassert(writer.getCapacity() >= NbTest*BytesTested);
            uassert(writer.getSize() == NbTest*BytesTested);
27 28

            FBufferReader reader(writer.getSize());
29 30
            uassert(reader.getSize() == 0);
            uassert(reader.getCapacity() == writer.getSize());
31 32 33 34 35 36 37 38 39

            memcpy(reader.data(), writer.data(), writer.getSize());
            for(int idxRead = 0 ; idxRead < NbTest ; ++idxRead){
                int intval;
                char charval;
                double doubleval;
                float floatval;
                reader >> intval >> charval >> doubleval >> floatval;

40 41 42 43
                uassert(intval == idxRead);
                uassert(charval == char(idxRead));
                uassert(doubleval == double(idxRead));
                uassert(floatval == float(idxRead));
44

45
                uassert(reader.tell() == (BytesTested * (idxRead+1)));
46 47
            }

48
            uassert(reader.tell() == reader.getCapacity());
49
            reader.seek(0);
50
            uassert(reader.tell() == 0);
51 52

            for(int idxRead = 0 ; idxRead < NbTest ; ++idxRead){
53 54 55 56
                uassert(reader.getValue<int>() == idxRead);
                uassert(reader.getValue<char>() == char(idxRead));
                uassert(reader.getValue<double>() == double(idxRead));
                uassert(reader.getValue<float>() == float(idxRead));
57

58
                uassert(reader.tell() == (BytesTested * (idxRead+1)));
59 60
            }

61
            uassert(reader.tell() == reader.getCapacity());
62 63 64 65 66 67 68 69 70
        }


        void TestWriteAt(){
            FBufferWriter writer;

            const int SizeOfInt = int(sizeof(int));
            const int NbTest = 5;
            for(int idxWrite = 0 ; idxWrite < NbTest ; ++idxWrite){
71
                const FSize position = writer.getSize();
72

73
                uassert(position == (NbTest * SizeOfInt * idxWrite) + (idxWrite * SizeOfInt));
74 75 76

                writer.FBufferWriter::write<int>(0);

77
                uassert(writer.getSize() == (NbTest * SizeOfInt * idxWrite) + (idxWrite * SizeOfInt) + SizeOfInt);
78 79 80 81 82 83 84 85 86

                for(int count = 1 ; count <= NbTest ; ++count) writer << count;
                writer.writeAt(position, idxWrite);
            }

            FBufferReader reader(writer.getSize());
            memcpy(reader.data(), writer.data(), writer.getSize());

            for(int idxWrite = 0 ; idxWrite < NbTest ; ++idxWrite){
87
                uassert(reader.FBufferReader::getValue<int>() == idxWrite);
88
                for(int count = 1 ; count <= NbTest ; ++count){
89
                    uassert(reader.FBufferReader::getValue<int>() == count);
90 91 92 93 94 95 96 97 98 99 100 101 102 103
                }
            }
        }


        // set test
        void SetTests(){
            AddTest(&TestBuffer::TestWriteRead,"Test Write then Read");
            AddTest(&TestBuffer::TestWriteAt,"Test Write at then Read");
        }
};

// You must do this
TestClass(TestBuffer)