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