home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ruby164.zip / rbemx164.zip / ruby / share / doc / textbuf-0.3.8 / test.rb < prev   
Text File  |  2001-03-15  |  4KB  |  325 lines

  1. #!/usr/local/bin/ruby -d -Ke
  2.  
  3. require 'textbuf'
  4.  
  5. puts
  6.  
  7. def ev( code, bind = nil )
  8.   eval code, bind || TOPLEVEL_BINDING
  9. end
  10.  
  11.  
  12. $assert_n = 1
  13.  
  14. def chk( code, bind = nil )
  15.   unless ev( code, bind ) then
  16.     raise ArgumentError, "\n\ntest #{$assert_n} fail: #{code}"
  17.   end
  18.   $assert_n += 1
  19. end
  20.  
  21. def try( code, bind = nil )
  22.   print "trying test #{$assert_n}: #{code} ..."
  23.   ev code, bind
  24.   puts "ok"
  25.   $assert_n += 1
  26. end
  27.  
  28. $stdout.sync = true
  29.  
  30. ###
  31. ###
  32. ###
  33.  
  34. puts 'fundamental test'
  35.  
  36. buf = TextBuffer.new
  37. try "buf.inspect"
  38.  
  39. m = buf.new_mark
  40. try "m.inspect"
  41.  
  42. try "GC.start"
  43.  
  44. #----
  45.  
  46. puts 'insertion test'
  47.  
  48. str1 = 'string1'
  49. str2 = 'string2'
  50. str3 = 'string3'
  51. m.insert str1
  52. m.insert str2
  53. m.insert str3
  54.  
  55. $whole = str1 + str2 + str3
  56.  
  57. chk 'm.index == $whole.size'
  58. chk "m.index == m.byte_index"
  59.  
  60. #----
  61.  
  62. puts 'back test'
  63.  
  64. m.back 1
  65. chk 'm.index == $whole.size - 1'
  66. chk 'm.index == m.byte_index'
  67.  
  68. m.back 1
  69. chk 'm.index == $whole.size - 2'
  70. chk "m.index == m.byte_index"
  71.  
  72. m.back 1
  73. chk 'm.index == $whole.size - 3'
  74. chk "m.index == m.byte_index"
  75.  
  76. #----
  77.  
  78. puts 'forward test'
  79.  
  80. m.forward 1
  81. chk 'm.index == $whole.size - 2'
  82. chk "m.index == m.byte_index"
  83.  
  84. m.forward 1
  85. chk 'm.index == $whole.size - 1'
  86. chk "m.index == m.byte_index"
  87.  
  88. m.forward 1
  89. chk 'm.index == $whole.size'
  90. chk "m.index == m.byte_index"
  91.  
  92. #----
  93.  
  94. puts 'motion test (big)'
  95.  
  96. m.forward 1000
  97. m.back    1000
  98. m.forward 1000
  99. m.back    1000
  100. m.forward 1000
  101. m.back    1000
  102.  
  103. chk 'm.index == 0'
  104. chk "m.index == m.byte_index"
  105.  
  106. #----
  107.  
  108. puts 'substr test'
  109.  
  110. chk 'm[1000] == $whole'
  111. chk 'm[5] == $whole[0,5]'
  112.  
  113. #---
  114.  
  115. puts 'mark list test'
  116.  
  117. m2 = buf.new_mark
  118. chk 'm2.index == 0'
  119. chk "m2.index == m2.byte_index"
  120.  
  121. m2.forward 1
  122. chk 'm2.index == 1'
  123. chk "m2.index == m2.byte_index"
  124.  
  125. m2.forward 1
  126. chk 'm2.index == 2'
  127. chk "m2.index == m2.byte_index"
  128.  
  129. m2.forward 1
  130. chk 'm2.index == 3'
  131. chk "m2.index == m2.byte_index"
  132.  
  133. chk 'm2[5] == $whole[3,5]'
  134.  
  135. m3 = buf.new_mark(0)
  136. chk 'm3.index == 0'
  137. chk "m3.index == m3.byte_index"
  138.  
  139. m2.back 1
  140. chk 'm2.index == 2'
  141. chk "m2.index == m2.byte_index"
  142.  
  143. m2.back 1
  144. chk 'm2.index == 1'
  145. chk "m2.index == m2.byte_index"
  146.  
  147. m2.back 1
  148. chk 'm2.index == 0'
  149. chk "m2.index == m2.byte_index"
  150.  
  151. chk 'm2[5] == $whole[0,5]'
  152.  
  153.  
  154. #----
  155.  
  156. puts 'RO each test'
  157.  
  158. m.head
  159. s = ''
  160. m.each_line_ro do |line|
  161.   s << line
  162.   try "GC.start", binding
  163. end
  164. chk 's == $whole'
  165. try "GC.start"
  166.  
  167.  
  168. #----
  169.  
  170. puts 'insert again'
  171.  
  172. m2.forward 1000
  173.  
  174. 1.times do
  175.   0.upto( 9 ) do |i|
  176.     m2.insert i.to_s
  177.     m2.back 1
  178.   end
  179. end
  180. m2.back 1000
  181.  
  182. chk "m2[1000] == buf.string"
  183.  
  184. chk "m2.line == 1"
  185.  
  186.  
  187. #----
  188.  
  189. puts 'motion test (2)'
  190.  
  191. m2.head
  192. chk "m2.index == 0"
  193. m2.tail
  194. chk "m2.index == buf.size"
  195. m2.head
  196. chk "m2.index == 0"
  197.  
  198. chk "m2.line == 1"
  199.  
  200. #----
  201.  
  202. puts 'delete test (2)'
  203.  
  204. buf.size.times do
  205.   m2.delete 1
  206. end
  207. chk "buf.size == 0"
  208.  
  209. chk "m2.line == 1"
  210.  
  211.  
  212. #----
  213.  
  214. puts 'freeze check'
  215.  
  216. mm = buf.new_mark
  217. # mm.insert 'str' * 10
  218. mm.freeze
  219. freeze_check = true
  220. begin
  221.   mm.move 1
  222.   freeze_check = false
  223. rescue
  224.   puts "frozen check worked: #{$!}"
  225. end
  226. chk 'freeze_check'
  227.  
  228.  
  229. #----
  230.  
  231. puts 'line test'
  232.  
  233. m.head
  234. m.insert "line\n" * 10
  235. chk 'm.line == 11'
  236.  
  237. m.prev_line
  238. chk 'm.line == 10'
  239.  
  240. m.prev_line
  241. m.prev_line
  242. chk 'm.line == 8'
  243.  
  244. m.prev_line
  245. m.next_line
  246. chk 'm.line == 8'
  247.  
  248. m.line -= 3
  249. chk 'm.line == 5'
  250.  
  251. m.line += 2
  252. chk 'm.line == 7'
  253.  
  254. m.head
  255. chk 'm.index == 0'
  256. chk 'm.line == 1'
  257.  
  258. #----
  259.  
  260. puts 'match test'
  261.  
  262. chk 'buf.index(/line/) == 0'
  263. chk "buf.index('line') == 0"
  264.  
  265. #----
  266.  
  267. puts 'etc test'
  268.  
  269. buf.clear
  270. chk 'buf.size == 0'
  271. chk 'm.index == 0'
  272.  
  273. #----
  274.  
  275. puts 'japanese test'
  276.  
  277. buf.clear
  278. m.insert 'ñóñññªñ¿ñ¬ñ½ñ¡ñ»ñ▒ñ│'
  279. chk 'buf.size == 10'
  280. chk 'buf.byte_size == 20'
  281.  
  282. m.head
  283. chk 'm.index == 0'
  284. m.forward 1
  285. chk 'm.index == 1'
  286. chk 'm.byte_index == 2'
  287. m.forward 1
  288. chk 'm.index == 2'
  289. chk 'm.byte_index == 4'
  290. m.back 1
  291. chk 'm.index == 1'
  292. chk 'm.byte_index == 2'
  293.  
  294. chk "m[1] == 'ññ'"
  295. chk "m[2] == 'ñññª'"
  296.  
  297. m.delete 1
  298. chk "m[1] == 'ñª'"
  299. chk "m[2] == 'ñªñ¿'"
  300.  
  301. #----
  302.  
  303. puts 'gc test (final)'
  304.  
  305. m2 = nil
  306. try 'GC.start'
  307.  
  308. buf = nil
  309. try 'GC.start'
  310.  
  311. m = nil
  312. try 'GC.start'
  313.  
  314. #----
  315.  
  316. puts 'insertion test (heavy)'
  317.  
  318. buf = TextBuffer.new
  319. a = 'a'
  320. 10000.times { buf << a }
  321.  
  322. #----
  323.  
  324. print "\n\n    ok\n\n\n"
  325.