From: John Baylor Date: 2006-07-01T09:27:14+09:00 Subject: Re: [QUIZ] C-Style Ints (#85) - not a spoiler, a test harness ------=_Part_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 = UnsignedFixedWidthInt.new(0xFF, 8) assert_equal( n, 255 ) n += 2 assert_equal( n, 1 ) n = n << 1 assert_equal( n, 2 ) n = n >> 1 assert_equal( n, 1 ) assert_equal( ~n, 254 ) n += 12 assert_equal( n, 13 ) n = n & 0x0E assert_equal( n, 12 ) end def test_quiz_example_too_wide n = UnsignedFixedWidthInt.new(0x0, 8) assert_equal( n, 0 ) n += 0xFFEE assert_equal( n, 238 ) end end class TestUnsignedFixedWidthInt < Test::Unit::TestCase def test_quiz_example_signed n = SignedFixedWidthInt.new(0x01, 8) assert_equal( n, 1 ) n = n << 7 assert_equal( n, -128 ) n -= 1 assert_equal( n, 127 ) n = n >> 6 assert_equal( n, 1 ) n -= 2 assert_equal( n, -1 ) n = n ^ 0xF3 assert_equal( n, 12 ) n = n | 0x01 assert_equal( n, 13 ) end end On 6/30/06, Ruby Quiz 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 = UnsignedFixedWidthInt.new(0xFF, 8) > => 255 > irb(main):002:0> n += 2 > => 1 > irb(main):003:0> n = n << 1 > => 2 > irb(main):004:0> n = n >> 1 > => 1 > irb(main):005:0> ~n > => 254 > irb(main):006:0> n += 12 > => 13 > irb(main):007:0> n = n & 0x0E > => 12 > irb(main):008:0> > > Now an example of an 8 bit signed int with an initial value of 0x01: > > irb(main):001:0> n = SignedFixedWidthInt.new(0x01, 8) > => 1 > irb(main):002:0> n = n << 7 > => -128 > irb(main):003:0> n -= 1 > => 127 > irb(main):004:0> n = n >> 6 > => 1 > irb(main):005:0> n -= 2 > => -1 > irb(main):006:0> n = n ^ 0xF3 > => 12 > irb(main):007:0> n = n | 0x01 > => 13 > irb(main):008:0> > > Here is an example of handling numbers that are too wide: > > irb(main):001:0> n = UnsignedFixedWidthInt.new(0x0, 8) > => 0 > irb(main):002:0> n += 0xFFEE > => 238 > irb(main):003:0> > > ------=_Part_4239_22721185.1151696070627--