Do the Unit Test cha cha!
And be happy you are alive.

Did you know that:

You burn more calories sleeping than you do watching television?

Search the Feed

Codeboxer search results for Unit Test
Alexa CSS Dreamhost Javascript Joyent Lighthouse Migration MySQL Oracle PHP Pivot SQL ssh status codes svn Unit Test

hobbits in holes and chipmunks in a phonebooth

21 May 2010 | 11:01 am by codeboxer

When writing unit tests, for unit/quantity measurements it is important to do the right thing. The rule is that you must test against either hobbits or chipmunks, in their appropriate quantities.

Also, there is such a thing as shaving yaks. I do it all day. :)

http://en.wiktionary.org/wiki/yak_shaving

Sample class with a unit test

12 December 2008 | 10:40 am by codeboxer

Also from a job interview a while back.

Here is the class that they gave me, which I extended -

class YPSet
#new and improved (sorry for the double email)
def initialize(*args)
@collect = []
args.each do |this|
@collect << this.to_i
end
@collect.sort!
@count = @collect.size
end

def average
sum = 0
@collect.each do |this|
sum+=this.to_f
end
(sum/@count)
end

def first_rec
@collect[0] ? @collect[0].to_i : nil
end

def lowest
first_rec
end

def highest
@collect[@count-1] ? @collect[@count-1].to_i : nil
end

def median
even_check=0
@collect.each do |this|
#loop to skip even method which is not importing properly
even_check = even_check==0 ? 1 : 0
end
is_even = even_check == 0 ? true : false
if is_even
#average median
half_position = (@count/2)
#first value
x1 = @collect[half_position-1]
#second value
x2 = @collect[half_position]
((x1+x2).to_f/2)
else
#single target
target_position = ((@count.to_i-1)/2) + 1
# value
@collect[target_position-1]
end
end

def average_dev
#return average deviation from main average per item
sum_of_deviations = 0
ave = self.average
@collect.each do |this|
sum_of_deviations += (this.to_f-ave).abs
end
sum_of_deviations/@count.to_i
end

def number_of_odds
count = 0
@collect.each do |this|
even_check = 0
for x in 1..this
#loop to skip even method (again)
even_check = even_check==0 ? 1 : 0
end
count += even_check
end
count
end

def number_of_evens
@count - number_of_odds
end

def partition(subdivisions)
#split into evenly sized partitions
single_part_size = (@count/subdivisions).to_i
new_array = []
cur_division = 1
x = 0
count_leftover = @count % subdivisions
original_leftover_count = count_leftover
while cur_division <= subdivisions
#test divisions
sub_array = []
while (x < ((single_part_size*cur_division) + (cur_division<=original_leftover_count && original_leftover_count>0) ? cur_division))
end

And the unit test I wrote for it -

require "test/unit"
require "yp_set"

class YPSetTest < Test::Unit::TestCase

def setup

end

def test_should_calculate_average
assert_equal 6, YPSet.new(8, 10, 2, 6, 4).average
assert_equal 5.5, YPSet.new(10, 11, 0, 1).average
end

# The median is the middle value in a set of values. If there are
# an even number of values, it should be the average of the two middle
# values
def test_should_calculate_median
assert_equal 6, YPSet.new(8, 10, 2, 6, 4).median
assert_equal 4.5, YPSet.new(10, 0, 1, 9, 8, 1).median
end

def test_should_calculate_with_string_args
assert_equal 2, YPSet.new(1, "2", 3, "2", 2).average
assert_equal 3, YPSet.new("7", "2", 3, "8", 2).median
end

def test_should_be_ordered_correctly
#added per point #3
assert_equal 2, YPSet.new(9, "11", 2, "2", 6).first_rec
end

def test_average_dev
#added per point #4
assert_equal 2, YPSet.new(1, 1, 2, 2, 3, 9).average_dev
end

def test_lowest_and_highest
#added per point #4
assert_operator YPSet.new(99, 44, 33).lowest, "<", YPSet.new(33, 99, 44).highest
end

def test_number_of_evens
#added per point #4 - no need to test odds as they use the same function
assert_equal 6, YPSet.new(99, 44, 33, 2, 6, 8, 9, 12, 14).number_of_evens
end

def test_should_split_into_evenly_sized_subsets
values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

subsets = YPSet.new(*values).partition(3)
assert_equal 3, subsets.length
subsets.each do |subset|
assert_equal 5, subset.length
end
assert_equal values.length, subsets.flatten.length

values << 16
subsets = YPSet.new(*values).partition(3)
assert_equal 3, subsets.length
subsets.each do |subset|
assert_in_delta 5, subset.length, 1
end
assert_equal values.length, subsets.flatten.length
end
end

Good times!Original post blogged on codeboxer.com.

My Site Links

Screenshots are featured above. If you visit gmgpulse, you may login as demo/demo.

Rockstar Television


© 2008-10 Krister Axel and codeboxer.com