|
| 1 | +//===-- Unittests for lsearch ---------------------------------------------===// |
| 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/lsearch.h" |
| 10 | +#include "test/UnitTest/Test.h" |
| 11 | + |
| 12 | +int compar(const void *a, const void *b) { |
| 13 | + return *reinterpret_cast<const int *>(a) != *reinterpret_cast<const int *>(b); |
| 14 | +} |
| 15 | + |
| 16 | +TEST(LlvmLibcLsearchTest, SearchHead) { |
| 17 | + int list[3] = {1, 2, 3}; |
| 18 | + size_t len = 3; |
| 19 | + int key = 1; |
| 20 | + void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar); |
| 21 | + ASSERT_TRUE(ret == &list[0]); |
| 22 | +} |
| 23 | + |
| 24 | +TEST(LlvmLibcLsearchTest, SearchMiddle) { |
| 25 | + int list[3] = {1, 2, 3}; |
| 26 | + size_t len = 3; |
| 27 | + int key = 2; |
| 28 | + void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar); |
| 29 | + ASSERT_TRUE(ret == &list[1]); |
| 30 | +} |
| 31 | + |
| 32 | +TEST(LlvmLibcLsearchTest, SearchTail) { |
| 33 | + int list[3] = {1, 2, 3}; |
| 34 | + size_t len = 3; |
| 35 | + int key = 3; |
| 36 | + void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar); |
| 37 | + ASSERT_TRUE(ret == &list[2]); |
| 38 | +} |
| 39 | + |
| 40 | +TEST(LlvmLibcLsearchTest, SearchNonExistent) { |
| 41 | + int list[4] = {1, 2, 3, 0}; |
| 42 | + size_t len = 3; |
| 43 | + int key = 4; |
| 44 | + void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar); |
| 45 | + ASSERT_TRUE(ret == &list[3]); |
| 46 | + ASSERT_EQ(key, list[3]); |
| 47 | + ASSERT_EQ(len, 4UL); |
| 48 | +} |
| 49 | + |
| 50 | +TEST(LlvmLibcLsearchTest, SearchExceptional) { |
| 51 | + int list[3] = {1, 2, 3}; |
| 52 | + size_t len = 3; |
| 53 | + size_t max_len = ~0; |
| 54 | + int key = 3; |
| 55 | + |
| 56 | + ASSERT_EQ(LIBC_NAMESPACE::lsearch(nullptr, list, &len, sizeof(int), compar), |
| 57 | + nullptr); |
| 58 | + ASSERT_EQ(LIBC_NAMESPACE::lsearch(&key, nullptr, &len, sizeof(int), compar), |
| 59 | + nullptr); |
| 60 | + ASSERT_EQ(LIBC_NAMESPACE::lsearch(&key, list, nullptr, sizeof(int), compar), |
| 61 | + nullptr); |
| 62 | + ASSERT_EQ(LIBC_NAMESPACE::lsearch(&key, list, &max_len, sizeof(int), compar), |
| 63 | + nullptr); |
| 64 | + ASSERT_EQ(LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), nullptr), |
| 65 | + nullptr); |
| 66 | +} |
0 commit comments