-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnmatrix_test.rb
75 lines (63 loc) · 1.94 KB
/
nmatrix_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require_relative 'test_helper'
class NMatrix::CreationTest < Minitest::Test
def setup
@i = NMatrix.new [2,2],[1, 4.2, 3, 4]
@b = NMatrix.new [2,2],[true, true, false, true], :nm_bool
@m = NMatrix.new [2,2,2],[1, 2, 3, 4, 5, 6, 7, 8]
@m_int = NMatrix.new [2,2,2],[1, 2, 3, 4, 5, 6, 7, 8], :nm_int
@n = NMatrix.new [2,1,2],[1, 2, 3, 4]
@s = NMatrix.new [2, 2],[1, 2, 3, 4]
@s_int = NMatrix.new [2, 2],[1, 2, 3, 4], :nm_int
end
def test_dims
assert_equal [2,2], @i.shape
assert_equal [2,2], @b.shape
assert_equal [2,2,2], @m.shape
assert_equal [2,1,2], @n.shape
end
def test_dtype
assert_equal :nm_float64, @i.dtype
end
def test_stype
assert_equal :nm_dense, @i.stype
end
def test_elements
assert_equal [1, 4.2, 3, 4], @i.elements
assert_equal [true, true, false, true], @b.elements
assert_equal [1, 2, 3, 4, 5, 6, 7, 8], @m.elements
assert_equal [1, 2, 3, 4], @n.elements
end
def test_dim
assert_equal 2, @i.dim
assert_equal 3, @m.dim
end
def test_accessor_get
assert_equal @i[0,1], 4.2
assert_equal @m[0,0,1], 2
assert_equal @n[1,0,0], 3
assert_equal @n[1,0,1], 4
end
def test_accessor_set
@i[0,0] = 6
assert_equal @i[0,0], 6
@m[1,1,0] = 11
assert_equal @m[1,1,0], 11
@n[0,0,1] = 12
assert_equal @n[0,0,1], 12
end
def test_slicing
assert_equal @m[0, 0..1, 0..1], @s
assert_equal @m_int[0, 0..1, 0..1], @s_int
end
def test_serializing
serialized_data_float = Marshal.dump(@m)
deserialized_data_float = Marshal.load(serialized_data_float)
serialized_data_int = Marshal.dump(@m_int)
deserialized_data_int = Marshal.load(serialized_data_int)
serialized_data_bool = Marshal.dump(@b)
deserialized_data_bool = Marshal.load(serialized_data_bool)
assert_equal @m, deserialized_data_float
assert_equal @m_int, deserialized_data_int
assert_equal @b, deserialized_data_bool
end
end