Mercurial > ecos
comparison packages/kernel/current/tests/mqueue1.cxx @ 115:6ed91473a1cd ecos-sw-2000-08-21
Merge from eCos master repository on 2000-08-21-22:40:54-BST
| author | jlarmour |
|---|---|
| date | Fri, 25 Aug 2000 17:32:38 +0000 |
| parents | |
| children | eb9fd8c04db3 |
comparison
equal
deleted
inserted
replaced
| 114:5ad2b71d525e | 115:6ed91473a1cd |
|---|---|
| 1 /*======================================================================== | |
| 2 // | |
| 3 // mqueue1.cxx | |
| 4 // | |
| 5 // Message queues tests | |
| 6 // | |
| 7 //======================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
| 9 // | |
| 10 // ------------------------------------------- | |
| 11 // The contents of this file are subject to the Red Hat eCos Public License | |
| 12 // Version 1.1 (the "License"); you may not use this file except in | |
| 13 // compliance with the License. You may obtain a copy of the License at | |
| 14 // http://www.redhat.com/ | |
| 15 // | |
| 16 // Software distributed under the License is distributed on an "AS IS" | |
| 17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | |
| 18 // License for the specific language governing rights and limitations under | |
| 19 // the License. | |
| 20 // | |
| 21 // The Original Code is eCos - Embedded Configurable Operating System, | |
| 22 // released September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Red Hat. | |
| 25 // Portions created by Red Hat are | |
| 26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. | |
| 27 // All Rights Reserved. | |
| 28 // ------------------------------------------- | |
| 29 // | |
| 30 //####COPYRIGHTEND#### | |
| 31 //======================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): jlarmour | |
| 35 // Contributors: | |
| 36 // Date: 2000-05-12 | |
| 37 // Purpose: This file provides tests for eCos mqueues | |
| 38 // Description: | |
| 39 // Usage: | |
| 40 // | |
| 41 //####DESCRIPTIONEND#### | |
| 42 // | |
| 43 //====================================================================== | |
| 44 */ | |
| 45 | |
| 46 /* CONFIGURATION */ | |
| 47 | |
| 48 #include <pkgconf/kernel.h> | |
| 49 | |
| 50 /* INCLUDES */ | |
| 51 | |
| 52 #include <cyg/infra/cyg_type.h> // common types and externC | |
| 53 #include <cyg/kernel/thread.hxx> // Cyg_Thread | |
| 54 #include <cyg/kernel/thread.inl> | |
| 55 #include <cyg/kernel/mqueue.hxx> // Mqueue Header | |
| 56 #include <cyg/kernel/sema.hxx> // semaphores | |
| 57 #include <cyg/infra/testcase.h> // test API | |
| 58 | |
| 59 // use the common kernel test magic to define 2 threads | |
| 60 #define NTHREADS 2 | |
| 61 #include "testaux.hxx" | |
| 62 | |
| 63 /* GLOBALS */ | |
| 64 | |
| 65 static char mempool[500]; | |
| 66 static size_t storedmempoollen; | |
| 67 Cyg_Mqueue *mq; | |
| 68 static Cyg_Binary_Semaphore t0sem, t1sem; | |
| 69 static int calledback; | |
| 70 | |
| 71 | |
| 72 /* FUNCTIONS */ | |
| 73 | |
| 74 static int | |
| 75 my_memcmp(const void *m1, const void *m2, size_t n) | |
| 76 { | |
| 77 char *s1 = (char *)m1; | |
| 78 char *s2 = (char *)m2; | |
| 79 | |
| 80 while (n--) { | |
| 81 if (*s1 != *s2) | |
| 82 return *s1 - *s2; | |
| 83 s1++; | |
| 84 s2++; | |
| 85 } | |
| 86 return 0; | |
| 87 } // my_memcmp() | |
| 88 | |
| 89 static void * | |
| 90 my_alloc( size_t len ) | |
| 91 { | |
| 92 if ( len > sizeof(mempool) ) | |
| 93 return NULL; | |
| 94 | |
| 95 storedmempoollen = len; | |
| 96 return &mempool[0]; | |
| 97 } | |
| 98 | |
| 99 static void | |
| 100 my_free( void *ptr, size_t len ) | |
| 101 { | |
| 102 CYG_TEST_PASS_FAIL( (ptr == &mempool[0]) && (len == storedmempoollen), | |
| 103 "Freed pool correctly"); | |
| 104 mq = NULL; // invalidate | |
| 105 } | |
| 106 | |
| 107 static void | |
| 108 callback(Cyg_Mqueue &mq, CYG_ADDRWORD data) | |
| 109 { | |
| 110 calledback += (int)data; | |
| 111 } | |
| 112 | |
| 113 //************************************************************************ | |
| 114 //************************************************************************ | |
| 115 | |
| 116 static void | |
| 117 t0( CYG_ADDRWORD data ) | |
| 118 { | |
| 119 Cyg_Mqueue::qerr_t err; | |
| 120 char buf[35]; | |
| 121 size_t len; | |
| 122 unsigned int prio; | |
| 123 bool b; | |
| 124 | |
| 125 Cyg_Mqueue the_mq(4, 32, &my_alloc, &my_free, &err ); | |
| 126 CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, | |
| 127 "Create queue" ); | |
| 128 mq = &the_mq; | |
| 129 | |
| 130 //------------------------------------------------------------------------ | |
| 131 | |
| 132 err = mq->put( "Peter piper picked", sizeof("Peter piper picked"), | |
| 133 5, true ); | |
| 134 CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, "Simple (put)"); | |
| 135 | |
| 136 t1sem.post(); | |
| 137 | |
| 138 //------------------------------------------------------------------------ | |
| 139 | |
| 140 t0sem.wait(); | |
| 141 | |
| 142 err = mq->get( buf, &len, &prio, true ); | |
| 143 b = (err == Cyg_Mqueue::OK); | |
| 144 if (b) | |
| 145 b = (len == sizeof("a peck of")); | |
| 146 if (b) | |
| 147 b = (prio == 100); | |
| 148 if (b) | |
| 149 b = (0 == | |
| 150 my_memcmp(buf, "a peck of", sizeof("a peck of")) | |
| 151 ); | |
| 152 CYG_TEST_PASS_FAIL( b, "Blocking get"); | |
| 153 | |
| 154 //------------------------------------------------------------------------ | |
| 155 | |
| 156 t0sem.wait(); | |
| 157 | |
| 158 CYG_TEST_PASS_FAIL( 4 == mq->count(), "mq count" ); | |
| 159 err = mq->get( buf, &len, &prio, false ); | |
| 160 b = (err == Cyg_Mqueue::OK); | |
| 161 if (b) | |
| 162 b = (len == sizeof("pickled peppers")); | |
| 163 if (b) | |
| 164 b = (prio == 300); | |
| 165 if (b) | |
| 166 b = (0 == | |
| 167 my_memcmp(buf, "pickled peppers", sizeof("pickled peppers")) | |
| 168 ); | |
| 169 if (b) | |
| 170 b = (3 == mq->count()); | |
| 171 CYG_TEST_PASS_FAIL( b, "Prioritized (get 1)"); | |
| 172 | |
| 173 err = mq->get( buf, &len, &prio, false ); | |
| 174 b = (err == Cyg_Mqueue::OK); | |
| 175 if (b) | |
| 176 b = (len == sizeof(".")); | |
| 177 if (b) | |
| 178 b = (prio == 250); | |
| 179 if (b) | |
| 180 b = (0 == | |
| 181 my_memcmp(buf, ".", sizeof(".")) | |
| 182 ); | |
| 183 if (b) | |
| 184 b = (2 == mq->count()); | |
| 185 CYG_TEST_PASS_FAIL( b, "Prioritized (get 2)"); | |
| 186 | |
| 187 err = mq->get( buf, &len, &prio, false ); | |
| 188 b = (err == Cyg_Mqueue::OK); | |
| 189 if (b) | |
| 190 b = (len == 1); | |
| 191 if (b) | |
| 192 b = (prio == 225); | |
| 193 if (b) | |
| 194 b = (0 == | |
| 195 my_memcmp(buf, "", 1) | |
| 196 ); | |
| 197 if (b) | |
| 198 b = (1 == mq->count()); | |
| 199 CYG_TEST_PASS_FAIL( b, "Prioritized (get 3)"); | |
| 200 | |
| 201 err = mq->get( buf, &len, &prio, false ); | |
| 202 b = (err == Cyg_Mqueue::OK); | |
| 203 if (b) | |
| 204 b = (len == sizeof("If Peter")); | |
| 205 if (b) | |
| 206 b = (prio == 200); | |
| 207 if (b) | |
| 208 b = (0 == | |
| 209 my_memcmp(buf, "If Peter", sizeof("If Peter")) | |
| 210 ); | |
| 211 if (b) | |
| 212 b = (0 == mq->count()); | |
| 213 CYG_TEST_PASS_FAIL( b, "Prioritized (get 4)"); | |
| 214 | |
| 215 //------------------------------------------------------------------------ | |
| 216 | |
| 217 err = mq->get( buf, &len, &prio, false ); | |
| 218 | |
| 219 CYG_TEST_PASS_FAIL( Cyg_Mqueue::WOULDBLOCK == err, | |
| 220 "Non-blocking get of empty queue" ); | |
| 221 | |
| 222 //------------------------------------------------------------------------ | |
| 223 | |
| 224 Cyg_Mqueue::callback_fn_t oldcallback; | |
| 225 | |
| 226 oldcallback = mq->setnotify( &callback, (CYG_ADDRWORD) 42 ); | |
| 227 | |
| 228 err = mq->put( "If Peter", sizeof("If Peter"), | |
| 229 200, false ); | |
| 230 CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, | |
| 231 "Prioritized (put in empty queue)"); | |
| 232 CYG_TEST_PASS_FAIL( 42 == calledback, "callback" ); | |
| 233 CYG_TEST_PASS_FAIL( NULL == oldcallback, "oldcallback" ); | |
| 234 | |
| 235 err = mq->get( buf, &len, &prio, false ); | |
| 236 b = (err == Cyg_Mqueue::OK); | |
| 237 if (b) | |
| 238 b = (len == sizeof("If Peter")); | |
| 239 if (b) | |
| 240 b = (prio == 200); | |
| 241 if (b) | |
| 242 b = (0 == | |
| 243 my_memcmp(buf, "If Peter", sizeof("If Peter")) | |
| 244 ); | |
| 245 CYG_TEST_PASS_FAIL( b, "Prioritized (get 2)"); | |
| 246 | |
| 247 t1sem.post(); | |
| 248 | |
| 249 err = mq->get( buf, &len, &prio, true ); | |
| 250 b = (err == Cyg_Mqueue::OK); | |
| 251 if (b) | |
| 252 b = (len == 32); | |
| 253 if (b) | |
| 254 b = (42 == calledback); | |
| 255 if (b) | |
| 256 b = (prio == 250); | |
| 257 if (b) | |
| 258 b = (0 == | |
| 259 my_memcmp(buf, "12345678901234567890123456789012", 32) | |
| 260 ); | |
| 261 CYG_TEST_PASS_FAIL( b, "callback (blocked wait)"); | |
| 262 | |
| 263 //------------------------------------------------------------------------ | |
| 264 | |
| 265 t1sem.post(); | |
| 266 t0sem.wait(); | |
| 267 | |
| 268 } // t0() | |
| 269 | |
| 270 | |
| 271 //************************************************************************ | |
| 272 //************************************************************************ | |
| 273 | |
| 274 | |
| 275 static void | |
| 276 t1( CYG_ADDRWORD data ) | |
| 277 { | |
| 278 Cyg_Mqueue::qerr_t err; | |
| 279 char buf[35]; | |
| 280 size_t len; | |
| 281 unsigned int prio; | |
| 282 bool b; | |
| 283 | |
| 284 //------------------------------------------------------------------------ | |
| 285 | |
| 286 // wait till t0 says we can go | |
| 287 t1sem.wait(); | |
| 288 | |
| 289 err = mq->get( buf, &len, &prio, true ); | |
| 290 b = (err == Cyg_Mqueue::OK); | |
| 291 if (b) | |
| 292 b = (len == sizeof("Peter piper picked")); | |
| 293 if (b) | |
| 294 b = (prio == 5); | |
| 295 if (b) | |
| 296 b = (0 == | |
| 297 my_memcmp(buf, "Peter piper picked", sizeof("Peter piper picked")) | |
| 298 ); | |
| 299 | |
| 300 CYG_TEST_PASS_FAIL( b, "Simple"); | |
| 301 | |
| 302 //------------------------------------------------------------------------ | |
| 303 | |
| 304 t0sem.post(); // t0 should run straight away | |
| 305 Cyg_Thread::yield(); // but just in case we have a funny sched | |
| 306 | |
| 307 // by now t0 is blocked in mq->get | |
| 308 | |
| 309 err = mq->put( "a peck of", sizeof("a peck of"), | |
| 310 100, false ); | |
| 311 CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, "Block (put in empty queue)"); | |
| 312 | |
| 313 //------------------------------------------------------------------------ | |
| 314 | |
| 315 err = mq->put( "If Peter", sizeof("If Peter"), | |
| 316 200, false ); | |
| 317 CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, | |
| 318 "Prioritized (put in empty queue)"); | |
| 319 | |
| 320 err = mq->put( "pickled peppers", sizeof("pickled peppers"), | |
| 321 300, false ); | |
| 322 CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, | |
| 323 "Prioritized (put in queue w/1)"); | |
| 324 | |
| 325 err = mq->put( ".", sizeof("."), 250, false ); | |
| 326 CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, | |
| 327 "Prioritized (put in queue w/2)"); | |
| 328 | |
| 329 err = mq->put( "", 1, 225, false ); | |
| 330 CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, | |
| 331 "Prioritized (put in queue w/3)"); | |
| 332 | |
| 333 err = mq->put( "foobar", 6, 1, false ); | |
| 334 CYG_TEST_PASS_FAIL( Cyg_Mqueue::WOULDBLOCK == err, | |
| 335 "Prioritized (full queue)"); | |
| 336 | |
| 337 t0sem.post(); | |
| 338 | |
| 339 //------------------------------------------------------------------------ | |
| 340 | |
| 341 t1sem.wait(); | |
| 342 Cyg_Thread::yield(); // but just in case we have a funny sched | |
| 343 | |
| 344 err = mq->put( "12345678901234567890123456789012xxxx", 32, | |
| 345 250, false ); | |
| 346 CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, | |
| 347 "callback (put in queue)"); | |
| 348 | |
| 349 //------------------------------------------------------------------------ | |
| 350 | |
| 351 t1sem.wait(); | |
| 352 | |
| 353 // Create an oversized queue | |
| 354 { | |
| 355 Cyg_Mqueue huge_mq(99999, 99999, &my_alloc, &my_free, &err ); | |
| 356 CYG_TEST_PASS_FAIL( Cyg_Mqueue::NOMEM == err, | |
| 357 "Oversized queue rejected" ); | |
| 358 // and it now gets destructed - but that shouldn't call free | |
| 359 // to be called | |
| 360 } | |
| 361 | |
| 362 //------------------------------------------------------------------------ | |
| 363 | |
| 364 t0sem.post(); // t0 should run straight away | |
| 365 Cyg_Thread::yield(); // but just in case we have a funny sched | |
| 366 | |
| 367 // check that mq was destroyed when t0 dropped off the end | |
| 368 CYG_TEST_PASS_FAIL( NULL == mq, "queue destroyed correctly" ); | |
| 369 | |
| 370 CYG_TEST_EXIT("kernel mqueue test 1"); | |
| 371 | |
| 372 } // t1() | |
| 373 | |
| 374 | |
| 375 //************************************************************************ | |
| 376 //************************************************************************ | |
| 377 | |
| 378 externC void | |
| 379 cyg_user_start(void) | |
| 380 { | |
| 381 CYG_TEST_INIT(); | |
| 382 | |
| 383 CYG_TEST_INFO( "Starting kernel mqueue test 1" ); | |
| 384 new_thread( t0, 0); | |
| 385 new_thread( t1, 1); | |
| 386 | |
| 387 #ifdef CYGIMP_THREAD_PRIORITY | |
| 388 thread[0]->set_priority( 4 ); | |
| 389 thread[1]->set_priority( 5 ); // make sure the threads execute as intended | |
| 390 #endif | |
| 391 } // cyg_user_start() | |
| 392 | |
| 393 //------------------------------------------------------------------------ | |
| 394 | |
| 395 | |
| 396 /* EOF mqueue1.cxx */ |
