| 1 | //===-- Unittests for lfind -----------------------------------------------===// |
| 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 | |
| 9 | #include "src/search/lfind.h" |
| 10 | #include "test/UnitTest/Test.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | int compar(const void *a, const void *b) { |
| 15 | return *reinterpret_cast<const int *>(a) != *reinterpret_cast<const int *>(b); |
| 16 | } |
| 17 | |
| 18 | } // namespace |
| 19 | |
| 20 | TEST(LlvmLibcLfindTest, SearchHead) { |
| 21 | int list[3] = {1, 2, 3}; |
| 22 | size_t len = 3; |
| 23 | int key = 1; |
| 24 | void *ret = LIBC_NAMESPACE::lfind(&key, list, &len, sizeof(int), compar); |
| 25 | ASSERT_TRUE(ret == &list[0]); |
| 26 | } |
| 27 | |
| 28 | TEST(LlvmLibcLfindTest, SearchMiddle) { |
| 29 | int list[3] = {1, 2, 3}; |
| 30 | size_t len = 3; |
| 31 | int key = 2; |
| 32 | void *ret = LIBC_NAMESPACE::lfind(&key, list, &len, sizeof(int), compar); |
| 33 | ASSERT_TRUE(ret == &list[1]); |
| 34 | } |
| 35 | |
| 36 | TEST(LlvmLibcLfindTest, SearchTail) { |
| 37 | int list[3] = {1, 2, 3}; |
| 38 | size_t len = 3; |
| 39 | int key = 3; |
| 40 | void *ret = LIBC_NAMESPACE::lfind(&key, list, &len, sizeof(int), compar); |
| 41 | ASSERT_TRUE(ret == &list[2]); |
| 42 | } |
| 43 | |
| 44 | TEST(LlvmLibcLfindTest, SearchNonExistent) { |
| 45 | int list[3] = {1, 2, 3}; |
| 46 | size_t len = 3; |
| 47 | int key = 5; |
| 48 | void *ret = LIBC_NAMESPACE::lfind(&key, list, &len, sizeof(int), compar); |
| 49 | ASSERT_TRUE(ret == nullptr); |
| 50 | } |
| 51 | |