Improve tests
[raycastlib.git] / programs / test.c
blobc60f8e86b8abea6fa260e1c5192d0f6bc8369b94
1 /**
2 Tests for raycastlib.
4 license: CC0
5 */
7 #define RCL_PROFILE
8 #define RCL_PIXEL_FUNCTION pixelFunc
10 #include <stdio.h>
11 #include "../raycastlib.h"
12 #include <sys/time.h>
14 static const char renderSimpleExpect[] =
15 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
16 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
17 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,DD,,,,,,,,,"
18 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,EEEEEDD,,,,,,,,,"
19 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,EEEEEEEEDD,,,,,,,,,"
20 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,EEEEEEEEEEEEEDD,,,,,,,,,"
21 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,FFEEEEEEEEEEEEEEEDD,,,,,,,,,"
22 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,FFFFFFEEEEEEEEEEEEEEEDD,,,,,,,,,"
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eeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiii,FFFFFFEEEEEEEEEEEEEEEDD,,,,,,,,,"
34 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDD,,,,,,,,,"
35 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDD,,,,,,,,,"
36 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
37 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
38 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
39 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
40 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
41 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
42 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
43 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhi.....FFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
44 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhh..........FFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
45 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggg...............FFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
46 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffg.....................FFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
47 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffff..........................FFFFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
48 "dddddddddddddddeeeeeeeeeeeeeeeeeeffff.................................FFFFEEEEEEEEEEEEEEEDDDDDDDDDDD"
49 "dddddddddddddddeeeeeeeeeeeeeeee............................................EEEEEEEEEEEEEEDDDDDDDDDDD"
50 "dddddddddddddddeeeeeeeeeee.....................................................EEEEEEEEEEDDDDDDDDDDD"
51 "dddddddddddddddeeeeee..............................................................EEEEEEDDDDDDDDDDD"
52 "ddddddddddddddde.......................................................................EEDDDDDDDDDDD"
53 "ddddddddddd.................................................................................DDDDDDDD"
54 "ddddddd........................................................................................DDDDD";
56 static const char renderComplexExpect[] =
57 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
58 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,DD,,,,,,,,,"
59 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,EEEEEDDe,,,,,,,,"
60 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,EEEEEEEEDDee,,,,,,,"
61 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,EEEEEEEEEEEEEDDe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EEDDeeeef,,,,"
72 "dddddddddddddddeeeeeeeeeeeeeeeeeeffff,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,FFFFFFEEEEEEEEEEEEEEEDDeeeef,,,,"
73 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffgggggg,,,,,,,,,,,,,,,,FFFFFFEEEEEEEEEEEEEEEDDeeeef,GGG"
74 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiii,FFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
75 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
76 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
77 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
78 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
79 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
80 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
81 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
82 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
83 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
84 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhiiiiiiFFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
85 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhhhhhhi.....FFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
86 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffggggggggghhh..........FFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
87 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffgggggg................FFFFFFEEEEEEEEEEEEEEEDDeeeefGGGG"
88 "dddddddddddddddeeeeeeeeeeeeeeeeeefffffffffffffg.....................FFFFFFEEEEEEEEEEEEEEEDDeeeef...."
89 "dddddddddddddddeeeeeeeeeeeeeeeeeeffffffff...........................FFFFFFEEEEEEEEEEEEEEEDDeeeef...."
90 "dddddddddddddddeeeeeeeeeeeeeeeeeefff...................................FFFEEEEEEEEEEEEEEEDDeeeef...."
91 "dddddddddddddddeeeeeeeeeeeeeeee............................................EEEEEEEEEEEEEEDDeeeefFFFE"
92 "dddddddddddddddeeeeeeeeeee.....................................................EEEEEEEEEEDDeeeefFFFE"
93 "dddddddddddddddeeeee................................................................EEEEEDDeeee....E"
94 "ddddddddddddddd..........................................................................DDeee......"
95 "ddddddddddd.................................................................................e......."
96 "dddd................................................................................................";
98 RCL_Unit testArrayFunc(int16_t x, int16_t y)
100 if (x == 1 & y == 8)
101 return RCL_UNITS_PER_SQUARE * 3;
103 if (x == 2 & y == 8)
104 return -1 * RCL_UNITS_PER_SQUARE / 4;
106 return (x < 0 || x >= 10 || y < 0 || y >= 10) ?
107 (RCL_UNITS_PER_SQUARE * 2) : 0;
110 int testSingleRay(RCL_Unit startX, RCL_Unit startY, RCL_Unit dirX,
111 RCL_Unit dirY, int16_t expectSquareX, int16_t expectSquareY,
112 int16_t expectPointX, int16_t expectPointY, int16_t tolerateError)
114 RCL_Ray r;
116 r.start.x = startX;
117 r.start.y = startY;
118 r.direction.x = dirX;
119 r.direction.y = dirY;
121 printf("- casting ray:\n");
122 RCL_logRay(r);
124 RCL_HitResult h = RCL_castRay(r,testArrayFunc);
126 printf("- result:\n");
127 RCL_logHitResult(h);
129 int result =
130 h.square.x == expectSquareX &&
131 h.square.y == expectSquareY &&
132 h.position.x <= expectPointX + tolerateError &&
133 h.position.x >= expectPointX - tolerateError &&
134 h.position.y <= expectPointY + tolerateError &&
135 h.position.y >= expectPointY - tolerateError;
137 if (result)
138 printf("\nOK\n\n");
139 else
140 printf("\nFAIL\n\n");
142 return result;
145 int testSingleMapping(RCL_Unit posX, RCL_Unit posY, RCL_Unit posZ, uint32_t resX,
146 uint32_t resY, RCL_Unit camX, RCL_Unit camY, RCL_Unit camZ, RCL_Unit camDir,
147 RCL_Unit expectX, RCL_Unit expectY, RCL_Unit expectZ)
149 int result;
151 RCL_Camera c;
153 RCL_initCamera(&c);
155 c.resolution.x = resX;
156 c.resolution.y = resY;
157 c.position.x = camX;
158 c.position.y = camY;
159 c.direction = camDir;
160 c.height = camZ;
162 RCL_Vector2D pos;
163 RCL_Unit height;
165 pos.x = posX;
166 pos.y = posY;
167 height = posZ;
169 RCL_PixelInfo p;
171 printf("- mapping pixel: %d %d %d\n",posX,posY,posZ);
173 p = RCL_mapToScreen(pos,height,c);
175 printf("- result:\n");
176 RCL_logPixelInfo(p);
178 result = p.position.x == expectX && p.position.y == expectY &&
179 p.depth == expectZ;
181 if (result)
182 printf("\nOK\n\n");
183 else
184 printf("\nFAIL\n\n");
186 return result;
189 // returns milliseconds
190 long measureTime(void (*func)(void))
192 long start, end;
193 struct timeval timecheck;
195 gettimeofday(&timecheck, NULL);
196 start = (long) timecheck.tv_sec * 1000 + (long) timecheck.tv_usec / 1000;
198 func();
200 gettimeofday(&timecheck, NULL);
201 end = (long) timecheck.tv_sec * 1000 + (long) timecheck.tv_usec / 1000;
203 return end - start;
206 void benchCastRays()
208 RCL_Ray r;
210 r.start.x = RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2;
211 r.start.y = 2 * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 4;
213 RCL_Vector2D directions[8];
215 for (int i = 0; i < 8; ++i)
216 directions[i] = RCL_angleToDirection(RCL_UNITS_PER_SQUARE / 8 * i);
218 for (int i = 0; i < 1000000; ++i)
220 r.direction = directions[i % 8];
221 RCL_castRay(r,testArrayFunc);
225 void benchmarkMapping()
227 RCL_Camera c;
229 c.resolution.x = 1024;
230 c.resolution.y = 768;
231 c.position.x = RCL_UNITS_PER_SQUARE / 2;
232 c.position.y = RCL_UNITS_PER_SQUARE * 2;
233 c.direction = RCL_UNITS_PER_SQUARE / 8;
234 c.height = 0;
236 RCL_PixelInfo p;
238 RCL_Vector2D pos;
239 RCL_Unit height;
241 pos.x = -1024 * RCL_UNITS_PER_SQUARE;
242 pos.y = -512 * RCL_UNITS_PER_SQUARE;
243 height = 0;
245 for (int i = 0; i < 1000000; ++i)
247 p = RCL_mapToScreen(pos,height,c);
249 pos.x += 4;
250 pos.y += 8;
251 height = (height + 16) % 1024;
255 int renderMode = 0;
256 uint32_t *pixelCounts = 0;
257 RCL_Camera countCamera;
258 int countOK = 1;
260 #define TEST_SCREEN_RES_X 100
261 #define TEST_SCREEN_RES_Y 40
263 char testScreen[TEST_SCREEN_RES_X * TEST_SCREEN_RES_Y];
265 void pixelFunc(RCL_PixelInfo *p)
267 if (renderMode == 0) // count pixels
269 if (p->position.x >= countCamera.resolution.x || p->position.x < 0 ||
270 p->position.y >= countCamera.resolution.y || p->position.y < 0)
272 printf("ERROR: writing pixel outside screen at %d %d!\n",
273 p->position.x,p->position.y);
275 countOK = 0;
277 else
278 pixelCounts[p->position.y * countCamera.resolution.x + p->position.x]++;
280 else if (renderMode == 1)
282 char c = '?';
284 if (p->isFloor)
285 c = '.';
286 else
287 c = ',';
289 if (p->isWall)
290 c = (p->hit.direction % 2 ? 'a' : 'A') + p->depth / 512;
292 testScreen[p->position.y * TEST_SCREEN_RES_X + p->position.x] = c;
296 int testPixelCount(RCL_Unit camX, RCL_Unit camY, RCL_Unit camZ,
297 RCL_Unit camDir, RCL_Unit camShear, uint16_t camResX, uint16_t camResY,
298 int complexRender)
300 printf("Counting rendered pixels (each should be rendered exactly once)...\n");
302 RCL_RayConstraints constraints;
303 RCL_Camera c;
305 RCL_initRayConstraints(&constraints);
306 constraints.maxSteps = 32;
307 RCL_initCamera(&c);
309 c.position.x = camX;
310 c.position.y = camY;
311 c.direction = camDir;
312 c.shear = camShear;
313 c.height = camZ;
314 c.resolution.x = camResX;
315 c.resolution.y = camResY;
317 uint32_t pixels[camResX * camResY];
319 for (int32_t i = 0; i < camResX * camResY; ++i)
320 pixels[i] = 0;
322 pixelCounts = pixels;
323 countCamera = c;
324 renderMode = 0;
326 countOK = 1;
328 if (complexRender)
329 RCL_renderComplex(c,testArrayFunc,testArrayFunc,0,constraints);
330 else
331 RCL_renderSimple(c,testArrayFunc,0,0,constraints);
333 for (uint32_t y = 0; y < camResY; ++y)
334 for (uint32_t x = 0; x < camResX; ++x)
336 uint32_t index = y * camResX + x;
338 if (pixels[index] != 1)
340 printf("ERROR: pixel at %d %d written %d times!\n",x,y,pixels[index]);
341 countOK = 0;
345 return countOK;
348 int testRender(int8_t simple)
350 printf("\nTesting rendering\n");
352 renderMode = 1;
354 RCL_Camera c;
355 RCL_initCamera(&c);
357 c.resolution.x = TEST_SCREEN_RES_X;
358 c.resolution.y = TEST_SCREEN_RES_Y;
359 c.position.x = 2 * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2; // 3000;//5 * RCL_UNITS_PER_SQUARE;
360 c.position.y = 5 * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2; //5000;//5 * RCL_UNITS_PER_SQUARE;
361 c.direction = 625;//700;
362 c.height = 1500;//1000;
364 RCL_RayConstraints constraints;
365 RCL_initRayConstraints(&constraints);
367 constraints.maxHits = 7;
368 constraints.maxSteps = 20;
370 renderMode = 1;
372 for (uint32_t i = 0; i < TEST_SCREEN_RES_X * TEST_SCREEN_RES_Y; ++i)
373 testScreen[i] = '?';
375 if (simple)
377 RCL_renderSimple(c,testArrayFunc,0,0,constraints);
379 else
381 RCL_renderComplex(c,testArrayFunc,0,0,constraints);
384 for (uint32_t i = 0; i < TEST_SCREEN_RES_X * TEST_SCREEN_RES_Y; ++i)
386 if ((i % TEST_SCREEN_RES_X) == 0)
387 printf(" \n");
389 printf("%c",testScreen[i]);
392 const char *expect = simple ? renderSimpleExpect : renderComplexExpect;
394 for (uint32_t i = 0; i < TEST_SCREEN_RES_X * TEST_SCREEN_RES_Y; ++i)
395 if (expect[i] != testScreen[i])
397 printf("\n\nFAIL!\n");
398 return 0;
401 printf("\n\nOK\n");
403 return 1;
406 void benchmarkRender()
408 RCL_Camera c;
409 RCL_initCamera(&c);
411 c.resolution.x = 640;
412 c.resolution.y = 300;
413 c.position.x = 10;
414 c.position.y = 12;
415 c.direction = 100;
416 c.height = 200;
418 RCL_RayConstraints constraints;
419 RCL_initRayConstraints(&constraints);
421 constraints.maxHits = 10;
422 constraints.maxSteps = 12;
424 renderMode = 255; // don't write pixels
426 for (int i = 0; i < 100; ++i)
427 RCL_renderComplex(c,testArrayFunc,testArrayFunc,0,constraints);
430 int main()
432 printf("Testing raycastlib.\n");
434 if (!testSingleRay(
435 3 * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2,
436 4 * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2,
437 100, 50,
438 10, 7,
439 10240, 7936,
440 16))
441 return 1;
443 if (!testSingleRay(
446 100, 100,
447 9, 10,
448 10240, 10240,
449 16))
450 return 1;
452 if (!testSingleRay(
453 400,
454 6811,
455 -629,805,
456 -1, 7,
457 -1, 7325,
458 16))
459 return 1;
461 if (!testSingleRay(
462 -4 * RCL_UNITS_PER_SQUARE - RCL_UNITS_PER_SQUARE / 2,
463 7 * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 3,
464 100,-100,
465 0, 2,
466 1, 2900,
467 16))
468 return 1;
470 printf("testing perspective scale...\n");
472 for (RCL_Unit i = 1; i < 100; ++i)
474 RCL_Unit size = i * 3;
475 RCL_Unit distance = i * 6 + 200;
477 RCL_Unit scaled = RCL_perspectiveScaleHorizontal(size,distance);
478 RCL_Unit distance2 = RCL_perspectiveScaleHorizontalInverse(size,scaled);
480 if (RCL_absVal(distance - distance2 > 2))
481 printf("ERROR: distance: %d, distance inverse: %d\n",distance,distance2);
484 printf("OK\n");
486 if (!testPixelCount(
487 RCL_UNITS_PER_SQUARE / 2,
488 RCL_UNITS_PER_SQUARE / 2,
489 RCL_UNITS_PER_SQUARE / 2,
492 128,
495 return 1;
497 if (!testPixelCount(
498 3 * RCL_UNITS_PER_SQUARE + 100,
499 4 * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 3,
500 RCL_UNITS_PER_SQUARE / 2,
501 512,
503 120,
506 return 1;
508 if (!testPixelCount(
509 - RCL_UNITS_PER_SQUARE,
511 300,
512 -600,
513 -120,
517 return 1;
519 printf("OK\n");
521 if (!testSingleMapping(
522 RCL_UNITS_PER_SQUARE,
523 RCL_UNITS_PER_SQUARE * 2,
525 800,
526 640,
527 RCL_UNITS_PER_SQUARE / 2,
529 RCL_UNITS_PER_SQUARE / 4,
530 (RCL_UNITS_PER_SQUARE * 5) / 6,
531 287,
532 370,
533 2027
535 return 1;
537 if (!testRender(0))
538 return 1;
540 if (!testRender(1))
541 return 1;
543 printf("benchmark:\n");
545 long t;
546 t = measureTime(benchCastRays);
547 printf("cast 1000000 rays: %ld ms\n",t);
549 t = measureTime(benchmarkMapping);
550 printf("map point to screen 1000000 times: %ld ms\n",t);
552 t = measureTime(benchmarkRender);
553 printf("render 100 times: %ld ms\n",t);
555 printf("\n");
556 printProfile();
558 printf("\n===== all OK =====\n");
560 return 0;