ntable 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.rdoc +3 -0
- data/README.rdoc +73 -0
- data/Version +1 -0
- data/lib/ntable.rb +46 -0
- data/lib/ntable/axis.rb +220 -0
- data/lib/ntable/errors.rb +75 -0
- data/lib/ntable/structure.rb +625 -0
- data/lib/ntable/table.rb +724 -0
- data/test/tc_axes.rb +124 -0
- data/test/tc_basic_values.rb +129 -0
- data/test/tc_decompose.rb +104 -0
- data/test/tc_enumeration.rb +238 -0
- data/test/tc_json.rb +137 -0
- data/test/tc_nested_object.rb +191 -0
- data/test/tc_reduce.rb +161 -0
- data/test/tc_slice.rb +130 -0
- data/test/tc_structure.rb +202 -0
- metadata +78 -0
data/test/tc_axes.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Axis object tests
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2012 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
require 'minitest/autorun'
|
38
|
+
require 'ntable'
|
39
|
+
|
40
|
+
|
41
|
+
module NTable
|
42
|
+
module Tests # :nodoc:
|
43
|
+
|
44
|
+
class TestAxes < ::MiniTest::Unit::TestCase # :nodoc:
|
45
|
+
|
46
|
+
|
47
|
+
def test_labeled_axis_size
|
48
|
+
axis_ = LabeledAxis.new([:one, :two])
|
49
|
+
assert_equal(2, axis_.size)
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def test_labeled_axis_label_to_index
|
54
|
+
axis_ = LabeledAxis.new([:one, :two])
|
55
|
+
assert_equal(0, axis_.label_to_index(:one))
|
56
|
+
assert_equal(1, axis_.label_to_index(:two))
|
57
|
+
assert_nil(axis_.label_to_index(:three))
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def test_labeled_axis_index_to_label
|
62
|
+
axis_ = LabeledAxis.new([:one, :two])
|
63
|
+
assert_equal('one', axis_.index_to_label(0))
|
64
|
+
assert_equal('two', axis_.index_to_label(1))
|
65
|
+
assert_nil(axis_.index_to_label(2))
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
def test_labeled_axis_equality
|
70
|
+
axis1_ = LabeledAxis.new([:one, :two])
|
71
|
+
axis2_ = LabeledAxis.new([:one, :two])
|
72
|
+
axis3_ = LabeledAxis.new([:one, :three])
|
73
|
+
assert_equal(axis1_, axis2_)
|
74
|
+
refute_equal(axis1_, axis3_)
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
def test_labeled_axis_empty
|
79
|
+
axis_ = LabeledAxis.new([])
|
80
|
+
assert_equal(0, axis_.size)
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
def test_indexed_axis_size
|
85
|
+
axis_ = IndexedAxis.new(2)
|
86
|
+
assert_equal(2, axis_.size)
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
def test_indexed_axis_label_to_index
|
91
|
+
axis_ = IndexedAxis.new(2, 4)
|
92
|
+
assert_equal(0, axis_.label_to_index(4))
|
93
|
+
assert_equal(1, axis_.label_to_index(5))
|
94
|
+
assert_nil(axis_.label_to_index(3))
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
def test_indexed_axis_index_to_label
|
99
|
+
axis_ = IndexedAxis.new(2, 4)
|
100
|
+
assert_equal(4, axis_.index_to_label(0))
|
101
|
+
assert_equal(5, axis_.index_to_label(1))
|
102
|
+
assert_nil(axis_.index_to_label(2))
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
def test_indexed_axis_equality
|
107
|
+
axis1_ = IndexedAxis.new(2, 4)
|
108
|
+
axis2_ = IndexedAxis.new(2, 4)
|
109
|
+
axis3_ = IndexedAxis.new(2, 3)
|
110
|
+
assert_equal(axis1_, axis2_)
|
111
|
+
refute_equal(axis1_, axis3_)
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
def test_indexed_axis_empty
|
116
|
+
axis_ = IndexedAxis.new(0)
|
117
|
+
assert_equal(0, axis_.size)
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Basic table values tests
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2012 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
require 'minitest/autorun'
|
38
|
+
require 'ntable'
|
39
|
+
|
40
|
+
|
41
|
+
module NTable
|
42
|
+
module Tests # :nodoc:
|
43
|
+
|
44
|
+
class TestBasicValues < ::MiniTest::Unit::TestCase # :nodoc:
|
45
|
+
|
46
|
+
|
47
|
+
def setup
|
48
|
+
@labeled_axis = LabeledAxis.new([:red, :white, :blue])
|
49
|
+
@indexed_axis = IndexedAxis.new(10)
|
50
|
+
@structure = Structure.new.add(@indexed_axis, :row).add(@labeled_axis, :column)
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
def test_fill
|
55
|
+
table_ = Table.new(@structure, :fill => 1)
|
56
|
+
assert_equal(1, table_.get(0, :red))
|
57
|
+
assert_equal(1, table_.get(5, :blue))
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def test_load_and_get_from_array
|
62
|
+
table_ = Table.new(@structure, :load => (0..29).to_a)
|
63
|
+
assert_equal(0, table_.get(0, :red))
|
64
|
+
assert_equal(17, table_.get(5, :blue))
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
def test_load_and_get_from_hash
|
69
|
+
table_ = Table.new(@structure, :load => (0..29).to_a)
|
70
|
+
assert_equal(0, table_.get(:row => 0, :column => :red))
|
71
|
+
assert_equal(17, table_.get(:row => 5, :column => :blue))
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
def test_load_bang
|
76
|
+
table_ = Table.new(@structure)
|
77
|
+
table_.load!((0..16).to_a)
|
78
|
+
assert_equal(0, table_.get(0, :red))
|
79
|
+
assert_equal(16, table_.get(5, :white))
|
80
|
+
assert_nil(table_.get(5, :blue))
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
def test_set_from_array
|
85
|
+
table_ = Table.new(@structure)
|
86
|
+
table_.set!(0, :red, "foo")
|
87
|
+
table_[5, :blue] = "bar"
|
88
|
+
assert_equal("foo", table_.get(0, :red))
|
89
|
+
assert_equal("bar", table_[5, :blue])
|
90
|
+
assert_nil(table_.get(5, :white))
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
def test_set_from_hash
|
95
|
+
table_ = Table.new(@structure)
|
96
|
+
table_.set!({:row => 0, :column => :red}, "foo")
|
97
|
+
table_[:column => :blue, :row => 5] = "bar"
|
98
|
+
assert_equal("foo", table_.get(0, :red))
|
99
|
+
assert_equal("bar", table_[5, :blue])
|
100
|
+
assert_nil(table_.get(5, :white))
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
def test_load_no_axes
|
105
|
+
t1_ = Table.new(Structure.new, :load => [1])
|
106
|
+
assert_equal(1, t1_.get)
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
def test_empty_equality
|
111
|
+
assert_equal(Table.new(Structure.new), Table.new(Structure.new))
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
def test_basic_equality
|
116
|
+
t1_ = Table.new(@structure, :fill => 0)
|
117
|
+
t2_ = Table.new(@structure, :fill => 0)
|
118
|
+
assert_equal(t1_, t2_)
|
119
|
+
t1_[:row => 0, :column => :red] = 1
|
120
|
+
refute_equal(t1_, t2_)
|
121
|
+
t2_[:row => 0, :column => :red] = 1
|
122
|
+
assert_equal(t1_, t2_)
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Table decomposition tests
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2012 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
require 'minitest/autorun'
|
38
|
+
require 'ntable'
|
39
|
+
|
40
|
+
|
41
|
+
module NTable
|
42
|
+
module Tests # :nodoc:
|
43
|
+
|
44
|
+
class TestDecompose < ::MiniTest::Unit::TestCase # :nodoc:
|
45
|
+
|
46
|
+
|
47
|
+
def setup
|
48
|
+
@labeled_axis_2 = LabeledAxis.new([:one, :two])
|
49
|
+
@indexed_axis_10 = IndexedAxis.new(10,1)
|
50
|
+
@indexed_axis_0 = IndexedAxis.new(0)
|
51
|
+
@scalar_structure = Structure.new
|
52
|
+
@structure_row = Structure.add(@labeled_axis_2)
|
53
|
+
@structure_1d = Structure.add(@indexed_axis_10)
|
54
|
+
@structure_2d = Structure.add(@indexed_axis_10).add(@labeled_axis_2)
|
55
|
+
@empty_structure = Structure.add(@indexed_axis_0)
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
def test_scalar_decompose
|
60
|
+
t1_ = Table.new(@scalar_structure, :load => [:foo])
|
61
|
+
t2_ = t1_.decompose([])
|
62
|
+
assert_equal(0, t2_.dim)
|
63
|
+
assert_equal(1, t2_.size)
|
64
|
+
assert_equal(t1_, t2_.get)
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
def test_1d_decompose_inner
|
69
|
+
t1_ = Table.new(@structure_1d, :load => (2..11).to_a)
|
70
|
+
t2_ = t1_.decompose([0])
|
71
|
+
assert_equal(0, t2_.dim)
|
72
|
+
assert_equal(1, t2_.size)
|
73
|
+
assert_equal(t1_, t2_.get)
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
def test_1d_decompose_outer
|
78
|
+
t1_ = Table.new(@structure_1d, :load => (2..11).to_a)
|
79
|
+
t2_ = t1_.decompose([])
|
80
|
+
assert_equal(Table.new(@scalar_structure, :load => [2]), t2_.get(1))
|
81
|
+
assert_equal(Table.new(@scalar_structure, :load => [3]), t2_.get(2))
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
def test_2d_decompose
|
86
|
+
t1_ = Table.new(@structure_2d, :load => (2..21).to_a)
|
87
|
+
t2_ = t1_.decompose([1])
|
88
|
+
assert_equal(Table.new(@structure_row, :load => [2, 3]), t2_.get(1))
|
89
|
+
assert_equal(Table.new(@structure_row, :load => [4, 5]), t2_.get(2))
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
def test_2d_decompose_reduce
|
94
|
+
t1_ = Table.new(@structure_2d, :load => (2..21).to_a)
|
95
|
+
t2_ = t1_.decompose_reduce([1], :*)
|
96
|
+
assert_equal(6, t2_.get(1))
|
97
|
+
assert_equal(20, t2_.get(2))
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,238 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Table enumeration (each/map) tests
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2012 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
require 'minitest/autorun'
|
38
|
+
require 'ntable'
|
39
|
+
|
40
|
+
|
41
|
+
module NTable
|
42
|
+
module Tests # :nodoc:
|
43
|
+
|
44
|
+
class TestEnumeration < ::MiniTest::Unit::TestCase # :nodoc:
|
45
|
+
|
46
|
+
|
47
|
+
def setup
|
48
|
+
@labeled_axis_2 = LabeledAxis.new([:one, :two])
|
49
|
+
@indexed_axis_10 = IndexedAxis.new(10,1)
|
50
|
+
@indexed_axis_0 = IndexedAxis.new(0)
|
51
|
+
@scalar_structure = Structure.new
|
52
|
+
@structure_2d = Structure.add(@indexed_axis_10).add(@labeled_axis_2)
|
53
|
+
@empty_structure = Structure.add(@indexed_axis_0)
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
def test_scalar_each
|
58
|
+
t1_ = Table.new(@scalar_structure, :load => [:foo])
|
59
|
+
size_ = 0
|
60
|
+
t1_.each do |v_|
|
61
|
+
assert_equal(:foo, v_)
|
62
|
+
size_ += 1
|
63
|
+
end
|
64
|
+
assert_equal(1, size_)
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
def test_scalar_each_with_position
|
69
|
+
t1_ = Table.new(@scalar_structure, :load => [:foo])
|
70
|
+
size_ = 0
|
71
|
+
t1_.each_with_position do |v_, p_|
|
72
|
+
assert_equal(:foo, v_)
|
73
|
+
assert_equal([], p_.coord_array)
|
74
|
+
size_ += 1
|
75
|
+
end
|
76
|
+
assert_equal(1, size_)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
def test_scalar_map
|
81
|
+
t1_ = Table.new(@scalar_structure, :load => [1])
|
82
|
+
t2_ = t1_.map do |v_|
|
83
|
+
v_ * 2
|
84
|
+
end
|
85
|
+
assert_equal(2, t2_.get)
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
def test_scalar_map_with_position
|
90
|
+
t1_ = Table.new(@scalar_structure, :load => [1])
|
91
|
+
t2_ = t1_.map_with_position do |v_, p_|
|
92
|
+
assert_equal([], p_.coord_array)
|
93
|
+
v_ * 2
|
94
|
+
end
|
95
|
+
assert_equal(2, t2_.get)
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
def test_scalar_map_bang
|
100
|
+
t1_ = Table.new(@scalar_structure, :load => [1])
|
101
|
+
t1_.map! do |v_|
|
102
|
+
v_ * 2
|
103
|
+
end
|
104
|
+
assert_equal(2, t1_.get)
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
def test_scalar_map_with_position_bang
|
109
|
+
t1_ = Table.new(@scalar_structure, :load => [1])
|
110
|
+
t1_.map_with_position! do |v_, p_|
|
111
|
+
assert_equal([], p_.coord_array)
|
112
|
+
v_ * 2
|
113
|
+
end
|
114
|
+
assert_equal(2, t1_.get)
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
def test_2d_each
|
119
|
+
t1_ = Table.new(@structure_2d, :load => (2..21).to_a)
|
120
|
+
size_ = 2
|
121
|
+
t1_.each do |v_|
|
122
|
+
assert_equal(size_, v_)
|
123
|
+
size_ += 1
|
124
|
+
end
|
125
|
+
assert_equal(22, size_)
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
def test_2d_each_with_position
|
130
|
+
t1_ = Table.new(@structure_2d, :load => (2..21).to_a)
|
131
|
+
size_ = 2
|
132
|
+
label1_ = 'one'
|
133
|
+
label2_ = 1
|
134
|
+
t1_.each_with_position do |v_, p_|
|
135
|
+
assert_equal([label2_, label1_], p_.coord_array)
|
136
|
+
assert_equal(size_, v_)
|
137
|
+
size_ += 1
|
138
|
+
if label1_ == 'one'
|
139
|
+
label1_ = 'two'
|
140
|
+
else
|
141
|
+
label1_ = 'one'
|
142
|
+
label2_ += 1
|
143
|
+
end
|
144
|
+
end
|
145
|
+
assert_equal(22, size_)
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
def test_2d_map
|
150
|
+
t1_ = Table.new(@structure_2d, :load => (2..21).to_a)
|
151
|
+
t2_ = t1_.map do |v_|
|
152
|
+
v_ * 2
|
153
|
+
end
|
154
|
+
assert_equal(4, t2_.get(1, :one))
|
155
|
+
assert_equal(6, t2_.get(1, :two))
|
156
|
+
assert_equal(42, t2_.get(10, :two))
|
157
|
+
end
|
158
|
+
|
159
|
+
|
160
|
+
def test_2d_map_with_position
|
161
|
+
t1_ = Table.new(@structure_2d, :load => (2..21).to_a)
|
162
|
+
label1_ = 'one'
|
163
|
+
label2_ = 1
|
164
|
+
t2_ = t1_.map_with_position do |v_, p_|
|
165
|
+
assert_equal([label2_, label1_], p_.coord_array)
|
166
|
+
if label1_ == 'one'
|
167
|
+
label1_ = 'two'
|
168
|
+
else
|
169
|
+
label1_ = 'one'
|
170
|
+
label2_ += 1
|
171
|
+
end
|
172
|
+
v_ * 2
|
173
|
+
end
|
174
|
+
assert_equal(4, t2_.get(1, :one))
|
175
|
+
assert_equal(6, t2_.get(1, :two))
|
176
|
+
assert_equal(42, t2_.get(10, :two))
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
def test_2d_map_bang
|
181
|
+
t1_ = Table.new(@structure_2d, :load => (2..21).to_a)
|
182
|
+
t1_.map! do |v_|
|
183
|
+
v_ * 2
|
184
|
+
end
|
185
|
+
assert_equal(4, t1_.get(1, :one))
|
186
|
+
assert_equal(6, t1_.get(1, :two))
|
187
|
+
assert_equal(42, t1_.get(10, :two))
|
188
|
+
end
|
189
|
+
|
190
|
+
|
191
|
+
def test_2d_map_with_position_bang
|
192
|
+
t1_ = Table.new(@structure_2d, :load => (2..21).to_a)
|
193
|
+
label1_ = 'one'
|
194
|
+
label2_ = 1
|
195
|
+
t1_.map_with_position! do |v_, p_|
|
196
|
+
assert_equal([label2_, label1_], p_.coord_array)
|
197
|
+
if label1_ == 'one'
|
198
|
+
label1_ = 'two'
|
199
|
+
else
|
200
|
+
label1_ = 'one'
|
201
|
+
label2_ += 1
|
202
|
+
end
|
203
|
+
v_ * 2
|
204
|
+
end
|
205
|
+
assert_equal(4, t1_.get(1, :one))
|
206
|
+
assert_equal(6, t1_.get(1, :two))
|
207
|
+
assert_equal(42, t1_.get(10, :two))
|
208
|
+
end
|
209
|
+
|
210
|
+
|
211
|
+
def test_empty
|
212
|
+
t1_ = Table.new(@empty_structure)
|
213
|
+
t1_.each do |v_|
|
214
|
+
flunk
|
215
|
+
end
|
216
|
+
t1_.each_with_position do |v_, p_|
|
217
|
+
flunk
|
218
|
+
end
|
219
|
+
t1_.map do |v_|
|
220
|
+
flunk
|
221
|
+
end
|
222
|
+
t1_.map_with_position do |v_, p_|
|
223
|
+
flunk
|
224
|
+
end
|
225
|
+
t1_.map! do |v_|
|
226
|
+
flunk
|
227
|
+
end
|
228
|
+
t1_.map_with_position! do |v_, p_|
|
229
|
+
flunk
|
230
|
+
end
|
231
|
+
pass
|
232
|
+
end
|
233
|
+
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
end
|
238
|
+
end
|