test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var vows = require('vows')
  2. , toposort = require('./index')
  3. , assert = require('assert')
  4. var suite = vows.describe('toposort')
  5. suite.addBatch(
  6. { 'acyclic graphs':
  7. { topic: function() {
  8. /*(read downwards)
  9. 6 3
  10. | |
  11. 5->2
  12. | |
  13. 4 1
  14. */
  15. return toposort(
  16. [ ["3", '2']
  17. , ["2", "1"]
  18. , ["6", "5"]
  19. , ["5", "2"]
  20. , ["5", "4"]
  21. ])
  22. }
  23. , 'should be sorted correctly': function(er, result) {
  24. assert.instanceOf(result, Array)
  25. var failed = [], passed
  26. // valid permutations
  27. ;[ [ '3','6','5','2','1','4' ]
  28. , [ '3','6','5','2','4','1' ]
  29. , [ '6','3','5','2','1','4' ]
  30. , [ '6','5','3','2','1','4' ]
  31. , [ '6','5','3','2','4','1' ]
  32. , [ '6','5', '4','3','2','1' ]
  33. ].forEach(function(solution) {
  34. try {
  35. assert.deepEqual(result, solution)
  36. passed = true
  37. }catch (e) {
  38. failed.push(e)
  39. }
  40. })
  41. if (!passed) {
  42. console.log(failed)
  43. throw failed[0];
  44. }
  45. }
  46. }
  47. , 'simple cyclic graphs':
  48. { topic: function() {
  49. /*
  50. foo<->bar
  51. */
  52. return toposort(
  53. [ ["foo", 'bar']
  54. , ["bar", "foo"]// cyclic dependecy
  55. ])
  56. }
  57. , 'should throw an exception': function(_, val) {
  58. assert.instanceOf(val, Error)
  59. }
  60. }
  61. , 'complex cyclic graphs':
  62. { topic: function() {
  63. /*
  64. foo
  65. |
  66. bar<-john
  67. | ^
  68. ron->tom
  69. */
  70. return toposort(
  71. [ ["foo", 'bar']
  72. , ["bar", "ron"]
  73. , ["john", "bar"]
  74. , ["tom", "john"]
  75. , ["ron", "tom"]// cyclic dependecy
  76. ])
  77. }
  78. , 'should throw an exception': function(_, val) {
  79. assert.instanceOf(val, Error)
  80. }
  81. }
  82. , 'unknown nodes in edges':
  83. { topic: function() {
  84. return toposort.array(['bla']
  85. [ ["foo", 'bar']
  86. , ["bar", "ron"]
  87. , ["john", "bar"]
  88. , ["tom", "john"]
  89. , ["ron", "tom"]
  90. ])
  91. }
  92. , 'should throw an exception': function(_, val) {
  93. assert.instanceOf(val, Error)
  94. }
  95. }
  96. , 'triangular dependency':
  97. { topic: function() {
  98. /*
  99. a-> b
  100. | /
  101. c<-
  102. */
  103. return toposort([
  104. ['a', 'b']
  105. , ['a', 'c']
  106. , ['b', 'c']
  107. ]);
  108. }
  109. , 'shouldn\'t throw an error': function(er, result) {
  110. assert.deepEqual(result, ['a', 'b', 'c'])
  111. }
  112. }
  113. , 'toposort.array':
  114. { topic: function() {
  115. return toposort.array(['d', 'c', 'a', 'b'], [['a','b'],['b','c']])
  116. }
  117. , 'should include unconnected nodes': function(er, result){
  118. var i = result.indexOf('d')
  119. assert(i >= 0)
  120. result.splice(i, 1)
  121. assert.deepEqual(result, ['a', 'b', 'c'])
  122. }
  123. }
  124. , 'toposort.array mutation':
  125. { topic: function() {
  126. var array = ['d', 'c', 'a', 'b']
  127. toposort.array(array, [['a','b'],['b','c']])
  128. return array
  129. }
  130. , 'should not mutate its arguments': function(er, result){
  131. assert.deepEqual(result, ['d', 'c', 'a', 'b'])
  132. }
  133. }
  134. , 'giant graphs':
  135. {
  136. topic: function() {
  137. var graph = []
  138. , nodeCount = 10000
  139. for (var i = 0; i < nodeCount; i++) {
  140. graph.push([i, i + 1])
  141. }
  142. return graph
  143. }
  144. , 'should sort quickly': function(er, result){
  145. var start = (new Date).getTime()
  146. var sorted = toposort(result)
  147. var end = (new Date).getTime()
  148. var elapsedSeconds = (end - start) / 1000
  149. assert(elapsedSeconds < 1)
  150. }
  151. }
  152. })
  153. .run(null, function() {
  154. (suite.results.broken+suite.results.errored) > 0 && process.exit(1)
  155. process.exit(0)
  156. })