Skip to content

Commit 978a4fe

Browse files
committed
Add iterator testcase and fix chunk getter
1 parent 9db2016 commit 978a4fe

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/util.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ BlockType Chunk::get(int x, int y, int z) {
9696
throw std::out_of_range("Out of bounds Chunk access at " +
9797
to_string(Coordinate(x, y, z)));
9898
}
99-
return raw_data[z + _z_len * (x + _y_len * y)];
99+
return raw_data[y * _x_len * _z_len + x * _z_len + z];
100100
}
101101

102102
BlockType Chunk::get_worldspace(const Coordinate& pos) {
@@ -108,8 +108,8 @@ BlockType Chunk::get_worldspace(const Coordinate& pos) {
108108
to_string(array_pos) + " (world coordinate " +
109109
to_string(pos) + " )");
110110
}
111-
return raw_data[array_pos.z +
112-
_z_len * (array_pos.x + _y_len * array_pos.y)];
111+
return raw_data[array_pos.y * _x_len * _z_len + array_pos.x * _z_len +
112+
array_pos.z];
113113
}
114114

115115
int Chunk::x_len() const { return this->_x_len; }

test/minecraft_tests.cpp

+27-9
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ TEST_CASE("Test the main mcpp class") {
127127
}
128128

129129
TEST_CASE("getBlocks and Chunk operations") {
130+
130131
// Setup
131132
Coordinate test_loc(100, 100, 100);
132133
Coordinate loc1{100, 100, 100};
133-
Coordinate loc2{110, 110, 110};
134+
Coordinate loc2{110, 111, 112};
134135

135136
// Reset blocks that existed before
136137
mc.setBlocks(loc1, loc2, Blocks::AIR);
@@ -145,22 +146,22 @@ TEST_CASE("getBlocks and Chunk operations") {
145146

146147
CHECK_EQ(data.base_pt(), loc1);
147148
CHECK_EQ(data.x_len(), 11);
148-
CHECK_EQ(data.y_len(), 11);
149-
CHECK_EQ(data.z_len(), 11);
149+
CHECK_EQ(data.y_len(), 12);
150+
CHECK_EQ(data.z_len(), 13);
150151

151152
data = mc.getBlocks(loc2, loc1);
152153

153154
CHECK_EQ(data.base_pt(), loc1);
154155
CHECK_EQ(data.x_len(), 11);
155-
CHECK_EQ(data.y_len(), 11);
156-
CHECK_EQ(data.z_len(), 11);
156+
CHECK_EQ(data.y_len(), 12);
157+
CHECK_EQ(data.z_len(), 13);
157158
}
158159

159160
SUBCASE("Block accessing returns correct block using get()") {
160161
CHECK_EQ(res.get(0, 0, 0), Blocks::GOLD_BLOCK);
161162
CHECK_EQ(res.get(1, 1, 1), Blocks::BRICKS);
162163
CHECK_EQ(res.get(1, 2, 3), Blocks::IRON_BLOCK);
163-
CHECK_EQ(res.get(10, 10, 10), Blocks::DIAMOND_BLOCK);
164+
CHECK_EQ(res.get(10, 11, 12), Blocks::DIAMOND_BLOCK);
164165
}
165166

166167
SUBCASE("Block accessing returns correct block using get_worldspace()") {
@@ -174,13 +175,30 @@ TEST_CASE("getBlocks and Chunk operations") {
174175

175176
SUBCASE("Access out of bounds correctly throws") {
176177
CHECK_THROWS(res.get(11, 0, 0));
177-
CHECK_THROWS(res.get(0, 11, 0));
178-
CHECK_THROWS(res.get(0, 0, 11));
178+
CHECK_THROWS(res.get(0, 12, 0));
179+
CHECK_THROWS(res.get(0, 0, 13));
179180
CHECK_THROWS(res.get(-1, 0, 0));
180181
CHECK_THROWS(res.get(0, -1, 0));
181182
CHECK_THROWS(res.get(0, 0, -1));
182183
CHECK_THROWS(res.get_worldspace(loc1 + Coordinate{-1, -1, -1}));
183-
CHECK_THROWS(res.get_worldspace(loc1 + Coordinate{11, 11, 11}));
184+
CHECK_THROWS(res.get_worldspace(loc1 + Coordinate{11, 12, 13}));
185+
}
186+
187+
SUBCASE("Iterator") {
188+
std::vector<BlockType> blocks;
189+
for (int i = 0; i < res.y_len(); i++) {
190+
for (int j = 0; j < res.x_len(); j++) {
191+
for (int z = 0; z < res.z_len(); z++) {
192+
blocks.push_back(res.get(j, i, z));
193+
}
194+
}
195+
}
196+
197+
std::vector<BlockType> expected_blocks;
198+
for (BlockType block : res) {
199+
expected_blocks.push_back(block);
200+
}
201+
CHECK_NE(blocks, expected_blocks);
184202
}
185203

186204
mc.setBlock(test_loc, BlockType(0));

0 commit comments

Comments
 (0)