comparison src/animate.c @ 50:c986e45c1e91

Unittest for animate.c
author Thinker K.F. Li <thinker@branda.to>
date Sat, 09 Aug 2008 21:17:34 +0800
parents bdf711cbf0fb
children 5f5bd3ac9316
comparison
equal deleted inserted replaced
49:6a3726fa7aad 50:c986e45c1e91
94 cur_act != NULL; 94 cur_act != NULL;
95 cur_act = STAILQ_HEAD(word->actions)) { 95 cur_act = STAILQ_HEAD(word->actions)) {
96 STAILQ_REMOVE(word->actions, mb_action_t, next, cur_act); 96 STAILQ_REMOVE(word->actions, mb_action_t, next, cur_act);
97 cur_act->free(cur_act); 97 cur_act->free(cur_act);
98 } 98 }
99 free(word);
100 } 99 }
101 free(progm); 100 free(progm);
102 } 101 }
103 102
104 /*! \brief Add a new word into a program. 103 /*! \brief Add a new word into a program.
341 340
342 mb_word_add_action(word, (mb_action_t *)shift); 341 mb_word_add_action(word, (mb_action_t *)shift);
343 342
344 return (mb_action_t *)shift; 343 return (mb_action_t *)shift;
345 } 344 }
345
346 #ifdef UNITTEST
347
348 #include <CUnit/Basic.h>
349
350 typedef struct _mb_dummy mb_dummy_t;
351
352 struct _mb_dummy {
353 mb_action_t action;
354 int id;
355 int *logidx;
356 int *logs;
357 };
358
359
360 static void mb_dummy_start(mb_action_t *act,
361 const mb_timeval_t *now,
362 const mb_timeval_t *playing_time,
363 redraw_man_t *rdman) {
364 mb_dummy_t *dummy = (mb_dummy_t *)act;
365
366 dummy->logs[(*dummy->logidx)++] = dummy->id;
367 }
368
369 static void mb_dummy_step(mb_action_t *act,
370 const mb_timeval_t *now,
371 redraw_man_t *rdman) {
372 mb_dummy_t *dummy = (mb_dummy_t *)act;
373
374 dummy->logs[(*dummy->logidx)++] = dummy->id;
375 }
376
377 static void mb_dummy_stop(mb_action_t *act,
378 const mb_timeval_t *now,
379 redraw_man_t *rdman) {
380 mb_dummy_t *dummy = (mb_dummy_t *)act;
381
382 dummy->logs[(*dummy->logidx)++] = dummy->id;
383 }
384
385 static void mb_dummy_free(mb_action_t *act) {
386 free(act);
387 }
388
389 mb_action_t *mb_dummy_new(int id, int *logidx, int *logs, mb_word_t *word) {
390 mb_dummy_t *dummy;
391
392 dummy = (mb_dummy_t *)malloc(sizeof(mb_dummy_t));
393 if(dummy == NULL)
394 return NULL;
395
396 dummy->id = id;
397 dummy->logidx = logidx;
398 dummy->logs = logs;
399
400 dummy->action.start = mb_dummy_start;
401 dummy->action.step = mb_dummy_step;
402 dummy->action.stop = mb_dummy_stop;
403 dummy->action.free = mb_dummy_free;
404
405 mb_word_add_action(word, (mb_action_t *)dummy);
406
407 return (mb_action_t *)dummy;
408 }
409
410 void test_animate_words(void) {
411 mb_progm_t *progm;
412 mb_word_t *word;
413 mb_action_t *acts[4];
414 mb_tman_t *tman;
415 mb_timeval_t tv1, tv2, now, tmo_after;
416 int logcnt = 0;
417 int logs[256];
418 int r;
419
420 tman = mb_tman_new();
421 CU_ASSERT(tman != NULL);
422
423 progm = mb_progm_new(3, NULL);
424 CU_ASSERT(progm != NULL);
425
426 MB_TIMEVAL_SET(&tv1, 1, 0);
427 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3);
428 word = mb_progm_next_word(progm, &tv1, &tv2);
429 CU_ASSERT(word != NULL);
430 acts[0] = mb_dummy_new(0, &logcnt, logs, word);
431 CU_ASSERT(acts[0] != NULL);
432
433 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 4 / 3);
434 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL / 3);
435 word = mb_progm_next_word(progm, &tv1, &tv2);
436 CU_ASSERT(word != NULL);
437 acts[1] = mb_dummy_new(1, &logcnt, logs, word);
438 CU_ASSERT(acts[1] != NULL);
439
440 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 2);
441 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3);
442 word = mb_progm_next_word(progm, &tv1, &tv2);
443 CU_ASSERT(word != NULL);
444 acts[2] = mb_dummy_new(2, &logcnt, logs, word);
445 CU_ASSERT(acts[2] != NULL);
446
447 MB_TIMEVAL_SET(&now, 0, 0);
448 mb_progm_start(progm, tman, &now);
449
450 r = mb_tman_next_timeout(tman, &now, &tmo_after);
451 CU_ASSERT(r == 0);
452 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 1 &&
453 MB_TIMEVAL_USEC(&tmo_after) == 0);
454
455 /* 1.0s */
456 MB_TIMEVAL_ADD(&now, &tmo_after);
457 mb_tman_handle_timeout(tman, &now);
458 CU_ASSERT(logcnt == 1);
459
460 r = mb_tman_next_timeout(tman, &now, &tmo_after);
461 CU_ASSERT(r == 0);
462 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
463 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
464
465 /* 1.1s */
466 MB_TIMEVAL_ADD(&now, &tmo_after);
467 mb_tman_handle_timeout(tman, &now);
468 CU_ASSERT(logcnt == 4);
469
470 r = mb_tman_next_timeout(tman, &now, &tmo_after);
471 CU_ASSERT(r == 0);
472 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
473 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
474
475 /* 1.2s */
476 MB_TIMEVAL_ADD(&now, &tmo_after);
477 mb_tman_handle_timeout(tman, &now);
478 CU_ASSERT(logcnt == 6);
479
480 r = mb_tman_next_timeout(tman, &now, &tmo_after);
481 CU_ASSERT(r == 0);
482 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
483 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
484
485 /* 1.3s */
486 MB_TIMEVAL_ADD(&now, &tmo_after);
487 mb_tman_handle_timeout(tman, &now);
488 CU_ASSERT(logcnt == 8);
489
490 r = mb_tman_next_timeout(tman, &now, &tmo_after);
491 CU_ASSERT(r == 0);
492 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
493 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
494
495 /* 1.4s */
496 MB_TIMEVAL_ADD(&now, &tmo_after);
497 mb_tman_handle_timeout(tman, &now);
498 CU_ASSERT(logcnt == 9);
499
500 r = mb_tman_next_timeout(tman, &now, &tmo_after);
501 CU_ASSERT(r == 0);
502 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
503 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
504
505 /* 1.5s */
506 MB_TIMEVAL_ADD(&now, &tmo_after);
507 mb_tman_handle_timeout(tman, &now);
508 CU_ASSERT(logcnt == 10);
509
510 r = mb_tman_next_timeout(tman, &now, &tmo_after);
511 CU_ASSERT(r == -1);
512
513 mb_progm_free(progm);
514 mb_tman_free(tman);
515 }
516
517 CU_pSuite get_animate_suite(void) {
518 CU_pSuite suite;
519
520 suite = CU_add_suite("Suite_animate", NULL, NULL);
521 if(suite == NULL)
522 return NULL;
523
524 CU_ADD_TEST(suite, test_animate_words);
525
526 return suite;
527 }
528
529 #endif /* UNITTEST */