Debian Codegraph @

Tests/Recast/Tests_Recast.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 #include <stdio.h>
2 #include <string.h>
3
4 #include "catch2/catch_all.hpp"
5
6 #include "Recast.h"
7 #include "RecastAlloc.h"
8 #include "RecastAssert.h"
9
10 // For comparing to rcVector in benchmarks.
11 #include <vector>
12
13 TEST_CASE("rcSwap")
14 {
15 SECTION("Swap two values")
16 {
17 int one = 1;
18 int two = 2;
19 rcSwap(one, two);
20 REQUIRE(one == 2);
21 REQUIRE(two == 1);
22 }
23 }
24
25 TEST_CASE("rcMin")
26 {
27 SECTION("Min returns the lowest value.")
28 {
29 REQUIRE(rcMin(1, 2) == 1);
30 REQUIRE(rcMin(2, 1) == 1);
31 }
32
33 SECTION("Min with equal args")
34 {
35 REQUIRE(rcMin(1, 1) == 1);
36 }
37 }
38
39 TEST_CASE("rcMax")
40 {
41 SECTION("Max returns the greatest value.")
42 {
43 REQUIRE(rcMax(1, 2) == 2);
44 REQUIRE(rcMax(2, 1) == 2);
45 }
46
47 SECTION("Max with equal args")
48 {
49 REQUIRE(rcMax(1, 1) == 1);
50 }
51 }
52
53 TEST_CASE("rcAbs")
54 {
55 SECTION("Abs returns the absolute value.")
56 {
57 REQUIRE(rcAbs(-1) == 1);
58 REQUIRE(rcAbs(1) == 1);
59 REQUIRE(rcAbs(0) == 0);
60 }
61 }
62
63 TEST_CASE("rcSqr")
64 {
65 SECTION("Sqr squares a number")
66 {
67 REQUIRE(rcSqr(2) == 4);
68 REQUIRE(rcSqr(-4) == 16);
69 REQUIRE(rcSqr(0) == 0);
70 }
71 }
72
73 TEST_CASE("rcClamp")
74 {
75 SECTION("Higher than range")
76 {
77 REQUIRE(rcClamp(2, 0, 1) == 1);
78 }
79
80 SECTION("Within range")
81 {
82 REQUIRE(rcClamp(1, 0, 2) == 1);
83 }
84
85 SECTION("Lower than range")
86 {
87 REQUIRE(rcClamp(0, 1, 2) == 1);
88 }
89 }
90
91 TEST_CASE("rcSqrt")
92 {
93 SECTION("Sqrt gets the sqrt of a number")
94 {
95 REQUIRE(rcSqrt(4) == Catch::Approx(2));
96 REQUIRE(rcSqrt(81) == Catch::Approx(9));
97 }
98 }
99
100 TEST_CASE("rcVcross")
101 {
102 SECTION("Computes cross product")
103 {
104 float v1[3] = {3, -3, 1};
105 float v2[3] = {4, 9, 2};
106 float result[3];
107 rcVcross(result, v1, v2);
108 REQUIRE(result[0] == Catch::Approx(-15));
109 REQUIRE(result[1] == Catch::Approx(-2));
110 REQUIRE(result[2] == Catch::Approx(39));
111 }
112
113 SECTION("Cross product with itself is zero")
114 {
115 float v1[3] = {3, -3, 1};
116 float result[3];
117 rcVcross(result, v1, v1);
118 REQUIRE(result[0] == Catch::Approx(0));
119 REQUIRE(result[1] == Catch::Approx(0));
120 REQUIRE(result[2] == Catch::Approx(0));
121 }
122 }
123
124 TEST_CASE("rcVdot")
125 {
126 SECTION("Dot normalized vector with itself")
127 {
128 float v1[] = { 1, 0, 0 };
129 float result = rcVdot(v1, v1);
130 REQUIRE(result == Catch::Approx(1));
131 }
132
133 SECTION("Dot zero vector with anything is zero")
134 {
135 float v1[] = { 1, 2, 3 };
136 float v2[] = { 0, 0, 0 };
137
138 float result = rcVdot(v1, v2);
139 REQUIRE(result == Catch::Approx(0));
140 }
141 }
142
143 TEST_CASE("rcVmad")
144 {
145 SECTION("scaled add two vectors")
146 {
147 float v1[3] = {1, 2, 3};
148 float v2[3] = {0, 2, 4};
149 float result[3];
150 rcVmad(result, v1, v2, 2);
151 REQUIRE(result[0] == Catch::Approx(1));
152 REQUIRE(result[1] == Catch::Approx(6));
153 REQUIRE(result[2] == Catch::Approx(11));
154 }
155
156 SECTION("second vector is scaled, first is not")
157 {
158 float v1[3] = {1, 2, 3};
159 float v2[3] = {5, 6, 7};
160 float result[3];
161 rcVmad(result, v1, v2, 0);
162 REQUIRE(result[0] == Catch::Approx(1));
163 REQUIRE(result[1] == Catch::Approx(2));
164 REQUIRE(result[2] == Catch::Approx(3));
165 }
166 }
167
168 TEST_CASE("rcVadd")
169 {
170 SECTION("add two vectors")
171 {
172 float v1[3] = {1, 2, 3};
173 float v2[3] = {5, 6, 7};
174 float result[3];
175 rcVadd(result, v1, v2);
176 REQUIRE(result[0] == Catch::Approx(6));
177 REQUIRE(result[1] == Catch::Approx(8));
178 REQUIRE(result[2] == Catch::Approx(10));
179 }
180 }
181
182 TEST_CASE("rcVsub")
183 {
184 SECTION("subtract two vectors")
185 {
186 float v1[3] = {5, 4, 3};
187 float v2[3] = {1, 2, 3};
188 float result[3];
189 rcVsub(result, v1, v2);
190 REQUIRE(result[0] == Catch::Approx(4));
191 REQUIRE(result[1] == Catch::Approx(2));
192 REQUIRE(result[2] == Catch::Approx(0));
193 }
194 }
195
196 TEST_CASE("rcVmin")
197 {
198 SECTION("selects the min component from the vectors")
199 {
200 float v1[3] = {5, 4, 0};
201 float v2[3] = {1, 2, 9};
202 rcVmin(v1, v2);
203 REQUIRE(v1[0] == Catch::Approx(1));
204 REQUIRE(v1[1] == Catch::Approx(2));
205 REQUIRE(v1[2] == Catch::Approx(0));
206 }
207
208 SECTION("v1 is min")
209 {
210 float v1[3] = {1, 2, 3};
211 float v2[3] = {4, 5, 6};
212 rcVmin(v1, v2);
213 REQUIRE(v1[0] == Catch::Approx(1));
214 REQUIRE(v1[1] == Catch::Approx(2));
215 REQUIRE(v1[2] == Catch::Approx(3));
216 }
217
218 SECTION("v2 is min")
219 {
220 float v1[3] = {4, 5, 6};
221 float v2[3] = {1, 2, 3};
222 rcVmin(v1, v2);
223 REQUIRE(v1[0] == Catch::Approx(1));
224 REQUIRE(v1[1] == Catch::Approx(2));
225 REQUIRE(v1[2] == Catch::Approx(3));
226 }
227 }
228
229 TEST_CASE("rcVmax")
230 {
231 SECTION("selects the max component from the vectors")
232 {
233 float v1[3] = {5, 4, 0};
234 float v2[3] = {1, 2, 9};
235 rcVmax(v1, v2);
236 REQUIRE(v1[0] == Catch::Approx(5));
237 REQUIRE(v1[1] == Catch::Approx(4));
238 REQUIRE(v1[2] == Catch::Approx(9));
239 }
240
241 SECTION("v2 is max")
242 {
243 float v1[3] = {1, 2, 3};
244 float v2[3] = {4, 5, 6};
245 rcVmax(v1, v2);
246 REQUIRE(v1[0] == Catch::Approx(4));
247 REQUIRE(v1[1] == Catch::Approx(5));
248 REQUIRE(v1[2] == Catch::Approx(6));
249 }
250
251 SECTION("v1 is max")
252 {
253 float v1[3] = {4, 5, 6};
254 float v2[3] = {1, 2, 3};
255 rcVmax(v1, v2);
256 REQUIRE(v1[0] == Catch::Approx(4));
257 REQUIRE(v1[1] == Catch::Approx(5));
258 REQUIRE(v1[2] == Catch::Approx(6));
259 }
260 }
261
262 TEST_CASE("rcVcopy")
263 {
264 SECTION("copies a vector into another vector")
265 {
266 float v1[3] = {5, 4, 0};
267 float result[3] = {1, 2, 9};
268 rcVcopy(result, v1);
269 REQUIRE(result[0] == Catch::Approx(5));
270 REQUIRE(result[1] == Catch::Approx(4));
271 REQUIRE(result[2] == Catch::Approx(0));
272 REQUIRE(v1[0] == Catch::Approx(5));
273 REQUIRE(v1[1] == Catch::Approx(4));
274 REQUIRE(v1[2] == Catch::Approx(0));
275 }
276 }
277
278 TEST_CASE("rcVdist")
279 {
280 SECTION("distance between two vectors")
281 {
282 float v1[3] = {3, 1, 3};
283 float v2[3] = {1, 3, 1};
284 float result = rcVdist(v1, v2);
285
286 REQUIRE(result == Catch::Approx(3.4641f));
287 }
288
289 SECTION("Distance from zero is magnitude")
290 {
291 float v1[3] = {3, 1, 3};
292 float v2[3] = {0, 0, 0};
293 float distance = rcVdist(v1, v2);
294 float magnitude = rcSqrt(rcSqr(v1[0]) + rcSqr(v1[1]) + rcSqr(v1[2]));
295 REQUIRE(distance == Catch::Approx(magnitude));
296 }
297 }
298
299 TEST_CASE("rcVdistSqr")
300 {
301 SECTION("squared distance between two vectors")
302 {
303 float v1[3] = {3, 1, 3};
304 float v2[3] = {1, 3, 1};
305 float result = rcVdistSqr(v1, v2);
306
307 REQUIRE(result == Catch::Approx(12));
308 }
309
310 SECTION("squared distance from zero is squared magnitude")
311 {
312 float v1[3] = {3, 1, 3};
313 float v2[3] = {0, 0, 0};
314 float distance = rcVdistSqr(v1, v2);
315 float magnitude = rcSqr(v1[0]) + rcSqr(v1[1]) + rcSqr(v1[2]);
316 REQUIRE(distance == Catch::Approx(magnitude));
317 }
318 }
319
320 TEST_CASE("rcVnormalize")
321 {
322 SECTION("normalizing reduces magnitude to 1")
323 {
324 float v[3] = {3, 3, 3};
325 rcVnormalize(v);
326 REQUIRE(v[0] == Catch::Approx(rcSqrt(1.0f / 3.0f)));
327 REQUIRE(v[1] == Catch::Approx(rcSqrt(1.0f / 3.0f)));
328 REQUIRE(v[2] == Catch::Approx(rcSqrt(1.0f / 3.0f)));
329 float magnitude = rcSqrt(rcSqr(v[0]) + rcSqr(v[1]) + rcSqr(v[2]));
330 REQUIRE(magnitude == Catch::Approx(1));
331 }
332 }
333
334 TEST_CASE("rcCalcBounds")
335 {
336 SECTION("bounds of one vector")
337 {
338 float verts[] = {1, 2, 3};
339 float bmin[3];
340 float bmax[3];
341 rcCalcBounds(verts, 1, bmin, bmax);
342
343 REQUIRE(bmin[0] == Catch::Approx(verts[0]));
344 REQUIRE(bmin[1] == Catch::Approx(verts[1]));
345 REQUIRE(bmin[2] == Catch::Approx(verts[2]));
346
347 REQUIRE(bmax[0] == Catch::Approx(verts[0]));
348 REQUIRE(bmax[1] == Catch::Approx(verts[1]));
349 REQUIRE(bmax[2] == Catch::Approx(verts[2]));
350 }
351
352 SECTION("bounds of more than one vector")
353 {
354 float verts[] = {
355 1, 2, 3,
356 0, 2, 5
357 };
358 float bmin[3];
359 float bmax[3];
360 rcCalcBounds(verts, 2, bmin, bmax);
361
362 REQUIRE(bmin[0] == Catch::Approx(0));
363 REQUIRE(bmin[1] == Catch::Approx(2));
364 REQUIRE(bmin[2] == Catch::Approx(3));
365
366 REQUIRE(bmax[0] == Catch::Approx(1));
367 REQUIRE(bmax[1] == Catch::Approx(2));
368 REQUIRE(bmax[2] == Catch::Approx(5));
369 }
370 }
371
372 TEST_CASE("rcCalcGridSize")
373 {
374 SECTION("computes the size of an x & z axis grid")
375 {
376 float verts[] = {
377 1, 2, 3,
378 0, 2, 6
379 };
380 float bmin[3];
381 float bmax[3];
382 rcCalcBounds(verts, 2, bmin, bmax);
383
384 float cellSize = 1.5f;
385
386 int width;
387 int height;
388
389 rcCalcGridSize(bmin, bmax, cellSize, &width, &height);
390
391 REQUIRE(width == 1);
392 REQUIRE(height == 2);
393 }
394 }
395
396 TEST_CASE("rcCreateHeightfield")
397 {
398 SECTION("create a heightfield")
399 {
400 float verts[] = {
401 1, 2, 3,
402 0, 2, 6
403 };
404 float bmin[3];
405 float bmax[3];
406 rcCalcBounds(verts, 2, bmin, bmax);
407
408 float cellSize = 1.5f;
409 float cellHeight = 2;
410
411 int width;
412 int height;
413
414 rcCalcGridSize(bmin, bmax, cellSize, &width, &height);
415
416 rcHeightfield heightfield;
417
418 bool result = rcCreateHeightfield(0, heightfield, width, height, bmin, bmax, cellSize, cellHeight);
419
420 REQUIRE(result);
421
422 REQUIRE(heightfield.width == width);
423 REQUIRE(heightfield.height == height);
424
425 REQUIRE(heightfield.bmin[0] == Catch::Approx(bmin[0]));
426 REQUIRE(heightfield.bmin[1] == Catch::Approx(bmin[1]));
427 REQUIRE(heightfield.bmin[2] == Catch::Approx(bmin[2]));
428
429 REQUIRE(heightfield.bmax[0] == Catch::Approx(bmax[0]));
430 REQUIRE(heightfield.bmax[1] == Catch::Approx(bmax[1]));
431 REQUIRE(heightfield.bmax[2] == Catch::Approx(bmax[2]));
432
433 REQUIRE(heightfield.cs == Catch::Approx(cellSize));
434 REQUIRE(heightfield.ch == Catch::Approx(cellHeight));
435
436 REQUIRE(heightfield.spans != 0);
437 REQUIRE(heightfield.pools == 0);
438 REQUIRE(heightfield.freelist == 0);
439 }
440 }
441
442 TEST_CASE("rcMarkWalkableTriangles")
443 {
444 rcContext* ctx = 0;
445 float walkableSlopeAngle = 45;
446 float verts[] = {
447 0, 0, 0,
448 1, 0, 0,
449 0, 0, -1
450 };
451 int nv = 3;
452 int walkable_tri[] = { 0, 1, 2 };
453 int unwalkable_tri[] = { 0, 2, 1 };
454 int nt = 1;
455 unsigned char areas[] = { RC_NULL_AREA };
456
457 SECTION("One walkable triangle")
458 {
459 rcMarkWalkableTriangles(ctx, walkableSlopeAngle, verts, nv, walkable_tri, nt, areas);
460 REQUIRE(areas[0] == RC_WALKABLE_AREA);
461 }
462
463 SECTION("One non-walkable triangle")
464 {
465 rcMarkWalkableTriangles(ctx, walkableSlopeAngle, verts, nv, unwalkable_tri, nt, areas);
466 REQUIRE(areas[0] == RC_NULL_AREA);
467 }
468
469 SECTION("Non-walkable triangle area id's are not modified")
470 {
471 areas[0] = 42;
472 rcMarkWalkableTriangles(ctx, walkableSlopeAngle, verts, nv, unwalkable_tri, nt, areas);
473 REQUIRE(areas[0] == 42);
474 }
475
476 SECTION("Slopes equal to the max slope are considered unwalkable.")
477 {
478 walkableSlopeAngle = 0;
479 rcMarkWalkableTriangles(ctx, walkableSlopeAngle, verts, nv, walkable_tri, nt, areas);
480 REQUIRE(areas[0] == RC_NULL_AREA);
481 }
482 }
483
484 TEST_CASE("rcClearUnwalkableTriangles")
485 {
486 rcContext* ctx = 0;
487 float walkableSlopeAngle = 45;
488 float verts[] = {
489 0, 0, 0,
490 1, 0, 0,
491 0, 0, -1
492 };
493 int nv = 3;
494 int walkable_tri[] = { 0, 1, 2 };
495 int unwalkable_tri[] = { 0, 2, 1 };
496 int nt = 1;
497 unsigned char areas[] = { 42 };
498
499 SECTION("Sets area ID of unwalkable triangle to RC_NULL_AREA")
500 {
501 rcClearUnwalkableTriangles(ctx, walkableSlopeAngle, verts, nv, unwalkable_tri, nt, areas);
502 REQUIRE(areas[0] == RC_NULL_AREA);
503 }
504
505 SECTION("Does not modify walkable triangle aread ID's")
506 {
507 rcClearUnwalkableTriangles(ctx, walkableSlopeAngle, verts, nv, walkable_tri, nt, areas);
508 REQUIRE(areas[0] == 42);
509 }
510
511 SECTION("Slopes equal to the max slope are considered unwalkable.")
512 {
513 walkableSlopeAngle = 0;
514 rcClearUnwalkableTriangles(ctx, walkableSlopeAngle, verts, nv, walkable_tri, nt, areas);
515 REQUIRE(areas[0] == RC_NULL_AREA);
516 }
517 }
518
519 TEST_CASE("rcAddSpan")
520 {
521 rcContext ctx(false);
522
523 float verts[] = {
524 1, 2, 3,
525 0, 2, 6
526 };
527 float bmin[3];
528 float bmax[3];
529 rcCalcBounds(verts, 2, bmin, bmax);
530
531 float cellSize = 1.5f;
532 float cellHeight = 2;
533
534 int width;
535 int height;
536
537 rcCalcGridSize(bmin, bmax, cellSize, &width, &height);
538
539 rcHeightfield hf;
540 REQUIRE(rcCreateHeightfield(&ctx, hf, width, height, bmin, bmax, cellSize, cellHeight));
541
542 int x = 0;
543 int y = 0;
544 unsigned short smin = 0;
545 unsigned short smax = 1;
546 unsigned char area = 42;
547 int flagMergeThr = 1;
548
549 SECTION("Add a span to an empty heightfield.")
550 {
551 bool result = rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr);
552 REQUIRE(result);
553 REQUIRE(hf.spans[0] != 0);
554 REQUIRE(hf.spans[0]->smin == smin);
555 REQUIRE(hf.spans[0]->smax == smax);
556 REQUIRE(hf.spans[0]->area == area);
557 }
558
559 SECTION("Add a span that gets merged with an existing span.")
560 {
561 bool result = rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr);
562 REQUIRE(result);
563 REQUIRE(hf.spans[0] != 0);
564 REQUIRE(hf.spans[0]->smin == smin);
565 REQUIRE(hf.spans[0]->smax == smax);
566 REQUIRE(hf.spans[0]->area == area);
567
568 smin = 1;
569 smax = 2;
570 result = rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr);
571 REQUIRE(result);
572 REQUIRE(hf.spans[0] != 0);
573 REQUIRE(hf.spans[0]->smin == 0);
574 REQUIRE(hf.spans[0]->smax == 2);
575 REQUIRE(hf.spans[0]->area == area);
576 }
577
578 SECTION("Add a span that merges with two spans above and below.")
579 {
580 smin = 0;
581 smax = 1;
582 REQUIRE(rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr));
583 REQUIRE(hf.spans[0] != 0);
584 REQUIRE(hf.spans[0]->smin == smin);
585 REQUIRE(hf.spans[0]->smax == smax);
586 REQUIRE(hf.spans[0]->area == area);
587 REQUIRE(hf.spans[0]->next == 0);
588
589 smin = 2;
590 smax = 3;
591 REQUIRE(rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr));
592 REQUIRE(hf.spans[0]->next != 0);
593 REQUIRE(hf.spans[0]->next->smin == smin);
594 REQUIRE(hf.spans[0]->next->smax == smax);
595 REQUIRE(hf.spans[0]->next->area == area);
596
597 smin = 1;
598 smax = 2;
599 REQUIRE(rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr));
600 REQUIRE(hf.spans[0] != 0);
601 REQUIRE(hf.spans[0]->smin == 0);
602 REQUIRE(hf.spans[0]->smax == 3);
603 REQUIRE(hf.spans[0]->area == area);
604 REQUIRE(hf.spans[0]->next == 0);
605 }
606 }
607
608 TEST_CASE("rcRasterizeTriangle")
609 {
610 rcContext ctx;
611 float verts[] = {
612 0, 0, 0,
613 1, 0, 0,
614 0, 0, -1
615 };
616 float bmin[3];
617 float bmax[3];
618 rcCalcBounds(verts, 3, bmin, bmax);
619
620 float cellSize = .5f;
621 float cellHeight = .5f;
622
623 int width;
624 int height;
625
626 rcCalcGridSize(bmin, bmax, cellSize, &width, &height);
627
628 rcHeightfield solid;
629 REQUIRE(rcCreateHeightfield(&ctx, solid, width, height, bmin, bmax, cellSize, cellHeight));
630
631 unsigned char area = 42;
632 int flagMergeThr = 1;
633
634 SECTION("Rasterize a triangle")
635 {
636 REQUIRE(rcRasterizeTriangle(&ctx, &verts[0], &verts[3], &verts[6], area, solid, flagMergeThr));
637
638 REQUIRE(solid.spans[0 + 0 * width]);
639 REQUIRE(!solid.spans[1 + 0 * width]);
640 REQUIRE(solid.spans[0 + 1 * width]);
641 REQUIRE(solid.spans[1 + 1 * width]);
642
643 REQUIRE(solid.spans[0 + 0 * width]->smin == 0);
644 REQUIRE(solid.spans[0 + 0 * width]->smax == 1);
645 REQUIRE(solid.spans[0 + 0 * width]->area == area);
646 REQUIRE(!solid.spans[0 + 0 * width]->next);
647
648 REQUIRE(solid.spans[0 + 1 * width]->smin == 0);
649 REQUIRE(solid.spans[0 + 1 * width]->smax == 1);
650 REQUIRE(solid.spans[0 + 1 * width]->area == area);
651 REQUIRE(!solid.spans[0 + 1 * width]->next);
652
653 REQUIRE(solid.spans[1 + 1 * width]->smin == 0);
654 REQUIRE(solid.spans[1 + 1 * width]->smax == 1);
655 REQUIRE(solid.spans[1 + 1 * width]->area == area);
656 REQUIRE(!solid.spans[1 + 1 * width]->next);
657 }
658 }
659
660 TEST_CASE("rcRasterizeTriangle overlapping bb but non-overlapping triangle")
661 {
662 // This is a minimal repro case for the issue fixed in PR #476 (https://github.com/recastnavigation/recastnavigation/pull/476)
663 rcContext ctx;
664
665 // create a heightfield
666 float cellSize = 1;
667 float cellHeight = 1;
668 int width = 10;
669 int height = 10;
670 float bmin[] = { 0, 0, 0 };
671 float bmax[] = { 10, 10, 10 };
672 rcHeightfield heightfield;
673 REQUIRE(rcCreateHeightfield(&ctx, heightfield, width, height, bmin, bmax, cellSize, cellHeight));
674
675 // rasterize a triangle outside of the heightfield.
676 unsigned char area = 42;
677 int flagMergeThr = 1;
678 float verts[] =
679 {
680 -10.0, 5.5, -10.0,
681 -10.0, 5.5, 3,
682 3.0, 5.5, -10.0
683 };
684 REQUIRE(rcRasterizeTriangle(&ctx, &verts[0], &verts[3], &verts[6], area, heightfield, flagMergeThr));
685
686 // ensure that no spans were created
687 for (int x = 0; x < width; ++x)
688 {
689 for (int z = 0; z < height; ++z)
690 {
691 rcSpan* span = heightfield.spans[x + z * heightfield.width];
692 REQUIRE(span == NULL);
693 }
694 }
695 }
696
697 TEST_CASE("rcRasterizeTriangle smaller than half a voxel size in x")
698 {
699 SECTION("Skinny triangle along x axis")
700 {
701 rcContext ctx;
702 float verts[] = {
703 5, 0, 0.005f,
704 5, 0, -0.005f,
705 -5, 0, 0.005f,
706
707 -5, 0, 0.005f,
708 5, 0, -0.005f,
709 -5, 0, -0.005f,
710 };
711 float bmin[3];
712 float bmax[3];
713 rcCalcBounds(verts, 3, bmin, bmax);
714
715 float cellSize = 1;
716 float cellHeight = 1;
717
718 int width;
719 int height;
720
721 rcCalcGridSize(bmin, bmax, cellSize, &width, &height);
722
723 rcHeightfield solid;
724 REQUIRE(rcCreateHeightfield(&ctx, solid, width, height, bmin, bmax, cellSize, cellHeight));
725
726 unsigned char areas[] = {42, 42};
727 int flagMergeThr = 1;
728 REQUIRE(rcRasterizeTriangles(&ctx, verts, areas, 2, solid, flagMergeThr));
729 }
730
731 SECTION("Skinny triangle along z axis")
732 {
733 rcContext ctx;
734 float verts[] = {
735 0.005f, 0, 5,
736 -0.005f, 0, 5,
737 0.005f, 0, -5,
738
739 0.005f, 0, -5,
740 -0.005f, 0, 5,
741 -0.005f, 0, -5
742 };
743 float bmin[3];
744 float bmax[3];
745 rcCalcBounds(verts, 3, bmin, bmax);
746
747 float cellSize = 1;
748 float cellHeight = 1;
749
750 int width;
751 int height;
752
753 rcCalcGridSize(bmin, bmax, cellSize, &width, &height);
754
755 rcHeightfield solid;
756 REQUIRE(rcCreateHeightfield(&ctx, solid, width, height, bmin, bmax, cellSize, cellHeight));
757
758 unsigned char areas[] = {42, 42};
759 int flagMergeThr = 1;
760 REQUIRE(rcRasterizeTriangles(&ctx, verts, areas, 2, solid, flagMergeThr));
761 }
762 }
763
764 TEST_CASE("rcRasterizeTriangles")
765 {
766 rcContext ctx;
767 float verts[] = {
768 0, 0, 0,
769 1, 0, 0,
770 0, 0, -1,
771 0, 0, 1
772 };
773 int tris[] = {
774 0, 1, 2,
775 0, 3, 1
776 };
777 unsigned char areas[] = {
778 1,
779 2
780 };
781 float bmin[3];
782 float bmax[3];
783 rcCalcBounds(verts, 4, bmin, bmax);
784
785 float cellSize = .5f;
786 float cellHeight = .5f;
787
788 int width;
789 int height;
790
791 rcCalcGridSize(bmin, bmax, cellSize, &width, &height);
792
793 rcHeightfield solid;
794 REQUIRE(rcCreateHeightfield(&ctx, solid, width, height, bmin, bmax, cellSize, cellHeight));
795
796 int flagMergeThr = 1;
797
798 SECTION("Rasterize some triangles")
799 {
800 REQUIRE(rcRasterizeTriangles(&ctx, verts, 4, tris, areas, 2, solid, flagMergeThr));
801
802 REQUIRE(solid.spans[0 + 0 * width]);
803 REQUIRE(solid.spans[0 + 1 * width]);
804 REQUIRE(solid.spans[0 + 2 * width]);
805 REQUIRE(solid.spans[0 + 3 * width]);
806 REQUIRE(!solid.spans[1 + 0 * width]);
807 REQUIRE(solid.spans[1 + 1 * width]);
808 REQUIRE(solid.spans[1 + 2 * width]);
809 REQUIRE(!solid.spans[1 + 3 * width]);
810
811 REQUIRE(solid.spans[0 + 0 * width]->smin == 0);
812 REQUIRE(solid.spans[0 + 0 * width]->smax == 1);
813 REQUIRE(solid.spans[0 + 0 * width]->area == 1);
814 REQUIRE(!solid.spans[0 + 0 * width]->next);
815
816 REQUIRE(solid.spans[0 + 1 * width]->smin == 0);
817 REQUIRE(solid.spans[0 + 1 * width]->smax == 1);
818 REQUIRE(solid.spans[0 + 1 * width]->area == 1);
819 REQUIRE(!solid.spans[0 + 1 * width]->next);
820
821 REQUIRE(solid.spans[0 + 2 * width]->smin == 0);
822 REQUIRE(solid.spans[0 + 2 * width]->smax == 1);
823 REQUIRE(solid.spans[0 + 2 * width]->area == 2);
824 REQUIRE(!solid.spans[0 + 2 * width]->next);
825
826 REQUIRE(solid.spans[0 + 3 * width]->smin == 0);
827 REQUIRE(solid.spans[0 + 3 * width]->smax == 1);
828 REQUIRE(solid.spans[0 + 3 * width]->area == 2);
829 REQUIRE(!solid.spans[0 + 3 * width]->next);
830
831 REQUIRE(solid.spans[1 + 1 * width]->smin == 0);
832 REQUIRE(solid.spans[1 + 1 * width]->smax == 1);
833 REQUIRE(solid.spans[1 + 1 * width]->area == 1);
834 REQUIRE(!solid.spans[1 + 1 * width]->next);
835
836 REQUIRE(solid.spans[1 + 2 * width]->smin == 0);
837 REQUIRE(solid.spans[1 + 2 * width]->smax == 1);
838 REQUIRE(solid.spans[1 + 2 * width]->area == 2);
839 REQUIRE(!solid.spans[1 + 2 * width]->next);
840 }
841
842 SECTION("Unsigned short overload")
843 {
844 unsigned short utris[] = {
845 0, 1, 2,
846 0, 3, 1
847 };
848 REQUIRE(rcRasterizeTriangles(&ctx, verts, 4, utris, areas, 2, solid, flagMergeThr));
849
850 REQUIRE(solid.spans[0 + 0 * width]);
851 REQUIRE(solid.spans[0 + 1 * width]);
852 REQUIRE(solid.spans[0 + 2 * width]);
853 REQUIRE(solid.spans[0 + 3 * width]);
854 REQUIRE(!solid.spans[1 + 0 * width]);
855 REQUIRE(solid.spans[1 + 1 * width]);
856 REQUIRE(solid.spans[1 + 2 * width]);
857 REQUIRE(!solid.spans[1 + 3 * width]);
858
859 REQUIRE(solid.spans[0 + 0 * width]->smin == 0);
860 REQUIRE(solid.spans[0 + 0 * width]->smax == 1);
861 REQUIRE(solid.spans[0 + 0 * width]->area == 1);
862 REQUIRE(!solid.spans[0 + 0 * width]->next);
863
864 REQUIRE(solid.spans[0 + 1 * width]->smin == 0);
865 REQUIRE(solid.spans[0 + 1 * width]->smax == 1);
866 REQUIRE(solid.spans[0 + 1 * width]->area == 1);
867 REQUIRE(!solid.spans[0 + 1 * width]->next);
868
869 REQUIRE(solid.spans[0 + 2 * width]->smin == 0);
870 REQUIRE(solid.spans[0 + 2 * width]->smax == 1);
871 REQUIRE(solid.spans[0 + 2 * width]->area == 2);
872 REQUIRE(!solid.spans[0 + 2 * width]->next);
873
874 REQUIRE(solid.spans[0 + 3 * width]->smin == 0);
875 REQUIRE(solid.spans[0 + 3 * width]->smax == 1);
876 REQUIRE(solid.spans[0 + 3 * width]->area == 2);
877 REQUIRE(!solid.spans[0 + 3 * width]->next);
878
879 REQUIRE(solid.spans[1 + 1 * width]->smin == 0);
880 REQUIRE(solid.spans[1 + 1 * width]->smax == 1);
881 REQUIRE(solid.spans[1 + 1 * width]->area == 1);
882 REQUIRE(!solid.spans[1 + 1 * width]->next);
883
884 REQUIRE(solid.spans[1 + 2 * width]->smin == 0);
885 REQUIRE(solid.spans[1 + 2 * width]->smax == 1);
886 REQUIRE(solid.spans[1 + 2 * width]->area == 2);
887 REQUIRE(!solid.spans[1 + 2 * width]->next);
888 }
889
890 SECTION("Triangle list overload")
891 {
892 float vertsList[] = {
893 0, 0, 0,
894 1, 0, 0,
895 0, 0, -1,
896 0, 0, 0,
897 0, 0, 1,
898 1, 0, 0,
899 };
900
901 REQUIRE(rcRasterizeTriangles(&ctx, vertsList, areas, 2, solid, flagMergeThr));
902
903 REQUIRE(solid.spans[0 + 0 * width]);
904 REQUIRE(solid.spans[0 + 1 * width]);
905 REQUIRE(solid.spans[0 + 2 * width]);
906 REQUIRE(solid.spans[0 + 3 * width]);
907 REQUIRE(!solid.spans[1 + 0 * width]);
908 REQUIRE(solid.spans[1 + 1 * width]);
909 REQUIRE(solid.spans[1 + 2 * width]);
910 REQUIRE(!solid.spans[1 + 3 * width]);
911
912 REQUIRE(solid.spans[0 + 0 * width]->smin == 0);
913 REQUIRE(solid.spans[0 + 0 * width]->smax == 1);
914 REQUIRE(solid.spans[0 + 0 * width]->area == 1);
915 REQUIRE(!solid.spans[0 + 0 * width]->next);
916
917 REQUIRE(solid.spans[0 + 1 * width]->smin == 0);
918 REQUIRE(solid.spans[0 + 1 * width]->smax == 1);
919 REQUIRE(solid.spans[0 + 1 * width]->area == 1);
920 REQUIRE(!solid.spans[0 + 1 * width]->next);
921
922 REQUIRE(solid.spans[0 + 2 * width]->smin == 0);
923 REQUIRE(solid.spans[0 + 2 * width]->smax == 1);
924 REQUIRE(solid.spans[0 + 2 * width]->area == 2);
925 REQUIRE(!solid.spans[0 + 2 * width]->next);
926
927 REQUIRE(solid.spans[0 + 3 * width]->smin == 0);
928 REQUIRE(solid.spans[0 + 3 * width]->smax == 1);
929 REQUIRE(solid.spans[0 + 3 * width]->area == 2);
930 REQUIRE(!solid.spans[0 + 3 * width]->next);
931
932 REQUIRE(solid.spans[1 + 1 * width]->smin == 0);
933 REQUIRE(solid.spans[1 + 1 * width]->smax == 1);
934 REQUIRE(solid.spans[1 + 1 * width]->area == 1);
935 REQUIRE(!solid.spans[1 + 1 * width]->next);
936
937 REQUIRE(solid.spans[1 + 2 * width]->smin == 0);
938 REQUIRE(solid.spans[1 + 2 * width]->smax == 1);
939 REQUIRE(solid.spans[1 + 2 * width]->area == 2);
940 REQUIRE(!solid.spans[1 + 2 * width]->next);
941 }
942 }
943
944 // Used to verify that rcVector constructs/destroys objects correctly.
945 struct Incrementor {
946 static int constructions;
947 static int destructions;
948 static int copies;
949 Incrementor() { constructions++; }
950 ~Incrementor() { destructions++; }
951 Incrementor(const Incrementor&) { copies++; }
952 Incrementor& operator=(const Incrementor&); // Deleted assignment.
953
954 static void Reset() {
955 constructions = 0;
956 destructions = 0;
957 copies = 0;
958 }
959 };
960 int Incrementor::constructions = 0;
961 int Incrementor::destructions = 0;
962 int Incrementor::copies = 0;
963
964 const int kMaxAllocSize = 1024;
965 const unsigned char kClearValue = 0xff;
966 // Simple alloc/free that clears the memory on free..
967 void* AllocAndInit(size_t size, rcAllocHint) {
968 rcAssert(kMaxAllocSize >= size);
969 return memset(malloc(kMaxAllocSize), 0, kMaxAllocSize);
970 }
971 void FreeAndClear(void* mem) {
972 if (mem) {
973 memset(mem, kClearValue, kMaxAllocSize);
974 }
975 free(mem);
976 }
977 // Verifies that memory has been initialized by AllocAndInit, and not cleared by FreeAndClear.
978 struct Copier {
979 const static int kAlive;
980 const static int kDead;
981 Copier() : value(kAlive) {}
982
983 // checks that the source of the copy is valid.
984 Copier(const Copier& other) : value(kAlive) {
985 other.Verify();
986 }
987 Copier& operator=(const Copier&);
988
989 // Marks the value as dead.
990 ~Copier() { value = kDead; }
991 void Verify() const {
992 REQUIRE(value == kAlive);
993 }
994 volatile int value;
995 };
996 const int Copier::kAlive = 0x1f;
997 const int Copier::kDead = 0xde;
998
999 struct NotDefaultConstructible {
1000 NotDefaultConstructible(int) {}
1001 };
1002
1003 TEST_CASE("rcVector")
1004 {
1005 SECTION("Vector basics.")
1006 {
1007 rcTempVector<int> vec;
1008 REQUIRE(vec.size() == 0);
1009 vec.push_back(10);
1010 vec.push_back(12);
1011 REQUIRE(vec.size() == 2);
1012 REQUIRE(vec.capacity() >= 2);
1013 REQUIRE(vec[0] == 10);
1014 REQUIRE(vec[1] == 12);
1015 vec.pop_back();
1016 REQUIRE(vec.size() == 1);
1017 REQUIRE(vec[0] == 10);
1018 vec.pop_back();
1019 REQUIRE(vec.size() == 0);
1020 vec.resize(100, 5);
1021 REQUIRE(vec.size() == 100);
1022 for (int i = 0; i < 100; i++) {
1023 REQUIRE(vec[i] == 5);
1024 vec[i] = i;
1025 }
1026 for (int i = 0; i < 100; i++) {
1027 REQUIRE(vec[i] == i);
1028 }
1029 }
1030
1031 SECTION("Constructors/Destructors")
1032 {
1033 Incrementor::Reset();
1034 rcTempVector<Incrementor> vec;
1035 REQUIRE(Incrementor::constructions == 0);
1036 REQUIRE(Incrementor::destructions == 0);
1037 REQUIRE(Incrementor::copies == 0);
1038 vec.push_back(Incrementor());
1039 // push_back() may create and copy objects internally.
1040 REQUIRE(Incrementor::constructions == 1);
1041 REQUIRE(Incrementor::destructions >= 1);
1042 // REQUIRE(Incrementor::copies >= 2);
1043
1044 vec.clear();
1045 Incrementor::Reset();
1046 vec.resize(100);
1047 // Initialized with default instance. Temporaries may be constructed, then destroyed.
1048 REQUIRE(Incrementor::constructions == 100);
1049 REQUIRE(Incrementor::destructions == 0);
1050 REQUIRE(Incrementor::copies == 0);
1051
1052 Incrementor::Reset();
1053 for (int i = 0; i < 100; i++) {
1054 REQUIRE(Incrementor::destructions == i);
1055 vec.pop_back();
1056 }
1057 REQUIRE(Incrementor::constructions == 0);
1058 REQUIRE(Incrementor::destructions == 100);
1059 REQUIRE(Incrementor::copies == 0);
1060
1061 vec.resize(100);
1062 Incrementor::Reset();
1063 vec.clear();
1064 // One temp object is constructed for the default argumnet of resize().
1065 REQUIRE(Incrementor::constructions == 0);
1066 REQUIRE(Incrementor::destructions == 100);
1067 REQUIRE(Incrementor::copies == 0);
1068
1069 Incrementor::Reset();
1070 vec.resize(100, Incrementor());
1071 REQUIRE(Incrementor::constructions == 1);
1072 REQUIRE(Incrementor::destructions == 1);
1073 REQUIRE(Incrementor::copies == 100);
1074 }
1075
1076 SECTION("Copying Contents")
1077 {
1078
1079 // veriyf event counts after doubling size -- should require a lot of copying and destorying.
1080 rcTempVector<Incrementor> vec;
1081 Incrementor::Reset();
1082 vec.resize(100);
1083 REQUIRE(Incrementor::constructions == 100);
1084 REQUIRE(Incrementor::destructions == 0);
1085 REQUIRE(Incrementor::copies == 0);
1086 Incrementor::Reset();
1087 vec.resize(200);
1088 REQUIRE(vec.size() == vec.capacity());
1089 REQUIRE(Incrementor::constructions == 100); // Construc new elements.
1090 REQUIRE(Incrementor::destructions == 100); // Destroy old contents.
1091 REQUIRE(Incrementor::copies == 100); // Copy old elements into new array.
1092 }
1093
1094 SECTION("Swap")
1095 {
1096 rcTempVector<int> a(10, 0xa);
1097 rcTempVector<int> b;
1098
1099 int* a_data = a.data();
1100 int* b_data = b.data();
1101
1102 a.swap(b);
1103 REQUIRE(a.size() == 0);
1104 REQUIRE(b.size() == 10);
1105 REQUIRE(b[0] == 0xa);
1106 REQUIRE(b[9] == 0xa);
1107 REQUIRE(a.data() == b_data);
1108 REQUIRE(b.data() == a_data);
1109 }
1110
1111 SECTION("Overlapping init")
1112 {
1113 rcAllocSetCustom(&AllocAndInit, &FreeAndClear);
1114 rcTempVector<Copier> vec;
1115 // Force a realloc during push_back().
1116 vec.resize(64);
1117 REQUIRE(vec.capacity() == vec.size());
1118 REQUIRE(vec.capacity() > 0);
1119 REQUIRE(vec.size() == vec.capacity());
1120
1121 // Don't crash.
1122 vec.push_back(vec[0]);
1123 rcAllocSetCustom(NULL, NULL);
1124 }
1125
1126 SECTION("Vector Destructor")
1127 {
1128 {
1129 rcTempVector<Incrementor> vec;
1130 vec.resize(10);
1131 Incrementor::Reset();
1132 }
1133 REQUIRE(Incrementor::destructions == 10);
1134 }
1135
1136 SECTION("Assign")
1137 {
1138 rcTempVector<int> a(10, 0xa);
1139 a.assign(5, 0xb);
1140 REQUIRE(a.size() == 5);
1141 REQUIRE(a[0] == 0xb);
1142 REQUIRE(a[4] == 0xb);
1143 a.assign(15, 0xc);
1144 REQUIRE(a.size() == 15);
1145 REQUIRE(a[0] == 0xc);
1146 REQUIRE(a[14] == 0xc);
1147
1148 rcTempVector<int> b;
1149 b.assign(a.data(), a.data() + a.size());
1150 REQUIRE(b.size() == a.size());
1151 REQUIRE(b[0] == a[0]);
1152 }
1153
1154 SECTION("Copy")
1155 {
1156 rcTempVector<int> a(10, 0xa);
1157 rcTempVector<int> b(a);
1158 REQUIRE(a.size() == 10);
1159 REQUIRE(a.size() == b.size());
1160 REQUIRE(a[0] == b[0]);
1161 REQUIRE(a.data() != b.data());
1162 rcTempVector<int> c(a.data(), a.data() + a.size());
1163 REQUIRE(c.size() == a.size());
1164 REQUIRE(c[0] == a[0]);
1165
1166 rcTempVector<Incrementor> d(10);
1167 Incrementor::Reset();
1168 rcTempVector<Incrementor> e(d);
1169 REQUIRE(Incrementor::constructions == 0);
1170 REQUIRE(Incrementor::destructions == 0);
1171 REQUIRE(Incrementor::copies == 10);
1172
1173 Incrementor::Reset();
1174 rcTempVector<Incrementor> f(d.data(), d.data() + d.size());
1175 REQUIRE(Incrementor::constructions == 0);
1176 REQUIRE(Incrementor::destructions == 0);
1177 REQUIRE(Incrementor::copies == 10);
1178 }
1179
1180 SECTION("Type Requirements")
1181 {
1182 // This section verifies that we don't enforce unnecessary
1183 // requirements on the types we hold.
1184
1185 // Implementing clear as resize(0) will cause this to fail
1186 // as resize(0) requires T to be default constructible.
1187 rcTempVector<NotDefaultConstructible> v;
1188 v.clear();
1189 }
1190 }
1191
1192 // TODO: Implement benchmarking for platforms other than posix.
1193 #ifdef __unix__
1194 #include <unistd.h>
1195 #ifdef _POSIX_TIMERS
1196 #include <time.h>
1197 #include <stdint.h>
1198
1199 int64_t NowNanos() {
1200 struct timespec tp;
1201 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
1202 return tp.tv_nsec + 1000000000LL * tp.tv_sec;
1203 }
1204
1205 #define BM(name, iterations) \
1206 struct BM_ ## name { \
1207 static void Run() { \
1208 int64_t begin_time = NowNanos(); \
1209 for (int i = 0 ; i < iterations; i++) { \
1210 Body(); \
1211 } \
1212 int64_t nanos = NowNanos() - begin_time; \
1213 printf("BM_%-35s %ld iterations in %10ld nanos: %10.2f nanos/it\n", #name ":", (int64_t)iterations, nanos, double(nanos) / iterations); \
1214 } \
1215 static void Body(); \
1216 }; \
1217 TEST_CASE(#name) { \
1218 BM_ ## name::Run(); \
1219 } \
1220 void BM_ ## name::Body()
1221
1222 const int64_t kNumLoops = 100;
1223 const int64_t kNumInserts = 100000;
1224
1225 // Prevent compiler from eliding a calculation.
1226 // TODO: Implement for MSVC.
1227 template <typename T>
1228 void DoNotOptimize(T* v) {
1229 asm volatile ("" : "+r" (v));
1230 }
1231
1232 BM(FlatArray_Push, kNumLoops)
1233 {
1234 int cap = 64;
1235 int* v = (int*)rcAlloc(cap * sizeof(int), RC_ALLOC_TEMP);
1236 for (int j = 0; j < kNumInserts; j++) {
1237 if (j == cap) {
1238 cap *= 2;
1239 int* tmp = (int*)rcAlloc(sizeof(int) * cap, RC_ALLOC_TEMP);
1240 memcpy(tmp, v, j * sizeof(int));
1241 rcFree(v);
1242 v = tmp;
1243 }
1244 v[j] = 2;
1245 }
1246
1247 DoNotOptimize(v);
1248 rcFree(v);
1249 }
1250 BM(FlatArray_Fill, kNumLoops)
1251 {
1252 int* v = (int*)rcAlloc(sizeof(int) * kNumInserts, RC_ALLOC_TEMP);
1253 for (int j = 0; j < kNumInserts; j++) {
1254 v[j] = 2;
1255 }
1256
1257 DoNotOptimize(v);
1258 rcFree(v);
1259 }
1260 BM(FlatArray_Memset, kNumLoops)
1261 {
1262 int* v = (int*)rcAlloc(sizeof(int) * kNumInserts, RC_ALLOC_TEMP);
1263 memset(v, 0, kNumInserts * sizeof(int));
1264
1265 DoNotOptimize(v);
1266 rcFree(v);
1267 }
1268
1269 BM(rcVector_Push, kNumLoops)
1270 {
1271 rcTempVector<int> v;
1272 for (int j = 0; j < kNumInserts; j++) {
1273 v.push_back(2);
1274 }
1275 DoNotOptimize(v.data());
1276 }
1277 BM(rcVector_PushPreallocated, kNumLoops)
1278 {
1279 rcTempVector<int> v;
1280 v.reserve(kNumInserts);
1281 for (int j = 0; j < kNumInserts; j++) {
1282 v.push_back(2);
1283 }
1284 DoNotOptimize(v.data());
1285 }
1286 BM(rcVector_Assign, kNumLoops)
1287 {
1288 rcTempVector<int> v;
1289 v.assign(kNumInserts, 2);
1290 DoNotOptimize(v.data());
1291 }
1292 BM(rcVector_AssignIndices, kNumLoops)
1293 {
1294 rcTempVector<int> v;
1295 v.resize(kNumInserts);
1296 for (int j = 0; j < kNumInserts; j++) {
1297 v[j] = 2;
1298 }
1299 DoNotOptimize(v.data());
1300 }
1301 BM(rcVector_Resize, kNumLoops)
1302 {
1303 rcTempVector<int> v;
1304 v.resize(kNumInserts, 2);
1305 DoNotOptimize(v.data());
1306 }
1307
1308 BM(stdvector_Push, kNumLoops)
1309 {
1310 std::vector<int> v;
1311 for (int j = 0; j < kNumInserts; j++) {
1312 v.push_back(2);
1313 }
1314 DoNotOptimize(v.data());
1315 }
1316 BM(stdvector_PushPreallocated, kNumLoops)
1317 {
1318 std::vector<int> v;
1319 v.reserve(kNumInserts);
1320 for (int j = 0; j < kNumInserts; j++) {
1321 v.push_back(2);
1322 }
1323 DoNotOptimize(v.data());
1324 }
1325 BM(stdvector_Assign, kNumLoops)
1326 {
1327 std::vector<int> v;
1328 v.assign(kNumInserts, 2);
1329 DoNotOptimize(v.data());
1330 }
1331 BM(stdvector_AssignIndices, kNumLoops)
1332 {
1333 std::vector<int> v;
1334 v.resize(kNumInserts);
1335 for (int j = 0; j < kNumInserts; j++) {
1336 v[j] = 2;
1337 }
1338 DoNotOptimize(v.data());
1339 }
1340 BM(stdvector_Resize, kNumLoops)
1341 {
1342 std::vector<int> v;
1343 v.resize(kNumInserts, 2);
1344 DoNotOptimize(v.data());
1345 }
1346
1347 #undef BM
1348 #endif // _POSIX_TIMERS
1349 #endif // __unix__