Source: tests.js

  1. /**
  2. * @file Some tests.
  3. * @version March 26, 2017
  4. *
  5. * @author Olivier Pirson --- http://www.opimedia.be/
  6. * @license GPLv3 --- Copyright (C) 2017 Olivier Pirson
  7. */
  8. (function () {
  9. "use strict";
  10. function add2Matchings() {
  11. const points = [new Point(10, 10),
  12. new Point(50, 200),
  13. new Point(100, 150),
  14. new Point(150, 250),
  15. new Point(200, 170),
  16. new Point(300, 150),
  17. new Point(400, 100),
  18. new Point(410, 290)];
  19. for (let point of points) {
  20. globalController.leftController.view.matching.pointAdd(point);
  21. }
  22. if (true) {
  23. for (let i = 0; i < points.length - 1; i += 2) {
  24. const leftSegment = new Segment(points[i], points[i + 1]);
  25. globalController.leftController.view.matching.segmentAdd(leftSegment);
  26. const rightSegment = new Segment(points[i + 1], points[(i + 2)%points.length]);
  27. globalController.rightController.view.matching.segmentAdd(rightSegment);
  28. }
  29. }
  30. globalController.globalView.update();
  31. }
  32. function testCompare() {
  33. assert(compare(42, 42) === 0);
  34. assert(compare(42, 666) === -1);
  35. assert(compare(-42, 666) === -1);
  36. assert(compare(666, 42) === 1);
  37. assert(compare(-666, 42) === -1);
  38. assert(compare("foo", "foo") === 0);
  39. assert(compare("foo", "bar") === 1);
  40. assert(compare("bar", "foo") === -1);
  41. assert(compare("bar", "FOO") === 1);
  42. }
  43. function testGetRandom() {
  44. for (let i = 0; i < 100; ++i) {
  45. const x = getRandom(-5, 17);
  46. assert(-5 <= x, x);
  47. assert(x < 17, x);
  48. }
  49. for (let i = 0; i < 100; ++i) {
  50. const x = getRandom(42.5, 42.8);
  51. assert(42.5 <= x, x);
  52. assert(x < 42.8, x);
  53. }
  54. }
  55. function testPoint() {
  56. {
  57. let p = new Point(0, 0);
  58. assert(p.x === 0, p);
  59. assert(p.y === 0, p);
  60. assert(p.distanceTo0Sqr() === 0, p);
  61. assert(p.distanceTo0() === 0, p);
  62. p = new Point(42, 0);
  63. assert(p.x === 42, p);
  64. assert(p.y === 0, p);
  65. assert(p.distanceTo0Sqr() === 42*42, p);
  66. assert(p.distanceTo0() === 42, p);
  67. p = new Point(0, 42);
  68. assert(p.x === 0, p);
  69. assert(p.y === 42, p);
  70. assert(p.distanceTo0Sqr() === 42*42, p);
  71. assert(p.distanceTo0() === 42, p);
  72. p = new Point(-42, 0);
  73. assert(p.x === -42, p);
  74. assert(p.y === 0, p);
  75. assert(p.distanceTo0Sqr() === 42*42, p);
  76. assert(p.distanceTo0() === 42, p);
  77. p = new Point(0, -42);
  78. assert(p.x === 0, p);
  79. assert(p.y === -42, p);
  80. assert(p.distanceTo0Sqr() === 42*42, p);
  81. assert(p.distanceTo0() === 42, p);
  82. }
  83. {
  84. const sqr = 2*42*42;
  85. const sqrt = Math.sqrt(sqr);
  86. assert(isFloatEqual(sqrt, 59,39696962), sqrt);
  87. let p = new Point(42, 42);
  88. assert(p.x === 42, p);
  89. assert(p.y === 42, p);
  90. assert(p.distanceTo0Sqr() === sqr, p);
  91. assert(p.distanceTo0() === sqrt, p);
  92. p = new Point(-42, 42);
  93. assert(p.x === -42, p);
  94. assert(p.y === 42, p);
  95. assert(p.distanceTo0Sqr() === sqr, p);
  96. assert(p.distanceTo0() === sqrt, p);
  97. p = new Point(-42, -42);
  98. assert(p.x === -42, p);
  99. assert(p.y === -42, p);
  100. assert(p.distanceTo0Sqr() === sqr, p);
  101. assert(p.distanceTo0() === sqrt, p);
  102. p = new Point(42, -42);
  103. assert(p.x === 42, p);
  104. assert(p.y === -42, p);
  105. assert(p.distanceTo0Sqr() === sqr, p);
  106. assert(p.distanceTo0() === sqrt, p);
  107. }
  108. {
  109. const p = new Point(-666, 365);
  110. assert(p.x === -666, p);
  111. assert(p.y === 365, p);
  112. assert(p.distanceTo0Sqr() === 666*666 + 365*365, p);
  113. assert(p.distanceTo0() === Math.sqrt(666*666 + 365*365), p);
  114. }
  115. for (let i = 0; i < 100; ++i) {
  116. const x = getRandom(-100, 100);
  117. const y = getRandom(-100, 100);
  118. const p = new Point(x, y);
  119. assert(p.x === x, x, p);
  120. assert(p.y === y, y, p);
  121. assert(isFloatEqual(p.distanceTo0Sqr(), x*x + y*y), x, y, p);
  122. assert(isFloatEqual(p.distanceTo0(), Math.sqrt(x*x + y*y)), x, y, p);
  123. }
  124. }
  125. function testSortedArrayBisectionLeft() {
  126. {
  127. const a = [];
  128. assert(sortedArrayBisectionLeft(a, 666) === 0);
  129. }
  130. {
  131. const a = [42];
  132. assert(sortedArrayBisectionLeft(a, 36) === 0);
  133. assert(sortedArrayBisectionLeft(a, 42) === 0);
  134. assert(sortedArrayBisectionLeft(a, 666) === 1);
  135. }
  136. {
  137. const a = [42, 666];
  138. assert(sortedArrayBisectionLeft(a, 36) === 0);
  139. assert(sortedArrayBisectionLeft(a, 42) === 0);
  140. assert(sortedArrayBisectionLeft(a, 666) === 1);
  141. assert(sortedArrayBisectionLeft(a, 1000) === 2);
  142. }
  143. {
  144. const a = [42, 666, 1000];
  145. assert(sortedArrayBisectionLeft(a, 36) === 0);
  146. assert(sortedArrayBisectionLeft(a, 42) === 0);
  147. assert(sortedArrayBisectionLeft(a, 666) === 1);
  148. assert(sortedArrayBisectionLeft(a, 1000) === 2);
  149. assert(sortedArrayBisectionLeft(a, 2000) === 3);
  150. }
  151. }
  152. function testSortedArrayInsert() {
  153. const a = [];
  154. sortedArrayInsert(a, 42);
  155. assert(arrayIsEquals(a, [42]));
  156. sortedArrayInsert(a, 36);
  157. assert(arrayIsEquals(a, [36, 42]));
  158. sortedArrayInsert(a, 666);
  159. assert(arrayIsEquals(a, [36, 42, 666]));
  160. sortedArrayInsert(a, 42);
  161. assert(arrayIsEquals(a, [36, 42, 42, 666]));
  162. sortedArrayInsert(a, 1000);
  163. assert(arrayIsEquals(a, [36, 42, 42, 666, 1000]));
  164. sortedArrayInsert(a, 500);
  165. assert(arrayIsEquals(a, [36, 42, 42, 500, 666, 1000]));
  166. sortedArrayInsert(a, 0);
  167. assert(arrayIsEquals(a, [0, 36, 42, 42, 500, 666, 1000]));
  168. }
  169. function runTest(fct, name) {
  170. console.log("--- BEGIN test", name, "---");
  171. fct();
  172. console.log("--- END test", name, "---");
  173. }
  174. window.addEventListener("load", function () {
  175. if (!isAssert()) {
  176. return;
  177. }
  178. console.log("===== BEGIN tests ======");
  179. runTest(testCompare, "compare");
  180. runTest(testGetRandom, "getRandom");
  181. runTest(testSortedArrayBisectionLeft, "sortedArrayBisectionLeft");
  182. runTest(testSortedArrayInsert, "sortedArrayInsert");
  183. runTest(testPoint, "class Point");
  184. console.log("===== END tests ======");
  185. add2Matchings();
  186. });
  187. }());