# clemensp/code_kata

kata two solution 1

1 parent 3351b69 commit 83c0436364a0ee6ae273331f4da989e8d11f97ff committed Dec 16, 2010
Showing with 73 additions and 0 deletions.
1. +2 −0 .gitignore
2. +13 −0 two/1/lib/kata_two.rb
3. +50 −0 two/1/test/tc_kata_two.rb
4. +8 −0 two/1/test/testall.rb
 @@ -0,0 +1,2 @@ +*.swp +*.swo
 @@ -0,0 +1,13 @@ +class KataTwo + + def self.chop(int, array_of_int) + i = -1 + array_of_int.each.with_index do |elem, index| + if elem == int + i = index + break + end + end + i + end +end
 @@ -0,0 +1,50 @@ +require "kata_two" +require "test/unit" + +class KataTwoTest < Test::Unit::TestCase + + def setup + # Set up some test conditions here + # This will be invoked before each test + end + + def teardown + # This will be invoked after each test + # Use to close connections, etc. + end + + def test_chop + assert_equal(-1, KataTwo.chop(3, [])) + assert_equal(-1, KataTwo.chop(3, [1])) + assert_equal(0, KataTwo.chop(1, [1])) + # + assert_equal(0, KataTwo.chop(1, [1, 3, 5])) + assert_equal(1, KataTwo.chop(3, [1, 3, 5])) + assert_equal(2, KataTwo.chop(5, [1, 3, 5])) + assert_equal(-1, KataTwo.chop(0, [1, 3, 5])) + assert_equal(-1, KataTwo.chop(2, [1, 3, 5])) + assert_equal(-1, KataTwo.chop(4, [1, 3, 5])) + assert_equal(-1, KataTwo.chop(6, [1, 3, 5])) + # + assert_equal(0, KataTwo.chop(1, [1, 3, 5, 7])) + assert_equal(1, KataTwo.chop(3, [1, 3, 5, 7])) + assert_equal(2, KataTwo.chop(5, [1, 3, 5, 7])) + assert_equal(3, KataTwo.chop(7, [1, 3, 5, 7])) + assert_equal(-1, KataTwo.chop(0, [1, 3, 5, 7])) + assert_equal(-1, KataTwo.chop(2, [1, 3, 5, 7])) + assert_equal(-1, KataTwo.chop(4, [1, 3, 5, 7])) + assert_equal(-1, KataTwo.chop(6, [1, 3, 5, 7])) + assert_equal(-1, KataTwo.chop(8, [1, 3, 5, 7])) + end + + def test_chop_with_large_array + large_array_length = 3451 #or any arbitrary large number + count_by = 4 #or any even, non-zero number + large_array = [] + (1..large_array_length).each{|x| large_array << x * count_by} + (1..large_array_length).each{|x| assert_equal( (x - 1), KataTwo.chop(x * count_by, large_array))} + (1..large_array_length).each{|x| assert_equal( -1, KataTwo.chop(x * count_by + 1, large_array))} + end + +end +
 @@ -0,0 +1,8 @@ +\$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == \$0 +\$LOAD_PATH.unshift("#{File.dirname(__FILE__)}") if __FILE__ == \$0 +puts "Checking for test cases:" +Dir['tc_*.rb'].each do |testcase| + puts "\t#{testcase}" + require testcase +end +puts " "