unistd_test.cpp revision 13613137bc4266656bffce464e525eb9ae6371f0
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <gtest/gtest.h> 18#include "ScopedSignalHandler.h" 19#include "TemporaryFile.h" 20 21#include <stdint.h> 22#include <unistd.h> 23 24TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) { 25 ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0); 26} 27 28TEST(unistd, sbrk) { 29 void* initial_break = sbrk(0); 30 31 void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 2000); 32 ASSERT_EQ(0, brk(new_break)); 33 34 void* final_break = sbrk(0); 35 ASSERT_EQ(final_break, new_break); 36} 37 38TEST(unistd, truncate) { 39 TemporaryFile tf; 40 ASSERT_EQ(0, close(tf.fd)); 41 ASSERT_EQ(0, truncate(tf.filename, 123)); 42 43 struct stat sb; 44 ASSERT_EQ(0, stat(tf.filename, &sb)); 45 ASSERT_EQ(123, sb.st_size); 46} 47 48TEST(unistd, truncate64) { 49 TemporaryFile tf; 50 ASSERT_EQ(0, close(tf.fd)); 51 ASSERT_EQ(0, truncate64(tf.filename, 123)); 52 53 struct stat sb; 54 ASSERT_EQ(0, stat(tf.filename, &sb)); 55 ASSERT_EQ(123, sb.st_size); 56} 57 58TEST(unistd, ftruncate) { 59 TemporaryFile tf; 60 ASSERT_EQ(0, ftruncate(tf.fd, 123)); 61 ASSERT_EQ(0, close(tf.fd)); 62 63 struct stat sb; 64 ASSERT_EQ(0, stat(tf.filename, &sb)); 65 ASSERT_EQ(123, sb.st_size); 66} 67 68TEST(unistd, ftruncate64) { 69 TemporaryFile tf; 70 ASSERT_EQ(0, ftruncate64(tf.fd, 123)); 71 ASSERT_EQ(0, close(tf.fd)); 72 73 struct stat sb; 74 ASSERT_EQ(0, stat(tf.filename, &sb)); 75 ASSERT_EQ(123, sb.st_size); 76} 77 78static bool gPauseTestFlag = false; 79static void PauseTestSignalHandler(int) { 80 gPauseTestFlag = true; 81} 82 83TEST(unistd, pause) { 84 ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler); 85 86 alarm(1); 87 ASSERT_FALSE(gPauseTestFlag); 88 ASSERT_EQ(-1, pause()); 89 ASSERT_TRUE(gPauseTestFlag); 90} 91