Compile fixes
[ACE_TAO.git] / ACE / tests / Simple_Message_Block_Test.cpp
blob2c331dd96968bb92ca8c84aeeadd77cac3321ae9
2 //=============================================================================
3 /**
4 * @file Simple_Message_Block_Test.cpp
6 * This test program is a torture test that illustrates how
7 * ACE_Message_Block reference counting works, how and when locks
8 * are used, how memory is managed, and how continuation chains
9 * of message blocks are made. Ideally used with purify :-)
11 * @author Irfan Pyarali <irfan@cs.wustl.edu>
13 //=============================================================================
16 #include "test_config.h"
17 #include "ace/Message_Block.h"
18 #include "ace/Synch_Traits.h"
19 #include "ace/Lock_Adapter_T.h"
20 #include "ace/OS_NS_string.h"
21 #include "ace/Thread_Mutex.h"
24 int
25 run_main (int, ACE_TCHAR *[])
27 ACE_START_TEST (ACE_TEXT ("Simple_Message_Block_Test"));
30 // Checks normal stack deletes.
31 ACE_Message_Block mb;
35 // Checks normal heap deletes.
36 ACE_Message_Block *mb = 0;
38 ACE_NEW_RETURN (mb, ACE_Message_Block, -1);
39 mb->release ();
43 // Checks continuation of message blocks on the stack.
44 ACE_Message_Block mb1 (1024);
45 ACE_Message_Block mb2 (1024);
47 mb1.cont (&mb2);
51 // Checks continuation of message blocks on the heap.
52 ACE_Message_Block *mb1;
53 ACE_Message_Block *mb2;
55 ACE_NEW_RETURN (mb1, ACE_Message_Block (1024), -1);
56 ACE_NEW_RETURN (mb2, ACE_Message_Block (1024), -1);
58 mb1->cont (mb2);
59 mb1->release ();
62 // Same set of tests but with locking_strategy set.
64 ACE_Lock_Adapter <ACE_SYNCH_MUTEX> mutex;
65 ACE_Lock *lock = &mutex;
68 // Checks normal stack deletes.
69 ACE_Message_Block mb;
70 mb.locking_strategy (lock);
74 // Checks normal heap deletes.
75 ACE_Message_Block *mb = 0;
76 ACE_NEW_RETURN (mb, ACE_Message_Block, -1);
77 mb->locking_strategy (lock);
78 mb->release ();
82 // Checks continuation of message blocks on the stack with one
83 // lock strategy.
84 ACE_Message_Block mb1 (1024);
85 ACE_Message_Block mb2 (1024);
87 mb1.locking_strategy (lock);
89 mb1.cont (&mb2);
93 // Checks continuation of message blocks on the heap with one
94 // lock strategy.
95 ACE_Message_Block *mb1;
96 ACE_Message_Block *mb2;
98 ACE_NEW_RETURN (mb1, ACE_Message_Block (1024), -1);
99 ACE_NEW_RETURN (mb2, ACE_Message_Block (1024), -1);
101 mb1->locking_strategy (lock);
103 mb1->cont (mb2);
104 mb1->release ();
108 // Checks continuation of message blocks on the stack with two
109 // lock strategy.
110 ACE_Message_Block mb1 (1024);
111 ACE_Message_Block mb2 (1024);
113 mb1.locking_strategy (lock);
114 mb2.locking_strategy (lock);
116 mb1.cont (&mb2);
120 // Checks continuation of message blocks on the heap with two
121 // lock strategy
122 ACE_Message_Block *mb1;
123 ACE_Message_Block *mb2;
125 ACE_NEW_RETURN (mb1, ACE_Message_Block (1024), -1);
126 ACE_NEW_RETURN (mb2, ACE_Message_Block (1024), -1);
128 mb1->locking_strategy (lock);
129 mb2->locking_strategy (lock);
131 mb1->cont (mb2);
132 mb1->release ();
136 // Checks continuation of message blocks on the heap with two
137 // lock strategy where the second one is a duplicate of the
138 // first
139 ACE_Message_Block *mb1;
140 ACE_NEW_RETURN (mb1, ACE_Message_Block (1024), -1);
141 mb1->locking_strategy (lock);
143 ACE_Message_Block *mb2 = mb1->duplicate ();
145 mb1->cont (mb2);
146 mb1->release ();
151 // Checks continuation of message blocks on the heap with two
152 // different lock strategies
154 ACE_Lock_Adapter <ACE_SYNCH_MUTEX> lock1;
155 ACE_Lock_Adapter <ACE_SYNCH_MUTEX> lock2;
157 ACE_Message_Block *mb1;
158 ACE_Message_Block *mb2;
160 ACE_NEW_RETURN (mb1, ACE_Message_Block (1024), -1);
161 ACE_NEW_RETURN (mb2, ACE_Message_Block (1024), -1);
163 mb1->locking_strategy (&lock1);
164 mb2->locking_strategy (&lock2);
166 mb1->cont (mb2);
167 mb1->release ();
171 // Checks failure of copy when "virtual" allocation (using mark)
172 // is too small
173 char message[]="abcdefghijklmnop";
174 ACE_Message_Block mb1 (ACE_OS::strlen (message) + 1);
175 ACE_Message_Block mb2 (ACE_OS::strlen (message) + 1);
177 // Resize mb2 so that we mark for use less than the allocated buffer
178 if (mb2.size (ACE_OS::strlen (message) + 1 - 10) == -1)
180 ACE_ERROR ((LM_ERROR,
181 ACE_TEXT ("(%P|%t) Resize test failed ..\n")));
184 // We expect this to succeed
185 if (mb1.copy (message, ACE_OS::strlen (message) + 1) == -1)
187 ACE_ERROR ((LM_ERROR,
188 ACE_TEXT ("(%P|%t) Copy test failed ..\n")));
191 // We expect this to fail
192 if (mb2.copy (message, ACE_OS::strlen (message) + 1) != -1)
194 ACE_ERROR ((LM_ERROR,
195 ACE_TEXT ("(%P|%t) Copy test succeeded when it should have failed ..\n")));
198 // We also expect this to fail
199 if (mb2.copy (message) != -1)
201 ACE_ERROR ((LM_ERROR,
202 ACE_TEXT ("(%P|%t) Copy test succeeded when it should have failed ..\n")));
205 ACE_END_TEST;
206 return 0;