]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - tests/semaphore_test.cpp
Merge "Ensure <fcntl.h> defines the S_* constants from <sys/stat.h>."
[android-sdk/platform-bionic.git] / tests / semaphore_test.cpp
1 /*
2  * Copyright (C) 2014 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  */
17 #include <semaphore.h>
19 #include <errno.h>
20 #include <gtest/gtest.h>
21 #include <limits.h>
22 #include <pthread.h>
23 #include <time.h>
24 #include <unistd.h>
26 #include "private/bionic_constants.h"
28 TEST(semaphore, sem_init) {
29   sem_t s;
31   // Perfectly fine initial values.
32   ASSERT_EQ(0, sem_init(&s, 0, 0));
33   ASSERT_EQ(0, sem_init(&s, 0, 1));
34   ASSERT_EQ(0, sem_init(&s, 0, 123));
36   // Too small an initial value.
37   errno = 0;
38   ASSERT_EQ(-1, sem_init(&s, 0, -1));
39   ASSERT_EQ(EINVAL, errno);
41   ASSERT_EQ(SEM_VALUE_MAX, sysconf(_SC_SEM_VALUE_MAX));
43   // The largest initial value.
44   ASSERT_EQ(0, sem_init(&s, 0, SEM_VALUE_MAX));
46   // Too large an initial value.
47   errno = 0;
48   ASSERT_EQ(-1, sem_init(&s, 0, SEM_VALUE_MAX + 1));
49   ASSERT_EQ(EINVAL, errno);
51   ASSERT_EQ(0, sem_destroy(&s));
52 }
54 TEST(semaphore, sem_trywait) {
55   sem_t s;
56   ASSERT_EQ(0, sem_init(&s, 0, 3));
57   ASSERT_EQ(0, sem_trywait(&s));
58   ASSERT_EQ(0, sem_trywait(&s));
59   ASSERT_EQ(0, sem_trywait(&s));
60   errno = 0;
61   ASSERT_EQ(-1, sem_trywait(&s));
62   ASSERT_EQ(EAGAIN, errno);
63   ASSERT_EQ(0, sem_destroy(&s));
64 }
66 static void SemWaitThreadTestFn(sem_t& sem) {
67   ASSERT_EQ(0, sem_wait(&sem));
68 }
70 static void* SemWaitThreadFn(void* arg) {
71   SemWaitThreadTestFn(*reinterpret_cast<sem_t*>(arg));
72   return nullptr;
73 }
75 TEST(semaphore, sem_wait__sem_post) {
76   sem_t s;
77   ASSERT_EQ(0, sem_init(&s, 0, 0));
79   pthread_t t1, t2, t3;
80   ASSERT_EQ(0, pthread_create(&t1, NULL, SemWaitThreadFn, &s));
81   ASSERT_EQ(0, pthread_create(&t2, NULL, SemWaitThreadFn, &s));
82   ASSERT_EQ(0, pthread_create(&t3, NULL, SemWaitThreadFn, &s));
84   ASSERT_EQ(0, sem_post(&s));
85   ASSERT_EQ(0, sem_post(&s));
86   ASSERT_EQ(0, sem_post(&s));
88   void* result;
89   ASSERT_EQ(0, pthread_join(t1, &result));
90   ASSERT_EQ(0, pthread_join(t2, &result));
91   ASSERT_EQ(0, pthread_join(t3, &result));
92 }
94 static inline void timespec_add_ms(timespec& ts, size_t ms) {
95   ts.tv_sec  += ms / 1000;
96   ts.tv_nsec += (ms % 1000) * 1000000;
97   if (ts.tv_nsec >= NS_PER_S) {
98     ts.tv_sec++;
99     ts.tv_nsec -= NS_PER_S;
100   }
103 TEST(semaphore, sem_timedwait) {
104   sem_t s;
105   ASSERT_EQ(0, sem_init(&s, 0, 0));
107   timespec ts;
108   ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
109   timespec_add_ms(ts, 100);
111   errno = 0;
112   ASSERT_EQ(-1, sem_timedwait(&s, &ts));
113   ASSERT_EQ(ETIMEDOUT, errno);
115   // A negative timeout is an error.
116   errno = 0;
117   ts.tv_nsec = -1;
118   ASSERT_EQ(-1, sem_timedwait(&s, &ts));
119   ASSERT_EQ(EINVAL, errno);
121   ASSERT_EQ(0, sem_destroy(&s));
124 TEST(semaphore, sem_getvalue) {
125   sem_t s;
126   ASSERT_EQ(0, sem_init(&s, 0, 0));
128   int i;
129   ASSERT_EQ(0, sem_getvalue(&s, &i));
130   ASSERT_EQ(0, i);
132   ASSERT_EQ(0, sem_post(&s));
133   ASSERT_EQ(0, sem_getvalue(&s, &i));
134   ASSERT_EQ(1, i);
136   ASSERT_EQ(0, sem_post(&s));
137   ASSERT_EQ(0, sem_getvalue(&s, &i));
138   ASSERT_EQ(2, i);
140   ASSERT_EQ(0, sem_wait(&s));
141   ASSERT_EQ(0, sem_getvalue(&s, &i));
142   ASSERT_EQ(1, i);