utestBuffer.cpp 4.24 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
#
#include <cstddef>

19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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;

33
            const int BytesTested = static_cast<int>(sizeof(int)+sizeof(char)+sizeof(double)+sizeof(float));
34
35
36
37
38
            const int NbTest = 5;
            for(int idxWrite = 0 ; idxWrite < NbTest ; ++idxWrite){
                writer << idxWrite << char(idxWrite) << double(idxWrite) << float(idxWrite);
            }

39
            uassert(writer.getSize() == (NbTest*BytesTested));
40
41

            FBufferReader reader(writer.getSize());
42
            uassert(reader.getSize() == writer.getSize());
43
44
45
46
47
48
49
50
51

            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;

52
53
54
55
                uassert(intval == idxRead);
                uassert(charval == char(idxRead));
                uassert(doubleval == double(idxRead));
                uassert(floatval == float(idxRead));
56

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

60
            uassert(reader.tell() == reader.getSize());
61
            reader.seek(0);
62
            uassert(reader.tell() == 0);
63
64

            for(int idxRead = 0 ; idxRead < NbTest ; ++idxRead){
65
66
67
68
                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));
69

70
                uassert(reader.tell() == (BytesTested * (idxRead+1)));
71
72
            }

73
            uassert(reader.tell() == reader.getSize());
74
75
76
77
78
79
80
81
82
83
84
        }


        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();

85
                uassert(position == (NbTest * SizeOfInt * idxWrite) + (idxWrite * SizeOfInt));
86
87
88

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

89
                uassert(writer.getSize() == (NbTest * SizeOfInt * idxWrite) + (idxWrite * SizeOfInt) + SizeOfInt);
90
91
92
93
94
95
96
97
98

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


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