TestFind.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <AK/Array.h>
  8. #include <AK/Find.h>
  9. TEST_CASE(should_return_end_if_not_in_container)
  10. {
  11. constexpr Array<int, 10> a {};
  12. static_assert(a.end() == AK::find(a.begin(), a.end(), 1));
  13. EXPECT(a.end() == AK::find(a.begin(), a.end(), 1));
  14. }
  15. TEST_CASE(should_return_iterator_to_first_matching_value_in_container)
  16. {
  17. static constexpr Array<int, 10> a { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
  18. constexpr auto expected = a.begin() + 4;
  19. static_assert(expected == AK::find(a.begin(), a.end(), 0));
  20. EXPECT(expected == AK::find(a.begin(), a.end(), 0));
  21. }
  22. TEST_CASE(should_return_iterator_to_first_predicate_matching_value_in_container)
  23. {
  24. static constexpr Array<int, 10> a { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
  25. constexpr auto expected = a.begin() + 4;
  26. static_assert(expected == AK::find_if(a.begin(), a.end(), [](auto v) { return v == 0; }));
  27. EXPECT(expected == AK::find_if(a.begin(), a.end(), [](auto v) { return v == 0; }));
  28. auto find_me = 8;
  29. EXPECT(find_me == *AK::find_if(a.begin(), a.end(), [&](auto v) { return v == find_me; }));
  30. }
  31. TEST_CASE(should_return_index_to_first_predicate_matching_value_in_container)
  32. {
  33. static constexpr Array<int, 10> a { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
  34. static_assert(4 == AK::find_index(a.begin(), a.end(), 0));
  35. EXPECT(4 == AK::find_index(a.begin(), a.end(), 0));
  36. }