diff options
| author | Siva Chandra Reddy <sivachandra@google.com> | 2020-01-03 12:00:45 -0800 |
|---|---|---|
| committer | Siva Chandra Reddy <sivachandra@google.com> | 2020-01-06 10:14:43 -0800 |
| commit | 5b24c088171d3bd7a8ff559c82926e5d4b04f032 (patch) | |
| tree | 54ac5d666fef87a0e6337e388f6f5087c18e9ef9 /libc/test/src | |
| parent | 0239526cccf8aa708e29eeb7e49de8f6dc6c1a5f (diff) | |
| download | bcm5719-llvm-5b24c088171d3bd7a8ff559c82926e5d4b04f032.tar.gz bcm5719-llvm-5b24c088171d3bd7a8ff559c82926e5d4b04f032.zip | |
[libc] Move all tests to a top level `test` directory.
A toplevel target, `check-libc` has also been added.
Reviewers: abrachet, phosek
Tags: #libc-project
Differential Revision: https://reviews.llvm.org/D72177
Diffstat (limited to 'libc/test/src')
| -rw-r--r-- | libc/test/src/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | libc/test/src/errno/CMakeLists.txt | 12 | ||||
| -rw-r--r-- | libc/test/src/errno/errno_test.cpp | 17 | ||||
| -rw-r--r-- | libc/test/src/string/CMakeLists.txt | 23 | ||||
| -rw-r--r-- | libc/test/src/string/strcat_test.cpp | 39 | ||||
| -rw-r--r-- | libc/test/src/string/strcpy_test.cpp | 36 | ||||
| -rw-r--r-- | libc/test/src/sys/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | libc/test/src/sys/mman/CMakeLists.txt | 14 | ||||
| -rw-r--r-- | libc/test/src/sys/mman/mmap_test.cpp | 48 |
9 files changed, 193 insertions, 0 deletions
diff --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt new file mode 100644 index 00000000000..daf0f1ce57c --- /dev/null +++ b/libc/test/src/CMakeLists.txt @@ -0,0 +1,3 @@ +add_subdirectory(errno) +add_subdirectory(string) +add_subdirectory(sys) diff --git a/libc/test/src/errno/CMakeLists.txt b/libc/test/src/errno/CMakeLists.txt new file mode 100644 index 00000000000..6c21da5701b --- /dev/null +++ b/libc/test/src/errno/CMakeLists.txt @@ -0,0 +1,12 @@ +add_custom_target(libc_errno_unittests) +add_dependencies(check_libc libc_errno_unittests) + +add_libc_unittest( + errno_test + SUITE + libc_errno_unittests + SRCS + errno_test.cpp + DEPENDS + __errno_location +) diff --git a/libc/test/src/errno/errno_test.cpp b/libc/test/src/errno/errno_test.cpp new file mode 100644 index 00000000000..1ca61d5c625 --- /dev/null +++ b/libc/test/src/errno/errno_test.cpp @@ -0,0 +1,17 @@ +//===---------------------- Unittests for errno --------------------------===// +// +// 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/errno/llvmlibc_errno.h" + +#include "gtest/gtest.h" + +TEST(ErrnoTest, Basic) { + int test_val = 123; + llvmlibc_errno = test_val; + ASSERT_EQ(test_val, llvmlibc_errno); +} diff --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt new file mode 100644 index 00000000000..bc5c088d169 --- /dev/null +++ b/libc/test/src/string/CMakeLists.txt @@ -0,0 +1,23 @@ +add_custom_target(libc_string_unittests) +add_dependencies(check_libc libc_string_unittests) + +add_libc_unittest( + strcat_test + SUITE + libc_string_unittests + SRCS + strcat_test.cpp + DEPENDS + strcat + strcpy +) + +add_libc_unittest( + strcpy_test + SUITE + libc_string_unittests + SRCS + strcpy_test.cpp + DEPENDS + strcpy +) diff --git a/libc/test/src/string/strcat_test.cpp b/libc/test/src/string/strcat_test.cpp new file mode 100644 index 00000000000..3b8a7a7e447 --- /dev/null +++ b/libc/test/src/string/strcat_test.cpp @@ -0,0 +1,39 @@ +//===---------------------- Unittests for strcat --------------------------===// +// +// 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 <string> + +#include "src/string/strcat/strcat.h" +#include "gtest/gtest.h" + +TEST(StrCatTest, EmptyDest) { + std::string abc = "abc"; + char dest[4]; + + dest[0] = '\0'; + + char *result = __llvm_libc::strcat(dest, abc.c_str()); + ASSERT_EQ(dest, result); + ASSERT_EQ(std::string(dest), abc); + ASSERT_EQ(std::string(dest).size(), abc.size()); +} + +TEST(StrCatTest, NonEmptyDest) { + std::string abc = "abc"; + char dest[7]; + + dest[0] = 'x'; + dest[1] = 'y'; + dest[2] = 'z'; + dest[3] = '\0'; + + char *result = __llvm_libc::strcat(dest, abc.c_str()); + ASSERT_EQ(dest, result); + ASSERT_EQ(std::string(dest), std::string("xyz") + abc); + ASSERT_EQ(std::string(dest).size(), abc.size() + 3); +} diff --git a/libc/test/src/string/strcpy_test.cpp b/libc/test/src/string/strcpy_test.cpp new file mode 100644 index 00000000000..e68ea5103db --- /dev/null +++ b/libc/test/src/string/strcpy_test.cpp @@ -0,0 +1,36 @@ +//===----------------------- Unittests for strcpy -------------------------===// +// +// 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 <string> + +#include "src/string/strcpy/strcpy.h" +#include "gtest/gtest.h" + +TEST(StrCpyTest, EmptyDest) { + std::string abc = "abc"; + char dest[4]; + + char *result = __llvm_libc::strcpy(dest, abc.c_str()); + ASSERT_EQ(dest, result); + ASSERT_EQ(std::string(dest), abc); + ASSERT_EQ(std::string(dest).size(), abc.size()); +} + +TEST(StrCpyTest, OffsetDest) { + std::string abc = "abc"; + char dest[7]; + + dest[0] = 'x'; + dest[1] = 'y'; + dest[2] = 'z'; + + char *result = __llvm_libc::strcpy(dest + 3, abc.c_str()); + ASSERT_EQ(dest + 3, result); + ASSERT_EQ(std::string(dest), std::string("xyz") + abc); + ASSERT_EQ(std::string(dest).size(), abc.size() + 3); +} diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt new file mode 100644 index 00000000000..03c59bfc4a0 --- /dev/null +++ b/libc/test/src/sys/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(mman) diff --git a/libc/test/src/sys/mman/CMakeLists.txt b/libc/test/src/sys/mman/CMakeLists.txt new file mode 100644 index 00000000000..3e153eb1134 --- /dev/null +++ b/libc/test/src/sys/mman/CMakeLists.txt @@ -0,0 +1,14 @@ +add_custom_target(libc_sys_mman_unittests) +add_dependencies(check_libc libc_sys_mman_unittests) + +add_libc_unittest( + mmap_test + SUITE + libc_sys_mman_unittests + SRCS + mmap_test.cpp + DEPENDS + mmap + munmap + __errno_location +) diff --git a/libc/test/src/sys/mman/mmap_test.cpp b/libc/test/src/sys/mman/mmap_test.cpp new file mode 100644 index 00000000000..ab2e6ceb4a9 --- /dev/null +++ b/libc/test/src/sys/mman/mmap_test.cpp @@ -0,0 +1,48 @@ +//===------------------ Unittests for mmap and munmap ---------------------===// +// +// 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/errno/llvmlibc_errno.h" +#include "src/sys/mman/mmap.h" +#include "src/sys/mman/munmap.h" + +#include "gtest/gtest.h" + +#include "errno.h" +#include "sys/mman.h" + +TEST(MMapTest, NoError) { + size_t alloc_size = 128; + llvmlibc_errno = 0; + void *addr = __llvm_libc::mmap(nullptr, alloc_size, PROT_READ, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + EXPECT_EQ(0, llvmlibc_errno); + EXPECT_NE(addr, MAP_FAILED); + + int *array = reinterpret_cast<int *>(addr); + // Reading from the memory should not crash the test. + // Since we used the MAP_ANONYMOUS flag, the contents of the newly + // allocated memory should be initialized to zero. + EXPECT_EQ(array[0], 0); + + int ret_val = __llvm_libc::munmap(addr, alloc_size); + EXPECT_EQ(0, ret_val); + EXPECT_EQ(0, llvmlibc_errno); +} + +TEST(MMapTest, Error_InvalidSize) { + llvmlibc_errno = 0; + void *addr = __llvm_libc::mmap(nullptr, 0, PROT_READ, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + EXPECT_EQ(EINVAL, llvmlibc_errno); + EXPECT_EQ(addr, MAP_FAILED); + + llvmlibc_errno = 0; + int ret_val = __llvm_libc::munmap(0, 0); + EXPECT_EQ(-1, ret_val); + EXPECT_EQ(EINVAL, llvmlibc_errno); +} |

