From 268861ff6b768673b523e3cad99364a21e5bf868 Mon Sep 17 00:00:00 2001 From: Beman Date: Mon, 13 Jul 2015 10:29:45 -0400 Subject: [PATCH] find_first_not_of passing tests --- include/boost/utility/string_view.hpp | 6 +- test/string_view_test3.cpp | 79 +++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/include/boost/utility/string_view.hpp b/include/boost/utility/string_view.hpp index c52b294..f7d3312 100644 --- a/include/boost/utility/string_view.hpp +++ b/include/boost/utility/string_view.hpp @@ -278,7 +278,11 @@ namespace boost { // find_first_not_of size_type find_first_not_of(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT { - const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s ); + if (pos >= len_) + return npos; + if (s.len_ == 0) + return pos; + const_iterator iter = find_not_of ( this->cbegin () + pos, this->cend (), s ); return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); } size_type find_first_not_of(charT c, size_type pos = 0) const BOOST_NOEXCEPT diff --git a/test/string_view_test3.cpp b/test/string_view_test3.cpp index 3e36aa8..8ddcd61 100644 --- a/test/string_view_test3.cpp +++ b/test/string_view_test3.cpp @@ -159,19 +159,19 @@ namespace BOOST_TEST_EQ(sv1.find_first_of(sv2), s1.find_first_of(s2)); for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) { - //std::cout << i << std::endl; + //std::cout << i << ": " << sv1.find_first_of(sv2, i) << " " << s1.find_first_of(s2, i) << std::endl; BOOST_TEST_EQ(sv1.find_first_of(sv2, i), s1.find_first_of(s2, i)); } for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) { - //std::cout << i << std::endl; + //std::cout << i << ": " << sv1.find_first_of(sv3, i) << " " << s1.find_first_of(s3, i) << std::endl; BOOST_TEST_EQ(sv1.find_first_of(sv3, i), s1.find_first_of(s3, i)); } // second signature for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) { - //std::cout << i << std::endl; + //std::cout << i << ": " << sv1.find_first_of('o', i) << " " << s1.find_first_of('o', i) << std::endl; BOOST_TEST_EQ(sv1.find_first_of('o', i), s1.find_first_of('o', i)); } @@ -179,14 +179,82 @@ namespace for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) for (std::string::size_type j = 0; j <= std::strlen(s2c) + 1; ++j) { - //std::cout << i << " " << j << std::endl; + //std::cout << i << "," << j << ": " << sv1.find_first_of(s2c, i, j) << " " << s1.find_first_of(s2c, i, j) << std::endl; BOOST_TEST_EQ(sv1.find_first_of(s2c, i, j), s1.find_first_of(s2c, i, j)); } // fourth signature BOOST_TEST_EQ(sv1.find_first_of(s2c), s1.find_first_of(s2c)); for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) - BOOST_TEST_EQ(sv1.find_first_of(s2c, i), s1.find_first_of(s2c, i)); + { + //std::cout << i << ": " << sv1.find_first_of(s2c, i) << " " << s1.find_first_of(s2c, i) << std::endl; + BOOST_TEST_EQ(sv1.find_first_of(s2c, i), s1.find_first_of(s2c, i)); + } + } + + void find_first_not_of_test() + { + std::cout << "find_first_not_of test..." << std::endl; + + // find_first_not_of - test two modified and two new signatures + std::string s1("Hello World!"); + boost::string_view sv1(s1); + std::string s2("o"); + boost::string_view sv2(s2); + const char* s2c = "Good Bye"; + boost::string_view sv2c(s2c); + std::string s3; + boost::string_view sv3(s3); + + // smoke test + BOOST_TEST_EQ(sv1.find_first_not_of(sv2), s1.find_first_not_of(s2)); + BOOST_TEST_EQ(sv1.find_first_not_of(sv2, 5), s1.find_first_not_of(s2, 5)); + BOOST_TEST_EQ(sv1.find_first_not_of(s2c), s1.find_first_not_of(s2c)); + BOOST_TEST_EQ(sv1.find_first_not_of(s2c, 0, 4), s1.find_first_not_of(s2c, 0, 4)); + + // first signature + BOOST_TEST_EQ(sv3.find_first_not_of(sv3), s3.find_first_not_of(s3)); // both strings empty + BOOST_TEST_EQ(sv1.find_first_not_of(sv3), s1.find_first_not_of(s3)); // search string empty + BOOST_TEST_EQ(sv3.find_first_not_of(sv2), s3.find_first_not_of(s2)); // searched string empty + BOOST_TEST_EQ(sv1.find_first_not_of(sv3, s1.size() + 2), s1.find_first_not_of(s3, s1.size() + 2)); + BOOST_TEST_EQ(sv1.find_first_not_of(sv3, s1.size() + 1), s1.find_first_not_of(s3, s1.size() + 1)); + BOOST_TEST_EQ(sv1.find_first_not_of(sv3, s1.size()), s1.find_first_not_of(s3, s1.size())); + BOOST_TEST_EQ(sv1.find_first_not_of(sv3, s1.size() - 1), s1.find_first_not_of(s3, s1.size() - 1)); + + BOOST_TEST_EQ(sv1.find_first_not_of(sv2), s1.find_first_not_of(s2)); + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + std::cout << i << ": " << sv1.find_first_not_of(sv2, i) << " " << s1.find_first_not_of(s2, i) << std::endl; + BOOST_TEST_EQ(sv1.find_first_not_of(sv2, i), s1.find_first_not_of(s2, i)); + } + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + std::cout << i << ": " << sv1.find_first_not_of(sv3, i) << " " << s1.find_first_not_of(s3, i) << std::endl; + BOOST_TEST_EQ(sv1.find_first_not_of(sv3, i), s1.find_first_not_of(s3, i)); + } + + // second signature + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + std::cout << i << ": " << sv1.find_first_not_of('o', i) << " " << s1.find_first_not_of('o', i) << std::endl; + BOOST_TEST_EQ(sv1.find_first_not_of('o', i), s1.find_first_not_of('o', i)); + } + + // third signature + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + for (std::string::size_type j = 0; j <= std::strlen(s2c) + 1; ++j) + { + std::cout << i << "," << j << ": " << sv1.find_first_not_of(s2c, i, j) << " " << s1.find_first_not_of(s2c, i, j) << std::endl; + BOOST_TEST_EQ(sv1.find_first_not_of(s2c, i, j), s1.find_first_not_of(s2c, i, j)); + } + + // fourth signature + BOOST_TEST_EQ(sv1.find_first_not_of(s2c), s1.find_first_not_of(s2c)); + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + std::cout << i << ": " << sv1.find_first_not_of(s2c, i) << " " << s1.find_first_not_of(s2c, i) << std::endl; + BOOST_TEST_EQ(sv1.find_first_not_of(s2c, i), s1.find_first_not_of(s2c, i)); + } } } // unnamed namespace @@ -203,6 +271,7 @@ int cpp_main(int argc, char* argv[]) find_test(); rfind_test(); find_first_of_test(); + find_first_not_of_test(); return boost::report_errors(); }