diff --git a/CMakeLists.txt b/CMakeLists.txt index ea981fe4e2f67149ce0f349175cc99d9e8346d17..61278f34ff79a0b1dc688835f369df0b144926e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project (cell_algorithms) enable_language(CXX) # compilation flags -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -Wall") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11 -pthread -Wall") # this generates a .json file with full compilation command for each file set(CMAKE_EXPORT_COMPILE_COMMANDS "YES") diff --git a/src/swcio.hpp b/src/swcio.hpp index 44b8a2b1610e6d6eba22cd184c8e102907c024f0..0348eb4f149303362c736d885afae8d1d77c2eb5 100644 --- a/src/swcio.hpp +++ b/src/swcio.hpp @@ -61,7 +61,7 @@ public: cell_record(const cell_record &other) = default; cell_record &operator=(const cell_record &other) = default; - bool strict_equals(const cell_record &other) + bool strict_equals(const cell_record &other) const { return id_ == other.id_ && x_ == other.x_ && @@ -223,6 +223,8 @@ public: : is_(is) , eof_(false) { + is_.clear(); + is_.seekg(std::ios_base::beg); read_next_record(); } @@ -231,6 +233,12 @@ public: , eof_(true) { } + cell_record_stream_iterator(const cell_record_stream_iterator &other) + : is_(other.is_) + , parser_(other.parser_) + , curr_record_(other.curr_record_) + , eof_(other.eof_) + { } cell_record_stream_iterator &operator++() { @@ -242,14 +250,23 @@ public: return *this; } - cell_record_stream_iterator operator++(int); + cell_record_stream_iterator operator++(int) + { + cell_record_stream_iterator ret(*this); + operator++(); + return ret; + } value_type operator*() { + if (eof_) { + throw std::out_of_range("attempt to read past eof"); + } + return curr_record_; } - bool operator==(const cell_record_stream_iterator &other) + bool operator==(const cell_record_stream_iterator &other) const { if (eof_ && other.eof_) { return true; @@ -263,6 +280,16 @@ public: return !(*this == other); } + friend std::ostream &operator<<(std::ostream &os, + const cell_record_stream_iterator &iter) + { + os << "{ is_.tellg(): " << iter.is_.tellg() << ", " + << "curr_record_: " << iter.curr_record_ << ", " + << "eof_: " << iter.eof_ << "}"; + + return os; + } + private: void read_next_record() { diff --git a/tests/test_swcio.cpp b/tests/test_swcio.cpp index 9d2d0b21781e1f1f20d7026050dd96f070bd9be4..5912a1e5086bfe6a25120bf2dcfe72ccd6e9f1ba 100644 --- a/tests/test_swcio.cpp +++ b/tests/test_swcio.cpp @@ -1,4 +1,5 @@ #include <array> +#include <exception> #include <iostream> #include <fstream> #include <numeric> @@ -302,9 +303,8 @@ TEST(cell_record_ranges, raw) using namespace nestmc::io; { - std::stringstream is; - // Check valid usage + std::stringstream is; is << "1 1 14.566132 34.873772 7.857000 0.717830 -1\n"; is << "2 1 14.566132 34.873772 7.857000 0.717830 1\n"; is << "3 1 14.566132 34.873772 7.857000 0.717830 1\n"; @@ -316,5 +316,43 @@ TEST(cell_record_ranges, raw) } EXPECT_EQ(4u, cells.size()); + + bool entered = false; + auto citer = cells.begin(); + for (auto c : get_cell_records<swc_io_raw>(is)) { + EXPECT_TRUE(c.strict_equals(*citer++)); + entered = true; + } + + EXPECT_TRUE(entered); + } + + { + // Check out of bounds reads + std::stringstream is; + is << "1 1 14.566132 34.873772 7.857000 0.717830 -1\n"; + + auto ibegin = get_cell_records<swc_io_raw>(is).begin(); + + EXPECT_NO_THROW(++ibegin); + EXPECT_THROW(*ibegin, std::out_of_range); + + } + + { + std::stringstream is; + is << "1 1 14.566132 34.873772 7.857000 0.717830 -1\n"; + + // Check postfix operator++ + auto iter = get_cell_records<swc_io_raw>(is).begin(); + auto iend = get_cell_records<swc_io_raw>(is).end(); + + cell_record c; + EXPECT_NO_THROW(c = *iter++); + EXPECT_EQ(-1, c.parent()); + EXPECT_EQ(iend, iter); + + // Try to read past eof + EXPECT_THROW(*iter, std::out_of_range); } } diff --git a/vector b/vector index a8dfadd460262ebbc1bc22b159efe9e33ad1d359..9c86d0a84efed0dd739888503d275378df67fe71 160000 --- a/vector +++ b/vector @@ -1 +1 @@ -Subproject commit a8dfadd460262ebbc1bc22b159efe9e33ad1d359 +Subproject commit 9c86d0a84efed0dd739888503d275378df67fe71