Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. // Testing setup.
  2. mocha.setup('bdd');
  3. var expect = chai.expect;
  4.  
  5. // The implementation.
  6. function Obj() {
  7. this.links = [];
  8. };
  9.  
  10. Obj.prototype.linkTo = function(to) {
  11. if (this.links.indexOf(to) === -1) {
  12. this.links.push(to);
  13. }
  14. };
  15.  
  16. Obj.prototype.isLinkedTo = function(to) {
  17. for (var i = 0; i < this.links.length; i++) {
  18. var link = this.links[i];
  19. if (link !== to && link === this) {
  20. continue;
  21. }
  22. if (link === to || link.isLinkedTo(to)) {
  23. return true;
  24. }
  25. }
  26. return false;
  27. };
  28.  
  29. // The tests.
  30. describe("Obj", function() {
  31.  
  32. it("can link to itself", function() {
  33. var foo = new Obj;
  34. foo.linkTo(foo);
  35. expect(foo.isLinkedTo(foo)).to.equal(true);
  36. });
  37.  
  38. it("does not link to itself", function() {
  39. var foo = new Obj;
  40. expect(foo.isLinkedTo(foo)).to.equal(false);
  41. });
  42.  
  43. it("has unidirectional link to neighbour", function() {
  44. var foo = new Obj;
  45. var bar = new Obj;
  46. bar.linkTo(foo);
  47. expect(bar.isLinkedTo(foo)).to.equal(true);
  48. expect(foo.isLinkedTo(bar)).to.equal(false);
  49. });
  50.  
  51. it("has neighbours with connections to themselves", function() {
  52. var foo = new Obj;
  53. var bar = new Obj;
  54. var baz = new Obj;
  55.  
  56. // Connect the Objs to themselves.
  57. foo.linkTo(foo);
  58. bar.linkTo(bar);
  59. baz.linkTo(baz);
  60.  
  61. // Connect baz => bar => foo.
  62. baz.linkTo(bar);
  63. bar.linkTo(foo);
  64.  
  65. expect(baz.isLinkedTo(foo)).to.equal(true);
  66. expect(baz.isLinkedTo(bar)).to.equal(true);
  67. expect(bar.isLinkedTo(foo)).to.equal(true);
  68. });
  69.  
  70. it("can be a cyclic graph", function() {
  71. var foo = new Obj;
  72. var bar = new Obj;
  73. var baz = new Obj;
  74.  
  75. // Connect the nodes baz => bar => foo => baz.
  76. baz.linkTo(bar);
  77. bar.linkTo(foo);
  78. foo.linkTo(baz);
  79.  
  80. expect(baz.isLinkedTo(foo)).to.equal(true);
  81. expect(baz.isLinkedTo(bar)).to.equal(true);
  82. expect(baz.isLinkedTo(baz)).to.equal(true);
  83. });
  84.  
  85. it("can have neighbours in cyclic graph", function() {
  86. var foo = new Obj;
  87. var bar = new Obj;
  88. var baz = new Obj;
  89.  
  90. // Connect the nodes baz => bar <=> foo.
  91. baz.linkTo(bar);
  92. bar.linkTo(foo);
  93. foo.linkTo(bar);
  94.  
  95. expect(baz.isLinkedTo(foo)).to.equal(true);
  96. expect(baz.isLinkedTo(bar)).to.equal(true);
  97. expect(baz.isLinkedTo(baz)).to.equal(false);
  98. });
  99.  
  100.  
  101. it("can have a cycle of more than 2 Objs", function() {
  102. var foo = new Obj;
  103. var bar = new Obj;
  104. var baz = new Obj;
  105. var qux = new Obj;
  106.  
  107. // Connect the nodes baz => bar => foo => qux => bar.
  108. baz.linkTo(bar);
  109. bar.linkTo(foo);
  110. foo.linkTo(qux);
  111. qux.linkTo(bar);
  112.  
  113. expect(qux.isLinkedTo(baz)).to.equal(false);
  114. expect(baz.isLinkedTo(foo)).to.equal(true);
  115. expect(baz.isLinkedTo(bar)).to.equal(true);
  116. expect(baz.isLinkedTo(qux)).to.equal(true);
  117. expect(baz.isLinkedTo(baz)).to.equal(false);
  118. });
  119.  
  120. it("can have a cycle of more than 5 Objs", function() {
  121. var foo = new Obj;
  122. var bar = new Obj;
  123. var baz = new Obj;
  124. var qux = new Obj;
  125. var quux = new Obj;
  126. var quuz = new Obj;
  127. var corge = new Obj;
  128.  
  129. // Connect the nodes baz => bar => foo => qux => bar.
  130. baz.linkTo(bar);
  131. bar.linkTo(foo);
  132. foo.linkTo(qux);
  133. qux.linkTo(bar);
  134. bar.linkTo(quux);
  135. quux.linkTo(quuz);
  136. quuz.linkTo(corge);
  137.  
  138. expect(qux.isLinkedTo(baz)).to.equal(false);
  139. expect(baz.isLinkedTo(foo)).to.equal(true);
  140. expect(baz.isLinkedTo(bar)).to.equal(true);
  141. expect(baz.isLinkedTo(qux)).to.equal(true);
  142. expect(baz.isLinkedTo(baz)).to.equal(false);
  143. expect(baz.isLinkedTo(quux)).to.equal(true);
  144. expect(baz.isLinkedTo(quuz)).to.equal(true);
  145. expect(baz.isLinkedTo(corge)).to.equal(true);
  146. });
  147.  
  148.  
  149. });
  150.  
  151. // Run the suite.
  152. mocha.run();
  153.  
  154.  
  155. passes: 5failures: 3duration: 0.04s
  156. Obj
  157. can link to itself‣
  158. does not link to itself‣
  159. has unidirectional link to neighbour‣
  160. has neighbours with connections to themselves‣
  161. can be a cyclic graph‣
  162. can have neighbours in cyclic graph‣
  163. RangeError: Maximum call stack size exceeded
  164. at Obj.isLinkedTo (:50:36)
  165. at Obj.isLinkedTo (:56:29)
  166. at Obj.isLinkedTo (:56:29)
  167. at Obj.isLinkedTo (:56:29)
  168. at Obj.isLinkedTo (:56:29)
  169. at Obj.isLinkedTo (:56:29)
  170. at Obj.isLinkedTo (:56:29)
  171. at Obj.isLinkedTo (:56:29)
  172. at Obj.isLinkedTo (:56:29)
  173. at Obj.isLinkedTo (:56:29)
  174. can have neighbours in cyclic graph‣
  175. RangeError: Maximum call stack size exceeded
  176. at Obj.isLinkedTo (:50:36)
  177. at Obj.isLinkedTo (:56:29)
  178. at Obj.isLinkedTo (:56:29)
  179. at Obj.isLinkedTo (:56:29)
  180. at Obj.isLinkedTo (:56:29)
  181. at Obj.isLinkedTo (:56:29)
  182. at Obj.isLinkedTo (:56:29)
  183. at Obj.isLinkedTo (:56:29)
  184. at Obj.isLinkedTo (:56:29)
  185. at Obj.isLinkedTo (:56:29)
  186. can have a cycle of more than 2 Objs‣
  187. RangeError: Maximum call stack size exceeded
  188. at Obj.isLinkedTo (:50:36)
  189. at Obj.isLinkedTo (:56:29)
  190. at Obj.isLinkedTo (:56:29)
  191. at Obj.isLinkedTo (:56:29)
  192. at Obj.isLinkedTo (:56:29)
  193. at Obj.isLinkedTo (:56:29)
  194. at Obj.isLinkedTo (:56:29)
  195. at Obj.isLinkedTo (:56:29)
  196. at Obj.isLinkedTo (:56:29)
  197. at Obj.isLinkedTo (:56:29)
  198. can have a cycle of more than 2 Objs‣
  199. RangeError: Maximum call stack size exceeded
  200. at Obj.isLinkedTo (:50:36)
  201. at Obj.isLinkedTo (:56:29)
  202. at Obj.isLinkedTo (:56:29)
  203. at Obj.isLinkedTo (:56:29)
  204. at Obj.isLinkedTo (:56:29)
  205. at Obj.isLinkedTo (:56:29)
  206. at Obj.isLinkedTo (:56:29)
  207. at Obj.isLinkedTo (:56:29)
  208. at Obj.isLinkedTo (:56:29)
  209. at Obj.isLinkedTo (:56:29)
  210. can have a cycle of more than 5 Objs‣
  211. RangeError: Maximum call stack size exceeded
  212. at Obj.isLinkedTo (:50:36)
  213. at Obj.isLinkedTo (:56:29)
  214. at Obj.isLinkedTo (:56:29)
  215. at Obj.isLinkedTo (:56:29)
  216. at Obj.isLinkedTo (:56:29)
  217. at Obj.isLinkedTo (:56:29)
  218. at Obj.isLinkedTo (:56:29)
  219. at Obj.isLinkedTo (:56:29)
  220. at Obj.isLinkedTo (:56:29)
  221. at Obj.isLinkedTo (:56:29)
  222. can have a cycle of more than 5 Objs‣
  223. RangeError: Maximum call stack size exceeded
  224. at Obj.isLinkedTo (:50:36)
  225. at Obj.isLinkedTo (:56:29)
  226. at Obj.isLinkedTo (:56:29)
  227. at Obj.isLinkedTo (:56:29)
  228. at Obj.isLinkedTo (:56:29)
  229. at Obj.isLinkedTo (:56:29)
  230. at Obj.isLinkedTo (:56:29)
  231. at Obj.isLinkedTo (:56:29)
  232. at Obj.isLinkedTo (:56:29)
  233. at Obj.isLinkedTo (:56:29)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement