brintos

brintos / llvm-project-archived public Read only

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