utestNeighborIndexes.cpp 8.39 KB
Newer Older
1
// See LICENCE file at project root
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#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;

24
                        const MortonIndex mindex = FTreeCoordinate(xbox, ybox, zbox).getMortonIndex();
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

                        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);
44
                                        const FTreeCoordinate neighCoord(neigh);
45 46 47
                                        uassert(xbox + idxX == neighCoord.getX());
                                        uassert(ybox + idxY == neighCoord.getY());
                                        uassert(zbox + idxZ == neighCoord.getZ());
48 49

                                        uassert(bitsindex.areNeighbors(mindex, neigh));
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

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

69
                const MortonIndex mindex = FTreeCoordinate(xbox, ybox, zbox).getMortonIndex();
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

                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);
89
                                const FTreeCoordinate neighCoord(neigh);
90 91 92
                                uassert(xbox + idxX == neighCoord.getX());
                                uassert(ybox + idxY == neighCoord.getY());
                                uassert(zbox + idxZ == neighCoord.getZ());
93 94 95 96 97

                                uassert(bitsindex.areNeighbors(mindex, neigh));
                                if(bitsindex.areNeighbors(mindex, neigh) == false){
                                    printf("Stop here\n");// TODO remove
                                }
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
                            }
                        }
                    }
                }
            }
        }
    }

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

114
                        const MortonIndex mindex = FTreeCoordinate(xbox, ybox, zbox).getMortonIndex();
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

                        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);
134
                                        const FTreeCoordinate neighCoord(neigh);
135 136 137
                                        uassert(xbox + idxX == neighCoord.getX());
                                        uassert(ybox + idxY == neighCoord.getY());
                                        uassert(zbox + idxZ == neighCoord.getZ());
138 139

                                        uassert(bitsindex.areNeighbors(mindex, neigh));
140 141 142 143 144 145 146 147 148 149
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

150 151 152 153 154 155
    void Neighbors(){
        {
            const int idxLevel = 5;
            const MortonIndex limit = FMath::pow2(idxLevel)*3L;
            for(MortonIndex idxV1 = 1 ; idxV1 < limit ; ++idxV1){
                FBitsNeighborIndex bitsindex(idxV1, idxLevel);
156
                const FTreeCoordinate coord1(idxV1);
157 158 159 160

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

161
                    const FTreeCoordinate coord2(idxV2);
162 163 164 165 166 167 168 169 170 171
                    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);
                }
            }
        }
    }

172 173 174 175 176 177

    // set test
    void SetTests(){
        AddTest(&TestIndexes::MortonLimite,"Test NeighborIndexes at limits");
        AddTest(&TestIndexes::Morton,"Test NeighborIndexes");
        AddTest(&TestIndexes::MortonAll,"Test All NeighborIndexes");
178
        AddTest(&TestIndexes::Neighbors,"Test Neighbors");
179 180 181 182 183
    }
};

// You must do this
TestClass(TestIndexes)