PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/cihash.rb

https://bitbucket.org/tagoh/prune
Ruby | 249 lines | 207 code | 26 blank | 16 comment | 3 complexity | 9a5021446e877e1962c64605974c3bdf MD5 | raw file
Possible License(s): GPL-2.0
  1. # cihash.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 'rbconfig'
  18. require 'rubygems'
  19. gem 'test-unit'
  20. require 'test/unit/testcase'
  21. require 'prune/cihash'
  22. class TestPRUNE__CiHash < Test::Unit::TestCase
  23. def setup
  24. end # def setup
  25. def teardown
  26. end # def teardown
  27. def test_functional
  28. t = PRUNE::CiHash.new
  29. assert_nothing_raised {t["Foo"] = 1}
  30. assert_nothing_raised {t[1] = "foo"}
  31. assert_nothing_raised {t[:Foo] = :bar}
  32. end # def test_functional
  33. def test_set_array
  34. t = PRUNE::CiHash.new
  35. assert_nothing_raised {t["Foo"] = 1}
  36. assert_nothing_raised {t[1] = "foo"}
  37. assert_nothing_raised {t[:Foo] = :bar}
  38. assert_equal(1, t["Foo"])
  39. assert_equal(1, t["foo"])
  40. assert_equal("foo", t[1])
  41. assert_equal(:bar, t[:Foo])
  42. assert_equal(:bar, t[:foo])
  43. assert_equal(nil, t["1"])
  44. end # def test_set_array
  45. def test_clear
  46. t = PRUNE::CiHash.new
  47. assert_nothing_raised {t["Foo"] = 1}
  48. assert_nothing_raised {t[1] = "foo"}
  49. assert_nothing_raised {t[:Foo] = :bar}
  50. assert_nothing_raised {t.clear}
  51. assert_equal(0, t.length)
  52. assert_equal(nil, t["Foo"])
  53. assert_equal(nil, t["foo"])
  54. assert_equal(nil, t[1])
  55. assert_equal(nil, t[:Foo])
  56. end # def test_clear
  57. def test_delete
  58. t = PRUNE::CiHash.new
  59. assert_nothing_raised {t["Foo"] = 1}
  60. assert_nothing_raised {t[1] = "foo"}
  61. assert_nothing_raised {t[:Foo] = :bar}
  62. assert_nothing_raised {t.delete("foo")}
  63. assert_equal(nil, t["Foo"])
  64. assert_equal(nil, t["foo"])
  65. assert_equal("foo", t[1])
  66. assert_equal(:bar, t[:Foo])
  67. assert_nothing_raised {t["#foo"] = 1}
  68. assert_equal(1, t["#foo"])
  69. assert_nothing_raised {t.delete("#foo")}
  70. assert_equal(nil, t["#foo"])
  71. end # def test_delete
  72. def test_fetch
  73. t = PRUNE::CiHash.new
  74. assert_nothing_raised {t["Foo"] = 1}
  75. assert_nothing_raised {t[1] = "foo"}
  76. assert_nothing_raised {t[:Foo] = :bar}
  77. assert_equal(1, t.fetch("Foo"))
  78. assert_equal(1, t.fetch("foo"))
  79. assert_equal("foo", t.fetch(1))
  80. assert_equal(:bar, t.fetch(:Foo))
  81. assert_equal("blahblahblah", t.fetch("Bar","blahblahblah"))
  82. assert_equal("No bar", t.fetch("bar"){|x| "No #{x}"})
  83. assert_equal(1, t.fetch("foo"){|x|x+1})
  84. if RbConfig::CONFIG['ruby_version'] >= "1.9.1" then
  85. assert_raises(KeyError) {t.fetch("Bar")}
  86. else
  87. assert_raises(IndexError) {t.fetch("Bar")}
  88. end
  89. end # def test_fetch
  90. def test_values_at
  91. t = PRUNE::CiHash.new
  92. assert_nothing_raised {t["Foo"] = 1}
  93. assert_nothing_raised {t[1] = "foo"}
  94. assert_nothing_raised {t[:Foo] = :bar}
  95. assert_equal([1, 1, "foo"], t.values_at("Foo", "foo", 1))
  96. assert_equal([:bar, :bar], t.values_at(:Foo, :foo))
  97. end # def test_values_at
  98. def test_has_key
  99. t = PRUNE::CiHash.new
  100. assert_nothing_raised {t["Foo"] = 1}
  101. assert_nothing_raised {t[1] = "foo"}
  102. assert_nothing_raised {t[:Foo] = :bar}
  103. assert_equal(true, t.has_key?("foo"))
  104. assert_equal(true, t.has_key?("fOO"))
  105. assert_equal(true, t.has_key?("Foo"))
  106. assert_equal(true, t.has_key?(:foo))
  107. assert_equal(true, t.has_key?(1))
  108. assert_equal(true, t.has_key?(:Foo))
  109. end # def test_has_key
  110. def test_to_origkey
  111. t = PRUNE::CiHash.new
  112. assert_nothing_raised {t["Foo"] = 1}
  113. assert_nothing_raised {t[1] = "foo"}
  114. assert_nothing_raised {t[:Foo] = :bar}
  115. assert_equal("Foo", t.to_origkey("fOO"))
  116. assert_equal(1, t.to_origkey(1))
  117. assert_equal(nil, t.to_origkey("1"))
  118. assert_equal(:Foo, t.to_origkey(:Foo))
  119. assert_equal(:Foo, t.to_origkey(:foo))
  120. end # def test_to_origkey
  121. def test_each_key
  122. t = PRUNE::CiHash.new
  123. assert_nothing_raised {t["Foo"] = 1}
  124. assert_nothing_raised {t[1] = "foo"}
  125. assert_nothing_raised {t[:Foo] = :bar}
  126. v = []
  127. assert_nothing_raised {t.each_key{|x|v<<x}}
  128. assert_equal(true, v.include?("foo"))
  129. assert_equal(true, v.include?(1))
  130. assert_equal(false, v.include?(:Foo))
  131. assert_equal(false, v.include?("Foo"))
  132. assert_equal(true, v.include?(:foo))
  133. end # def test_each_key
  134. def test_each_origkey
  135. t = PRUNE::CiHash.new
  136. assert_nothing_raised {t["Foo"] = 1}
  137. assert_nothing_raised {t[1] = "foo"}
  138. assert_nothing_raised {t[:Foo] = :bar}
  139. v = []
  140. assert_nothing_raised {t.each_origkey{|x|v<<x}}
  141. assert_equal(true, v.include?("Foo"))
  142. assert_equal(true, v.include?(1))
  143. assert_equal(true, v.include?(:Foo))
  144. assert_equal(false, v.include?("foo"))
  145. assert_equal(false, v.include?("fOO"))
  146. assert_equal(false, v.include?("1"))
  147. assert_equal(false, v.include?(:foo))
  148. end # def test_each_origkey
  149. def test_each_pair
  150. t = PRUNE::CiHash.new
  151. assert_nothing_raised {t["Foo"] = 1}
  152. assert_nothing_raised {t[1] = "foo"}
  153. assert_nothing_raised {t[:Foo] = :bar}
  154. k = []
  155. v = []
  156. assert_nothing_raised {t.each_pair{|x,y|k<<x;v<<y}}
  157. assert_equal(true, k.include?("foo"))
  158. assert_equal(1, v[k.index("foo")])
  159. assert_equal(true, k.include?(1))
  160. assert_equal("foo", v[k.index(1)])
  161. assert_equal(false, k.include?(:Foo))
  162. assert_equal(nil, k.index(:Foo))
  163. assert_equal(false, k.include?("fOO"))
  164. assert_equal(false, k.include?("1"))
  165. assert_equal(true, k.include?(:foo))
  166. end # def test_each_pair
  167. def test_each_origpair
  168. t = PRUNE::CiHash.new
  169. assert_nothing_raised {t["Foo"] = 1}
  170. assert_nothing_raised {t[1] = "foo"}
  171. assert_nothing_raised {t[:Foo] = :bar}
  172. k = []
  173. v = []
  174. assert_nothing_raised {t.each_origpair{|x,y|k<<x;v<<y}}
  175. assert_equal(true, k.include?("Foo"))
  176. assert_equal(1, v[k.index("Foo")])
  177. assert_equal(true, k.include?(1))
  178. assert_equal("foo", v[k.index(1)])
  179. assert_equal(true, k.include?(:Foo))
  180. assert_equal(:bar, v[k.index(:Foo)])
  181. assert_equal(false, k.include?("fOO"))
  182. assert_equal(false, k.include?("foo"))
  183. assert_equal(false, k.include?("1"))
  184. assert_equal(false, k.include?(:foo))
  185. end # def test_each_origpair
  186. def test_keys
  187. t = PRUNE::CiHash.new
  188. assert_nothing_raised {t["Foo"] = 1}
  189. assert_nothing_raised {t[1] = "foo"}
  190. assert_nothing_raised {t[:Foo] = :bar}
  191. v = nil
  192. assert_nothing_raised {v = t.keys}
  193. assert_equal(true, v.include?("foo"))
  194. assert_equal(true, v.include?(1))
  195. assert_equal(false, v.include?(:Foo))
  196. assert_equal(false, v.include?("Foo"))
  197. assert_equal(false, v.include?("fOO"))
  198. assert_equal(false, v.include?("1"))
  199. assert_equal(true, v.include?(:foo))
  200. end # def test_keys
  201. def test_origkeys
  202. t = PRUNE::CiHash.new
  203. assert_nothing_raised {t["Foo"] = 1}
  204. assert_nothing_raised {t[1] = "foo"}
  205. assert_nothing_raised {t[:Foo] = :bar}
  206. v = nil
  207. assert_nothing_raised {v = t.origkeys}
  208. assert_equal(true, v.include?("Foo"))
  209. assert_equal(true, v.include?(1))
  210. assert_equal(true, v.include?(:Foo))
  211. assert_equal(false, v.include?("foo"))
  212. assert_equal(false, v.include?("fOO"))
  213. assert_equal(false, v.include?("1"))
  214. assert_equal(false, v.include?(:foo))
  215. end # def test_keys
  216. end # class TestPRUNE__CiHash
  217. if $0 == __FILE__ then
  218. begin
  219. require 'main'
  220. rescue LoadError
  221. require 'tests/main'
  222. end
  223. end