misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1 | // Copyright 2017 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
nik7273 | 38b7043 | 2019-03-08 10:27:53 -0500 | [diff] [blame] | 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "absl/algorithm/algorithm.h" |
| 16 | |
Abseil Team | 00478de | 2024-08-05 22:54:56 -0700 | [diff] [blame] | 17 | #include <array> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
Derek Mauro | 10ec11d | 2022-06-14 13:10:59 -0700 | [diff] [blame] | 21 | #include "absl/base/config.h" |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 25 | class LinearSearchTest : public testing::Test { |
| 26 | protected: |
| 27 | LinearSearchTest() : container_{1, 2, 3} {} |
| 28 | |
| 29 | static bool Is3(int n) { return n == 3; } |
| 30 | static bool Is4(int n) { return n == 4; } |
| 31 | |
| 32 | std::vector<int> container_; |
| 33 | }; |
| 34 | |
| 35 | TEST_F(LinearSearchTest, linear_search) { |
| 36 | EXPECT_TRUE(absl::linear_search(container_.begin(), container_.end(), 3)); |
| 37 | EXPECT_FALSE(absl::linear_search(container_.begin(), container_.end(), 4)); |
| 38 | } |
| 39 | |
| 40 | TEST_F(LinearSearchTest, linear_searchConst) { |
| 41 | const std::vector<int> *const const_container = &container_; |
| 42 | EXPECT_TRUE( |
| 43 | absl::linear_search(const_container->begin(), const_container->end(), 3)); |
| 44 | EXPECT_FALSE( |
| 45 | absl::linear_search(const_container->begin(), const_container->end(), 4)); |
| 46 | } |
| 47 | |
Abseil Team | 00478de | 2024-08-05 22:54:56 -0700 | [diff] [blame] | 48 | #if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \ |
| 49 | ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L |
| 50 | |
| 51 | TEST_F(LinearSearchTest, Constexpr) { |
| 52 | static constexpr std::array<int, 3> kArray = {1, 2, 3}; |
| 53 | static_assert(absl::linear_search(kArray.begin(), kArray.end(), 3)); |
| 54 | static_assert(!absl::linear_search(kArray.begin(), kArray.end(), 4)); |
| 55 | } |
| 56 | |
| 57 | #endif // defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && |
| 58 | // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L |
| 59 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 60 | } // namespace |