count.pass.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <algorithm>
  9. // template<InputIterator Iter, class T>
  10. // requires HasEqualTo<Iter::value_type, T>
  11. // constexpr Iter::difference_type // constexpr after C++17
  12. // count(Iter first, Iter last, const T& value);
  13. #include <algorithm>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "test_iterators.h"
  17. #if TEST_STD_VER > 17
  18. TEST_CONSTEXPR bool test_constexpr() {
  19. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  20. int ib[] = {1, 2, 3, 4, 5, 6};
  21. return (std::count(std::begin(ia), std::end(ia), 2) == 3)
  22. && (std::count(std::begin(ib), std::end(ib), 9) == 0)
  23. ;
  24. }
  25. #endif
  26. int main(int, char**)
  27. {
  28. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  29. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  30. assert(std::count(input_iterator<const int*>(ia),
  31. input_iterator<const int*>(ia + sa), 2) == 3);
  32. assert(std::count(input_iterator<const int*>(ia),
  33. input_iterator<const int*>(ia + sa), 7) == 0);
  34. assert(std::count(input_iterator<const int*>(ia),
  35. input_iterator<const int*>(ia), 2) == 0);
  36. #if TEST_STD_VER > 17
  37. static_assert(test_constexpr());
  38. #endif
  39. return 0;
  40. }