42 lines · cpp
1//===-- Unittests for madvise ---------------------------------------------===//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/sys/mman/madvise.h"10#include "src/sys/mman/mmap.h"11#include "src/sys/mman/munmap.h"12#include "test/UnitTest/ErrnoCheckingTest.h"13#include "test/UnitTest/ErrnoSetterMatcher.h"14#include "test/UnitTest/Test.h"15 16using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;17using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;18using LlvmLibcMadviseTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;19 20TEST_F(LlvmLibcMadviseTest, NoError) {21 size_t alloc_size = 128;22 void *addr = LIBC_NAMESPACE::mmap(nullptr, alloc_size, PROT_READ,23 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);24 ASSERT_ERRNO_SUCCESS();25 EXPECT_NE(addr, MAP_FAILED);26 27 EXPECT_THAT(LIBC_NAMESPACE::madvise(addr, alloc_size, MADV_RANDOM),28 Succeeds());29 30 int *array = reinterpret_cast<int *>(addr);31 // Reading from the memory should not crash the test.32 // Since we used the MAP_ANONYMOUS flag, the contents of the newly33 // allocated memory should be initialized to zero.34 EXPECT_EQ(array[0], 0);35 EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, alloc_size), Succeeds());36}37 38TEST_F(LlvmLibcMadviseTest, Error_BadPtr) {39 EXPECT_THAT(LIBC_NAMESPACE::madvise(nullptr, 8, MADV_SEQUENTIAL),40 Fails(ENOMEM));41}42