Skip to content

Commit 49440b8

Browse files
committed
Add test for Ractor
1 parent 6f5c912 commit 49440b8

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

test/ractor_test.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require_relative "test_helper"
2+
3+
class NArrayRactorTest < NArrayTestBase
4+
def setup
5+
super
6+
skip unless defined? Ractor
7+
end
8+
9+
data(:dtype, TYPES, keep: true)
10+
def test_non_frozen(data)
11+
dtype = data.fetch(:dtype)
12+
ary = random_array(dtype)
13+
r = Ractor.new(ary) {|x| x }
14+
ary2 = r.take
15+
assert_equal(ary, ary2)
16+
assert_not_same(ary, ary2)
17+
end
18+
19+
def test_frozen(data)
20+
dtype = data.fetch(:dtype)
21+
ary1 = random_array(dtype)
22+
ary1.freeze
23+
r = Ractor.new(ary1) do |ary2|
24+
[ary2, ary2 * 10]
25+
end
26+
ary2, res = r.take
27+
assert_same(ary1, ary2)
28+
assert_equal(ary1*10, res)
29+
end
30+
31+
def test_parallel(data)
32+
dtype = data.fetch(:dtype)
33+
ary1 = random_array(dtype, 100000)
34+
r1 = Ractor.new(ary1) do |ary2|
35+
ary2 * 10
36+
end
37+
r2 = Ractor.new(ary1) do |ary4|
38+
ary4 * 10
39+
end
40+
assert_equal(r1.take, r2.take)
41+
end
42+
43+
def random_array(dtype, n=1000)
44+
case dtype
45+
when Numo::DFloat, Numo::SFloat, Numo::DComplex, Numo::SComplex
46+
dtype.new(n).rand_norm
47+
else
48+
dtype.new(n).rand(10)
49+
end
50+
end
51+
end

test/test_helper.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,25 @@
22
require "numo/narray"
33
require "test/unit"
44
require "stringio"
5+
6+
class NArrayTestBase < Test::Unit::TestCase
7+
FLOAT_TYPES = [
8+
Numo::DFloat,
9+
Numo::DComplex,
10+
]
11+
12+
TYPES = [
13+
*FLOAT_TYPES,
14+
Numo::SFloat,
15+
Numo::SComplex,
16+
Numo::Int64,
17+
Numo::Int32,
18+
Numo::Int16,
19+
Numo::Int8,
20+
Numo::UInt64,
21+
Numo::UInt32,
22+
Numo::UInt16,
23+
Numo::UInt8,
24+
Numo::RObject,
25+
]
26+
end

0 commit comments

Comments
 (0)