utestNeighborIndexes.cpp 9.17 KB
Newer Older
1
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
// ===================================================================================
// 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".
// ===================================================================================
#include "FUTester.hpp"

#include "Containers/FTreeCoordinate.hpp"
#include "Containers/FNeighborIndexes.hpp"

/**
* This file is a unit test for the FNeigborIndexes classes
*/


/** this class test the list container */
class TestIndexes : public FUTester<TestIndexes> {
    void MortonLimite(){
        for(int idxLevel = 1 ; idxLevel < MaxTreeHeight ; ++idxLevel){
            const int limit = FMath::pow2(idxLevel);
            for(int x = 0 ; x < 3 ; ++x){
                const int xbox = (x*(limit-1))/2;
                for(int y = 0 ; y < 3 ; ++y){
                    const int ybox = (y*(limit-1))/2;
                    for(int z = 0 ; z < 3 ; ++z){
                        const int zbox = (z*(limit-1))/2;

38
                        const MortonIndex mindex = FTreeCoordinate(xbox, ybox, zbox).getMortonIndex();
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

                        FCoordinateNeighborIndex coordindexes(mindex, idxLevel);
                        FBitsNeighborIndex bitsindex(mindex, idxLevel);

                        uassert(coordindexes.minX() == bitsindex.minX());
                        uassert(coordindexes.minY() == bitsindex.minY());
                        uassert(coordindexes.minZ() == bitsindex.minZ());

                        uassert(coordindexes.maxX() == bitsindex.maxX());
                        uassert(coordindexes.maxY() == bitsindex.maxY());
                        uassert(coordindexes.maxZ() == bitsindex.maxZ());

                        for(int idxX = coordindexes.minX() ; idxX <= coordindexes.maxX() ; ++idxX){
                            for(int idxY = coordindexes.minY() ; idxY <= coordindexes.maxY() ; ++idxY){
                                for(int idxZ = coordindexes.minZ() ; idxZ <= coordindexes.maxZ() ; ++idxZ){
                                    if(idxX || idxY || idxZ){
                                        uassert(coordindexes.getIndex(idxX, idxY, idxZ)
                                                == bitsindex.getIndex(idxX, idxY, idxZ));
                                        const MortonIndex neigh = bitsindex.getIndex(idxX, idxY, idxZ);
58
                                        const FTreeCoordinate neighCoord(neigh);
59
60
61
                                        uassert(xbox + idxX == neighCoord.getX());
                                        uassert(ybox + idxY == neighCoord.getY());
                                        uassert(zbox + idxZ == neighCoord.getZ());
62
63

                                        uassert(bitsindex.areNeighbors(mindex, neigh));
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    void Morton(){
        srand(0);
        for(int idxLevel = 1 ; idxLevel < MaxTreeHeight ; ++idxLevel){
            const int limit = FMath::pow2(idxLevel);
            for(int idxTest = 0 ; idxTest < 100 ; ++idxTest){
                const int xbox = int(drand48()*double(limit));
                const int ybox = int(drand48()*double(limit));
                const int zbox = int(drand48()*double(limit));

83
                const MortonIndex mindex = FTreeCoordinate(xbox, ybox, zbox).getMortonIndex();
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

                FCoordinateNeighborIndex coordindexes(mindex, idxLevel);
                FBitsNeighborIndex bitsindex(mindex, idxLevel);

                uassert(coordindexes.minX() == bitsindex.minX());
                uassert(coordindexes.minY() == bitsindex.minY());
                uassert(coordindexes.minZ() == bitsindex.minZ());

                uassert(coordindexes.maxX() == bitsindex.maxX());
                uassert(coordindexes.maxY() == bitsindex.maxY());
                uassert(coordindexes.maxZ() == bitsindex.maxZ());

                for(int idxX = coordindexes.minX() ; idxX <= coordindexes.maxX() ; ++idxX){
                    for(int idxY = coordindexes.minY() ; idxY <= coordindexes.maxY() ; ++idxY){
                        for(int idxZ = coordindexes.minZ() ; idxZ <= coordindexes.maxZ() ; ++idxZ){
                            if(idxX || idxY || idxZ){
                                uassert(coordindexes.getIndex(idxX, idxY, idxZ)
                                        == bitsindex.getIndex(idxX, idxY, idxZ));
                                const MortonIndex neigh = bitsindex.getIndex(idxX, idxY, idxZ);
103
                                const FTreeCoordinate neighCoord(neigh);
104
105
106
                                uassert(xbox + idxX == neighCoord.getX());
                                uassert(ybox + idxY == neighCoord.getY());
                                uassert(zbox + idxZ == neighCoord.getZ());
107
108
109
110
111

                                uassert(bitsindex.areNeighbors(mindex, neigh));
                                if(bitsindex.areNeighbors(mindex, neigh) == false){
                                    printf("Stop here\n");// TODO remove
                                }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
                            }
                        }
                    }
                }
            }
        }
    }

    void MortonAll(){
        {
            const int idxLevel = 5;
            const int limit = FMath::pow2(idxLevel);
            for(int xbox = 0 ; xbox < limit ; ++xbox){
                for(int ybox = 0 ; ybox < limit ; ++ybox){
                    for(int zbox = 0 ; zbox < limit ; ++zbox){

128
                        const MortonIndex mindex = FTreeCoordinate(xbox, ybox, zbox).getMortonIndex();
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

                        FCoordinateNeighborIndex coordindexes(mindex, idxLevel);
                        FBitsNeighborIndex bitsindex(mindex, idxLevel);

                        uassert(coordindexes.minX() == bitsindex.minX());
                        uassert(coordindexes.minY() == bitsindex.minY());
                        uassert(coordindexes.minZ() == bitsindex.minZ());

                        uassert(coordindexes.maxX() == bitsindex.maxX());
                        uassert(coordindexes.maxY() == bitsindex.maxY());
                        uassert(coordindexes.maxZ() == bitsindex.maxZ());

                        for(int idxX = coordindexes.minX() ; idxX <= coordindexes.maxX() ; ++idxX){
                            for(int idxY = coordindexes.minY() ; idxY <= coordindexes.maxY() ; ++idxY){
                                for(int idxZ = coordindexes.minZ() ; idxZ <= coordindexes.maxZ() ; ++idxZ){
                                    if(idxX || idxY || idxZ){
                                        uassert(coordindexes.getIndex(idxX, idxY, idxZ)
                                                == bitsindex.getIndex(idxX, idxY, idxZ));
                                        const MortonIndex neigh = bitsindex.getIndex(idxX, idxY, idxZ);
148
                                        const FTreeCoordinate neighCoord(neigh);
149
150
151
                                        uassert(xbox + idxX == neighCoord.getX());
                                        uassert(ybox + idxY == neighCoord.getY());
                                        uassert(zbox + idxZ == neighCoord.getZ());
152
153

                                        uassert(bitsindex.areNeighbors(mindex, neigh));
154
155
156
157
158
159
160
161
162
163
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

164
165
166
167
168
169
    void Neighbors(){
        {
            const int idxLevel = 5;
            const MortonIndex limit = FMath::pow2(idxLevel)*3L;
            for(MortonIndex idxV1 = 1 ; idxV1 < limit ; ++idxV1){
                FBitsNeighborIndex bitsindex(idxV1, idxLevel);
170
                const FTreeCoordinate coord1(idxV1);
171
172
173
174

                for(MortonIndex idxV2 = 0 ; idxV2 < limit ; ++idxV2){
                    const bool isneig = bitsindex.areNeighbors(idxV1, idxV2);

175
                    const FTreeCoordinate coord2(idxV2);
176
177
178
179
180
181
182
183
184
185
                    const bool isreallyneig = (FMath::Abs(coord1.getX()-coord2.getX()) <= 1)
                            && (FMath::Abs(coord1.getY()-coord2.getY()) <= 1)
                            && (FMath::Abs(coord1.getZ()-coord2.getZ()) <= 1);

                    uassert(isneig == isreallyneig);
                }
            }
        }
    }

186
187
188
189
190
191

    // set test
    void SetTests(){
        AddTest(&TestIndexes::MortonLimite,"Test NeighborIndexes at limits");
        AddTest(&TestIndexes::Morton,"Test NeighborIndexes");
        AddTest(&TestIndexes::MortonAll,"Test All NeighborIndexes");
192
        AddTest(&TestIndexes::Neighbors,"Test Neighbors");
193
194
195
196
197
    }
};

// You must do this
TestClass(TestIndexes)