Add unit test for AsciiMapSource and fix issues.

This commit is contained in:
King_DuckZ 2015-08-17 00:41:21 +02:00
parent 7085a98995
commit 42da727a67
8 changed files with 66 additions and 11 deletions

View file

@ -61,6 +61,9 @@ namespace dk {
TileCoordsType m_tile_range;; TileCoordsType m_tile_range;;
qualif_vector_type* m_data; qualif_vector_type* m_data;
}; };
template <uint32_t D>
Vector<D> make_past_end_coords ( const Vector<D>& parTo ) a_pure;
} //namespace dk } //namespace dk
#include "doorkeeper/implem/tileiterator.inl" #include "doorkeeper/implem/tileiterator.inl"

View file

@ -27,7 +27,7 @@ namespace dk {
template <uint32_t D> template <uint32_t D>
template <typename T> template <typename T>
void BaseMapSource<D>::fetch (std::vector<T>& parOut, const coords& parFrom, const coords& parTo) { void BaseMapSource<D>::fetch (std::vector<T>& parOut, const coords& parFrom, const coords& parTo) {
const auto tile_count = tile_volume<D>(parTo - parFrom + 1); const auto tile_count = tile_volume<D>(parTo - parFrom);
const std::size_t casted_tile_count = static_cast<std::size_t>(tile_count); const std::size_t casted_tile_count = static_cast<std::size_t>(tile_count);
DK_ASSERT(static_cast<decltype(tile_count)>(casted_tile_count) == tile_count); DK_ASSERT(static_cast<decltype(tile_count)>(casted_tile_count) == tile_count);

View file

@ -44,7 +44,7 @@ namespace dk {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
template <typename T, uint32_t D> template <typename T, uint32_t D>
typename Layer<T, D>::iterator Layer<T, D>::end() { typename Layer<T, D>::iterator Layer<T, D>::end() {
return iterator(&m_tiles, this->m_count, this->m_count); return iterator(&m_tiles, make_past_end_coords<D>(this->m_count - 1), make_past_end_coords<D>(this->m_count - 1));
} }
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------

View file

@ -37,7 +37,7 @@ namespace dk {
template <typename T, uint32_t D, typename T1> template <typename T, uint32_t D, typename T1>
bool TileIterator<T, D, T1>::equal (const TileIterator& parOther) const { bool TileIterator<T, D, T1>::equal (const TileIterator& parOther) const {
return m_data == parOther.m_data and m_tile_range == parOther.m_tile_range and m_tile_range == parOther.m_tile_range; return m_data == parOther.m_data and m_tile_range.position() == parOther.m_tile_range.position();
} }
template <typename T, uint32_t D, typename T1> template <typename T, uint32_t D, typename T1>
@ -57,4 +57,14 @@ namespace dk {
auto TileIterator<T, D, T1>::position() const -> const coords& { auto TileIterator<T, D, T1>::position() const -> const coords& {
return m_tile_range.position(); return m_tile_range.position();
} }
template <uint32_t D>
inline Vector<D> make_past_end_coords (const Vector<D>& parTo) {
Vector<D> retval(0);
//for (CoordinateDistType d = 0; d < D - 1; ++d) {
//retval[d] = parFrom[d];
//}
retval[D - 1] = parTo[D - 1] + 1;
return retval;
}
} //namespace dk } //namespace dk

View file

@ -20,15 +20,14 @@ namespace dk {
template <typename T> template <typename T>
typename Layer<T, D>::iterator Viewport<D>::begin (Layer<T, D>& parLayer) const { typename Layer<T, D>::iterator Viewport<D>::begin (Layer<T, D>& parLayer) const {
typedef typename Layer<T, D>::iterator IterType; typedef typename Layer<T, D>::iterator IterType;
return IterType(&parLayer.m_tiles, m_position, m_position + m_size, coords(0), m_tyler.map_size()); return IterType(&parLayer.m_tiles, m_position, m_tyler.map_size() - 1);
} }
template <uint32_t D> template <uint32_t D>
template <typename T> template <typename T>
typename Layer<T, D>::iterator Viewport<D>::end (Layer<T, D>& parLayer) const { typename Layer<T, D>::iterator Viewport<D>::end (Layer<T, D>& parLayer) const {
typedef typename Layer<T, D>::iterator IterType; typedef typename Layer<T, D>::iterator IterType;
const auto to(m_position + m_size); return IterType(&parLayer.m_tiles, make_past_end_coords<D>(m_tyler.map_size() - 1), make_past_end_coords<D>(m_tyler.map_size() - 1));
return IterType(&parLayer.m_tiles, m_tyler.map_size(), m_tyler.map_size());
} }
template <uint32_t D> template <uint32_t D>
@ -47,15 +46,14 @@ namespace dk {
template <typename T> template <typename T>
typename Layer<T, D>::const_iterator Viewport<D>::cbegin (const Layer<T, D>& parLayer) const { typename Layer<T, D>::const_iterator Viewport<D>::cbegin (const Layer<T, D>& parLayer) const {
typedef typename Layer<T, D>::const_iterator IterType; typedef typename Layer<T, D>::const_iterator IterType;
return IterType(&parLayer.m_tiles, m_position, m_position + m_size, m_tyler.map_size()); return IterType(&parLayer.m_tiles, m_position, m_tyler.map_size() - 1);
} }
template <uint32_t D> template <uint32_t D>
template <typename T> template <typename T>
typename Layer<T, D>::const_iterator Viewport<D>::cend (const Layer<T, D>& parLayer) const { typename Layer<T, D>::const_iterator Viewport<D>::cend (const Layer<T, D>& parLayer) const {
typedef typename Layer<T, D>::const_iterator IterType; typedef typename Layer<T, D>::const_iterator IterType;
const auto to(m_position + m_size); return IterType(&parLayer.m_tiles, make_past_end_coords<D>(m_tyler.map_size() - 1), make_past_end_coords<D>(m_tyler.map_size() - 1));
return IterType(&parLayer.m_tiles, m_tyler.map_size(), m_tyler.map_size());
} }
template <uint32_t D> template <uint32_t D>

View file

@ -93,10 +93,10 @@ namespace dkh {
void AsciiMapSource::fetch_raw (char* parOut, const coords& parFrom, const coords& parTo, std::size_t parSize) { void AsciiMapSource::fetch_raw (char* parOut, const coords& parFrom, const coords& parTo, std::size_t parSize) {
const std::size_t from = parFrom.x() + parFrom.y() * m_mapSize.x(); const std::size_t from = parFrom.x() + parFrom.y() * m_mapSize.x();
const std::size_t to = parTo.x() + parTo.y() * m_mapSize.x() + 1; const std::size_t to = parTo.x() - 1 + (parTo.y() - 1) * m_mapSize.x() + 1;
DK_ASSERT(to >= from); DK_ASSERT(to >= from);
DK_ASSERT(parSize / sizeof(MapTileType) == to - from); DK_ASSERT(parSize / sizeof(MapTileType) == to - from);
std::copy(m_wholedata.begin() + from, m_wholedata.begin() + to, parOut); std::copy(m_wholedata.begin() + from, m_wholedata.begin() + to, reinterpret_cast<MapTileType*>(parOut));
} }
dk::MapTypes AsciiMapSource::mapType() const { dk::MapTypes AsciiMapSource::mapType() const {

View file

@ -13,6 +13,7 @@ add_executable(${PROJECT_NAME}
tilecoords.cpp tilecoords.cpp
vector.cpp vector.cpp
tileiterator.cpp tileiterator.cpp
asciimapsource.cpp
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}

View file

@ -0,0 +1,43 @@
#include <gtest/gtest.h>
#include "doorkeeper/helpers/asciimapsource.hpp"
#include "doorkeeper/doorkeeper.hpp"
#include <sstream>
#include <memory>
TEST(load, asciimapsource) {
typedef dk::Tyler<2>::coords coords2;
typedef std::unique_ptr<dkh::AsciiMapSource> AsciiMapSourceUPtr;
const char map_data[] =
"1101111001\n1101111001\n1110111100\n1110111000\n"
"1111011011\n1111010111\n1111101111\n1111111111\n";
const dk::CoordinateScalarType tile_size = 64;
const dk::CoordinateScalarType map_width = 10;
const dk::CoordinateScalarType map_height = 8;
std::istringstream iss((std::string(map_data)));
dk::Tyler<2> tiler((coords2(tile_size)));
AsciiMapSourceUPtr loader(new dkh::AsciiMapSource(iss, coords2(map_width, map_height), dk::MapType_Isometric, coords2(tile_size)));
auto layer = &tiler.push_layer<dkh::AsciiMapSource::MapTileType>(loader.get(), 0);
dk::Viewport<2> full_view(tiler, coords2(map_width, map_height), coords2(0));
EXPECT_EQ(coords2(map_width, map_height), tiler.map_size());
dk::CoordinateDistType index = 0;
int data_index = 0;
full_view.setFrom(coords2(0));
for (auto itTile = full_view.begin(*layer), itTileEND = full_view.end(*layer); itTile != itTileEND; ++itTile) {
EXPECT_EQ(index, dk::to_index(itTile.raw_coords()));
EXPECT_LT(index, sizeof(map_data));
ASSERT_LT(data_index, sizeof(map_data));
const auto expected_value = static_cast<dk::CoordinateScalarType>(map_data[data_index] - '0');
EXPECT_EQ(expected_value, *itTile);
++index;
++data_index;
if (map_data[data_index] == '\n') {
++data_index;
}
}
EXPECT_EQ(map_width * map_height, index);
}