diff --git a/include/duckhandy/infix_iterator.hpp b/include/duckhandy/infix_iterator.hpp index ca65e34..a0d16db 100644 --- a/include/duckhandy/infix_iterator.hpp +++ b/include/duckhandy/infix_iterator.hpp @@ -5,12 +5,12 @@ #if !defined(INFIX_ITERATOR_H_) #define INFIX_ITERATOR_H_ #include -#include + +namespace dhandy { template > -class infix_ostream_iterator : - public std::iterator +class infix_ostream_iterator { std::basic_ostream *os; charT const* delimiter; @@ -19,6 +19,13 @@ public: typedef charT char_type; typedef traits traits_type; typedef std::basic_ostream ostream_type; + + using iterator_category = std::output_iterator_tag; + using value_type = void; + using difference_type = void; + using pointer = void; + using reference = void; + infix_ostream_iterator(ostream_type& s) : os(&s),delimiter(0), first_elem(true) {} @@ -45,4 +52,5 @@ public: return *this; } }; +} //namespace dhandy #endif diff --git a/test/unit/infix_iterator.cpp b/test/unit/infix_iterator.cpp new file mode 100644 index 0000000..9d32e81 --- /dev/null +++ b/test/unit/infix_iterator.cpp @@ -0,0 +1,58 @@ +/* Copyright 2016-2024 Michele Santullo + * This file is part of "duckhandy". + * + * "duckhandy" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "duckhandy" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "duckhandy". If not, see . + */ + +#include "catch2/catch_test_macros.hpp" +#include "duckhandy/infix_iterator.hpp" +#include +#include +#include + +TEST_CASE("infix_iterator tests", "[iterator][infix_iterator]") { + using std::vector; + using std::string; + using std::copy; + using std::ostringstream; + using dhandy::infix_ostream_iterator; + + { + ostringstream oss; + vector empty{}; + copy(empty.begin(), empty.end(), infix_ostream_iterator(oss, "lalala")); + CHECK(oss.str() == ""); + } + + { + ostringstream oss; + vector data{"apple", "pear", "cherry", "mango"}; + copy(data.begin(), data.end(), infix_ostream_iterator(oss, "lalala")); + CHECK(oss.str() == "applelalalapearlalalacherrylalalamango"); + } + + { + ostringstream oss; + vector data{1, 2, 3, 4, 5, 6, 7}; + copy(data.begin(), data.end(), infix_ostream_iterator(oss, ", ")); + CHECK(oss.str() == "1, 2, 3, 4, 5, 6, 7"); + } + + { + ostringstream oss; + vector data{"just one value"}; + copy(data.begin(), data.end(), infix_ostream_iterator(oss, ", ")); + CHECK(oss.str() == "just one value"); + } +} diff --git a/test/unit/meson.build b/test/unit/meson.build index bbf8914..5ec1bd0 100644 --- a/test/unit/meson.build +++ b/test/unit/meson.build @@ -9,6 +9,7 @@ unit_test_prog = executable(meson.project_name(), 'resource_pool_test.cpp', 'version_test.cpp', 'tiger_test.cpp', + 'infix_iterator.cpp', install: false, dependencies: [sprout_dep, catch2_dep], include_directories: [public_incl],