hessian2 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +10 -10
  3. data/README.md +197 -197
  4. data/hessian2.gemspec +0 -2
  5. data/lib/hessian2/client.rb +57 -50
  6. data/lib/hessian2/constants.rb +164 -164
  7. data/lib/hessian2/fault.rb +3 -3
  8. data/lib/hessian2/handler.rb +18 -18
  9. data/lib/hessian2/hessian_client.rb +3 -3
  10. data/lib/hessian2/parser.rb +619 -619
  11. data/lib/hessian2/type_wrapper.rb +49 -49
  12. data/lib/hessian2/version.rb +3 -3
  13. data/lib/hessian2/writer.rb +499 -499
  14. data/lib/hessian2.rb +14 -14
  15. data/spec/binary_spec.rb +51 -51
  16. data/spec/boolean_spec.rb +26 -26
  17. data/spec/class_wrapper_spec.rb +52 -52
  18. data/spec/create_monkeys.rb +14 -14
  19. data/spec/date_spec.rb +45 -45
  20. data/spec/double_spec.rb +78 -78
  21. data/spec/int_spec.rb +54 -54
  22. data/spec/list_spec.rb +66 -66
  23. data/spec/long_spec.rb +68 -68
  24. data/spec/map_spec.rb +36 -36
  25. data/spec/null_spec.rb +17 -17
  26. data/spec/object_spec.rb +78 -78
  27. data/spec/ref_spec.rb +43 -43
  28. data/spec/spec_helper.rb +23 -23
  29. data/spec/string_spec.rb +61 -61
  30. data/spec/struct_wrapper_spec.rb +47 -47
  31. data/spec/type_wrapper_spec.rb +102 -102
  32. data/test/app.rb +24 -24
  33. data/test/async/em_http_asleep.rb +25 -25
  34. data/test/async/em_http_sleep.rb +25 -25
  35. data/test/async/monkey.asleep.rb +27 -27
  36. data/test/async/mysql2_aquery.rb +37 -37
  37. data/test/fault/monkey.undefined_method.rb +5 -5
  38. data/test/fault/monkey.wrong_arguments.rb +5 -5
  39. data/test/fiber_concurrency/em_http_asleep.rb +17 -17
  40. data/test/fiber_concurrency/em_http_sleep.rb +17 -17
  41. data/test/fiber_concurrency/monkey.asleep.fiber_aware.rb +18 -18
  42. data/test/fiber_concurrency/mysql2_query.rb +29 -29
  43. data/test/fiber_concurrency/net_http_asleep.rb +19 -19
  44. data/test/fiber_concurrency/net_http_sleep.rb +19 -19
  45. data/test/fibered_rainbows/Gemfile +15 -15
  46. data/test/fibered_rainbows/config.ru +11 -11
  47. data/test/fibered_rainbows/rainbows.rb +13 -13
  48. data/test/monkey_service.rb +16 -16
  49. data/test/prepare.rb +7 -7
  50. data/test/thread_concurrency/active_record_execute.rb +29 -29
  51. data/test/thread_concurrency/monkey.asleep.rb +22 -22
  52. data/test/thread_concurrency/net_http_asleep.rb +24 -24
  53. data/test/thread_concurrency/net_http_sleep.rb +24 -24
  54. data/test/threaded_rainbows/Gemfile +13 -13
  55. data/test/threaded_rainbows/config.ru +9 -9
  56. data/test/threaded_rainbows/rainbows.rb +13 -13
  57. metadata +4 -74
