[libcxx] r178764 - Somehow search_n never got tested, so of course it had a bug in it. This fixes http://llvm.org/bugs/show_bug.cgi?id=15667.
Howard Hinnant
hhinnant at apple.com
Thu Apr 4 08:40:48 PDT 2013
Author: hhinnant
Date: Thu Apr 4 10:40:48 2013
New Revision: 178764
URL: http://llvm.org/viewvc/llvm-project?rev=178764&view=rev
Log:
Somehow search_n never got tested, so of course it had a bug in it. This fixes http://llvm.org/bugs/show_bug.cgi?id=15667.
Added:
libcxx/trunk/test/algorithms/alg.nonmodifying/alg.search/search_n.pass.cpp
libcxx/trunk/test/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp
Modified:
libcxx/trunk/include/algorithm
Modified: libcxx/trunk/include/algorithm
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/algorithm?rev=178764&r1=178763&r2=178764&view=diff
==============================================================================
--- libcxx/trunk/include/algorithm (original)
+++ libcxx/trunk/include/algorithm Thu Apr 4 10:40:48 2013
@@ -1398,7 +1398,7 @@ __search_n(_RandomAccessIterator __first
// Find first element in sequence that matchs __value_, with a mininum of loop checks
while (true)
{
- if (__first == __s) // return __last if no element matches __value_
+ if (__first >= __s) // return __last if no element matches __value_
return __last;
if (__pred(*__first, __value_))
break;
Added: libcxx/trunk/test/algorithms/alg.nonmodifying/alg.search/search_n.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/algorithms/alg.nonmodifying/alg.search/search_n.pass.cpp?rev=178764&view=auto
==============================================================================
--- libcxx/trunk/test/algorithms/alg.nonmodifying/alg.search/search_n.pass.cpp (added)
+++ libcxx/trunk/test/algorithms/alg.nonmodifying/alg.search/search_n.pass.cpp Thu Apr 4 10:40:48 2013
@@ -0,0 +1,73 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// template<class ForwardIterator, class Size, class T>
+// ForwardIterator
+// search_n(ForwardIterator first, ForwardIterator last, Size count,
+// const T& value);
+
+#include <algorithm>
+#include <cassert>
+
+#include "test_iterators.h"
+
+template <class Iter>
+void
+test()
+{
+ int ia[] = {0, 1, 2, 3, 4, 5};
+ const unsigned sa = sizeof(ia)/sizeof(ia[0]);
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 0) == Iter(ia));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 0) == Iter(ia+0));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 0) == Iter(ia+sa));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 0) == Iter(ia+sa));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 3) == Iter(ia));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 3) == Iter(ia+3));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 3) == Iter(ia+sa));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 3) == Iter(ia+sa));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 5) == Iter(ia));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 5) == Iter(ia+5));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 5) == Iter(ia+sa));
+ assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 5) == Iter(ia+sa));
+
+ int ib[] = {0, 0, 1, 1, 2, 2};
+ const unsigned sb = sizeof(ib)/sizeof(ib[0]);
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 0) == Iter(ib));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 0) == Iter(ib+0));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 0) == Iter(ib+0));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 0) == Iter(ib+sb));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 0) == Iter(ib+sb));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 1) == Iter(ib));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 1) == Iter(ib+2));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 1) == Iter(ib+2));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 1) == Iter(ib+sb));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 1) == Iter(ib+sb));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 2) == Iter(ib));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 2) == Iter(ib+4));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 2) == Iter(ib+4));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 2) == Iter(ib+sb));
+ assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 2) == Iter(ib+sb));
+
+ int ic[] = {0, 0, 0};
+ const unsigned sc = sizeof(ic)/sizeof(ic[0]);
+ assert(std::search_n(Iter(ic), Iter(ic+sc), 0, 0) == Iter(ic));
+ assert(std::search_n(Iter(ic), Iter(ic+sc), 1, 0) == Iter(ic));
+ assert(std::search_n(Iter(ic), Iter(ic+sc), 2, 0) == Iter(ic));
+ assert(std::search_n(Iter(ic), Iter(ic+sc), 3, 0) == Iter(ic));
+ assert(std::search_n(Iter(ic), Iter(ic+sc), 4, 0) == Iter(ic+sc));
+}
+
+int main()
+{
+ test<forward_iterator<const int*> >();
+ test<bidirectional_iterator<const int*> >();
+ test<random_access_iterator<const int*> >();
+}
Added: libcxx/trunk/test/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp?rev=178764&view=auto
==============================================================================
--- libcxx/trunk/test/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp (added)
+++ libcxx/trunk/test/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp Thu Apr 4 10:40:48 2013
@@ -0,0 +1,148 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// template<class ForwardIterator, class Size, class T, class BinaryPredicate>
+// ForwardIterator
+// search_n(ForwardIterator first, ForwardIterator last, Size count,
+// const T& value, BinaryPredicate pred);
+
+#include <algorithm>
+#include <cassert>
+
+#include "test_iterators.h"
+
+struct count_equal
+{
+ static unsigned count;
+ template <class T>
+ bool operator()(const T& x, const T& y)
+ {++count; return x == y;}
+};
+
+unsigned count_equal::count = 0;
+
+template <class Iter>
+void
+test()
+{
+ int ia[] = {0, 1, 2, 3, 4, 5};
+ const unsigned sa = sizeof(ia)/sizeof(ia[0]);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 0, count_equal()) == Iter(ia));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 0, count_equal()) == Iter(ia+0));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 0, count_equal()) == Iter(ia+sa));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 0, count_equal()) == Iter(ia+sa));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 3, count_equal()) == Iter(ia));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 3, count_equal()) == Iter(ia+3));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 3, count_equal()) == Iter(ia+sa));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 3, count_equal()) == Iter(ia+sa));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 5, count_equal()) == Iter(ia));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 5, count_equal()) == Iter(ia+5));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 5, count_equal()) == Iter(ia+sa));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 5, count_equal()) == Iter(ia+sa));
+ assert(count_equal::count <= sa);
+ count_equal::count = 0;
+
+ int ib[] = {0, 0, 1, 1, 2, 2};
+ const unsigned sb = sizeof(ib)/sizeof(ib[0]);
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 0, count_equal()) == Iter(ib));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 0, count_equal()) == Iter(ib+0));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 0, count_equal()) == Iter(ib+0));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 0, count_equal()) == Iter(ib+sb));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 0, count_equal()) == Iter(ib+sb));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 1, count_equal()) == Iter(ib));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 1, count_equal()) == Iter(ib+2));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 1, count_equal()) == Iter(ib+2));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 1, count_equal()) == Iter(ib+sb));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 1, count_equal()) == Iter(ib+sb));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 2, count_equal()) == Iter(ib));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 2, count_equal()) == Iter(ib+4));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 2, count_equal()) == Iter(ib+4));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 2, count_equal()) == Iter(ib+sb));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 2, count_equal()) == Iter(ib+sb));
+ assert(count_equal::count <= sb);
+ count_equal::count = 0;
+
+ int ic[] = {0, 0, 0};
+ const unsigned sc = sizeof(ic)/sizeof(ic[0]);
+ assert(std::search_n(Iter(ic), Iter(ic+sc), 0, 0, count_equal()) == Iter(ic));
+ assert(count_equal::count <= sc);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ic), Iter(ic+sc), 1, 0, count_equal()) == Iter(ic));
+ assert(count_equal::count <= sc);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ic), Iter(ic+sc), 2, 0, count_equal()) == Iter(ic));
+ assert(count_equal::count <= sc);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ic), Iter(ic+sc), 3, 0, count_equal()) == Iter(ic));
+ assert(count_equal::count <= sc);
+ count_equal::count = 0;
+ assert(std::search_n(Iter(ic), Iter(ic+sc), 4, 0, count_equal()) == Iter(ic+sc));
+ assert(count_equal::count <= sc);
+ count_equal::count = 0;
+}
+
+int main()
+{
+ test<forward_iterator<const int*> >();
+ test<bidirectional_iterator<const int*> >();
+ test<random_access_iterator<const int*> >();
+}
More information about the cfe-commits
mailing list