fork download
  1. # coding: utf-8
  2. require 'minitest/autorun'
  3.  
  4. F=
  5. ->i{w=i.index(?\n)+1
  6. t=[i.index(/[^ _] *\n\z/)]
  7. a=->x,c{(i[x]==c||i[x]==?_)&&t<<x}
  8. ((x=t.pop)&&(s=x-w;c=i[x])<?0?(a[s+1,?/];a[s,?\\]):c<?]?(a[s-1,?\\];a[s,?/]):c<?`?(a[x-1,?\\];a[x+1,?/]):a[s,?|]
  9. i[x]=' ')while t!=[]
  10. !i[/\S/]}
  11.  
  12.  
  13. describe '#F' do
  14. # Valid trees
  15. #############
  16.  
  17. def test_young_tree
  18. assert F['|
  19. ']
  20. end
  21.  
  22. def test_valid_tree
  23. assert F[' _
  24. \/ /
  25. \_/
  26. |
  27. |
  28. ']
  29. end
  30.  
  31. def test_valid_italic_tree
  32. assert F['
  33. / / \/
  34. \ \____/
  35. \/
  36. /
  37. /
  38. ']
  39. end
  40.  
  41. def test_valid_cat_tree
  42. assert F['
  43. \___/
  44. / \
  45. \___/
  46. |
  47. |
  48. ']
  49. end
  50.  
  51. def test_valid_stair_tree
  52. assert F['
  53. __/
  54. _/
  55. /
  56. ']
  57. end
  58.  
  59. def test_valid_old_tree
  60. assert F['
  61. ____
  62. \ ___
  63. \ \/
  64. \/\_____/
  65. \/ \/
  66. \__/
  67. |
  68. |
  69. ']
  70. end
  71.  
  72. # Invalid trees
  73. ###############
  74.  
  75. def test_invalid_reachable_only_with_downward_route
  76. refute F['
  77. \/
  78. \_______/
  79. \__ /
  80. | \_/
  81. |
  82. ']
  83. end
  84.  
  85. def test_invalid_multiple_roots
  86. refute F['_']
  87. end
  88.  
  89. def test_invalid_multiple_root_characters
  90. refute F['\/
  91. ']
  92. end
  93.  
  94. def test_invalid_multiple_roots
  95. refute F['/\
  96. ']
  97. end
  98.  
  99. def test_invalid_unreachable_part
  100. refute F['
  101. |
  102.  
  103. |
  104. ']
  105. end
  106.  
  107. def test_invalid_unreachable_parts
  108. refute F['
  109. __/
  110. /
  111. |
  112. ']
  113. end
  114.  
  115. def test_invalid_multiple_roots_2
  116. refute F['
  117. \____/
  118. | |
  119. ']
  120. end
  121.  
  122. def test_invalid_unreachable_parts_2
  123. refute F['
  124. _\__/
  125. |
  126. ']
  127. end
  128. end
  129.  
Success #stdin #stdout 0.05s 8544KB
stdin
Standard input is empty
stdout
Run options: --seed 34692

# Running tests:

.............

Finished tests in 0.001753s, 7415.0633 tests/s, 7415.0633 assertions/s.

13 tests, 13 assertions, 0 failures, 0 errors, 0 skips