public

  • Download Gist
test_solution_acceptance.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
require 'test/unit'
require 'fair_distribution'
 
#FairDistribution = LowMemoryFairDistribution
 
class FairQueueTest < Test::Unit::TestCase
 
def test_basic1
jobs = [10, 15, 20, 24, 30, 45, 75]
number_of_presses = 2
exp_max = 110
fd = FairDistribution.new(jobs, number_of_presses)
assert_equal exp_max, fd.time_required
 
# A Solution
# [75, 20, 15],
# [45, 30, 24, 10,]
end
def test_basic2
jobs = [1.0, 4.75, 2.83, 1.1, 5.8, 3.5, 4.4]
number_of_presses = 4
exp_max = 6.33
exp_distribution = [
[1.0, 4.75],
[2.83, 3.5],
[1.1, 4.4],
[5.8]
]
fd = FairDistribution.new(jobs, number_of_presses)
assert_equal exp_max, fd.time_required
assert_distributions_are_equivalent exp_distribution, fd.distribution
end
def test_basic3
jobs = [0.23, 0.47, 0.73, 1.5, 3.0, 3.2, 1.75, 2.3, 0.11, 0.27, 1.09]
number_of_presses = 4
exp_max = 3.73
fd = FairDistribution.new(jobs, number_of_presses)
assert_equal exp_max, fd.time_required
# A Solution
# [3.2, 0.47],
# [3.0, 0.73],
# [2.3, 1.09, 0.23],
# [1.75, 1.5, 0.27, 0.11]
end if ARGV[0] == "full" # only use this TC if 'full' is added as an argument.
def test_basic4
jobs = [5,5,4,4,3,3,3]
number_of_presses = 3
exp_max = 9
exp_distribution = [
[5,4],
[5,4],
[3,3,3],
]
fd = FairDistribution.new(jobs, number_of_presses)
assert_equal exp_max, fd.time_required
assert_distributions_are_equivalent exp_distribution, fd.distribution
end
 
def test_basic5
fd = FairDistribution.new([0.23, 0.47, 0.73, 1.5, 3.0, 3.2], 4)
assert_equal 3.2, fd.time_required
assert_distributions_are_equivalent [[3.0], [3.2], [0.23, 0.47, 0.73], [1.5]], fd.distribution
end
# Testing Implementation
# def test_arrays_have_same_elements
# assert arrays_have_same_elements?([1,2,3], [3,1,2])
# assert !arrays_have_same_elements?([], [1,2,3])
# assert !arrays_have_same_elements?([1,2,3], [1,2,3,4])
# assert !arrays_have_same_elements?([1,2,3,4], [1,2,3])
# assert !arrays_have_same_elements?([1,1], [1])
# assert !arrays_have_same_elements?([1], [1,1])
# end
#
# def test_distributions_are_equivalent
# assert distributions_are_equivalent?([[1, 2], [2, 3]], [[3, 2], [2, 1]])
# assert !distributions_are_equivalent?([[1, 3], [2, 2]], [[3, 2], [2, 1]])
# assert !distributions_are_equivalent?([[1, 2], [3, 2]], [[3, 2], [3, 2]])
# assert !distributions_are_equivalent?([[1, 2], [1, 2]], [[3, 2], [2, 1]])
# end
private
def assert_distributions_are_equivalent(dist1, dist2, msg=nil)
failure_message = "Distributions not equivalent: #{msg}: #{dist1.inspect} not equivalent to #{dist2.inspect}"
assert(distributions_are_equivalent?(dist1, dist2), failure_message)
end
def distributions_are_equivalent?(dist1, dist2)
return false if dist1.size != dist2.size
my_dist1 = dist1.dup
my_dist2 = dist2.dup
my_dist1.reject! {|queue1|
dist2.detect {|queue2| arrays_have_same_elements?(queue1, queue2)}
}
my_dist2.reject! {|queue2|
dist1.detect {|queue1| arrays_have_same_elements?(queue1, queue2)}
}
my_dist1.empty? && my_dist2.empty?
end
def arrays_have_same_elements?(arr1, arr2)
arr1.size == arr2.size && (arr1 - arr2).empty?
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.