torture.c 13.9 KB
Newer Older
yoogx's avatar
yoogx committed
1 2 3
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
yoogx's avatar
yoogx committed
4 5 6 7 8
#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h>        /* For mode constants */
#include <semaphore.h>
#include <string.h>
#include <stdbool.h>
yoogx's avatar
yoogx committed
9 10 11 12

/* Files generated from AADL model */
#include <request.h>
#include <deployment.h>
yoogx's avatar
yoogx committed
13

yoogx's avatar
yoogx committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <po_hi_transport.h>
#include <po_hi_gqueue.h>

/*****************************************************************************/
/* Helper macros to access AADL entities                                     */

#define LOCAL_PORT(THREAD_INSTANCE_NAME, PORT_NAME) THREAD_INSTANCE_NAME ## _local_ ## PORT_NAME
#define REQUEST_PORT(THREAD_INSTANCE_NAME, PORT_NAME) THREAD_INSTANCE_NAME ## _global_ ## PORT_NAME
#define PORT_VARIABLE(THREAD_INSTANCE_NAME, PORT_NAME) vars.REQUEST_PORT(THREAD_INSTANCE_NAME,PORT_NAME).REQUEST_PORT(THREAD_INSTANCE_NAME,PORT_NAME)

#define MACRO_TEST 42
#define MACRO_TEST_SECOND 33
#define MAX_TEST 0

int lvl = MACRO_TEST;
int sent_lvl;

int level = MACRO_TEST_SECOND;
int sent_level;

void test_sporad_1(__po_hi_task_id self);
void test_sporad_2(__po_hi_task_id self);
void test_sporad_3(__po_hi_task_id self);
void test_sporad_4(__po_hi_task_id self);
void test_sporad_5(__po_hi_task_id self);

bool init = true;
bool first_iteration = true;
int number = 0;

int count_p1;
int count_p2;
int count_p4;
yoogx's avatar
yoogx committed
47 48
int count_p5;
int count_p6;
yoogx's avatar
yoogx committed
49 50 51 52 53

__po_hi_local_port_t port_p1 = LOCAL_PORT (spo_thread,p1);
__po_hi_local_port_t port_p2 = LOCAL_PORT (spo_thread,p2);
__po_hi_local_port_t port_p4 = LOCAL_PORT (per_thread,p4);
__po_hi_local_port_t port_p3 = LOCAL_PORT (per_thread,p3);
yoogx's avatar
yoogx committed
54 55
__po_hi_local_port_t port_p5 = LOCAL_PORT (per_thread,p5);
__po_hi_local_port_t port_p6 = LOCAL_PORT (per_thread,p6);
yoogx's avatar
yoogx committed
56 57 58 59 60 61 62 63 64 65 66

/*
 * Semaphore used to coordinate the period and sporad tasks : At the
 * end of the first battery of test (number of message == 1), a
 * sem_post is done which liberates a sem_wait.  The number of
 * messages is increased by one.
 */
sem_t *semaphore;

/*****************************************************************************/
void period(__po_hi_task_id self) {
yoogx's avatar
yoogx committed
67 68
  int i, j;

69 70 71 72 73 74
  if (init == true){
    semaphore = sem_open("/aadl", O_CREAT|O_EXCL, S_IRUSR | S_IWUSR, 1);
    if (semaphore == NULL) {
      sem_unlink ("/aadl");
      semaphore = sem_open("/aadl", O_CREAT|O_EXCL, S_IRUSR | S_IWUSR, 1);
    }
yoogx's avatar
yoogx committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    init = false;
  }

  /* *** Boolean and semaphore launching the following test with
     *** "number" iterations *** */
  if (first_iteration){
    sem_wait(semaphore);
    printf("\n**** Starting Tests **** \n");
    first_iteration = false;
    number++;
  }

  __po_hi_request_t r1;
  __po_hi_request_t r2;

yoogx's avatar
yoogx committed
90
/*****************************************************************************/
yoogx's avatar
yoogx committed
91
  /* *** Initial Test of the different functions *** */
yoogx's avatar
yoogx committed
92
  /* *** Test of gqueue with one message *** */
yoogx's avatar
yoogx committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  if (number < 2){
    /* Message sent on Period Port 1 to Sporad port 2 */
    sent_lvl = lvl;
    r1.port = REQUEST_PORT (per_thread, p1);
    r1.PORT_VARIABLE (per_thread,p1) = lvl;
    lvl++;

#if defined (TEST_VERBOSE)
    printf("\n Storeout P1 to P2, task id = %d, port id = %d", self, LOCAL_PORT (per_thread, p1));
#endif

    __po_hi_gqueue_store_out
      (self,
       LOCAL_PORT (per_thread, p1),
       &r1);

    /* Message sent on Period Port 2 to Sporad Port 1 */
    sent_level = level;
    r2.port = REQUEST_PORT (per_thread, p2);
    r2.PORT_VARIABLE (per_thread,p2) = level;
    level++;
#if defined (TEST_VERBOSE)
    printf("\n Storeout P2 to P1, task id = %d, port id = %d\n",
           self, LOCAL_PORT (per_thread, p2));
#endif
    __po_hi_gqueue_store_out
      (self,
       LOCAL_PORT (per_thread, p2),
       &r2);
  }

yoogx's avatar
yoogx committed
124
/*****************************************************************************/
yoogx's avatar
yoogx committed
125 126 127 128 129 130 131
  /* *** Test of two messages sent on one port *** */
  /* Transmission */
  count_p4 = __po_hi_gqueue_get_count(self, port_p4);
  if ((number == 2)&&(count_p4 == 0)){
    printf("\n*** TEST PERIODIC 1 ***\n");
    count_p4 = __po_hi_gqueue_get_count(self, port_p4);
    assert (count_p4 == 0);
yoogx's avatar
yoogx committed
132
    for (i = 1; i <= number; i++){
yoogx's avatar
yoogx committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
      sent_lvl = lvl;
      r1.port = REQUEST_PORT (per_thread, p3);
      r1.PORT_VARIABLE (per_thread,p3) = lvl;
      lvl++;
#if defined (TEST_VERBOSE)
      printf("\n Storeout P1 to P2, task id = %d, port id = %d", self, LOCAL_PORT (per_thread, p3));
#endif
      __po_hi_gqueue_store_out
        (self,
         LOCAL_PORT (per_thread, p3),
         &r1);
      __po_hi_send_output (self,REQUEST_PORT(per_thread, p3));
    }
    assert (count_p4 == 0);
  }
  /* Reception */
  count_p4 = __po_hi_gqueue_get_count(self, port_p4);
  if ((number == 2)&&(count_p4 == number)){
    __po_hi_request_t request;
    int reception;
    count_p4 = __po_hi_gqueue_get_count(self, port_p4);
yoogx's avatar
yoogx committed
154
    for (j = 0; j < count_p4; j++) {
yoogx's avatar
yoogx committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    __po_hi_gqueue_get_value(self,port_p4,&(request));
    reception = request.PORT_VARIABLE(per_thread,p4);
#if defined (TEST_VERBOSE)
    printf("\nfirst request = %d", MACRO_TEST);
    printf("\nsent_lvl = %d", sent_lvl);
    printf("\nreceived = %d", reception);
    printf("\nnumber, j = %d %d\n", number, j);
#endif
    assert(reception == sent_lvl - number + j + 1);
    __po_hi_gqueue_next_value (self,port_p4);
   }
   printf ("Two Messages test passed\n");
    first_iteration = true;
    sem_post(semaphore);
  }

yoogx's avatar
yoogx committed
171 172 173 174 175 176 177 178 179 180
/*****************************************************************************/
   /* *** Test of gqueue error message *** */
   /* Transmission */

  count_p6 = __po_hi_gqueue_get_count(self, port_p6);
  if ((number == 3 )&&(count_p6 == 0)){

   printf("\n\n*** TEST PERIODIC 2 ***\n");
   count_p6 = __po_hi_gqueue_get_count(self, port_p6);
   assert (count_p6 == 0);
yoogx's avatar
yoogx committed
181
   for (i = 1; i < number ; i++){
yoogx's avatar
yoogx committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      sent_lvl = lvl;
      r1.port = REQUEST_PORT (per_thread, p5);
      r1.PORT_VARIABLE (per_thread,p5) = lvl;
      lvl++;
#if defined (TEST_VERBOSE)
      printf("\n Storeout P1 to P2, task id = %d, port id = %d", self, LOCAL_PORT (per_thread, p5));
#endif
      __po_hi_gqueue_store_out
        (self,
         LOCAL_PORT (per_thread, p5),
         &r1);
      __po_hi_send_output (self,REQUEST_PORT(per_thread, p5));
    }
  }
  /* Reception */

  count_p6 = __po_hi_gqueue_get_count(self, port_p6);
  if ((number == 3)&&(count_p6 == 1)){
    printf ("\n*** An error message should appear *** \n\n");
    count_p6 = __po_hi_gqueue_get_count(self, port_p6);
yoogx's avatar
yoogx committed
202
    for (j = 0; j < count_p6; j++) {
yoogx's avatar
yoogx committed
203 204 205 206 207 208 209 210 211 212 213
    __po_hi_gqueue_next_value (self,port_p6);
    }
     printf ("\n*** If so, error message test passed *** \n\n");
    first_iteration = true;
    sem_post(semaphore);
  }

  if (number > 3){
          sem_unlink("/aadl");
          sem_close(semaphore);
          exit(0);
yoogx's avatar
yoogx committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  }
}


