1 #include "objects/containers/Grid.h"
2 #include "catch.hpp"
3 #include "utils/RecordType.h"
4 #include "utils/Utils.h"
5 
6 using namespace Sph;
7 
8 TEST_CASE("SparseGrid const", "[grid]") {
9     const SparseGrid<RecordType> grid(4, RecordType(-1));
10 
11     RecordType::resetStats();
12     REQUIRE(grid.size() == 4);
13     REQUIRE(grid.voxelCount() == 64);
14     REQUIRE_FALSE(grid.empty());
15     REQUIRE(grid[Indices(0, 1, 2)] == -1);
16     REQUIRE(grid[Indices(3, 3, 3)] == -1);
17     REQUIRE(grid[Indices(2, 0, 0)] == -1);
18     REQUIRE(RecordType::constructedNum == 3); // temporaries on the rhs
19 
20     REQUIRE_SPH_ASSERT(grid[Indices(4, 0, 1)]);
21 }
22 
23 TEST_CASE("SparseGrid mutable", "[grid]") {
24     SparseGrid<RecordType> grid(4, RecordType(-1));
25 
26     RecordType rhs(5);
27     RecordType::resetStats();
28     grid[Indices(2, 0, 1)] = rhs;
29     REQUIRE(RecordType::constructedNum == 1);
30     REQUIRE(grid[Indices(2, 0, 1)] == rhs);
31     REQUIRE(RecordType::constructedNum == 1);
32     REQUIRE(asConst(grid)[Indices(2, 0, 1)] == rhs);
33 
34     grid[Indices(0, 0, 0)] = 6;
35     grid[Indices(3, 3, 3)] = 2;
36     REQUIRE(grid[Indices(0, 0, 0)] == 6);
37     REQUIRE(grid[Indices(2, 0, 1)] == 5);
38     REQUIRE(grid[Indices(3, 3, 3)] == 2);
39     REQUIRE(grid[Indices(2, 0, 0)] == -1);
40     REQUIRE(grid[Indices(2, 0, 2)] == -1);
41 }
42 
43 TEST_CASE("SparseGrid iterate", "[grid]") {
44     SparseGrid<int> grid(4, 0);
45     grid[Indices(1, 0, 0)] = 6;
46     grid[Indices(3, 2, 2)] = 3;
47     grid[Indices(2, 1, 0)] = 4;
48     grid[Indices(1, 3, 0)] = 5;
49 
50     Array<Tuple<int, Indices>> expected = {
51         { 6, Indices(1, 0, 0) },
52         { 4, Indices(2, 1, 0) },
53         { 5, Indices(1, 3, 0) },
54         { 3, Indices(3, 2, 2) },
55     };
56 
57     Size visitedCnt = 0;
__anon1ed15af30102(int value, Indices idxs) 58     grid.iterate([&](int value, Indices idxs) {
59         int expectedValue;
60         Indices expectedIdxs;
61         tieToTuple(expectedValue, expectedIdxs) = expected[visitedCnt];
62         REQUIRE(value == expectedValue);
63         REQUIRE(all(idxs == expectedIdxs));
64         visitedCnt++;
65     });
66     REQUIRE(visitedCnt == 4);
67 }
68