Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/test/tile_array_test.cpp
blob: a6fbd130b009b294f2e12c5f4c9bb3ff6251a98f (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "StarTileSectorArray.hpp"

#include "gtest/gtest.h"

using namespace Star;

TEST(TileSectorArrayTest, All) {
  typedef TileSectorArray<int, 32> TileArray;
  TileArray tileSectorArray({100, 100}, -1);

  EXPECT_TRUE(tileSectorArray.sectorValid(TileArray::Sector(1, 1)));
  EXPECT_TRUE(tileSectorArray.sectorValid(TileArray::Sector(3, 3)));
  EXPECT_FALSE(tileSectorArray.sectorValid(TileArray::Sector(4, 4)));

  EXPECT_TRUE(List<TileArray::Sector>({{0, 0}, {1, 0}}) == tileSectorArray.validSectorsFor(RectI(0, -32, 64, 32)));

  EXPECT_TRUE(TileArray::Sector(0, 0) == tileSectorArray.sectorFor({0, 0}));
  EXPECT_TRUE(TileArray::Sector(3, 1) == tileSectorArray.sectorFor({-1, 33}));
  EXPECT_TRUE(-1 == tileSectorArray.tile({-1, -1}));
  EXPECT_TRUE(nullptr == tileSectorArray.modifyTile({-1, -1}));
  EXPECT_TRUE(RectI(32, 32, 64, 64) == tileSectorArray.sectorRegion({1, 1}));
  EXPECT_TRUE(TileArray::Sector(0, 3) == tileSectorArray.adjacentSector({3, 3}, {1, 0}));

  tileSectorArray.loadSector({0, 0}, make_unique<TileArray::Array>(1));
  tileSectorArray.loadSector({1, 0}, make_unique<TileArray::Array>(1));
  tileSectorArray.loadSector({2, 0}, make_unique<TileArray::Array>(1));
  tileSectorArray.loadSector({3, 0}, make_unique<TileArray::Array>(1));
  tileSectorArray.loadSector({0, 1}, make_unique<TileArray::Array>(2));
  tileSectorArray.loadSector({1, 1}, make_unique<TileArray::Array>(2));
  tileSectorArray.loadSector({2, 1}, make_unique<TileArray::Array>(2));
  tileSectorArray.loadSector({3, 1}, make_unique<TileArray::Array>(2));

  Set<Vec2I> found;
  tileSectorArray.tileEach(RectI(-2, 0, 3, 1),
      [&found](Vec2I const& pos, int tile) {
        found.add(pos);
        EXPECT_TRUE(pos[0] >= -2 && pos[0] < 3);
        EXPECT_TRUE(pos[1] == 0);
        EXPECT_EQ(1, tile);
      });
  EXPECT_TRUE(found.contains(Vec2I(0, 0)));
  EXPECT_TRUE(found.contains(Vec2I(-1, 0)));
  EXPECT_TRUE(found.contains(Vec2I(-2, 0)));
  EXPECT_TRUE(found.contains(Vec2I(1, 0)));

  tileSectorArray.tileEach(RectI(-10, 0, -1, 1),
      [](Vec2I const& pos, int tile) {
        EXPECT_TRUE(pos[0] >= -10 && pos[0] < -1);
        EXPECT_EQ(1, tile);
      });

  tileSectorArray.tileEach(RectI(-10, -1, -1, 0),
      [](Vec2I const& pos, int tile) {
        EXPECT_TRUE(pos[0] >= -10 && pos[0] < -1);
        EXPECT_TRUE(pos[1] == -1);
        EXPECT_EQ(-1, tile);
      });

  found.clear();
  tileSectorArray.tileEach(RectI(110, 101, 120, 102),
      [&found](Vec2I const& pos, int tile) {
        found.add(pos);
        EXPECT_TRUE(pos[0] >= 110 && pos[0] < 120);
        EXPECT_TRUE(pos[1] == 101);
        EXPECT_EQ(-1, tile);
      });
  EXPECT_TRUE(found.contains(Vec2I(110, 101)));
  EXPECT_TRUE(found.contains(Vec2I(119, 101)));

  auto res1 = tileSectorArray.tileEachResult(RectI(110, 110, 120, 120),
      [](Vec2I const& pos, int tile) -> int {
        return (pos[0] >= 110 && pos[0] < 120 && pos[1] >= 110 && pos[1] < 120 && tile == -1) ? 1 : 0;
      });
  MultiArray<int, 2> res1comp({10, 10}, 1);

  EXPECT_TRUE(res1.size() == res1comp.size());
  res1.forEach([](Array2S const&, int elem) { EXPECT_TRUE(elem == 1); });

  auto res2 = tileSectorArray.tileEachResult(RectI(32, 32, 64, 64),
      [](Vec2I const& pos, int tile) -> int {
        return (pos[0] >= 32 && pos[0] < 64 && pos[1] >= 32 && pos[1] < 64 && tile == 2) ? 1 : 0;
      });
  MultiArray<int, 2> res2comp({32, 32}, 1);

  EXPECT_TRUE(res2.size() == res2comp.size());
  res2.forEach([](Array2S const&, int elem) { EXPECT_TRUE(elem == 1); });

  auto res3 = tileSectorArray.tileEachResult(RectI(-10, -10, 1, 1),
      [](Vec2I const& pos, int tile) -> int {
        if (pos[1] < 0)
          return tile == -1;
        else
          return tile == 1;
      });
  MultiArray<int, 2> res3comp({11, 11}, 1);

  EXPECT_TRUE(res3.size() == res3comp.size());
  res3.forEach([](Array2S const&, int elem) { EXPECT_TRUE(elem == 1); });
}