data/lib/hessian2.rb CHANGED
@@ -1,14 +1,14 @@
1
- require 'hessian2/class_wrapper'
2
- require 'hessian2/client'
3
- require 'hessian2/fault'
4
- require 'hessian2/handler'
5
- require 'hessian2/hessian_client'
6
- require 'hessian2/parser'
7
- require 'hessian2/struct_wrapper'
8
- require 'hessian2/type_wrapper'
9
- require 'hessian2/version'
10
- require 'hessian2/writer'
11
-
12
- module Hessian2
13
- extend Parser, Writer
14
- end
1
+ require 'hessian2/class_wrapper'
2
+ require 'hessian2/client'
3
+ require 'hessian2/fault'
4
+ require 'hessian2/handler'
5
+ require 'hessian2/hessian_client'
6
+ require 'hessian2/parser'
7
+ require 'hessian2/struct_wrapper'
8
+ require 'hessian2/type_wrapper'
9
+ require 'hessian2/version'
10
+ require 'hessian2/writer'
11
+
12
+ module Hessian2
13
+ extend Parser, Writer
14
+ end
data/spec/binary_spec.rb CHANGED
@@ -1,51 +1,51 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- module Hessian2
4
- describe Writer do
5
- context "when binary" do
6
-
7
- it "should write binary data length 0-15 ::= [x20-x2f] <binary-data>" do
8
- (0..15).to_a.each do |len|
9
- val = ['b' * len].pack('a*')
10
- bin = Hessian2.write(Hessian2::TypeWrapper.new(:bin, val))
11
-
12
- expect(bin[0].unpack('C').first - 0x20).to eq(val.size)
13
- expect(bin.size).to eq(1 + val.size)
14
- expect(Hessian2.parse(bin)).to eq(val)
15
- end
16
- end
17
-
18
-
19
- it "should write binary data length 0-1023 ::= [x34-x37] <binary-data>" do
20
- [ 16, 256, 512, 1023 ].each do |len|
21
- val = ['b' * len].pack('a*')
22
- bin = Hessian2.write(Hessian2::TypeWrapper.new(:bin, val))
23
-
24
- b1, b0 = bin[0, 2].unpack('CC')
25
- expect(256 * (b1 - 0x34) + b0).to eq(val.size)
26
- expect(bin.size).to eq(2 + val.size)
27
- expect(Hessian2.parse(bin)).to eq(val)
28
- end
29
- end
30
-
31
-
32
- it "should write 8-bit binary data non-final chunk ('A') ::= x41 b1 b0 <binary-data> and 8-bit binary data final chunk ('B') ::= 'B' b1 b0 <binary-data>" do
33
- val = IO.binread(File.expand_path("../Lighthouse.jpg", __FILE__))
34
- bin = Hessian2.write(Hessian2::TypeWrapper.new(:bin, val))
35
- chunks = val.size / 0x8000
36
-
37
- chunks.times do |c|
38
- i = c * 0x8003
39
- expect(bin[i]).to eq('A')
40
- expect(bin[i + 1, 2].unpack('n').first).to eq(0x8000)
41
- end
42
-
43
- i = chunks * 0x8003
44
- expect(bin[i]).to eq('B')
45
- expect(bin[i + 1, 2].unpack('n').first).to eq(val.size - 0x8000 * chunks)
46
- expect(Hessian2.parse(bin)).to eq(val)
47
- end
48
-
49
- end
50
- end
51
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ module Hessian2
4
+ describe Writer do
5
+ context "when binary" do
6
+
7
+ it "should write binary data length 0-15 ::= [x20-x2f] <binary-data>" do
8
+ (0..15).to_a.each do |len|
9
+ val = ['b' * len].pack('a*')
10
+ bin = Hessian2.write(Hessian2::TypeWrapper.new(:bin, val))
11
+
12
+ expect(bin[0].unpack('C').first - 0x20).to eq(val.size)
13
+ expect(bin.size).to eq(1 + val.size)
14
+ expect(Hessian2.parse(bin)).to eq(val)
15
+ end
16
+ end
17
+
18
+
19
+ it "should write binary data length 0-1023 ::= [x34-x37] <binary-data>" do
20
+ [ 16, 256, 512, 1023 ].each do |len|
21
+ val = ['b' * len].pack('a*')
22
+ bin = Hessian2.write(Hessian2::TypeWrapper.new(:bin, val))
23
+
24
+ b1, b0 = bin[0, 2].unpack('CC')
25
+ expect(256 * (b1 - 0x34) + b0).to eq(val.size)
26
+ expect(bin.size).to eq(2 + val.size)
27
+ expect(Hessian2.parse(bin)).to eq(val)
28
+ end
29
+ end
30
+
31
+
32
+ it "should write 8-bit binary data non-final chunk ('A') ::= x41 b1 b0 <binary-data> and 8-bit binary data final chunk ('B') ::= 'B' b1 b0 <binary-data>" do
33
+ val = IO.binread(File.expand_path("../Lighthouse.jpg", __FILE__))
34
+ bin = Hessian2.write(Hessian2::TypeWrapper.new(:bin, val))
35
+ chunks = val.size / 0x8000
36
+
37
+ chunks.times do |c|
38
+ i = c * 0x8003
39
+ expect(bin[i]).to eq('A')
40
+ expect(bin[i + 1, 2].unpack('n').first).to eq(0x8000)
41
+ end
42
+
43
+ i = chunks * 0x8003
44
+ expect(bin[i]).to eq('B')
45
+ expect(bin[i + 1, 2].unpack('n').first).to eq(val.size - 0x8000 * chunks)
46
+ expect(Hessian2.parse(bin)).to eq(val)
47
+ end
48
+
49
+ end
50
+ end
51
+ end
data/spec/boolean_spec.rb CHANGED
@@ -1,26 +1,26 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- module Hessian2
4
- describe Writer do
5
- context "when boolean" do
6
-
7
- it "should write true ::= 'T'" do
8
- val = true
9
- bin = Hessian2.write(val)
10
-
11
- expect(bin).to eq('T')
12
- expect(Hessian2.parse(bin)).to eq(val)
13
- end
14
-
15
-
16
- it "should write false ::= 'F'" do
17
- val = false
18
- bin = Hessian2.write(val)
19
-
20
- expect(bin).to eq('F')
21
- expect(Hessian2.parse(bin)).to eq(val)
22
- end
23
-
24
- end
25
- end
26
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ module Hessian2
4
+ describe Writer do
5
+ context "when boolean" do
6
+
7
+ it "should write true ::= 'T'" do
8
+ val = true
9
+ bin = Hessian2.write(val)
10
+
11
+ expect(bin).to eq('T')
12
+ expect(Hessian2.parse(bin)).to eq(val)
13
+ end
14
+
15
+
16
+ it "should write false ::= 'F'" do
17
+ val = false
18
+ bin = Hessian2.write(val)
19
+
20
+ expect(bin).to eq('F')
21
+ expect(Hessian2.parse(bin)).to eq(val)
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -1,52 +1,52 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- module Hessian2
4
- describe ClassWrapper do
5
- hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
6
-
7
- it "should raise error" do
8
- expect(lambda{ Hessian2::ClassWrapper.new('com.sun.java.Monkey') }).to raise_error
9
- expect(lambda{ Hessian2::ClassWrapper.new('com.sun.java.Monkey', 59) }).to raise_error
10
- end
11
-
12
-
13
- it "should wrap nil" do
14
- bin = Hessian2.write(Hessian2::ClassWrapper.new('com.sun.java.Monkey', nil))
15
-
16
- expect(bin).to eq('N')
17
- expect(Hessian2.parse(bin)).to eq(nil)
18
- end
19
-
20
-
21
- it "should wrap hash, monkey, another monkey" do
22
- bin = Hessian2.write(Hessian2::ClassWrapper.new('com.sun.java.Monkey', hash))
23
-
24
- [ hash, Monkey.new(hash), AnotherMonkey.new(hash) ].each do |val|
25
- bin = Hessian2.write(Hessian2::ClassWrapper.new('com.sun.java.Monkey', val))
26
-
27
- bytes = bin.each_byte
28
- expect([ bytes.next ].pack('C')).to eq('C')
29
- expect(Hessian2.parse_string(bytes)).to eq('com.sun.java.Monkey')
30
- expect(Hessian2.parse_int(bytes)).to eq(4)
31
- 4.times{ Hessian2.parse_string(bytes) }
32
- expect(bytes.next - 0x60).to eq(0)
33
- _monkey = Hessian2.parse(bin)
34
- expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
35
- end
36
- end
37
-
38
-
39
- it "should wrap array" do
40
- arr = [nil, hash, Monkey.new(hash), AnotherMonkey.new(hash)]
41
- bin = Hessian2.write(Hessian2::ClassWrapper.new('[com.sun.java.Monkey', arr))
42
-
43
- _monkey1, _monkey2, _monkey3, _monkey4 = Hessian2.parse(bin)
44
- expect(_monkey1).to eq(nil)
45
- [ _monkey2, _monkey3, _monkey4 ].each do |_monkey|
46
- expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
47
- end
48
-
49
- end
50
-
51
- end
52
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ module Hessian2
4
+ describe ClassWrapper do
5
+ hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
6
+
7
+ it "should raise error" do
8
+ expect(lambda{ Hessian2::ClassWrapper.new('com.sun.java.Monkey') }).to raise_error
9
+ expect(lambda{ Hessian2::ClassWrapper.new('com.sun.java.Monkey', 59) }).to raise_error
10
+ end
11
+
12
+
13
+ it "should wrap nil" do
14
+ bin = Hessian2.write(Hessian2::ClassWrapper.new('com.sun.java.Monkey', nil))
15
+
16
+ expect(bin).to eq('N')
17
+ expect(Hessian2.parse(bin)).to eq(nil)
18
+ end
19
+
20
+
21
+ it "should wrap hash, monkey, another monkey" do
22
+ bin = Hessian2.write(Hessian2::ClassWrapper.new('com.sun.java.Monkey', hash))
23
+
24
+ [ hash, Monkey.new(hash), AnotherMonkey.new(hash) ].each do |val|
25
+ bin = Hessian2.write(Hessian2::ClassWrapper.new('com.sun.java.Monkey', val))
26
+
27
+ bytes = bin.each_byte
28
+ expect([ bytes.next ].pack('C')).to eq('C')
29
+ expect(Hessian2.parse_string(bytes)).to eq('com.sun.java.Monkey')
30
+ expect(Hessian2.parse_int(bytes)).to eq(4)
31
+ 4.times{ Hessian2.parse_string(bytes) }
32
+ expect(bytes.next - 0x60).to eq(0)
33
+ _monkey = Hessian2.parse(bin)
34
+ expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
35
+ end
36
+ end
37
+
38
+
39
+ it "should wrap array" do
40
+ arr = [nil, hash, Monkey.new(hash), AnotherMonkey.new(hash)]
41
+ bin = Hessian2.write(Hessian2::ClassWrapper.new('[com.sun.java.Monkey', arr))
42
+
43
+ _monkey1, _monkey2, _monkey3, _monkey4 = Hessian2.parse(bin)
44
+ expect(_monkey1).to eq(nil)
45
+ [ _monkey2, _monkey3, _monkey4 ].each do |_monkey|
46
+ expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
@@ -1,14 +1,14 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- ActiveRecord::Base.connection.execute("drop table if exists monkeys")
4
- ActiveRecord::Base.connection.execute("create table monkeys(id integer(11) not null auto_increment, born_at datetime, name varchar(255), price decimal(10, 2), primary key(id)) charset=utf8")
5
-
6
- born_at = Time.new(1989, 5, 8)
7
- 1000.times.each do |i|
8
- Monkey.create(
9
- id: i + 1,
10
- name: "#{i}号猴",
11
- price: 0.25 * i,
12
- born_at: born_at + 86400 * i
13
- )
14
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ ActiveRecord::Base.connection.execute("drop table if exists monkeys")
4
+ ActiveRecord::Base.connection.execute("create table monkeys(id integer(11) not null auto_increment, born_at datetime, name varchar(255), price decimal(10, 2), primary key(id)) charset=utf8")
5
+
6
+ born_at = Time.new(1989, 5, 8)
7
+ 1000.times.each do |i|
8
+ Monkey.create(
9
+ id: i + 1,
10
+ name: "#{i}号猴",
11
+ price: 0.25 * i,
12
+ born_at: born_at + 86400 * i
13
+ )
14
+ end
data/spec/date_spec.rb CHANGED
@@ -1,45 +1,45 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- module Hessian2
4
- describe Writer do
5
- context "when date" do
6
-
7
- it "should write 32-bit UTC minute date ::= x4b b3 b2 b1 b0" do
8
- val = Time.new(2008, 8, 8, 8, 8)
9
- bin = Hessian2.write(val)
10
-
11
- expect(bin[0].unpack('C').first).to eq(0x4b)
12
- expect(Time.at(bin[1, 4].unpack('l>').first * 60)).to eq(val)
13
- expect(Hessian2.parse(bin)).to eq(val)
14
- end
15
-
16
-
17
- it "should write 64-bit UTC millisecond date ::= x4a b7 b6 b5 b4 b3 b2 b1 b0" do
18
- val = Time.new(2008, 8, 8, 8, 8, 8)
19
- bin = Hessian2.write(val)
20
-
21
- expect(bin[0].unpack('C').first).to eq(0x4a)
22
- _val = bin[1, 8].unpack('q>').first
23
- expect(Time.at(_val / 1000, _val % 1000 * 1000)).to eq(val)
24
- expect(Hessian2.parse(bin)).to eq(val)
25
-
26
- val2 = Time.at(946684800, 123456.789)
27
- bin2 = Hessian2.write(val2)
28
-
29
- expect(bin2[0].unpack('C').first).to eq(0x4a)
30
- _val2 = bin2[1, 8].unpack('Q>').first
31
- expect(Time.at(_val2 / 1000, _val2 % 1000 * 1000)).to eq(Time.at(val2.to_i, val2.usec / 1000 * 1000))
32
- expect(Hessian2.parse(bin2)).to eq(Time.at(val2.to_i, val2.usec / 1000 * 1000))
33
-
34
- val3 = Time.at(946684800.2)
35
- bin3 = Hessian2.write(val3)
36
-
37
- expect(bin3[0].unpack('C').first).to eq(0x4a)
38
- _val3 = bin3[1, 8].unpack('Q>').first
39
- expect(Time.at(_val3 / 1000, _val3 % 1000 * 1000)).to eq(Time.at(val3.to_i, val3.usec / 1000 * 1000))
40
- expect(Hessian2.parse(bin3)).to eq(Time.at(val3.to_i, val3.usec / 1000 * 1000))
41
- end
42
-
43
- end
44
- end
45
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ module Hessian2
4
+ describe Writer do
5
+ context "when date" do
6
+
7
+ it "should write 32-bit UTC minute date ::= x4b b3 b2 b1 b0" do
8
+ val = Time.new(2008, 8, 8, 8, 8)
9
+ bin = Hessian2.write(val)
10
+
11
+ expect(bin[0].unpack('C').first).to eq(0x4b)
12
+ expect(Time.at(bin[1, 4].unpack('l>').first * 60)).to eq(val)
13
+ expect(Hessian2.parse(bin)).to eq(val)
14
+ end
15
+
16
+
17
+ it "should write 64-bit UTC millisecond date ::= x4a b7 b6 b5 b4 b3 b2 b1 b0" do
18
+ val = Time.new(2008, 8, 8, 8, 8, 8)
19
+ bin = Hessian2.write(val)
20
+
21
+ expect(bin[0].unpack('C').first).to eq(0x4a)
22
+ _val = bin[1, 8].unpack('q>').first
23
+ expect(Time.at(_val / 1000, _val % 1000 * 1000)).to eq(val)
24
+ expect(Hessian2.parse(bin)).to eq(val)
25
+
26
+ val2 = Time.at(946684800, 123456.789)
27
+ bin2 = Hessian2.write(val2)
28
+
29
+ expect(bin2[0].unpack('C').first).to eq(0x4a)
30
+ _val2 = bin2[1, 8].unpack('Q>').first
31
+ expect(Time.at(_val2 / 1000, _val2 % 1000 * 1000)).to eq(Time.at(val2.to_i, val2.usec / 1000 * 1000))
32
+ expect(Hessian2.parse(bin2)).to eq(Time.at(val2.to_i, val2.usec / 1000 * 1000))
33
+
34
+ val3 = Time.at(946684800.2)
35
+ bin3 = Hessian2.write(val3)
36
+
37
+ expect(bin3[0].unpack('C').first).to eq(0x4a)
38
+ _val3 = bin3[1, 8].unpack('Q>').first
39
+ expect(Time.at(_val3 / 1000, _val3 % 1000 * 1000)).to eq(Time.at(val3.to_i, val3.usec / 1000 * 1000))
40
+ expect(Hessian2.parse(bin3)).to eq(Time.at(val3.to_i, val3.usec / 1000 * 1000))
41
+ end
42
+
43
+ end
44
+ end
45
+ end
data/spec/double_spec.rb CHANGED
@@ -1,78 +1,78 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- module Hessian2
4
- describe Writer do
5
- context "when double" do
6
-
7
- it "should write double 0.0 ::= x5b" do
8
- bin = Hessian2.write(0.0)
9
-
10
- expect(bin.unpack('C').first).to eq(0x5b)
11
- expect(Hessian2.parse(bin)).to eq(0)
12
- end
13
-
14
-
15
- it "should write double 1.0 ::= x5c" do
16
- bin = Hessian2.write(1.0)
17
-
18
- expect(bin.unpack('C').first).to eq(0x5c)
19
- expect(Hessian2.parse(bin)).to eq(1)
20
- end
21
-
22
-
23
- it "should write double represented as byte (-128.0 to 127.0) ::= x5d b0" do
24
- [ -128.0, 127.0 ].each do |val|
25
- bin = Hessian2.write(val)
26
-
27
- expect(bin[0].unpack('C').first).to eq(0x5d)
28
- expect(bin.bytesize).to eq(2)
29
- expect(Hessian2.parse(bin)).to eq(val)
30
- end
31
- end
32
-
33
-
34
- it "should write double represented as short (-32768.0 to 327676.0) ::= x5e b1 b0" do
35
- [ -32768.0, 32767.0, -129.0, 128.0 ].each do |val|
36
- bin = Hessian2.write(val)
37
-
38
- expect(bin[0].unpack('C').first).to eq(0x5e)
39
- expect(bin.bytesize).to eq(3)
40
- expect(Hessian2.parse(bin)).to eq(val)
41
- end
42
- end
43
-
44
-
45
- it "should write double represented as float ::= x5f b3 b2 b1 b0" do
46
- [ -123.456, 123.456 ].each do |val|
47
- bin = Hessian2.write(val)
48
-
49
- expect(bin[0].unpack('C').first).to eq(0x5f)
50
- expect(bin.bytesize).to eq(5)
51
- expect(Hessian2.parse(bin)).to eq(val)
52
- end
53
- end
54
-
55
-
56
- it "should write 64-bit IEEE encoded double ('D') ::= 'D' b7 b6 b5 b4 b3 b2 b1 b0" do
57
- [ 4.9E-324, 1.7976931348623157E308, Float::INFINITY, -Float::INFINITY ].each do |val|
58
- bin = Hessian2.write(val)
59
-
60
- expect(bin[0]).to eq('D')
61
- expect(bin.bytesize).to eq(9)
62
- expect(Hessian2.parse(bin)).to eq(val)
63
- end
64
- end
65
-
66
-
67
- it "should write NAN" do
68
- val = Float::NAN
69
- bin = Hessian2.write(val)
70
-
71
- expect(bin[0]).to eq('D')
72
- expect(bin.bytesize).to eq(9)
73
- expect(Hessian2.parse(bin).nan?).to eq(true)
74
- end
75
-
76
- end
77
- end
78
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ module Hessian2
4
+ describe Writer do
5
+ context "when double" do
6
+
7
+ it "should write double 0.0 ::= x5b" do
8
+ bin = Hessian2.write(0.0)
9
+
10
+ expect(bin.unpack('C').first).to eq(0x5b)
11
+ expect(Hessian2.parse(bin)).to eq(0)
12
+ end
13
+
14
+
15
+ it "should write double 1.0 ::= x5c" do
16
+ bin = Hessian2.write(1.0)
17
+
18
+ expect(bin.unpack('C').first).to eq(0x5c)
19
+ expect(Hessian2.parse(bin)).to eq(1)
20
+ end
21
+
22
+
23
+ it "should write double represented as byte (-128.0 to 127.0) ::= x5d b0" do
24
+ [ -128.0, 127.0 ].each do |val|
25
+ bin = Hessian2.write(val)
26
+
27
+ expect(bin[0].unpack('C').first).to eq(0x5d)
28
+ expect(bin.bytesize).to eq(2)
29
+ expect(Hessian2.parse(bin)).to eq(val)
30
+ end
31
+ end
32
+
33
+
34
+ it "should write double represented as short (-32768.0 to 327676.0) ::= x5e b1 b0" do
35
+ [ -32768.0, 32767.0, -129.0, 128.0 ].each do |val|
36
+ bin = Hessian2.write(val)
37
+
38
+ expect(bin[0].unpack('C').first).to eq(0x5e)
39
+ expect(bin.bytesize).to eq(3)
40
+ expect(Hessian2.parse(bin)).to eq(val)
41
+ end
42
+ end
43
+
44
+
45
+ it "should write double represented as float ::= x5f b3 b2 b1 b0" do
46
+ [ -123.456, 123.456 ].each do |val|
47
+ bin = Hessian2.write(val)
48
+
49
+ expect(bin[0].unpack('C').first).to eq(0x5f)
50
+ expect(bin.bytesize).to eq(5)
51
+ expect(Hessian2.parse(bin)).to eq(val)
52
+ end
53
+ end
54
+
55
+
56
+ it "should write 64-bit IEEE encoded double ('D') ::= 'D' b7 b6 b5 b4 b3 b2 b1 b0" do
57
+ [ 4.9E-324, 1.7976931348623157E308, Float::INFINITY, -Float::INFINITY ].each do |val|
58
+ bin = Hessian2.write(val)
59
+
60
+ expect(bin[0]).to eq('D')
61
+ expect(bin.bytesize).to eq(9)
62
+ expect(Hessian2.parse(bin)).to eq(val)
63
+ end
64
+ end
65
+
66
+
67
+ it "should write NAN" do
68
+ val = Float::NAN
69
+ bin = Hessian2.write(val)
70
+
71
+ expect(bin[0]).to eq('D')
72
+ expect(bin.bytesize).to eq(9)
73
+ expect(Hessian2.parse(bin).nan?).to eq(true)
74
+ end
75
+
76
+ end
77
+ end
78
+ end