ntable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,137 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Test serialization as JSON
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 TestJSON < ::MiniTest::Unit::TestCase # :nodoc:
45
+
46
+
47
+ def setup
48
+ @labeled_axis_2 = LabeledAxis.new([:one, :two])
49
+ @labeled_axis_3 = LabeledAxis.new([:red, :white, :blue])
50
+ @indexed_axis_2 = IndexedAxis.new(2)
51
+ @indexed_axis_10 = IndexedAxis.new(10, 1)
52
+ end
53
+
54
+
55
+ def test_labeled_axis_2
56
+ json_ = {}
57
+ @labeled_axis_2.to_json_object(json_)
58
+ assert_equal({'labels' => ['one', 'two']}, json_)
59
+ naxis_ = LabeledAxis.allocate
60
+ naxis_.from_json_object(json_)
61
+ assert_equal(@labeled_axis_2, naxis_)
62
+ end
63
+
64
+
65
+ def test_labeled_axis_3
66
+ json_ = {}
67
+ @labeled_axis_3.to_json_object(json_)
68
+ assert_equal({'labels' => ['red', 'white', 'blue']}, json_)
69
+ naxis_ = LabeledAxis.allocate
70
+ naxis_.from_json_object(json_)
71
+ assert_equal(@labeled_axis_3, naxis_)
72
+ end
73
+
74
+
75
+ def test_indexed_axis_2
76
+ json_ = {}
77
+ @indexed_axis_2.to_json_object(json_)
78
+ assert_equal({'size' => 2}, json_)
79
+ naxis_ = IndexedAxis.allocate
80
+ naxis_.from_json_object(json_)
81
+ assert_equal(@indexed_axis_2, naxis_)
82
+ end
83
+
84
+
85
+ def test_indexed_axis_10
86
+ json_ = {}
87
+ @indexed_axis_10.to_json_object(json_)
88
+ assert_equal({'size' => 10, 'start' => 1}, json_)
89
+ naxis_ = IndexedAxis.allocate
90
+ naxis_.from_json_object(json_)
91
+ assert_equal(@indexed_axis_10, naxis_)
92
+ end
93
+
94
+
95
+ def test_structure
96
+ structure_ = Structure.add(@indexed_axis_10, 'row').add(@indexed_axis_2).add(@labeled_axis_3, 'col')
97
+ json_ = structure_.to_json_array
98
+ expected_json_ = [
99
+ {'type' => 'indexed', 'name' => 'row', 'size' => 10, 'start' => 1},
100
+ {'type' => 'indexed', 'size' => 2},
101
+ {'type' => 'labeled', 'name' => 'col', 'labels' => ['red', 'white', 'blue']}
102
+ ]
103
+ assert_equal(expected_json_, json_)
104
+ nstructure_ = Structure.from_json_array(json_)
105
+ assert_equal(structure_, nstructure_)
106
+ end
107
+
108
+
109
+ def test_empty_structure
110
+ structure_ = Structure.new
111
+ json_ = structure_.to_json_array
112
+ assert_equal([], json_)
113
+ nstructure_ = Structure.from_json_array(json_)
114
+ assert_equal(structure_, nstructure_)
115
+ end
116
+
117
+
118
+ def test_table_2d_json_object
119
+ table_ = Table.new(Structure.add(@indexed_axis_10, 'row').add(@labeled_axis_3, 'col'),
120
+ :load => (0..29).to_a)
121
+ json_ = table_.to_json_object
122
+ expected_json_ = {
123
+ 'type' => 'ntable',
124
+ 'axes' => [{'type' => 'indexed', 'name' => 'row', 'size' => 10, 'start' => 1},
125
+ {'type' => 'labeled', 'name' => 'col', 'labels' => ['red', 'white', 'blue']}],
126
+ 'values' => (0..29).to_a
127
+ }
128
+ assert_equal(expected_json_, json_)
129
+ ntable_ = Table.from_json_object(json_)
130
+ assert_equal(table_, ntable_)
131
+ end
132
+
133
+
134
+ end
135
+
136
+ end
137
+ end
@@ -0,0 +1,191 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Test serialization as nested objects
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 TestNestedObject < ::MiniTest::Unit::TestCase # :nodoc:
45
+
46
+
47
+ def setup
48
+ @labeled_axis_2 = LabeledAxis.new([:one, :two])
49
+ @labeled_axis_3 = LabeledAxis.new([:blue, :red, :white])
50
+ @indexed_axis_2 = IndexedAxis.new(2)
51
+ @indexed_axis_10 = IndexedAxis.new(10, 1)
52
+ @labeled_axis_0 = LabeledAxis.new([])
53
+ @indexed_axis_0 = IndexedAxis.new(0)
54
+ end
55
+
56
+
57
+ def test_from_empty_labeled
58
+ t1_ = Table.from_nested_object({})
59
+ assert_equal(Table.new(Structure.add(@labeled_axis_0)), t1_)
60
+ end
61
+
62
+
63
+ def test_to_empty_labeled
64
+ t1_ = Table.new(Structure.add(@labeled_axis_0))
65
+ assert_equal({}, t1_.to_nested_object)
66
+ end
67
+
68
+
69
+ def test_from_level_1_labeled
70
+ obj_ = {:one => 1, :two => 2}
71
+ t1_ = Table.from_nested_object(obj_, [{:sort => true}])
72
+ assert_equal(Table.new(Structure.add(@labeled_axis_2), :load => [1,2]), t1_)
73
+ end
74
+
75
+
76
+ def test_to_level_1_labeled
77
+ t1_ = Table.new(Structure.add(@labeled_axis_2), :load => [1,2])
78
+ assert_equal({'one' => 1, 'two' => 2}, t1_.to_nested_object)
79
+ end
80
+
81
+
82
+ def test_from_level_2_labeled
83
+ obj_ = {:one => {:red => 1, :white => 2}, :two => {:white => 3, :blue => 4}}
84
+ t1_ = Table.from_nested_object(obj_, [{:sort => true}, {:sort => true}])
85
+ assert_equal(Table.new(Structure.add(@labeled_axis_2).add(@labeled_axis_3),
86
+ :load => [nil, 1, 2, 4, nil, 3]), t1_)
87
+ t2_ = Table.from_nested_object(obj_, [{:sort => true}, {:sort => true}], :fill => :foo)
88
+ assert_equal(Table.new(Structure.add(@labeled_axis_2).add(@labeled_axis_3),
89
+ :load => [:foo, 1, 2, 4, :foo, 3]), t2_)
90
+ end
91
+
92
+
93
+ def test_to_level_2_labeled
94
+ t1_ = Table.new(Structure.add(@labeled_axis_2).add(@labeled_axis_3),
95
+ :load => [nil, 1, 2, 4, nil, 3])
96
+ assert_equal({'one' => {'red' => 1, 'white' => 2}, 'two' => {'white' => 3, 'blue' => 4}},
97
+ t1_.to_nested_object(:exclude_value => nil))
98
+ assert_equal({'one' => {'red' => 1, 'white' => 2, 'blue' => nil}, 'two' => {'red' => nil, 'white' => 3, 'blue' => 4}},
99
+ t1_.to_nested_object)
100
+ end
101
+
102
+
103
+ def test_from_empty_indexed
104
+ t1_ = Table.from_nested_object([])
105
+ assert_equal(Table.new(Structure.add(@indexed_axis_0)), t1_)
106
+ end
107
+
108
+
109
+ def test_to_empty_indexed
110
+ t1_ = Table.new(Structure.add(@indexed_axis_0))
111
+ assert_equal([], t1_.to_nested_object)
112
+ end
113
+
114
+
115
+ def test_from_level_1_start_0_indexed
116
+ obj_ = [2, 3]
117
+ t1_ = Table.from_nested_object(obj_)
118
+ assert_equal(Table.new(Structure.add(@indexed_axis_2), :load => [2,3]), t1_)
119
+ end
120
+
121
+
122
+ def test_to_level_1_start_0_indexed
123
+ t1_ = Table.new(Structure.add(@indexed_axis_2), :load => [2, 3])
124
+ assert_equal([2, 3], t1_.to_nested_object)
125
+ end
126
+
127
+
128
+ def test_from_level_1_start_1_indexed
129
+ obj_ = [nil] + (2..11).to_a
130
+ t1_ = Table.from_nested_object(obj_)
131
+ assert_equal(Table.new(Structure.add(@indexed_axis_10), :load => (2..11).to_a), t1_)
132
+ end
133
+
134
+
135
+ def test_to_level_1_start_1_indexed
136
+ t1_ = Table.new(Structure.add(@indexed_axis_10), :load => (2..11).to_a)
137
+ assert_equal([nil] + (2..11).to_a, t1_.to_nested_object)
138
+ end
139
+
140
+
141
+ def test_from_level_2_indexed
142
+ obj_ = [[nil, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], []]
143
+ t1_ = Table.from_nested_object(obj_)
144
+ assert_equal(Table.new(Structure.add(@indexed_axis_2).add(@indexed_axis_10),
145
+ :load => (2..11).to_a + ::Array.new(10)), t1_)
146
+ end
147
+
148
+
149
+ def test_to_level_2_indexed
150
+ t1_ = Table.new(Structure.add(@indexed_axis_2).add(@indexed_axis_10),
151
+ :load => (2..21).to_a)
152
+ assert_equal([[nil] + (2..11).to_a, [nil] + (12..21).to_a], t1_.to_nested_object)
153
+ end
154
+
155
+
156
+ def test_from_labeled_within_indexed
157
+ obj_ = [{:red => 1, :white => 2, :blue => 3}, {:red => 4, :white => 5}]
158
+ t1_ = Table.from_nested_object(obj_, [{:name => 'row'}, {:name => 'col', :sort => true}])
159
+ assert_equal(Table.new(Structure.add(@indexed_axis_2, 'row').add(@labeled_axis_3, 'col'),
160
+ :load => [3, 1, 2, nil, 4, 5]), t1_)
161
+ end
162
+
163
+
164
+ def test_to_labeled_within_indexed
165
+ t1_ = Table.new(Structure.add(@indexed_axis_2, 'row').add(@labeled_axis_3, 'col'),
166
+ :load => [3, 1, 2, 6, 4, 5])
167
+ assert_equal([{'red' => 1, 'white' => 2, 'blue' => 3}, {'red' => 4, 'white' => 5, 'blue' => 6}],
168
+ t1_.to_nested_object)
169
+ end
170
+
171
+
172
+ def test_from_indexed_within_labeled
173
+ obj_ = {:red => [1,2], :white => [3], :blue => [4,5]}
174
+ t1_ = Table.from_nested_object(obj_, [{:name => 'row', :sort => true}, {:name => 'col'}])
175
+ assert_equal(Table.new(Structure.add(@labeled_axis_3, 'row').add(@indexed_axis_2, 'col'),
176
+ :load => [4, 5, 1, 2, 3]), t1_)
177
+ end
178
+
179
+
180
+ def test_to_indexed_within_labeled
181
+ t1_ = Table.new(Structure.add(@labeled_axis_3, 'row').add(@indexed_axis_2, 'col'),
182
+ :load => [4, 5, 1, 2, 3, 6])
183
+ assert_equal({'red' => [1,2], 'white' => [3, 6], 'blue' => [4,5]},
184
+ t1_.to_nested_object)
185
+ end
186
+
187
+
188
+ end
189
+
190
+ end
191
+ end
@@ -0,0 +1,161 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Table reduce 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 TestReduce < ::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_1d = Structure.add(@indexed_axis_10)
53
+ @structure_2d = Structure.add(@indexed_axis_10).add(@labeled_axis_2)
54
+ @empty_structure = Structure.add(@indexed_axis_0)
55
+ end
56
+
57
+
58
+ def test_empty_reduce
59
+ t1_ = Table.new(@empty_structure)
60
+ assert_equal(nil, t1_.reduce(:+))
61
+ assert_equal(2, t1_.reduce(2, :+))
62
+ assert_equal(nil, t1_.reduce{ |s_, v_| flunk })
63
+ assert_equal(2, t1_.reduce(2){ |s_, v_| flunk })
64
+ end
65
+
66
+
67
+ def test_empty_reduce_with_position
68
+ t1_ = Table.new(@empty_structure)
69
+ assert_equal(nil, t1_.reduce_with_position{ |s_, v_, p_| flunk })
70
+ assert_equal(2, t1_.reduce_with_position(2){ |s_, v_, p_| flunk })
71
+ end
72
+
73
+
74
+ def test_scalar_reduce
75
+ t1_ = Table.new(@scalar_structure, :load => [2])
76
+ assert_equal(2, t1_.reduce(:+))
77
+ assert_equal(5, t1_.reduce(3, :+))
78
+ assert_equal(2, t1_.reduce{ |s_, v_| flunk })
79
+ assert_equal(5, t1_.reduce(3){ |s_, v_| s_ + v_ })
80
+ end
81
+
82
+
83
+ def test_scalar_reduce_with_position
84
+ t1_ = Table.new(@scalar_structure, :load => [2])
85
+ assert_equal(2, t1_.reduce_with_position{ |s_, v_, p_| flunk })
86
+ assert_equal(5, t1_.reduce_with_position(3){ |s_, v_, p_| assert_equal([], p_.coord_array); s_ + v_ })
87
+ end
88
+
89
+
90
+ def test_1d_reduce
91
+ t1_ = Table.new(@structure_1d, :load => (2..11).to_a)
92
+ assert_equal(65, t1_.reduce(:+))
93
+ assert_equal(165, t1_.reduce(100, :+))
94
+ assert_equal(65, t1_.reduce{ |s_, v_| s_ + v_ })
95
+ assert_equal(165, t1_.reduce(100){ |s_, v_| s_ + v_ })
96
+ end
97
+
98
+
99
+ def test_1d_reduce_with_position
100
+ t1_ = Table.new(@structure_1d, :load => (2..11).to_a)
101
+ label_ = 2
102
+ val_ = t1_.reduce_with_position do |s_, v_, p_|
103
+ assert_equal([label_], p_.coord_array)
104
+ label_ += 1
105
+ s_ + v_
106
+ end
107
+ assert_equal(65, val_)
108
+ label_ = 1
109
+ val_ = t1_.reduce_with_position(100) do |s_, v_, p_|
110
+ assert_equal([label_], p_.coord_array)
111
+ label_ += 1
112
+ s_ + v_
113
+ end
114
+ assert_equal(165, val_)
115
+ end
116
+
117
+
118
+ def test_2d_reduce
119
+ t1_ = Table.new(@structure_2d, :load => (2..21).to_a)
120
+ assert_equal(230, t1_.reduce(:+))
121
+ assert_equal(330, t1_.reduce(100, :+))
122
+ assert_equal(230, t1_.reduce{ |s_, v_| s_ + v_ })
123
+ assert_equal(330, t1_.reduce(100){ |s_, v_| s_ + v_ })
124
+ end
125
+
126
+
127
+ def test_2d_reduce_with_position
128
+ t1_ = Table.new(@structure_2d, :load => (2..21).to_a)
129
+ label1_ = 'two'
130
+ label2_ = 1
131
+ val_ = t1_.reduce_with_position do |s_, v_, p_|
132
+ assert_equal([label2_, label1_], p_.coord_array)
133
+ if label1_ == 'one'
134
+ label1_ = 'two'
135
+ else
136
+ label1_ = 'one'
137
+ label2_ += 1
138
+ end
139
+ s_ + v_
140
+ end
141
+ assert_equal(230, val_)
142
+ label1_ = 'one'
143
+ label2_ = 1
144
+ val_ = t1_.reduce_with_position(100) do |s_, v_, p_|
145
+ assert_equal([label2_, label1_], p_.coord_array)
146
+ if label1_ == 'one'
147
+ label1_ = 'two'
148
+ else
149
+ label1_ = 'one'
150
+ label2_ += 1
151
+ end
152
+ s_ + v_
153
+ end
154
+ assert_equal(330, val_)
155
+ end
156
+
157
+
158
+ end
159
+
160
+ end
161
+ end