blob: 2308ad95f6e10a15afcdd2230c6effddddfc52e8 [file] [log] [blame]
Elliott Hughesa55f6302013-01-02 14:23:43 -08001/*
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>
Christopher Ferris13613132013-10-28 15:24:04 -070018#include "ScopedSignalHandler.h"
Elliott Hughesb4f76162013-09-19 16:27:24 -070019#include "TemporaryFile.h"
Elliott Hughesa55f6302013-01-02 14:23:43 -080020
Elliott Hughes428f5562013-02-05 16:10:59 -080021#include <stdint.h>
Elliott Hughesa55f6302013-01-02 14:23:43 -080022#include <unistd.h>
23
24TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) {
25 ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0);
26}
Elliott Hughes428f5562013-02-05 16:10:59 -080027
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}
Elliott Hughesb4f76162013-09-19 16:27:24 -070037
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}
Elliott Hughes11952072013-10-24 15:15:14 -070077
78static bool gPauseTestFlag = false;
79static void PauseTestSignalHandler(int) {
80 gPauseTestFlag = true;
81}
82
83TEST(unistd, pause) {
Christopher Ferris13613132013-10-28 15:24:04 -070084 ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
85
Elliott Hughes11952072013-10-24 15:15:14 -070086 alarm(1);
87 ASSERT_FALSE(gPauseTestFlag);
88 ASSERT_EQ(-1, pause());
89 ASSERT_TRUE(gPauseTestFlag);
90}