David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | |
| 3 | shared_examples_for 'a binary protocol' do |
| 4 | before(:each) do |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 5 | @trans = Thrift::MemoryBuffer.new |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 6 | @prot = protocol_class.new(@trans) |
| 7 | end |
| 8 | |
Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 9 | it "should define the proper VERSION_1, VERSION_MASK AND TYPE_MASK" do |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 10 | protocol_class.const_get(:VERSION_MASK).should == 0xffff0000 |
| 11 | protocol_class.const_get(:VERSION_1).should == 0x80010000 |
Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 12 | protocol_class.const_get(:TYPE_MASK).should == 0x000000ff |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 13 | end |
| 14 | |
Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 15 | it "should make strict_read readable" do |
| 16 | @prot.strict_read.should eql(true) |
| 17 | end |
| 18 | |
| 19 | it "should make strict_write readable" do |
| 20 | @prot.strict_write.should eql(true) |
| 21 | end |
| 22 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 23 | it "should write the message header" do |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 24 | @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 25 | @trans.read(1000).should == [protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL, "testMessage".size, "testMessage", 17].pack("NNa11N") |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 26 | end |
Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 27 | |
| 28 | it "should write the message header without version when writes are not strict" do |
| 29 | @prot = protocol_class.new(@trans, true, false) # no strict write |
| 30 | @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17) |
| 31 | @trans.read(1000).should == "\000\000\000\vtestMessage\001\000\000\000\021" |
| 32 | end |
| 33 | |
| 34 | it "should write the message header with a version when writes are strict" do |
| 35 | @prot = protocol_class.new(@trans) # strict write |
| 36 | @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17) |
| 37 | @trans.read(1000).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021" |
| 38 | end |
| 39 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 40 | |
| 41 | # message footer is a noop |
| 42 | |
| 43 | it "should write the field header" do |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 44 | @prot.write_field_begin('foo', Thrift::Types::DOUBLE, 3) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 45 | @trans.read(1000).should == [Thrift::Types::DOUBLE, 3].pack("cn") |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 46 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 47 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 48 | # field footer is a noop |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 49 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 50 | it "should write the STOP field" do |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 51 | @prot.write_field_stop |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 52 | @trans.read(1).should == "\000" |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 53 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 54 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 55 | it "should write the map header" do |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 56 | @prot.write_map_begin(Thrift::Types::STRING, Thrift::Types::LIST, 17) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 57 | @trans.read(1000).should == [Thrift::Types::STRING, Thrift::Types::LIST, 17].pack("ccN"); |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 58 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 59 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 60 | # map footer is a noop |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 61 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 62 | it "should write the list header" do |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 63 | @prot.write_list_begin(Thrift::Types::I16, 42) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 64 | @trans.read(1000).should == [Thrift::Types::I16, 42].pack("cN") |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 65 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 66 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 67 | # list footer is a noop |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 68 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 69 | it "should write the set header" do |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 70 | @prot.write_set_begin(Thrift::Types::I16, 42) |
| 71 | @trans.read(1000).should == [Thrift::Types::I16, 42].pack("cN") |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 72 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 73 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 74 | it "should write a bool" do |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 75 | @prot.write_bool(true) |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 76 | @prot.write_bool(false) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 77 | @trans.read(1000).should == "\001\000" |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 78 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 79 | |
Kevin Clark | 14fe791 | 2008-08-04 18:46:19 +0000 | [diff] [blame] | 80 | it "should treat a nil bool as false" do |
Kevin Clark | 14fe791 | 2008-08-04 18:46:19 +0000 | [diff] [blame] | 81 | @prot.write_bool(nil) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 82 | @trans.read(1).should == "\000" |
Kevin Clark | 14fe791 | 2008-08-04 18:46:19 +0000 | [diff] [blame] | 83 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 84 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 85 | it "should write a byte" do |
| 86 | # byte is small enough, let's check -128..127 |
| 87 | (-128..127).each do |i| |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 88 | @prot.write_byte(i) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 89 | @trans.read(1).should == [i].pack('c') |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 90 | end |
| 91 | (-128..127).each do |i| |
| 92 | end |
| 93 | # handing it numbers out of signed range should clip |
| 94 | @trans.rspec_verify |
| 95 | (128..255).each do |i| |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 96 | @prot.write_byte(i) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 97 | @trans.read(1).should == [i].pack('c') |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 98 | end |
| 99 | # and lastly, a Bignum is going to error out |
| 100 | lambda { @prot.write_byte(2**65) }.should raise_error(RangeError) |
| 101 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 102 | |
Kevin Clark | 14fe791 | 2008-08-04 18:46:19 +0000 | [diff] [blame] | 103 | it "should error gracefully when trying to write a nil byte" do |
| 104 | lambda { @prot.write_byte(nil) }.should raise_error |
| 105 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 106 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 107 | it "should write an i16" do |
| 108 | # try a random scattering of values |
| 109 | # include the signed i16 minimum/maximum |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 110 | [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i| |
| 111 | @prot.write_i16(i) |
| 112 | end |
| 113 | # and try something out of signed range, it should clip |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 114 | @prot.write_i16(2**15 + 5) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 115 | |
| 116 | @trans.read(1000).should == "\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005" |
| 117 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 118 | # a Bignum should error |
| 119 | # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError) |
| 120 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 121 | |
Kevin Clark | 14fe791 | 2008-08-04 18:46:19 +0000 | [diff] [blame] | 122 | it "should error gracefully when trying to write a nil i16" do |
| 123 | lambda { @prot.write_i16(nil) }.should raise_error |
| 124 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 125 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 126 | it "should write an i32" do |
| 127 | # try a random scattering of values |
| 128 | # include the signed i32 minimum/maximum |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 129 | [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i| |
| 130 | @prot.write_i32(i) |
| 131 | end |
| 132 | # try something out of signed range, it should clip |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 133 | @trans.read(1000).should == "\200\000\000\000" + "\377\376\037\r" + "\377\377\366\034" + "\377\377\377\375" + "\000\000\000\000" + "\000#\340\203" + "\000\0000+" + "\177\377\377\377" |
| 134 | [2 ** 31 + 5, 2 ** 65 + 5].each do |i| |
| 135 | lambda { @prot.write_i32(i) }.should raise_error(RangeError) |
| 136 | end |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 137 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 138 | |
Kevin Clark | 14fe791 | 2008-08-04 18:46:19 +0000 | [diff] [blame] | 139 | it "should error gracefully when trying to write a nil i32" do |
| 140 | lambda { @prot.write_i32(nil) }.should raise_error |
| 141 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 142 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 143 | it "should write an i64" do |
| 144 | # try a random scattering of values |
| 145 | # try the signed i64 minimum/maximum |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 146 | [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i| |
| 147 | @prot.write_i64(i) |
| 148 | end |
| 149 | # try something out of signed range, it should clip |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 150 | @trans.read(1000).should == ["\200\000\000\000\000\000\000\000", |
| 151 | "\377\377\364\303\035\244+]", |
| 152 | "\377\377\377\377\376\231:\341", |
| 153 | "\377\377\377\377\377\377\377\026", |
| 154 | "\000\000\000\000\000\000\000\000", |
| 155 | "\000\000\000\000\000\000\004\317", |
| 156 | "\000\000\000\000\000#\340\204", |
| 157 | "\000\000\000\002\340\311~\365", |
| 158 | "\177\377\377\377\377\377\377\377"].join("") |
| 159 | lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError) |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 160 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 161 | |
Kevin Clark | 14fe791 | 2008-08-04 18:46:19 +0000 | [diff] [blame] | 162 | it "should error gracefully when trying to write a nil i64" do |
| 163 | lambda { @prot.write_i64(nil) }.should raise_error |
| 164 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 165 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 166 | it "should write a double" do |
| 167 | # try a random scattering of values, including min/max |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 168 | values = [Float::MIN,-1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX] |
| 169 | values.each do |f| |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 170 | @prot.write_double(f) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 171 | @trans.read(1000).should == [f].pack("G") |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 172 | end |
| 173 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 174 | |
Kevin Clark | 14fe791 | 2008-08-04 18:46:19 +0000 | [diff] [blame] | 175 | it "should error gracefully when trying to write a nil double" do |
| 176 | lambda { @prot.write_double(nil) }.should raise_error |
| 177 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 178 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 179 | it "should write a string" do |
| 180 | str = "hello world" |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 181 | @prot.write_string(str) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 182 | @trans.read(1000).should == [str.size].pack("N") + str |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 183 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 184 | |
Kevin Clark | 14fe791 | 2008-08-04 18:46:19 +0000 | [diff] [blame] | 185 | it "should error gracefully when trying to write a nil string" do |
| 186 | lambda { @prot.write_string(nil) }.should raise_error |
| 187 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 188 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 189 | # message footer is a noop |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 190 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 191 | it "should read a field header" do |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 192 | @trans.write([Thrift::Types::STRING, 3].pack("cn")) |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 193 | @prot.read_field_begin.should == [nil, Thrift::Types::STRING, 3] |
| 194 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 195 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 196 | # field footer is a noop |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 197 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 198 | it "should read a stop field" do |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 199 | @trans.write([Thrift::Types::STOP].pack("c")); |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 200 | @prot.read_field_begin.should == [nil, Thrift::Types::STOP, 0] |
| 201 | end |
| 202 | |
| 203 | it "should read a map header" do |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 204 | @trans.write([Thrift::Types::DOUBLE, Thrift::Types::I64, 42].pack("ccN")) |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 205 | @prot.read_map_begin.should == [Thrift::Types::DOUBLE, Thrift::Types::I64, 42] |
| 206 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 207 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 208 | # map footer is a noop |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 209 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 210 | it "should read a list header" do |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 211 | @trans.write([Thrift::Types::STRING, 17].pack("cN")) |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 212 | @prot.read_list_begin.should == [Thrift::Types::STRING, 17] |
| 213 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 214 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 215 | # list footer is a noop |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 216 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 217 | it "should read a set header" do |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 218 | @trans.write([Thrift::Types::STRING, 17].pack("cN")) |
| 219 | @prot.read_set_begin.should == [Thrift::Types::STRING, 17] |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 220 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 221 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 222 | # set footer is a noop |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 223 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 224 | it "should read a bool" do |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 225 | @trans.write("\001\000"); |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 226 | @prot.read_bool.should == true |
| 227 | @prot.read_bool.should == false |
| 228 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 229 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 230 | it "should read a byte" do |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 231 | [-128, -57, -3, 0, 17, 24, 127].each do |i| |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 232 | @trans.write([i].pack("c")) |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 233 | @prot.read_byte.should == i |
| 234 | end |
| 235 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 236 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 237 | it "should read an i16" do |
| 238 | # try a scattering of values, including min/max |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 239 | [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i| |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 240 | @trans.write([i].pack("n")); |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 241 | @prot.read_i16.should == i |
| 242 | end |
| 243 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 244 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 245 | it "should read an i32" do |
| 246 | # try a scattering of values, including min/max |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 247 | [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i| |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 248 | @trans.write([i].pack("N")) |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 249 | @prot.read_i32.should == i |
| 250 | end |
| 251 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 252 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 253 | it "should read an i64" do |
| 254 | # try a scattering of values, including min/max |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 255 | [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i| |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 256 | @trans.write([i >> 32, i & 0xFFFFFFFF].pack("NN")) |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 257 | @prot.read_i64.should == i |
| 258 | end |
| 259 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 260 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 261 | it "should read a double" do |
| 262 | # try a random scattering of values, including min/max |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 263 | [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f| |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 264 | @trans.write([f].pack("G")); |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 265 | @prot.read_double.should == f |
| 266 | end |
| 267 | end |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 268 | |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 269 | it "should read a string" do |
| 270 | str = "hello world" |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 271 | @trans.write([str.size].pack("N") + str) |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 272 | @prot.read_string.should == str |
| 273 | end |
| 274 | end |