Make test more lenient for custom clang version strings
[llvm-project.git] / libc / test / src / string / strnlen_test.cpp
blobbd1002c0ae0784090c8db88dfa9da237d54fa1f8
1 //===-- Unittests for strnlen ---------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "src/string/strnlen.h"
10 #include "test/UnitTest/Test.h"
11 #include <stddef.h>
13 TEST(LlvmLibcStrNLenTest, EmptyString) {
14 const char *empty = "";
15 ASSERT_EQ(static_cast<size_t>(0), LIBC_NAMESPACE::strnlen(empty, 0));
16 // If N is greater than string length, this should still return 0.
17 ASSERT_EQ(static_cast<size_t>(0), LIBC_NAMESPACE::strnlen(empty, 1));
20 TEST(LlvmLibcStrNLenTest, OneCharacterString) {
21 const char *single = "X";
22 ASSERT_EQ(static_cast<size_t>(1), LIBC_NAMESPACE::strnlen(single, 1));
23 // If N is zero, this should return 0.
24 ASSERT_EQ(static_cast<size_t>(0), LIBC_NAMESPACE::strnlen(single, 0));
25 // If N is greater than string length, this should still return 1.
26 ASSERT_EQ(static_cast<size_t>(1), LIBC_NAMESPACE::strnlen(single, 2));
29 TEST(LlvmLibcStrNLenTest, ManyCharacterString) {
30 const char *many = "123456789";
31 ASSERT_EQ(static_cast<size_t>(9), LIBC_NAMESPACE::strnlen(many, 9));
32 // If N is smaller than the string length, it should return N.
33 ASSERT_EQ(static_cast<size_t>(3), LIBC_NAMESPACE::strnlen(many, 3));
34 // If N is zero, this should return 0.
35 ASSERT_EQ(static_cast<size_t>(0), LIBC_NAMESPACE::strnlen(many, 0));
36 // If N is greater than the string length, this should still return 9.
37 ASSERT_EQ(static_cast<size_t>(9), LIBC_NAMESPACE::strnlen(many, 42));
40 TEST(LlvmLibcStrNLenTest, CharactersAfterNullTerminatorShouldNotBeIncluded) {
41 const char str[5] = {'a', 'b', 'c', '\0', 'd'};
42 ASSERT_EQ(static_cast<size_t>(3), LIBC_NAMESPACE::strnlen(str, 3));
43 // This should only read up to the null terminator.
44 ASSERT_EQ(static_cast<size_t>(3), LIBC_NAMESPACE::strnlen(str, 4));
45 ASSERT_EQ(static_cast<size_t>(3), LIBC_NAMESPACE::strnlen(str, 5));