/*****************************************************************************/
void sporad(__po_hi_task_id self) {
  /* Test with one message sent by the Period task */
  if (number == 1){
    /* Test of the get_count function */
    test_sporad_1 (self);
    /* Test of the next_value function */
    test_sporad_2 (self);
    /* Test of the function get_value on an empty port */
    test_sporad_3 (self);
    /* FIFO test on reception port P1 */
    test_sporad_4 (self);
    /* FIFO test on reception port P2 */
    test_sporad_5 (self);
  }
yoogx's avatar
yoogx committed
233

yoogx's avatar
yoogx committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
  /* Boolean and semaphore launching the next type of test */
  first_iteration = true;
  sem_post(semaphore);
}

/*****************************************************************************/
void test_sporad_1(__po_hi_task_id self) {
  printf ("\n*** TEST SPORADIC 1 ***\n");
  /*After reception the size used must be 1 */
  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 1);
  assert (count_p2 == 1);
  printf ("get_count test passed\n");
}

/*****************************************************************************/
void test_sporad_2(__po_hi_task_id self) {
yoogx's avatar
yoogx committed
252
  int i, reception;
yoogx's avatar
yoogx committed
253 254 255 256 257 258 259 260
  __po_hi_request_t request;
  __po_hi_local_port_t tryport;
  printf ("\n*** TEST SPORADIC 2 ***\n");
  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 1);
  assert (count_p2 == 1);
  /* All values are dequeued */
yoogx's avatar
yoogx committed
261
  for (i = 0; i < count_p1 + count_p2 ; i++) {
yoogx's avatar
yoogx committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    __po_hi_gqueue_wait_for_incoming_event(self, &tryport);
    if (i == 0){
      /* The sending is done first on port p1 to port p1 */
      assert(tryport == port_p2);
      printf ("wait_for_incoming_event test passed \n");
    }
    __po_hi_gqueue_get_value(self,tryport,&(request));
    if (tryport == port_p1){
      reception = request.PORT_VARIABLE(spo_thread,p1);
      assert (reception == sent_level);
      printf ("get_value first test passed \n");
    }
    if (tryport == port_p2){
      reception = request.PORT_VARIABLE(spo_thread,p2);
      assert (reception == sent_lvl);
      printf ("get_value first test passed \n");
    }
    __po_hi_gqueue_next_value (self, tryport);
  }
  /* The ports are supposedly all empty */
  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 0);
  assert (count_p2 == 0);
  printf ("next_value test passed\n");
}

/*****************************************************************************/
void test_sporad_3(__po_hi_task_id self) {
  printf ("\n*** TEST SPORADIC 3 ***\n");
  __po_hi_request_t request;

  /* Test of Get_value on an empty port: */
  /* The function supposedly blocks the thread until something is received: */
  __po_hi_gqueue_get_value(self,port_p2,&(request));

  /* Values have been received on each port*/
  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 1);
  assert (count_p2 == 1);
  printf ("get_value second test passed \n");
}
/*****************************************************************************/
void test_sporad_4(__po_hi_task_id self) {
  int reception;
  __po_hi_request_t request;

  printf ("\n*** TEST SPORADIC 4 ***\n");
  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 1);
  assert (count_p2 == 1);

  /* Dequeuing port P2 */
  __po_hi_gqueue_get_value(self,port_p2,&(request));
  __po_hi_gqueue_next_value (self, port_p2);

  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);

  assert (count_p1 == 1);
  assert (count_p2 == 0);

  /* Blocking on port P2 */
  __po_hi_gqueue_get_value(self,port_p2,&(request));

  /* Reception */

  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 2);
  assert (count_p2 == 1);
  /* We have attained optimal configuration */

  /* No issue on port P2 */
  __po_hi_gqueue_get_value(self,port_p2,&(request));
  reception = request.PORT_VARIABLE(spo_thread,p2);

