Skip to content

Commit

Permalink
fix tests when mruby was build with UTF-8 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Asmod4n committed Mar 25, 2024
1 parent 808d7f3 commit e56b791
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
10 changes: 10 additions & 0 deletions test/msgpack.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <mruby/msgpack.h>
#include <limits.h>

static mrb_value
mrb_msgpack_test_pack(mrb_state *mrb, mrb_value self)
Expand All @@ -23,4 +24,13 @@ mrb_mruby_simplemsgpack_gem_test(mrb_state *mrb)
mrb_define_module_function(mrb, msgpack_test, "test_unpack", mrb_msgpack_test_unpack, MRB_ARGS_REQ(1));
mrb_define_const(mrb, msgpack_test, "FIXNUM_MAX", mrb_int_value(mrb, MRB_INT_MAX));
mrb_define_const(mrb, msgpack_test, "FIXNUM_MIN", mrb_int_value(mrb, MRB_INT_MIN));
#ifndef MRB_WITHOUT_FLOAT
#ifdef MRB_USE_FLOAT
mrb_define_const(mrb, msgpack_test, "FLOAT_MAX", mrb_float_value(mrb, FLT_MAX));
mrb_define_const(mrb, msgpack_test, "FLOAT_MIN", mrb_float_value(mrb, FLT_MIN));
#else
mrb_define_const(mrb, msgpack_test, "FLOAT_MAX", mrb_float_value(mrb, DBL_MAX));
mrb_define_const(mrb, msgpack_test, "FLOAT_MIN", mrb_float_value(mrb, DBL_MIN));
#endif
#endif
}
23 changes: 11 additions & 12 deletions test/msgpack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

if Object.const_defined? "Float"
assert("Float#to_msgpack") do
floats = [2.2250738585072014e-308, -1.7976931348623157e+308, 1.192092896e-07, 3.402823466e+38, 1.175494351e-38, 1.7976931348623157e+308, -2.2250738585072014e-308]
floats.each do |float|
[MessagePackTest::FLOAT_MIN, -1.0, 0.0, 1.0, MessagePackTest::FLOAT_MAX].each do |float|
assert_equal(float, MessagePack.unpack(float.to_msgpack))
assert_equal(float, MessagePack.unpack(MessagePack.pack(float)))
end
Expand Down Expand Up @@ -73,29 +72,29 @@
packed3 = value3.to_msgpack

packed = packed1 + packed2 + packed3
chunk1 = packed[0..packed1.length+5]
chunk2 = packed[packed1.length+5..-1]
chunk1 = packed.byteslice(0..packed1.bytesize+5)
chunk2 = packed.byteslice(packed1.bytesize+5..-1)

unpacked = []

# unpack everything possible from chunk1
unpacked_length = MessagePack.unpack(chunk1) { |value| unpacked << value }
unpacked_length -= 1
unpacked_bytesize = MessagePack.unpack(chunk1) { |value| unpacked << value }
unpacked_bytesize -= 1

# only value1 can be unpacked
assert_equal(unpacked_length, packed1.length)
assert_equal(unpacked_bytesize, packed1.bytesize)
assert_equal(unpacked, [value1])

# remove unpacked bytes from chunk1
slice_start = unpacked_length
slice_length = chunk1.length - (unpacked_length+1)
chunk1 = chunk1.slice(slice_start, slice_length)
slice_start = unpacked_bytesize
slice_bytesize = chunk1.bytesize - (unpacked_bytesize+1)
chunk1 = chunk1.byteslice(slice_start, slice_bytesize)

# continue unpacking with rest of chunk1 and chunk2
unpacked_length = MessagePack.unpack(chunk1 + chunk2) { |value| unpacked << value }
unpacked_bytesize = MessagePack.unpack(chunk1 + chunk2) { |value| unpacked << value }

# now, everything is unpacked
assert_equal(unpacked_length, chunk1.length + chunk2.length)
assert_equal(unpacked_bytesize, chunk1.bytesize + chunk2.bytesize)
assert_equal(unpacked, [value1, value2, value3])
end

Expand Down

0 comments on commit e56b791

Please sign in to comment.