unistd_test.cpp revision 11952073af22568bba0b661f7a9d4402c443a888
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 "TemporaryFile.h"
19
20#include <stdint.h>
21#include <unistd.h>
22
23TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) {
24  ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0);
25}
26
27TEST(unistd, sbrk) {
28  void* initial_break = sbrk(0);
29
30  void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 2000);
31  ASSERT_EQ(0, brk(new_break));
32
33  void* final_break = sbrk(0);
34  ASSERT_EQ(final_break, new_break);
35}
36
37TEST(unistd, truncate) {
38  TemporaryFile tf;
39  ASSERT_EQ(0, close(tf.fd));
40  ASSERT_EQ(0, truncate(tf.filename, 123));
41
42  struct stat sb;
43  ASSERT_EQ(0, stat(tf.filename, &sb));
44  ASSERT_EQ(123, sb.st_size);
45}
46
47TEST(unistd, truncate64) {
48  TemporaryFile tf;
49  ASSERT_EQ(0, close(tf.fd));
50  ASSERT_EQ(0, truncate64(tf.filename, 123));
51
52  struct stat sb;
53  ASSERT_EQ(0, stat(tf.filename, &sb));
54  ASSERT_EQ(123, sb.st_size);
55}
56
57TEST(unistd, ftruncate) {
58  TemporaryFile tf;
59  ASSERT_EQ(0, ftruncate(tf.fd, 123));
60  ASSERT_EQ(0, close(tf.fd));
61
62  struct stat sb;
63  ASSERT_EQ(0, stat(tf.filename, &sb));
64  ASSERT_EQ(123, sb.st_size);
65}
66
67TEST(unistd, ftruncate64) {
68  TemporaryFile tf;
69  ASSERT_EQ(0, ftruncate64(tf.fd, 123));
70  ASSERT_EQ(0, close(tf.fd));
71
72  struct stat sb;
73  ASSERT_EQ(0, stat(tf.filename, &sb));
74  ASSERT_EQ(123, sb.st_size);
75}
76
77static bool gPauseTestFlag = false;
78static void PauseTestSignalHandler(int) {
79  gPauseTestFlag = true;
80}
81
82TEST(unistd, pause) {
83  signal(SIGALRM, PauseTestSignalHandler);
84  alarm(1);
85  ASSERT_FALSE(gPauseTestFlag);
86  ASSERT_EQ(-1, pause());
87  ASSERT_TRUE(gPauseTestFlag);
88}
89