Last active
August 29, 2015 13:57
-
-
Save jgn/9379874 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HalfAdder | |
attr_reader :sum, :carry | |
def initialize(augend, addend, logger) | |
@augend, @addend, @logger = augend, addend, logger | |
end | |
def compute | |
@carry = @augend & @addend | |
@sum = @augend ^ @addend | |
@logger.info "augend: #{@augend}" | |
@logger.info "addend: #{@addend}" | |
@logger.info "carry: #{@carry}" | |
@logger.info "sum: #{@sum}" | |
end | |
end | |
def assert_equality(example, actual, expected) | |
print "example #{example}: " | |
if actual == expected | |
puts 'Pass' | |
else | |
puts "Fail: got #{actual.inspect}; expected #{expected.inspect}" | |
end | |
end | |
examples = [ | |
[0, 0, 0, 0], | |
[1, 0, 0, 1], | |
[0, 1, 0, 1], | |
[1, 1, 1, 0] | |
] | |
class Stub | |
def info(*) | |
end | |
end | |
stub = Stub.new | |
examples.each do |example| | |
# The 4 phases: seup, exercise, verify, teardown | |
a, b, c, s = example # setup | |
half_adder = HalfAdder.new(a, b, stub) # setup | |
half_adder.compute # exercise | |
assert_equality(example, half_adder.carry, c) # verify | |
assert_equality(example, half_adder.sum, s) # verify | |
adder = nil # teardown | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment