shared_memory.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 /*****************************************************************************
14 ** Includes
15 *****************************************************************************/
16 
17 #include <iostream>
18 #include <cstdlib>
19 #include <gtest/gtest.h>
21 #include <ecl/time/sleep.hpp>
22 #include "../../include/ecl/ipc/shared_memory.hpp"
23 
24 #ifdef ECL_HAS_SHARED_MEMORY
25 // Should check for posix support too because we're using posix fork, but
26 // posix is all we got for now, so don't care too much.
27 
28 /*****************************************************************************
29 ** Using
30 *****************************************************************************/
31 
32 using std::cout;
33 using std::endl;
34 using std::cerr;
35 using std::string;
36 using ecl::SharedMemory;
37 using ecl::Sleep;
38 
39 /*****************************************************************************
40 ** Doxygen
41 *****************************************************************************/
42 
47 /*****************************************************************************
48 ** Namespaces
49 *****************************************************************************/
50 
51 namespace ecl {
52 namespace ipc {
53 namespace tests {
54 
55 /*****************************************************************************
56 ** Data Storage Class
57 *****************************************************************************/
58 
59 class Data {
60  public:
61  Data(double d1 = 0.0, double d2 = 0.0) {value[0] = d1; value[1] = d2;}
62  double value[2];
63 };
64 
65 } // namespace tests
66 } // namespace ipc
67 } // namespace ecl
68 
69 /*****************************************************************************
70 ** Using
71 *****************************************************************************/
72 
73 using ecl::ipc::tests::Data;
74 
75 /*****************************************************************************
76 ** Doxygen
77 *****************************************************************************/
78 
83 /*****************************************************************************
84 ** Tests
85 *****************************************************************************/
86 
87 TEST(SharedMemoryTests,access) {
88 
89  string name("shared_memory");
90  Sleep sleep;
91 
92  pid_t pID = fork();
93  if (pID == 0)
94  {
95  /*********************************************************************
96  ** Child
97  *********************************************************************/
98  sleep(1);
99  try {
100  SharedMemory<Data> sm(name.c_str());
101  Data *data = sm.data();
102  EXPECT_EQ(1.3,data->value[0]);
103  EXPECT_EQ(2.3,data->value[1]);
104 // cout << "Child read: " << data->value[0] << " " << data->value[1] << endl;
105  } catch ( ecl::StandardException &e) {
106  // Don't fail the test, hudson doesn't let us have permissions for instance.
107  std::cout << e.what() << std::endl;
108  }
109  }
110  else if (pID < 0) // failed to fork
111  {
112  cerr << "Failed to fork" << endl;
113  exit(1);
114  }
115  else
116  {
117  /*********************************************************************
118  ** Parent
119  *********************************************************************/
120  try {
121  SharedMemory<Data> sm(name.c_str());
122  Data *data = sm.data();
123  data->value[0] = 1.3;
124  data->value[1] = 2.3;
125  EXPECT_EQ(1.3,data->value[0]);
126  EXPECT_EQ(2.3,data->value[1]);
127 // cout << "Parent wrote: " << data->value[0] << " " << data->value[1] << endl;
128  sleep(4);
129  } catch ( ecl::StandardException &e) {
130  // Don't fail the test, hudson doesn't let us have permissions for instance.
131  std::cout << e.what() << std::endl;
132  }
133  }
134 }
135 
136 /*****************************************************************************
137 ** Main program
138 *****************************************************************************/
139 
140 int main(int argc, char **argv) {
141 
142  testing::InitGoogleTest(&argc,argv);
143  return RUN_ALL_TESTS();
144 }
145 
146 #else
147 
148 /*****************************************************************************
149 ** Alternative Main
150 *****************************************************************************/
151 
152 int main(int argc, char **argv) {
153  std::cout << std::endl;
154  std::cout << "Shared memory is not supported on this platform (or ecl is just lacking)." << std::endl;
155  std::cout << std::endl;
156  return 0;
157 }
158 
159 #endif /* ECL_HAS_SHARED_MEMORY */
160 
161 
162 
Embedded control libraries.
const char * what() const
TEST(TypeTests, fundamentals)
int main(int argc, char **argv)


ecl_ipc
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:17