utestBuffer.cpp 4.2 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Bérenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.  
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info". 
// "http://www.gnu.org/licenses".
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// ===================================================================================
#include "FUTester.hpp"

#include "../Src/Containers/FBufferReader.hpp"
#include "../Src/Containers/FBufferWriter.hpp"



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

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

            const int BytesTested = (sizeof(int)+sizeof(char)+sizeof(double)+sizeof(float));
            const int NbTest = 5;
            for(int idxWrite = 0 ; idxWrite < NbTest ; ++idxWrite){
                writer << idxWrite << char(idxWrite) << double(idxWrite) << float(idxWrite);
            }

36
            uassert(writer.getSize() == (NbTest*BytesTested));
37 38

            FBufferReader reader(writer.getSize());
39
            uassert(reader.getSize() == writer.getSize());
40 41 42 43 44 45 46 47 48

            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;

49 50 51 52
                uassert(intval == idxRead);
                uassert(charval == char(idxRead));
                uassert(doubleval == double(idxRead));
                uassert(floatval == float(idxRead));
53

54
                uassert(reader.tell() == (BytesTested * (idxRead+1)));
55 56
            }

57
            uassert(reader.tell() == reader.getSize());
58
            reader.seek(0);
59
            uassert(reader.tell() == 0);
60 61

            for(int idxRead = 0 ; idxRead < NbTest ; ++idxRead){
62 63 64 65
                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));
66

67
                uassert(reader.tell() == (BytesTested * (idxRead+1)));
68 69
            }

70
            uassert(reader.tell() == reader.getSize());
71 72 73 74 75 76 77 78 79 80 81
        }


        void TestWriteAt(){
            FBufferWriter writer;

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

82
                uassert(position == (NbTest * SizeOfInt * idxWrite) + (idxWrite * SizeOfInt));
83 84 85

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

86
                uassert(writer.getSize() == (NbTest * SizeOfInt * idxWrite) + (idxWrite * SizeOfInt) + SizeOfInt);
87 88 89 90 91 92 93 94 95

                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){
96
                uassert(reader.FBufferReader::getValue<int>() == idxWrite);
97
                for(int count = 1 ; count <= NbTest ; ++count){
98
                    uassert(reader.FBufferReader::getValue<int>() == count);
99 100 101 102 103 104 105 106 107 108 109 110 111 112
                }
            }
        }


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