diff options
author | Sutou Kouhei <kou@clear-code.com> | 2020-07-09 20:19:20 +0900 |
---|---|---|
committer | Sutou Kouhei <kou@cozmixng.org> | 2020-11-18 09:05:13 +0900 |
commit | ae7b53546ca18b56c23f612b6935e98268a07602 (patch) | |
tree | 6669206356ba05e4ee4d0e34dd99901ad1ea268c /test/fiddle | |
parent | 64926d500782cadf578724c3d1e7f59e7aaf200f (diff) |
[ruby/fiddle] Add TYPE_CONST_STRING and SIZEOF_CONST_STRING for "const char *"
Add rb_fiddle_ prefix to conversion functions.h to keep backward
compatibility but value_to_generic() isn't safe for TYPE_CONST_STRING
and not String src. Use rb_fiddle_value_to_generic() instead.
https://github.com/ruby/fiddle/commit/0ffcaa39e5
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3780
Diffstat (limited to 'test/fiddle')
-rw-r--r-- | test/fiddle/test_func.rb | 23 | ||||
-rw-r--r-- | test/fiddle/test_import.rb | 13 |
2 files changed, 27 insertions, 9 deletions
diff --git a/test/fiddle/test_func.rb b/test/fiddle/test_func.rb index 29a0a455f1..ca9d4ccb34 100644 --- a/test/fiddle/test_func.rb +++ b/test/fiddle/test_func.rb @@ -98,7 +98,7 @@ module Fiddle [ TYPE_VOIDP, TYPE_SIZE_T, - TYPE_VOIDP, + TYPE_CONST_STRING, TYPE_VARIADIC, ], TYPE_INT) @@ -107,20 +107,31 @@ module Fiddle written = snprintf.call(output, output.size, - "int: %d, string: %.*s\n", + "int: %d, string: %.*s, const string: %s\n", TYPE_INT, -29, TYPE_INT, 4, - TYPE_VOIDP, "Hello") - assert_equal("int: -29, string: Hell\n", + TYPE_VOIDP, "Hello", + TYPE_CONST_STRING, "World") + assert_equal("int: -29, string: Hell, const string: World\n", output_buffer[0, written]) + string_like_class = Class.new do + def initialize(string) + @string = string + end + + def to_str + @string + end + end written = snprintf.call(output, output.size, - "string: %.*s, uint: %u\n", + "string: %.*s, const string: %s, uint: %u\n", TYPE_INT, 2, TYPE_VOIDP, "Hello", + TYPE_CONST_STRING, string_like_class.new("World"), TYPE_INT, 29) - assert_equal("string: He, uint: 29\n", + assert_equal("string: He, const string: World, uint: 29\n", output_buffer[0, written]) end end diff --git a/test/fiddle/test_import.rb b/test/fiddle/test_import.rb index c56d81c351..4afd8e5562 100644 --- a/test/fiddle/test_import.rb +++ b/test/fiddle/test_import.rb @@ -112,10 +112,17 @@ module Fiddle Fiddle.constants.grep(/\ATYPE_(?!VOID|VARIADIC\z)(.*)/) do type = $& - size = Fiddle.const_get("SIZEOF_#{$1}") - name = $1.sub(/P\z/,"*").gsub(/_(?!T\z)/, " ").downcase + const_type_name = $1 + size = Fiddle.const_get("SIZEOF_#{const_type_name}") + if const_type_name == "CONST_STRING" + name = "const_string" + type_name = "const char*" + else + name = $1.sub(/P\z/,"*").gsub(/_(?!T\z)/, " ").downcase + type_name = name + end define_method("test_sizeof_#{name}") do - assert_equal(size, Fiddle::Importer.sizeof(name), type) + assert_equal(size, Fiddle::Importer.sizeof(type_name), type) end end |