#if defined (TEST_VERBOSE)
  printf("\nfirst request = %d", MACRO_TEST);
  printf("\nsent_lvl = %d", sent_lvl);
  printf("\nreceived = %d", reception);
#endif
  assert (reception == sent_lvl);

  /* On port P1 : Due to the FIFO aspect of the gqueue, the value
     received is not the one just sent, but the previous one */
  __po_hi_gqueue_get_value(self,port_p1,&(request));
  reception = request.PORT_VARIABLE(spo_thread,p1);

#if defined (TEST_VERBOSE)
  printf("\nfirst request = %d", MACRO_TEST_SECOND);
  printf("\nsent_level = %d", sent_level);
  printf("\nreceived = %d", reception);
#endif

  assert (reception != sent_level );
  assert (reception == sent_level - 1);

  /* We ensure to have only one value on each port */
  __po_hi_gqueue_next_value (self,port_p1);

  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 1);
  assert (count_p2 == 1);

  printf ("FIFO test on port p1 passed \n");

}

/*****************************************************************************/
void test_sporad_5(__po_hi_task_id self) {

yoogx's avatar
yoogx committed
377
  int i, j, reception;
yoogx's avatar
yoogx committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  __po_hi_request_t request;

  printf ("\n*** TEST SPORADIC 5 ****\n");
  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 1);
  assert (count_p2 == 1);

  /* Dequeuing port P1  */
  __po_hi_gqueue_get_value(self,port_p1,&(request));
  __po_hi_gqueue_next_value (self, port_p1);

  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);

  assert (count_p1 == 0);
  assert (count_p2 == 1);

  /* Blocking on port P1 */
  __po_hi_gqueue_get_value(self,port_p1,&(request));
  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 1);
  assert (count_p2 == 2);

  /* On port p2 : Due to the FIFO aspect of the gqueue, the value
     received is not the one just sent, but the previous one */
  __po_hi_gqueue_get_value(self,port_p2,&(request));
  reception = request.PORT_VARIABLE(spo_thread,p2);

#if defined (TEST_VERBOSE)
  printf("\nfirst request = %d", MACRO_TEST);
  printf("\nsent_lvl = %d", sent_lvl);
  printf("\nreceived = %d", reception);
#endif

  assert (reception != sent_lvl);
  assert (reception == sent_lvl - 1);

  /* No issue on port P1 */
  __po_hi_gqueue_get_value(self,port_p1,&(request));
  reception = request.PORT_VARIABLE(spo_thread,p1);

#if defined (TEST_VERBOSE)
  printf("\nfirst request = %d", MACRO_TEST_SECOND);
  printf("\nsent_level = %d", sent_level);
  printf("\nreceived = %d", reception);
#endif

  assert (reception == sent_level);
  __po_hi_gqueue_next_value (self,port_p2);

  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 1);
  assert (count_p2 == 1);

yoogx's avatar
yoogx committed
435
  for (i = 0; i < count_p1; i++) {
yoogx's avatar
yoogx committed
436 437
    __po_hi_gqueue_next_value (self,port_p1);
  }
yoogx's avatar
yoogx committed
438
  for (j = 0; j < count_p2; j++) {
yoogx's avatar
yoogx committed
439 440 441 442 443 444 445 446 447 448
    __po_hi_gqueue_next_value (self,port_p2);
  }

  count_p1 = __po_hi_gqueue_get_count(self, port_p1);
  count_p2 = __po_hi_gqueue_get_count(self, port_p2);
  assert (count_p1 == 0);
  assert (count_p2 == 0);

  printf ("FIFO test on port p2 passed\n");
}