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
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 = [ |
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) | |
@augend, @addend = augend, addend | |
end | |
def compute | |
end | |
end |
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
example [0, 0, 0, 0]: Fail: got nil; expected 0 | |
example [0, 0, 0, 0]: Fail: got nil; expected 0 | |
example [1, 0, 0, 1]: Fail: got nil; expected 0 | |
example [1, 0, 0, 1]: Fail: got nil; expected 1 | |
example [0, 1, 0, 1]: Fail: got nil; expected 0 | |
example [0, 1, 0, 1]: Fail: got nil; expected 1 | |
example [1, 1, 1, 0]: Fail: got nil; expected 1 | |
example [1, 1, 1, 0]: Fail: got nil; expected 0 |
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) | |
@augend, @addend = augend, addend | |
end | |
def compute | |
@carry = @augend & @addend | |
@sum = @augend ^ @addend |
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 |
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 |
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 Logger | |
attr_reader :log_output | |
def info(s) | |
@log_output ||= "" | |
@log_output << s << "\n" | |
end | |
end | |
examples.each do |example| | |
a, b, c, s = example |
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
require 'logger' | |
def get_logger | |
logger = Logger.new(STDOUT) | |
def logger.info(s) | |
@log_output ||= "" | |
@log_output << s << "\n" | |
end | |
def logger.log_output | |
@log_output | |
end |
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 UnexpectedOutputError < StandardError; end | |
class MockLogger | |
def initialize(expected_indirect_outputs) | |
@eio = Array(expected_indirect_outputs) | |
end | |
def info(s) | |
expected = @eio.shift | |
if expected != s | |
raise UnexpectedOutputError.new("Fail: got #{s}; expected #{expected}") | |
end |
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
#!/usr/bin/env ruby | |
# http://en.wikipedia.org/wiki/RADIX-50 | |
class String | |
R50_CHARS = " ABCDEFGHIJKLMNOPQRSTUVWXYZ$.%0123456789" | |
R50_LOOKUP = {}.tap { |h| String::R50_CHARS.split(//).each_with_index { |c, i| h[c] = i } } | |
R50_CLEAN = /[[:upper:]|[:digit:]| |.|$|%]/ | |
def radix50 | |
[].tap do |r| | |
self.upcase.scan(R50_CLEAN).inject([]) do |m, c| | |
m << R50_LOOKUP[c] |
OlderNewer