PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/queue.rb

https://bitbucket.org/tagoh/prune
Ruby | 215 lines | 157 code | 42 blank | 16 comment | 2 complexity | 8c38223293dbccc45dee00f4dd6428a2 MD5 | raw file
Possible License(s): GPL-2.0
  1. # queue.rb
  2. # Copyright (C) 2009 Akira TAGOH
  3. # Authors:
  4. # Akira TAGOH <akira@tagoh.org>
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place - Suite 330,
  16. # Boston, MA 02111-1307, USA.
  17. require 'rubygems'
  18. gem 'test-unit'
  19. require 'test/unit/testcase'
  20. require 'prune/queue'
  21. class TestPRUNE__Queue < Test::Unit::TestCase
  22. def setup
  23. end # def setup
  24. def teardown
  25. end # def teardown
  26. def test_clear
  27. t = PRUNE::Queue.new
  28. assert_nothing_raised {t.push(1)}
  29. assert_equal("[]", t.clear.inspect)
  30. assert_equal("[]", t.clear.inspect)
  31. end # def test_clear
  32. def test_delete
  33. t = PRUNE::Queue.new
  34. assert_nothing_raised {t.push("foo")}
  35. assert_nothing_raised {t.push(1)}
  36. assert_equal("foo", t.delete("foo"))
  37. assert_equal("[1]", t.inspect)
  38. end # def test_delete
  39. def test_delete_if
  40. t = PRUNE::Queue.new
  41. assert_nothing_raised {t.push(true)}
  42. assert_nothing_raised {t.push(false)}
  43. assert_nothing_raised {t.push(true)}
  44. assert_nothing_raised {t.push(false)}
  45. assert_nothing_raised {t.delete_if {|x| x}}
  46. assert_equal("[false, false]", t.inspect)
  47. end # def test_delete_if
  48. def test_each
  49. t = PRUNE::Queue.new
  50. assert_nothing_raised {t.push("foo")}
  51. assert_nothing_raised {t.push(1)}
  52. v = []
  53. assert_nothing_raised {t.each {|x|v<<x}}
  54. assert_equal(["foo", 1], v)
  55. end # def test_each
  56. def test_empty?
  57. t = PRUNE::Queue.new
  58. assert_equal(true, t.empty?)
  59. assert_nothing_raised {t.push(1)}
  60. assert_equal(false, t.empty?)
  61. assert_nothing_raised {t.shift}
  62. assert_equal(true, t.empty?)
  63. end # def test_empty?
  64. def test_include
  65. t = PRUNE::Queue.new
  66. assert_nothing_raised {t.push("foo")}
  67. assert_nothing_raised {t.push(1)}
  68. assert_equal(true, t.include?("foo"))
  69. assert_equal(true, t.include?(1))
  70. assert_equal(false, t.include?("fOO"))
  71. assert_equal(false, t.include?("1"))
  72. end # def test_include
  73. def test_push
  74. t = PRUNE::Queue.new
  75. assert_equal("[nil]", t.push(nil).inspect)
  76. assert_equal("[nil, 0]", t.push(0).inspect)
  77. assert_equal("[nil, 0, [1, 2, 3]]", t.push([1, 2, 3]).inspect)
  78. assert_equal("[nil, 0, [1, 2, 3], {\"foo\"=>0}]", t.push({"foo"=>0}).inspect)
  79. t = PRUNE::Queue.new
  80. assert_equal("[[1]]", t.push([1]).inspect)
  81. end # def test_push
  82. def test_reverse
  83. t = PRUNE::Queue.new
  84. assert_nothing_raised {t.push("foo")}
  85. assert_nothing_raised {t.push(1)}
  86. assert_equal('[1, "foo"]', t.reverse.inspect)
  87. end # def test_reverse
  88. def test_reverse_each
  89. t = PRUNE::Queue.new
  90. assert_nothing_raised {t.push("foo")}
  91. assert_nothing_raised {t.push(1)}
  92. v = []
  93. assert_nothing_raised {t.reverse_each{|x|v<<x}}
  94. assert_equal([1, "foo"], v)
  95. end # def test_reverse_each
  96. def test_shift
  97. t = PRUNE::Queue.new
  98. assert_nothing_raised {t.push(0)}
  99. assert_equal(0, t.shift)
  100. assert_nothing_raised {t.push("abc")}
  101. assert_equal("abc", t.shift)
  102. assert_nothing_raised {t.push([1, 2, 3])}
  103. assert_equal([1, 2, 3], t.shift)
  104. assert_nothing_raised {t.push([1])}
  105. assert_equal([1], t.shift)
  106. assert_nothing_raised {t.push({"foo"=>1})}
  107. assert_equal({"foo"=>1}, t.shift)
  108. end # def test_shift
  109. def test_size
  110. t = PRUNE::Queue.new
  111. assert_nothing_raised {t.push("foo")}
  112. assert_nothing_raised {t.push(1)}
  113. assert_equal(2, t.size)
  114. end # def test_size
  115. def test_sort
  116. t = PRUNE::Queue.new
  117. assert_nothing_raised {t.push(1)}
  118. assert_nothing_raised {t.push("foo")}
  119. assert_raises(ArgumentError, "comparison of Fixnum with String failed") {t.sort}
  120. t = PRUNE::Queue.new
  121. assert_nothing_raised {t.push(5)}
  122. assert_nothing_raised {t.push(2)}
  123. assert_nothing_raised {t.push(4)}
  124. assert_nothing_raised {t.push(1)}
  125. t2 = nil
  126. assert_nothing_raised {t2 = t.sort}
  127. assert_equal('[1, 2, 4, 5]', t2.inspect)
  128. assert_equal(true, t2.kind_of?(PRUNE::Queue))
  129. end # def test_sort
  130. def test_sort_
  131. t = PRUNE::Queue.new
  132. assert_nothing_raised {t.push(1)}
  133. assert_nothing_raised {t.push("foo")}
  134. assert_raises(ArgumentError, "comparison of Fixnum with String failed") {t.sort!}
  135. t = PRUNE::Queue.new
  136. assert_nothing_raised {t.push(5)}
  137. assert_nothing_raised {t.push(2)}
  138. assert_nothing_raised {t.push(4)}
  139. assert_nothing_raised {t.push(1)}
  140. assert_nothing_raised {t.sort!}
  141. assert_equal('[1, 2, 4, 5]', t.inspect)
  142. assert_equal(true, t.kind_of?(PRUNE::Queue))
  143. end # def test_sort_
  144. def test_to_a
  145. t = PRUNE::Queue.new
  146. assert_nothing_raised {t.push(1)}
  147. assert_nothing_raised {t.push("foo")}
  148. assert_equal([1, "foo"], t.to_a)
  149. end # def test_to_a
  150. def test_unshift
  151. t = PRUNE::Queue.new
  152. assert_nothing_raised {t.push(1)}
  153. assert_nothing_raised {t.push("foo")}
  154. v = nil
  155. assert_nothing_raised {v = t.shift}
  156. assert_nothing_raised {t.unshift(v)}
  157. assert_equal([1, "foo"], t.to_a)
  158. end # def test_unshift
  159. def test_set_array
  160. t = PRUNE::Queue.new
  161. assert_nothing_raised {t[1] = "foo"}
  162. assert_nothing_raised {t[2] = "bar"}
  163. assert_equal(nil, t[0])
  164. assert_equal([nil, "foo", "bar"], t.to_a)
  165. end # def test_set_array
  166. end # class TestPRUNE__Queue
  167. if $0 == __FILE__ then
  168. begin
  169. require 'main'
  170. rescue LoadError
  171. require 'tests/main'
  172. end
  173. end