llvm-project/libc/test/src/strings/strncasecmp_test.cpp
Nick Desaulniers 431ea2d076
[libc] move bcmp, bzero, bcopy, index, rindex, strcasecmp, strncasecmp to strings.h (#118899)
docgen relies on the convention that we have a file foo.cpp in
libc/src/\<header\>/. Because the above functions weren't in libc/src/strings/
but rather libc/src/string/, docgen could not find that we had implemented
these.

Rather than add special carve outs to docgen, let's fix up our sources for
these 7 functions to stick with the existing conventions the rest of the
codebase follows.

Link: #118860
Fixes: #118875
2024-12-10 08:58:45 -08:00

49 lines
1.5 KiB
C++

//===-- Unittests for strncasecmp -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "src/strings/strncasecmp.h"
#include "test/UnitTest/Test.h"
TEST(LlvmLibcStrNCaseCmpTest,
EmptyStringsShouldReturnZeroWithSufficientLength) {
const char *s1 = "";
const char *s2 = "";
int result = LIBC_NAMESPACE::strncasecmp(s1, s2, 1);
ASSERT_EQ(result, 0);
// Verify operands reversed.
result = LIBC_NAMESPACE::strncasecmp(s2, s1, 1);
ASSERT_EQ(result, 0);
}
TEST(LlvmLibcStrNCaseCmpTest,
EmptyStringShouldNotEqualNonEmptyStringWithSufficientLength) {
const char *empty = "";
const char *s2 = "abc";
int result = LIBC_NAMESPACE::strncasecmp(empty, s2, 3);
// This should be '\0' - 'a' = -97
ASSERT_LT(result, 0);
// Similar case if empty string is second argument.
const char *s3 = "123";
result = LIBC_NAMESPACE::strncasecmp(s3, empty, 3);
// This should be '1' - '\0' = 49
ASSERT_GT(result, 0);
}
TEST(LlvmLibcStrNCaseCmpTest, Case) {
const char *s1 = "aB";
const char *s2 = "ab";
int result = LIBC_NAMESPACE::strncasecmp(s1, s2, 2);
ASSERT_EQ(result, 0);
// Verify operands reversed.
result = LIBC_NAMESPACE::strncasecmp(s2, s1, 2);
ASSERT_EQ(result, 0);
}