15 #include <gtest/gtest.h> 17 #include "../../include/ecl/ipc/semaphore.hpp" 18 #include "../../include/ecl/ipc/shared_memory.hpp" 20 #ifdef ECL_HAS_SEMAPHORES 27 using ecl::SharedMemory;
51 class SemaphoreTestData {
54 void initialise() { value[0] = 1.0; value[1] = 2.0; };
65 using ecl::ipc::tests::SemaphoreTestData;
79 TEST(SemaphoreTests,access) {
81 string sm_name(
"shared_memory");
82 string sem_name(
"shared_semaphore");
93 SharedMemory<SemaphoreTestData> sm(sm_name);
94 Semaphore semaphore(sem_name);
97 if ( !semaphore.trylock() ) {
102 SemaphoreTestData *data = sm.data();
104 EXPECT_EQ(1.3,data->value[0]);
105 EXPECT_EQ(2.3,data->value[1]);
109 std::cout << e.
what() << std::endl;
114 std::cerr <<
"Failed to fork" << std::endl;
123 SharedMemory<SemaphoreTestData> sm(sm_name);
124 Semaphore semaphore(sem_name);
125 SemaphoreTestData *data = sm.data();
129 data->value[0] = 1.3;
130 data->value[1] = 2.3;
131 EXPECT_EQ(1.3,data->value[0]);
132 EXPECT_EQ(2.3,data->value[1]);
139 std::cout << e.
what() << std::endl;
150 int main(
int argc,
char **argv) {
152 std::cout << std::endl;
153 std::cout <<
"What you should see:" << std::endl;
154 std::cout <<
" - Process forks" << std::endl;
155 std::cout <<
" - Parent creates shared memory." << std::endl;
156 std::cout <<
" - Parent locks a semaphore and enters shared memory for 2 seconds." << std::endl;
157 std::cout <<
" - Child waits one second and attempts to lock the already locked semaphore." << std::endl;
158 std::cout <<
" - Parent writes into shared memory and unlocks the semaphore." << std::endl;
159 std::cout <<
" - Child finally gains access and reads the shared memory." << std::endl;
160 std::cout <<
" - Parent process sleeps for four seconds while child dies." << std::endl;
161 std::cout <<
" - Parent dies." << std::endl;
162 std::cout << std::endl;
164 testing::InitGoogleTest(&argc,argv);
165 return RUN_ALL_TESTS();
174 int main(
int argc,
char **argv) {
175 std::cout << std::endl;
176 std::cout <<
"Semaphores are not supported on this platform (or ecl is just lacking)." << std::endl;
177 std::cout << std::endl;
int main(int argc, char **argv)
Embedded control libraries.
const char * what() const
TEST(TypeTests, fundamentals)