this repo has no description
at trunk 87 lines 2.3 kB view raw
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 2#include "os.h" 3 4#include <csignal> 5#include <cstring> 6 7#include "gtest/gtest.h" 8 9namespace py { 10 11static int count(const byte* array, byte ch, int length) { 12 int result = 0; 13 for (int i = 0; i < length; i++) { 14 if (array[i] == ch) { 15 result++; 16 } 17 } 18 return result; 19} 20 21TEST(OsTest, allocateUseAndFreeOnePage) { 22 // Allocate a page of memory. 23 byte* page = OS::allocateMemory(OS::kPageSize, nullptr); 24 ASSERT_NE(page, nullptr); 25 26 // Read from every allocated byte. 27 int num_zeroes = count(page, 0, OS::kPageSize); 28 // Every byte should have a value of zero. 29 ASSERT_EQ(num_zeroes, OS::kPageSize); 30 31 // Write to every allocated byte. 32 std::memset(page, 1, OS::kPageSize); 33 int num_ones = count(page, 1, OS::kPageSize); 34 // Every byte should have a value of one. 35 ASSERT_EQ(num_ones, OS::kPageSize); 36 37 // Release the page. 38 bool is_free = OS::freeMemory(page, OS::kPageSize); 39 EXPECT_TRUE(is_free); 40} 41 42TEST(OsTest, allocateUseAndFreeMultiplePages) { 43 // Not a multiple of a page. 44 const int size = 17 * kKiB; 45 46 // Allocate the pages. 47 byte* page = OS::allocateMemory(size, nullptr); 48 ASSERT_NE(page, nullptr); 49 50 // Read from every allocated byte. 51 int num_zeroes = count(page, 0, size); 52 // Every byte should have a value of zero. 53 ASSERT_EQ(num_zeroes, size); 54 55 // Write to every allocated byte. 56 std::memset(page, 1, size); 57 int num_ones = count(page, 1, size); 58 // Every byte should have a value of one. 59 ASSERT_EQ(num_ones, size); 60 61 // Release the pages. 62 bool is_free = OS::freeMemory(page, size); 63 EXPECT_TRUE(is_free); 64} 65 66TEST(OsTest, SignalHandlerWithSigusr1SetsSignalHandler) { 67 SignalHandler dummy = [](int) {}; 68 SignalHandler original = OS::signalHandler(SIGUSR1); 69 SignalHandler old = OS::setSignalHandler(SIGUSR1, dummy); 70 EXPECT_EQ(old, original); 71 72 SignalHandler current = OS::signalHandler(SIGUSR1); 73 EXPECT_EQ(current, dummy); 74 75 old = OS::setSignalHandler(SIGUSR1, original); 76 EXPECT_EQ(old, dummy); 77} 78 79TEST(OsTest, SetSignalHandlerWithSigkillReturnsSigErr) { 80 EXPECT_EQ(OS::setSignalHandler(SIGKILL, SIG_IGN), SIG_ERR); 81} 82 83TEST(OsTest, SetSignalHandlerWithSigstopReturnsSigErr) { 84 EXPECT_EQ(OS::setSignalHandler(SIGSTOP, SIG_IGN), SIG_ERR); 85} 86 87} // namespace py