Skip to content

Instantly share code, notes, and snippets.

@btc
Created April 16, 2012 18:01
Show Gist options
  • Save btc/2400373 to your computer and use it in GitHub Desktop.
Save btc/2400373 to your computer and use it in GitHub Desktop.
cs142 project 2 unit test
require "./squared.rb"
require "./sort.rb"
require "./filter.rb"
require "./group.rb"
require "./adder.rb"
require "test/unit"
class TestSquared < Test::Unit::TestCase
def test_squared
for i in (0..10) do
assert_equal(i*i, i.squared)
end
end
end
class TestSort < Test::Unit::TestCase
def test_sort
a = ['abc99.6','-100x500', 'what', 'w9', 'e2']
aFunnySorted = ['what', 'e2', 'w9', 'abc99.6', '-100x500']
assert_equal(aFunnySorted, funny_sort(a))
b = ['cat32.102','something','-178','e3']
bFunnySorted = ['something', 'e3', 'cat32.102', '-178']
assert_equal(bFunnySorted, funny_sort(b))
c = ['abc99.6','-105x500', 'what', 'w9', 'e2', 'e2', 'e2', '-100x501']
cFunnySorted = ['what', 'e2', 'e2', 'e2', 'w9', 'abc99.6', '-100x501', '-105x500']
assert_equal(cFunnySorted, funny_sort(c))
d = ['a5.12a', 'b1.119e', '0.100', '-19.20']
dFunnySorted = [ '0.100', 'b1.119e', 'a5.12a', '-19.20']
assert_equal(dFunnySorted, funny_sort(d))
end
end
class TestFilter < Test::Unit::TestCase
def test_filter
nums = [6, -5, 319, 400, 18, 94, 11]
output = Array.new
filter(nums, :min => 10, :max => 350, :odd => 1, :scale => 2) {|n| output << n}
assert_equal([638, 22], output)
empty = []
filter(nums, :max => 0) {|x| empty << x }
same = []
filter(nums) {|n| same << n}
assert_equal(nums, same)
end
end
class TestGroup < Test::Unit::TestCase
def test_group
pre = ["hello", "iam", "chilling", "dude"]
post = {"h" => ["hello"], "i" => ["iam"], "c" => ["chilling"], "d" => ["dude"]}
out = {}
pre.each_group_by_first_letter do |letter, words|
out[letter] = words
end
assert_equal(post, out)
end
end
class TestAdder < Test::Unit::TestCase
def test_method_missing
x = Adder.new(10)
assert_raise NoMethodError do
x.plus1a
end
assert_raise NoMethodError do
x.minus2
end
end
def test_correct_usage
x = Adder.new(10)
assert_equal(30, x.plus20)
assert_equal(35, x.plus25)
end
end
@btc
Copy link
Author

btc commented Apr 23, 2012

Goes in the working directory. Run it: ruby testfilename.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment