1#!/usr/bin/ruby 2# 3# generated_code.rb is in the same directory as this test. 4$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) 5 6old_gc = GC.stress 7# Ruby 2.7.0 - 2.7.1 has a GC bug in its parser, so turn off stress for now 8# See https://bugs.ruby-lang.org/issues/16807 9GC.stress = 0x01 | 0x04 unless RUBY_VERSION.match?(/^2\.7\./) 10require 'generated_code_pb' 11require 'generated_code_proto2_pb' 12GC.stress = old_gc 13 14require 'test/unit' 15 16class GCTest < Test::Unit::TestCase 17 def get_msg_proto3 18 A::B::C::TestMessage.new( 19 :optional_int32 => 1, 20 :optional_int64 => 1, 21 :optional_uint32 => 1, 22 :optional_uint64 => 1, 23 :optional_bool => true, 24 :optional_double => 1.0, 25 :optional_float => 1.0, 26 :optional_string => "a", 27 :optional_bytes => "b", 28 :optional_enum => A::B::C::TestEnum::A, 29 :optional_msg => A::B::C::TestMessage.new(), 30 :repeated_int32 => [1], 31 :repeated_int64 => [1], 32 :repeated_uint32 => [1], 33 :repeated_uint64 => [1], 34 :repeated_bool => [true], 35 :repeated_double => [1.0], 36 :repeated_float => [1.0], 37 :repeated_string => ["a"], 38 :repeated_bytes => ["b"], 39 :repeated_enum => [A::B::C::TestEnum::A], 40 :repeated_msg => [A::B::C::TestMessage.new()], 41 :map_int32_string => {1 => "a"}, 42 :map_int64_string => {1 => "a"}, 43 :map_uint32_string => {1 => "a"}, 44 :map_uint64_string => {1 => "a"}, 45 :map_bool_string => {true => "a"}, 46 :map_string_string => {"a" => "a"}, 47 :map_string_msg => {"a" => A::B::C::TestMessage.new()}, 48 :map_string_int32 => {"a" => 1}, 49 :map_string_bool => {"a" => true}, 50 ) 51 end 52 53 def get_msg_proto2 54 A::B::Proto2::TestMessage.new( 55 :optional_int32 => 1, 56 :optional_int64 => 1, 57 :optional_uint32 => 1, 58 :optional_uint64 => 1, 59 :optional_bool => true, 60 :optional_double => 1.0, 61 :optional_float => 1.0, 62 :optional_string => "a", 63 :optional_bytes => "b", 64 :optional_enum => A::B::Proto2::TestEnum::A, 65 :optional_msg => A::B::Proto2::TestMessage.new(), 66 :repeated_int32 => [1], 67 :repeated_int64 => [1], 68 :repeated_uint32 => [1], 69 :repeated_uint64 => [1], 70 :repeated_bool => [true], 71 :repeated_double => [1.0], 72 :repeated_float => [1.0], 73 :repeated_string => ["a"], 74 :repeated_bytes => ["b"], 75 :repeated_enum => [A::B::Proto2::TestEnum::A], 76 :repeated_msg => [A::B::Proto2::TestMessage.new()], 77 :required_int32 => 1, 78 :required_int64 => 1, 79 :required_uint32 => 1, 80 :required_uint64 => 1, 81 :required_bool => true, 82 :required_double => 1.0, 83 :required_float => 1.0, 84 :required_string => "a", 85 :required_bytes => "b", 86 :required_enum => A::B::Proto2::TestEnum::A, 87 :required_msg => A::B::Proto2::TestMessage.new(), 88 ) 89 end 90 91 def test_generated_msg 92 old_gc = GC.stress 93 GC.stress = 0x01 | 0x04 94 from = get_msg_proto3 95 data = A::B::C::TestMessage.encode(from) 96 to = A::B::C::TestMessage.decode(data) 97 98 from = get_msg_proto2 99 data = A::B::Proto2::TestMessage.encode(from) 100 to = A::B::Proto2::TestMessage.decode(data) 101 GC.stress = old_gc 102 puts "passed" 103 end 104end 105