Tests_Recast.cpp 29 KB

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