------ art_4239_22721185.1151696070627
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Given the number of edge conditions for this quiz it seemed like a good
place for a test harness. Here is the irb session in the quiz translated to
a test harness. It is my first use of the test framework so hopefully I
haven't made any horrible errors. Just put this in the file quiz85.rb and
then use "ruby quiz85.rb" to test for correctness (it works better from the
command line than from irb - I don't know why but someone here does). Have
fun!
# test harness for ruby quiz 85
# see it at:
# http://www.rubyquiz.com/quiz85.html
require 'test/unit'
require 'UnsignedFixedWidthInt.rb'
require 'SignedFixedWidthInt.rb'
class TestUnsignedFixedWidthInt < Test::Unit::TestCase
def test_quiz_example_unsigned
n nsignedFixedWidthInt.new(0xFF, 8)
assert_equal( n, 255 )
n +
assert_equal( n, 1 )
n << 1
assert_equal( n, 2 )
n >> 1
assert_equal( n, 1 )
assert_equal( ~n, 254 )
n + 2
assert_equal( n, 13 )
n & 0x0E
assert_equal( n, 12 )
end
def test_quiz_example_too_wide
n nsignedFixedWidthInt.new(0x0, 8)
assert_equal( n, 0 )
n + xFFEE
assert_equal( n, 238 )
end
end
class TestUnsignedFixedWidthInt < Test::Unit::TestCase
def test_quiz_example_signed
n ignedFixedWidthInt.new(0x01, 8)
assert_equal( n, 1 )
n << 7
assert_equal( n, -128 )
n -
assert_equal( n, 127 )
n >> 6
assert_equal( n, 1 )
n -
assert_equal( n, -1 )
n ^ 0xF3
assert_equal( n, 12 )
n | 0x01
assert_equal( n, 13 )
end
end
On 6/30/06, Ruby Quiz <james / grayproductions.net> wrote:
>
> The three rules of Ruby Quiz:
>
> 1. Please do not post any solutions or spoiler discussion for this quiz
> until
> 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rubyquiz.com/
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem helps
> everyone
> on Ruby Talk follow the discussion. Please reply to the original quiz
> message,
> if you can.
>
>
> - - - - - - - - - - - - - - - - - - - -
>
> by Aaron Patterson
>
> Write a class that can represent a signed or unsigned number with an
> arbitrary
> number of bits. This class should support all bitwise operations ( & ^ ~
> | ),
> basic math operations ( + - / * ), and comparison operators. It would
> behave
> like an integer in C (except with arbitrary length!), so an unsigned int
> 0xFFFFFFFF + 1 would equal 0x00000000.
>
> One edge case is what to do in an overflow case ( see the first irb
> session
> number 2 ). Another is how to handle numbers that are wider than the
> specified
> number of bits. I'm not really sure how to handle that part, but what I
> do is
> just take the last N number of bits. So if 0xFFEE was passed in to my 8
> bit
> vector, I would just take 0xEE.
>
> Example irb sessions
>
> Here is an example of using an 8 bit unsigned int with an initial value of
> 0xFF:
>
> irb(main):001:0> n nsignedFixedWidthInt.new(0xFF, 8)
>