utestBuffer.cpp 3.43 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
            uassert(writer.getSize() == (NbTest*BytesTested));
26 27

            FBufferReader reader(writer.getSize());
28
            uassert(reader.getSize() == writer.getSize());
29 30 31 32 33 34 35 36 37

            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;

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

43
                uassert(reader.tell() == (BytesTested * (idxRead+1)));
44 45
            }

46
            uassert(reader.tell() == reader.getSize());
47
            reader.seek(0);
48
            uassert(reader.tell() == 0);
49 50

            for(int idxRead = 0 ; idxRead < NbTest ; ++idxRead){
51 52 53 54
                uassert(reader.FBufferReader::getValue<int>() == idxRead);
                uassert(reader.FBufferReader::getValue<char>() == char(idxRead));
                uassert(reader.FBufferReader::getValue<double>() == double(idxRead));
                uassert(reader.FBufferReader::getValue<float>() == float(idxRead));
55

56
                uassert(reader.tell() == (BytesTested * (idxRead+1)));
57 58
            }

59
            uassert(reader.tell() == reader.getSize());
60 61 62 63 64 65 66 67 68
        }


        void TestWriteAt(){
            FBufferWriter writer;

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

71
                uassert(position == (NbTest * SizeOfInt * idxWrite) + (idxWrite * SizeOfInt));
72 73 74

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

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

                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){
85
                uassert(reader.FBufferReader::getValue<int>() == idxWrite);
86
                for(int count = 1 ; count <= NbTest ; ++count){
87
                    uassert(reader.FBufferReader::getValue<int>() == count);
88 89 90 91 92 93 94 95 96 97 98 99 100 101
                }
            }
        }


        // 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)