[PATCH] D26612: Protect std::string tests under libcpp-no-exceptions

Marshall Clow via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 15 07:14:57 PST 2016


mclow.lists added a comment.

We've been using a different pattern for this kind of tests (as we support more and more noexcept cases). Something like this (for the second change):

  		template <class S>
  		void
  		test(const S& s, typename S::size_type pos, typename S::size_type n)
  		{
  			if (pos <= s.size())
  			{
  				S str = s.substr(pos, n);
  				LIBCPP_ASSERT(str.__invariants());
  				assert(pos <= s.size());
  				typename S::size_type rlen = std::min(n, s.size() - pos);
  				assert(str.size() == rlen);
  				assert(S::traits_type::compare(s.data()+pos, str.data(), rlen) == 0);
  			}
  		#ifndef TEST_HAS_NO_EXCEPTIONS
  			else
  			{
  				try
  				{
  					S str = s.substr(pos, n);
  					assert(false);
  				}
  				catch (std::out_of_range&)
  				{
  					assert(pos > s.size());
  				}
  			}
  		#endif
  		}

I think that's much easier to read (and avoids duplication of the tests).


https://reviews.llvm.org/D26612





More information about the cfe-commits mailing list