My dotfiles. Period.

topological-sort.vim 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "=============================================================================
  2. " $Id: topological-sort.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  3. " File: tests/lh/topological-sort.vim {{{1
  4. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5. " <URL:http://code.google.com/p/lh-vim/>
  6. " License: GPLv3 with exceptions
  7. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  8. " Version: 3.0.0
  9. " Created: 17th Apr 2008
  10. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  11. "------------------------------------------------------------------------
  12. " Description: «description»
  13. "
  14. "------------------------------------------------------------------------
  15. " Installation: «install details»
  16. " History: «history»
  17. " TODO: «missing features»
  18. " }}}1
  19. "=============================================================================
  20. let s:cpo_save=&cpo
  21. set cpo&vim
  22. "------------------------------------------------------------------------
  23. UTSuite [lh-vim-lib] topological sort
  24. " Fully defineds DAGs {{{1
  25. " A Direct Acyclic Graph {{{2
  26. let s:dag1 = {}
  27. let s:dag1[7] = [11, 8]
  28. let s:dag1[5] = [11]
  29. let s:dag1[3] = [8, 10]
  30. let s:dag1[11] = [2, 9, 10]
  31. let s:dag1[8] = [9]
  32. " A Direct Cyclic Graph {{{2
  33. let s:dcg1 = deepcopy(s:dag1)
  34. let s:dcg1[9] = [11]
  35. " Check routine: are the elements correctly sorted? {{{2
  36. function! s:DoTestOrder(elements)
  37. Assert! len(a:elements) == 8
  38. Assert index(a:elements, 7) < index(a:elements, 11)
  39. Assert index(a:elements, 7) < index(a:elements, 8)
  40. Assert index(a:elements, 5) < index(a:elements, 11)
  41. Assert index(a:elements, 3) < index(a:elements, 8)
  42. Assert index(a:elements, 3) < index(a:elements, 10)
  43. Assert index(a:elements, 11) < index(a:elements, 2)
  44. Assert index(a:elements, 11) < index(a:elements, 9)
  45. Assert index(a:elements, 11) < index(a:elements, 10)
  46. Assert index(a:elements, 8) < index(a:elements, 9)
  47. endfunction
  48. " Test DAG1 {{{2
  49. function! s:TestDAG_depth()
  50. let res = lh#graph#tsort#depth(s:dag1, [3, 5,7])
  51. call s:DoTestOrder(res)
  52. echo "D(s:dag1)=".string(res)
  53. endfunction
  54. function! s:TestDAG_breadth()
  55. let res = lh#graph#tsort#breadth(s:dag1, [3, 5,7])
  56. call s:DoTestOrder(res)
  57. echo "B(s:dag1)=".string(res)
  58. endfunction
  59. " Test DCG1 {{{2
  60. function! s:TestDCG_depth()
  61. let expr = 'lh#graph#tsort#depth('.string(s:dcg1).', [3, 5,7])'
  62. Assert should#throw(expr, 'Tsort: cyclic graph detected')
  63. endfunction
  64. function! s:TestDCG_breadth()
  65. let expr = 'lh#graph#tsort#breadth('.string(s:dcg1).', [3, 5,7])'
  66. Assert should#throw(expr, 'Tsort: cyclic graph detected')
  67. endfunction
  68. " Lazzy Evaluated DAGs {{{1
  69. " Emulated lazzyness {{{2
  70. " The time-consumings evaluation function
  71. let s:called = 0
  72. function! Fetch(node)
  73. let s:called += 1
  74. return has_key(s:dag1, a:node) ? (s:dag1[a:node]) : []
  75. endfunction
  76. " Test Fetch on a DAG {{{2
  77. function! s:TestDAG_fetch()
  78. let s:called = 0
  79. let res = lh#graph#tsort#depth(function('Fetch'), [3,5,7])
  80. call s:DoTestOrder(res)
  81. echo "D(fetch)=".string(res)
  82. echo "Fetch has been evaluated ".s:called." times / ".len(res)
  83. Assert s:called == len(res)
  84. endfunction
  85. " Setup/Teardown functions {{{1
  86. " display the test name before each assertion
  87. function! s:Setup()
  88. if exists('g:UT_print_test')
  89. let s:old_print_test = g:UT_print_test
  90. endif
  91. let g:UT_print_test = 1
  92. endfunction
  93. function! s:Teardown()
  94. if exists('s:old_print_test')
  95. let g:UT_print_test = s:old_print_test
  96. unlet s:old_print_test
  97. else
  98. unlet g:UT_print_test
  99. endif
  100. endfunction
  101. " }}}1
  102. let &cpo=s:cpo_save
  103. "=============================================================================
  104. " vim600: set fdm=marker: