Mercurial > MadButterfly
annotate 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 |
rev | line source |
---|---|
41 | 1 /*! \brief Animation tools. |
2 * | |
3 * XXX: Program is a sequence of actions duration a perior. | |
4 * Actions are grouped into words. A program defines | |
5 * the order and time of playing of words. A word | |
6 * defines how long to perform a set of actions. Actions | |
7 * in a word are performed concurrently. | |
45 | 8 * |
9 * Animation shapes are updated periodically. Every action | |
10 * are working with start, step, and stop 3 calls. start is | |
11 * called when start time the word, contain it, due. step is | |
12 * called periodically in duration of playing time start at | |
13 * 'start time'. When the clock run out the playing time of | |
14 * a word, it call stop of actions in the word. | |
41 | 15 */ |
16 #include <stdio.h> | |
17 #include <stdlib.h> | |
18 #include <string.h> | |
19 #include "mb_types.h" | |
20 #include "redraw_man.h" | |
21 #include "mb_timer.h" | |
22 #include "animate.h" | |
23 | |
24 | |
42 | 25 #define STEP_INTERVAL 100000 |
41 | 26 #define ASSERT(x) |
27 | |
28 /*! \brief A word is a set of concurrent actions in a program. | |
29 */ | |
30 struct _mb_word { | |
31 mb_timeval_t start_time; /*!< time to start the word */ | |
32 mb_timeval_t playing_time; /*!< time duration of playing */ | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
33 mb_timeval_t abs_start, abs_stop; |
41 | 34 STAILQ(mb_action_t) actions; |
35 }; | |
36 | |
37 /*! \brief A program describe a series of actions to animate shapes. | |
38 */ | |
39 struct _mb_progm { | |
40 redraw_man_t *rdman; | |
41 | |
46 | 42 mb_timeval_t start_time; |
41 | 43 int first_playing; /*!< first playing word. */ |
44 mb_tman_t *tman; | |
45 | |
46 int n_words; | |
47 int max_words; | |
48 mb_word_t words[1]; | |
49 }; | |
50 | |
51 /*! \brief Basic class of nnimation actions. | |
52 */ | |
53 struct _mb_action { | |
54 int act_type; | |
55 void (*start)(mb_action_t *act, | |
56 const mb_timeval_t *now, | |
57 const mb_timeval_t *playing_time, | |
58 redraw_man_t *rdman); | |
42 | 59 void (*step)(mb_action_t *act, const mb_timeval_t *now, |
60 redraw_man_t *rdman); | |
61 void (*stop)(mb_action_t *act, const mb_timeval_t *now, | |
62 redraw_man_t *rdman); | |
41 | 63 void (*free)(mb_action_t *act); |
64 mb_action_t *next; | |
65 }; | |
66 | |
67 mb_progm_t *mb_progm_new(int max_words, redraw_man_t *rdman) { | |
68 mb_progm_t *progm; | |
69 int i; | |
70 | |
71 progm = (mb_progm_t *)malloc(sizeof(mb_progm_t) + | |
72 (sizeof(mb_word_t) * (max_words - 1))); | |
73 if(progm == NULL) | |
74 return NULL; | |
75 | |
76 progm->rdman = rdman; | |
77 progm->n_words = 0; | |
78 progm->max_words = max_words; | |
79 for(i = 0; i < max_words; i++) | |
80 STAILQ_INIT(progm->words[i].actions); | |
81 return progm; | |
82 } | |
83 | |
84 void mb_progm_free(mb_progm_t *progm) { | |
85 int n_words; | |
86 mb_word_t *word; | |
87 mb_action_t *cur_act; | |
88 int i; | |
89 | |
90 n_words = progm->n_words; | |
91 for(i = 0; i < n_words; i++) { | |
92 word = progm->words + i; | |
93 for(cur_act = STAILQ_HEAD(word->actions); | |
94 cur_act != NULL; | |
95 cur_act = STAILQ_HEAD(word->actions)) { | |
96 STAILQ_REMOVE(word->actions, mb_action_t, next, cur_act); | |
97 cur_act->free(cur_act); | |
98 } | |
99 } | |
100 free(progm); | |
101 } | |
102 | |
103 /*! \brief Add a new word into a program. | |
104 * | |
105 * The start time of new word should bigger or equal to last one. | |
106 * The time should be specified in incremental order. | |
107 */ | |
108 mb_word_t *mb_progm_next_word(mb_progm_t *progm, | |
109 const mb_timeval_t *start, | |
110 const mb_timeval_t *playing) { | |
111 mb_word_t *word; | |
112 if(progm->n_words >= progm->max_words) | |
113 return NULL; | |
114 if(progm->n_words && | |
115 MB_TIMEVAL_LATER(&progm->words[progm->n_words - 1].start_time, start)) | |
116 return NULL; | |
117 word = progm->words + progm->n_words++; | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
118 MB_TIMEVAL_CP(&word->start_time, start); |
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
119 MB_TIMEVAL_CP(&word->playing_time, playing); |
41 | 120 return word; |
121 } | |
122 | |
47
f3818d996f4f
change interface of creating a animation action
Thinker K.F. Li <thinker@branda.to>
parents:
46
diff
changeset
|
123 static void mb_word_add_action(mb_word_t *word, mb_action_t *act) { |
41 | 124 STAILQ_INS_TAIL(word->actions, mb_action_t, next, act); |
125 } | |
126 | |
127 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo, | |
128 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 129 mb_action_t *act; |
130 | |
131 for(act = STAILQ_HEAD(word->actions); | |
132 act != NULL; | |
133 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
134 act->start(act, tmo, &word->playing_time, rdman); | |
135 } | |
41 | 136 } |
137 | |
138 static void mb_word_step(mb_word_t *word, const mb_timeval_t *tmo, | |
139 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 140 mb_action_t *act; |
141 | |
142 for(act = STAILQ_HEAD(word->actions); | |
143 act != NULL; | |
144 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
145 act->step(act, tmo, rdman); | |
146 } | |
41 | 147 } |
148 | |
149 static void mb_word_stop(mb_word_t *word, const mb_timeval_t *tmo, | |
150 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 151 mb_action_t *act; |
152 | |
153 for(act = STAILQ_HEAD(word->actions); | |
154 act != NULL; | |
155 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
156 act->stop(act, tmo, rdman); | |
157 } | |
41 | 158 } |
159 | |
46 | 160 /* |
161 * Any two actions in concurrent words never change the same attribute. | |
162 * Start time of words in a program are specified in incremental order. | |
163 */ | |
41 | 164 static void mb_progm_step(const mb_timeval_t *tmo, |
46 | 165 const mb_timeval_t *now, |
166 void *arg) { | |
41 | 167 mb_progm_t *progm = (mb_progm_t *)arg; |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
168 mb_timeval_t next_tmo; |
41 | 169 mb_word_t *word; |
170 mb_timer_t *timer; | |
171 int i; | |
172 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
173 MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL); |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
174 MB_TIMEVAL_ADD(&next_tmo, tmo); |
41 | 175 |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
176 /* _step() or _stop() words that in playing. */ |
41 | 177 i = progm->first_playing; |
178 for(word = progm->words + i; | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
179 i < progm->n_words && MB_TIMEVAL_LATER(tmo, &word->abs_start); |
41 | 180 word = progm->words + ++i) { |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
181 if(MB_TIMEVAL_LATER(tmo, &word->abs_stop)) |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
182 continue; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
183 if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop)) |
46 | 184 mb_word_stop(word, tmo, now, progm->rdman); |
185 else | |
41 | 186 mb_word_step(word, tmo, now, progm->rdman); |
187 } | |
188 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
189 /* Start words that their abs_start is in duration |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
190 * from now to timeout for next update. |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
191 */ |
41 | 192 for(word = progm->words + i; |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
193 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_start); |
41 | 194 word = progm->words + ++i) { |
195 mb_word_start(word, tmo, now, progm->rdman); | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
196 if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop)) |
46 | 197 mb_word_stop(word, tmo, now, progm->rdman); |
41 | 198 } |
199 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
200 /* Find a new first_playing if any consequence words, following current |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
201 * first_playing word, are stoped. |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
202 */ |
46 | 203 i = progm->first_playing; |
204 for(word = progm->words + i; | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
205 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop); |
46 | 206 word = progm->words + ++i) { |
207 progm->first_playing++; | |
208 } | |
209 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
210 /* Setup timeout for next update. */ |
41 | 211 if(progm->first_playing < progm->n_words) { |
42 | 212 word = progm->words + progm->first_playing; |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
213 if(MB_TIMEVAL_LATER(&word->abs_start, &next_tmo)) |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
214 MB_TIMEVAL_CP(&next_tmo, &word->abs_start); |
46 | 215 timer = mb_tman_timeout(progm->tman, &next_tmo, |
216 mb_progm_step, progm); | |
41 | 217 } |
218 } | |
219 | |
220 void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman, | |
221 mb_timeval_t *now) { | |
222 mb_timer_t *timer; | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
223 mb_word_t *word; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
224 int i; |
41 | 225 |
226 if(progm->n_words == 0) | |
227 return; | |
228 | |
229 progm->tman = tman; | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
230 MB_TIMEVAL_CP(&progm->start_time, now); |
41 | 231 progm->first_playing = 0; |
232 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
233 for(i = 0; i < progm->n_words; i++) { |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
234 word = progm->words + i; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
235 MB_TIMEVAL_CP(&word->abs_start, &word->start_time); |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
236 MB_TIMEVAL_ADD(&word->abs_start, now); |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
237 MB_TIMEVAL_CP(&word->abs_stop, &word->abs_start); |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
238 MB_TIMEVAL_ADD(&word->abs_stop, &word->playing_time); |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
239 } |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
240 |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
241 if(MB_TIMEVAL_EQ(&progm->words[0].abs_start, now)) { |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
242 mb_progm_step(now, now, progm); |
41 | 243 return; |
244 } | |
245 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
246 timer = mb_tman_timeout(tman, &progm->words[0].abs_start, |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
247 mb_progm_step, progm); |
41 | 248 ASSERT(timer != NULL); |
249 } | |
250 | |
42 | 251 void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) { |
252 } | |
253 | |
41 | 254 typedef struct _mb_shift mb_shift_t; |
255 /*! \brief Animation action for shift a coordination. */ | |
256 struct _mb_shift { | |
257 mb_action_t action; | |
258 | |
259 co_aix x, y; | |
260 coord_t *coord; | |
261 | |
262 mb_timeval_t start_time; | |
263 co_aix saved_matrix[6]; | |
264 const mb_timeval_t *playing_time; | |
265 }; | |
266 | |
267 static float comp_mb_timeval_ratio(mb_timeval_t *a, const mb_timeval_t *b) { | |
268 float ratio; | |
269 | |
270 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a); | |
271 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b); | |
272 return ratio; | |
273 } | |
274 | |
275 static void mb_shift_start(mb_action_t *act, | |
276 const mb_timeval_t *now, | |
277 const mb_timeval_t *playing_time, | |
278 redraw_man_t *rdman) { | |
279 mb_shift_t *shift = (mb_shift_t *)act; | |
280 coord_t *coord; | |
281 | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
282 MB_TIMEVAL_CP(&shift->start_time, now); |
41 | 283 coord = shift->coord; |
284 memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6])); | |
285 shift->playing_time = playing_time; | |
286 } | |
287 | |
42 | 288 static void mb_shift_step(mb_action_t *act, const mb_timeval_t *now, |
41 | 289 redraw_man_t *rdman) { |
290 mb_shift_t *shift = (mb_shift_t *)act; | |
291 mb_timeval_t diff; | |
292 coord_t *coord; | |
293 float ratio; | |
294 | |
295 | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
296 MB_TIMEVAL_CP(&diff, now); |
41 | 297 MB_TIMEVAL_DIFF(&diff, &shift->start_time); |
298 ratio = comp_mb_timeval_ratio(&diff, shift->playing_time); | |
299 | |
300 coord = shift->coord; | |
301 coord->matrix[2] = shift->saved_matrix[2] + shift->x * ratio; | |
302 coord->matrix[5] = shift->saved_matrix[5] + shift->y * ratio; | |
303 | |
304 rdman_coord_changed(rdman, coord); | |
305 } | |
306 | |
42 | 307 static void mb_shift_stop(mb_action_t *act, const mb_timeval_t *now, |
41 | 308 redraw_man_t *rdman) { |
309 mb_shift_t *shift = (mb_shift_t *)act; | |
310 coord_t *coord; | |
311 | |
312 coord = shift->coord; | |
313 coord->matrix[2] = shift->saved_matrix[2] + shift->x; | |
314 coord->matrix[5] = shift->saved_matrix[5] + shift->y; | |
315 | |
316 rdman_coord_changed(rdman, coord); | |
317 } | |
318 | |
319 | |
320 static void mb_shift_free(mb_action_t *act) { | |
321 free(act); | |
322 } | |
323 | |
47
f3818d996f4f
change interface of creating a animation action
Thinker K.F. Li <thinker@branda.to>
parents:
46
diff
changeset
|
324 mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord, |
f3818d996f4f
change interface of creating a animation action
Thinker K.F. Li <thinker@branda.to>
parents:
46
diff
changeset
|
325 mb_word_t *word) { |
41 | 326 mb_shift_t *shift; |
327 | |
328 shift = (mb_shift_t *)malloc(sizeof(mb_shift_t)); | |
329 if(shift == NULL) | |
330 return (mb_action_t *)shift; | |
331 | |
332 shift->x = x; | |
333 shift->y = y; | |
42 | 334 shift->coord = coord; |
335 | |
41 | 336 shift->action.start = mb_shift_start; |
337 shift->action.step = mb_shift_step; | |
338 shift->action.stop = mb_shift_stop; | |
339 shift->action.free = mb_shift_free; | |
340 | |
47
f3818d996f4f
change interface of creating a animation action
Thinker K.F. Li <thinker@branda.to>
parents:
46
diff
changeset
|
341 mb_word_add_action(word, (mb_action_t *)shift); |
f3818d996f4f
change interface of creating a animation action
Thinker K.F. Li <thinker@branda.to>
parents:
46
diff
changeset
|
342 |
41 | 343 return (mb_action_t *)shift; |
344 } | |
50 | 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 */ |