summaryrefslogtreecommitdiff
path: root/test/fiddle/test_memory_view.rb
diff options
context:
space:
mode:
authorKenta Murata <mrkn@users.noreply.github.com>2020-12-11 09:41:12 +0900
committerGitHub <noreply@github.com>2020-12-11 09:41:12 +0900
commit9b0c36b39032cffff3c62a2b0e1fc38fa429f5ea (patch)
tree667648b7563a97e5d5270baccd5654d5de91358c /test/fiddle/test_memory_view.rb
parent6b1d2de6cc2e85fda7885fe77dbd7c99c4eb1ef2 (diff)
Import fiddle-1.0.4 (#3860)
I don't use tool/sync_default_gem.rb because the last sync was incomplete. Co-authored-by: Hiroshi SHIBATA <hsbt@ruby-lang.org> Co-authored-by: Alan Wu <XrXr@users.noreply.github.com> Co-authored-by: sinisterchipmunk <sinisterchipmunk@gmail.com> Co-authored-by: Sutou Kouhei <kou@clear-code.com>
Notes
Notes: Merged-By: mrkn <mrkn@ruby-lang.org>
Diffstat (limited to 'test/fiddle/test_memory_view.rb')
-rw-r--r--test/fiddle/test_memory_view.rb115
1 files changed, 115 insertions, 0 deletions
diff --git a/test/fiddle/test_memory_view.rb b/test/fiddle/test_memory_view.rb
new file mode 100644
index 0000000000..3c310c2d68
--- /dev/null
+++ b/test/fiddle/test_memory_view.rb
@@ -0,0 +1,115 @@
+# frozen_string_literal: true
+begin
+ require_relative 'helper'
+rescue LoadError
+end
+
+begin
+ require '-test-/memory_view'
+rescue LoadError
+end
+
+module Fiddle
+ class TestMemoryView < TestCase
+ def setup
+ skip "MemoryView is unavailable" unless defined? Fiddle::MemoryView
+ end
+
+ def test_null_ptr
+ assert_raise(ArgumentError) do
+ MemoryView.new(Fiddle::NULL)
+ end
+ end
+
+ def test_memory_view_from_unsupported_obj
+ obj = Object.new
+ assert_raise(ArgumentError) do
+ MemoryView.new(obj)
+ end
+ end
+
+ def test_memory_view_from_pointer
+ str = Marshal.load(Marshal.dump("hello world"))
+ ptr = Pointer[str]
+ mview = MemoryView.new(ptr)
+ assert_same(ptr, mview.obj)
+ assert_equal(str.length, mview.length)
+ assert_equal(true, mview.readonly?)
+ assert_equal(nil, mview.format)
+ assert_equal(1, mview.item_size)
+ assert_equal(1, mview.ndim)
+ assert_equal(nil, mview.shape)
+ assert_equal(nil, mview.strides)
+ assert_equal(nil, mview.sub_offsets)
+
+ codes = str.codepoints
+ assert_equal(codes, (0...str.length).map {|i| mview[i] })
+ end
+
+ def test_memory_view_multi_dimensional
+ skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
+
+ buf = [ 1, 2, 3, 4,
+ 5, 6, 7, 8,
+ 9, 10, 11, 12 ].pack("l!*")
+ shape = [3, 4]
+ md = MemoryViewTestUtils::MultiDimensionalView.new(buf, "l!", shape, nil)
+ mview = Fiddle::MemoryView.new(md)
+ assert_equal(buf.length, mview.length)
+ assert_equal("l!", mview.format)
+ assert_equal(Fiddle::SIZEOF_LONG, mview.item_size)
+ assert_equal(2, mview.ndim)
+ assert_equal(shape, mview.shape)
+ assert_equal([Fiddle::SIZEOF_LONG*4, Fiddle::SIZEOF_LONG], mview.strides)
+ assert_equal(nil, mview.sub_offsets)
+ assert_equal(1, mview[0, 0])
+ assert_equal(4, mview[0, 3])
+ assert_equal(6, mview[1, 1])
+ assert_equal(10, mview[2, 1])
+ end
+
+ def test_memory_view_multi_dimensional_with_strides
+ skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
+
+ buf = [ 1, 2, 3, 4, 5, 6, 7, 8,
+ 9, 10, 11, 12, 13, 14, 15, 16 ].pack("l!*")
+ shape = [2, 8]
+ strides = [4*Fiddle::SIZEOF_LONG*2, Fiddle::SIZEOF_LONG*2]
+ md = MemoryViewTestUtils::MultiDimensionalView.new(buf, "l!", shape, strides)
+ mview = Fiddle::MemoryView.new(md)
+ assert_equal("l!", mview.format)
+ assert_equal(Fiddle::SIZEOF_LONG, mview.item_size)
+ assert_equal(buf.length, mview.length)
+ assert_equal(2, mview.ndim)
+ assert_equal(shape, mview.shape)
+ assert_equal(strides, mview.strides)
+ assert_equal(nil, mview.sub_offsets)
+ assert_equal(1, mview[0, 0])
+ assert_equal(5, mview[0, 2])
+ assert_equal(9, mview[1, 0])
+ assert_equal(15, mview[1, 3])
+ end
+
+ def test_memory_view_multi_dimensional_with_multiple_members
+ skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
+
+ buf = [ 1, 2, 3, 4, 5, 6, 7, 8,
+ -1, -2, -3, -4, -5, -6, -7, -8].pack("s*")
+ shape = [2, 4]
+ strides = [4*Fiddle::SIZEOF_SHORT*2, Fiddle::SIZEOF_SHORT*2]
+ md = MemoryViewTestUtils::MultiDimensionalView.new(buf, "ss", shape, strides)
+ mview = Fiddle::MemoryView.new(md)
+ assert_equal("ss", mview.format)
+ assert_equal(Fiddle::SIZEOF_SHORT*2, mview.item_size)
+ assert_equal(buf.length, mview.length)
+ assert_equal(2, mview.ndim)
+ assert_equal(shape, mview.shape)
+ assert_equal(strides, mview.strides)
+ assert_equal(nil, mview.sub_offsets)
+ assert_equal([1, 2], mview[0, 0])
+ assert_equal([5, 6], mview[0, 2])
+ assert_equal([-1, -2], mview[1, 0])
+ assert_equal([-7, -8], mview[1, 3])
+ end
+ end
+end