summaryrefslogtreecommitdiff
path: root/test/fiddle/test_import.rb
diff options
context:
space:
mode:
authorsinisterchipmunk <sinisterchipmunk@gmail.com>2020-01-22 02:55:16 -0500
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-05-23 14:29:16 +0900
commit4a835621ced099316850a16f2a13534dea62a0b8 (patch)
tree5cd9aa7b8d9f2c9f76874176bfe1aa802f608f03 /test/fiddle/test_import.rb
parentaa1d3c7d2c020ec927acaa487e8593172fb64bb0 (diff)
[ruby/fiddle] Make array access override compatible with base class (#25)
* Allow access to a struct's underlying memory with `struct[offset, length]`. * Make accessing a struct's underlying memory more convenient. * refactor memory access unit tests for improved clarity https://github.com/ruby/fiddle/commit/c082c81bb5
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3068
Diffstat (limited to 'test/fiddle/test_import.rb')
-rw-r--r--test/fiddle/test_import.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/test/fiddle/test_import.rb b/test/fiddle/test_import.rb
index 99294ea161..778327b4b2 100644
--- a/test/fiddle/test_import.rb
+++ b/test/fiddle/test_import.rb
@@ -54,6 +54,28 @@ module Fiddle
assert_match(/call dlload before/, err.message)
end
+ def test_struct_memory_access()
+ # check memory operations performed directly on struct
+ my_struct = Fiddle::Importer.struct(['int id']).malloc
+ my_struct[0, Fiddle::SIZEOF_INT] = "\x01".b * Fiddle::SIZEOF_INT
+ assert_equal 0x01010101, my_struct.id
+
+ my_struct.id = 0
+ assert_equal "\x00".b * Fiddle::SIZEOF_INT, my_struct[0, Fiddle::SIZEOF_INT]
+ end
+
+ def test_struct_ptr_array_subscript_multiarg()
+ # check memory operations performed on struct#to_ptr
+ struct = Fiddle::Importer.struct([ 'int x' ]).malloc
+ ptr = struct.to_ptr
+
+ struct.x = 0x02020202
+ assert_equal("\x02".b * Fiddle::SIZEOF_INT, ptr[0, Fiddle::SIZEOF_INT])
+
+ ptr[0, Fiddle::SIZEOF_INT] = "\x01".b * Fiddle::SIZEOF_INT
+ assert_equal 0x01010101, struct.x
+ end
+
def test_malloc()
s1 = LIBC::Timeval.malloc()
s2 = LIBC::Timeval.malloc()