safestruct 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/safestruct.rb +2 -2
- data/lib/safestruct/safe_array.rb +22 -11
- data/lib/safestruct/version.rb +1 -1
- data/test/test_array.rb +96 -10
- data/test/test_hash.rb +5 -4
- data/test/test_struct.rb +109 -15
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e972352c0dbb2c3684a3de9385f33efb706a7cca
|
4
|
+
data.tar.gz: 9fb3502f891afab88650aa288bfc39d84ac5f236
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de271ceb36e33cf627911d60e69c5120e2e8cde48ac84d1ef258571a98e36757695345b4e3ac58a77e0086e76f8ad393a13a6567f8b738fe380c41a47c63bb94
|
7
|
+
data.tar.gz: 4863ee4959e548d6686ca1020b8be99897a0945ebb3eef35becc7bc77d028ba1858d0d2c9b4e6f9b9066baedf636c6c3b2911aaa8a49d1e1b33bda9c2b6420e8
|
data/lib/safestruct.rb
CHANGED
@@ -67,8 +67,8 @@ class Array
|
|
67
67
|
## note: for nested Hash.of or Array.of a ("prototype") object
|
68
68
|
## gets passed in (NOT class) - auto-convert to use class
|
69
69
|
klass_value = klass_or_proto_value.is_a?( Class ) ? klass_or_proto_value : klass_or_proto_value.class
|
70
|
-
klass = Safe::SafeArray.build_class( klass_value )
|
71
|
-
klass.new
|
70
|
+
klass = Safe::SafeArray.build_class( klass_value, size )
|
71
|
+
klass.new
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
@@ -5,14 +5,15 @@ module Safe
|
|
5
5
|
class SafeArray
|
6
6
|
|
7
7
|
## e.g.
|
8
|
-
## Array.of( Address ), Array.of( Integer),
|
9
|
-
|
10
|
-
def self.build_class( klass_value )
|
11
|
-
## note: care for now only about value type / class
|
8
|
+
## Array.of( Address ), Array.of( Integer),
|
9
|
+
## Array.of( Integer, 3 ) or Array.of( Integer, 3*3 ) etc.
|
12
10
|
|
11
|
+
def self.build_class( klass_value, size=0 )
|
13
12
|
## note: keep a class cache
|
14
|
-
|
15
|
-
|
13
|
+
## note: klasses may have different init sizes (default 0)
|
14
|
+
cache = @@cache ||= {}
|
15
|
+
cache_all_sizes = cache[ klass_value ] ||= {}
|
16
|
+
klass = cache_all_sizes[ size ]
|
16
17
|
|
17
18
|
if klass.nil?
|
18
19
|
|
@@ -21,9 +22,12 @@ class SafeArray
|
|
21
22
|
def self.klass_value
|
22
23
|
@klass_value ||= #{klass_value}
|
23
24
|
end
|
25
|
+
def self.klass_size
|
26
|
+
@klass_size ||= #{size}
|
27
|
+
end
|
24
28
|
RUBY
|
25
29
|
## add to cache for later (re)use
|
26
|
-
cache[ klass_value ] = klass
|
30
|
+
cache[ klass_value ][ size ] = klass
|
27
31
|
end
|
28
32
|
klass
|
29
33
|
end
|
@@ -33,9 +37,7 @@ RUBY
|
|
33
37
|
def self.zero() @zero ||= new_zero.freeze; end
|
34
38
|
|
35
39
|
|
36
|
-
|
37
|
-
def initialize( size=0 )
|
38
|
-
## todo/check: if array works if value is a (nested/multi-dimensional) array
|
40
|
+
def initialize( size=self.class.klass_size )
|
39
41
|
@ary = []
|
40
42
|
self.size = size if size > 0 ## auto-init with zeros
|
41
43
|
self # return reference to self
|
@@ -102,8 +104,17 @@ RUBY
|
|
102
104
|
@ary.size
|
103
105
|
end
|
104
106
|
|
107
|
+
def clear
|
108
|
+
## note: reset ary to zero (NOT empty e.g. [])
|
109
|
+
## differes for "fixed" size arrays
|
110
|
+
@ary = []
|
111
|
+
self.size = self.class.klass_size if self.class.klass_size > 0 ## auto-init with zeros
|
112
|
+
self # return reference to self
|
113
|
+
end
|
114
|
+
|
115
|
+
|
105
116
|
extend Forwardable
|
106
|
-
def_delegators :@ary, :size, :length,
|
117
|
+
def_delegators :@ary, :size, :length,
|
107
118
|
:each, :each_with_index
|
108
119
|
|
109
120
|
|
data/lib/safestruct/version.rb
CHANGED
data/test/test_array.rb
CHANGED
@@ -18,6 +18,10 @@ class TestArray < MiniTest::Test
|
|
18
18
|
## multi-dimensional / nested array
|
19
19
|
Array_Array_Integer = SafeArray.build_class( Array_Integer )
|
20
20
|
|
21
|
+
## "fixed" size
|
22
|
+
Array_3_Integer = SafeArray.build_class( Integer, 3 )
|
23
|
+
Array_Array_3x3_Integer = SafeArray.build_class( Array_3_Integer, 3 )
|
24
|
+
|
21
25
|
|
22
26
|
def test_push_and_clear
|
23
27
|
ary = Array_Integer.new
|
@@ -40,6 +44,7 @@ def test_integer
|
|
40
44
|
pp ary = Array_Integer.new(2)
|
41
45
|
|
42
46
|
assert_equal Integer, Array_Integer.klass_value
|
47
|
+
assert_equal 0, Array_Integer.klass_size
|
43
48
|
assert_equal 0, ary[0]
|
44
49
|
assert_equal 0, ary[1]
|
45
50
|
|
@@ -56,14 +61,15 @@ def test_integer
|
|
56
61
|
ary.each_with_index { |item,i| puts "[#{i}] #{item}"}
|
57
62
|
|
58
63
|
ary.size = 3
|
59
|
-
assert_equal 3,
|
60
|
-
assert_equal 0,
|
61
|
-
|
64
|
+
assert_equal 3, ary.size
|
65
|
+
assert_equal 0, ary[2]
|
66
|
+
assert_equal false, Array_Integer.zero == ary
|
62
67
|
|
63
68
|
pp Array_Integer.zero
|
64
|
-
assert_equal true,
|
65
|
-
assert_equal Array_Integer.zero
|
66
|
-
assert_equal Array_Integer.zero
|
69
|
+
assert_equal true, Array_Integer.zero.frozen?
|
70
|
+
assert_equal true, Array_Integer.zero == Array_Integer.zero
|
71
|
+
assert_equal true, Array_Integer.zero == Array_Integer.new
|
72
|
+
assert_equal true, Array_Integer.zero == Array_Integer.new_zero
|
67
73
|
|
68
74
|
## check Array.of (uses cached classes)
|
69
75
|
assert_equal Array_Integer, Array.of( Integer ).class
|
@@ -75,6 +81,7 @@ def test_bool
|
|
75
81
|
pp ary = Array_Bool.new(2)
|
76
82
|
|
77
83
|
assert_equal Bool, Array_Bool.klass_value
|
84
|
+
assert_equal 0, Array_Bool.klass_size
|
78
85
|
assert_equal false, ary[0]
|
79
86
|
assert_equal false, ary[1]
|
80
87
|
|
@@ -96,10 +103,6 @@ def test_array_of
|
|
96
103
|
end
|
97
104
|
|
98
105
|
def test_multi # nested (multi-dimensional) array
|
99
|
-
## todo/fix: try with size: 3 e.g. 3x3
|
100
|
-
## make it work with size too!!!!!
|
101
|
-
## remember size if passed in in c'tor!!!!!!
|
102
|
-
|
103
106
|
multi = Array_Array_Integer.new
|
104
107
|
multi.push( Array_Integer.new )
|
105
108
|
multi[0].push( 100 )
|
@@ -138,4 +141,87 @@ def test_multi # nested (multi-dimensional) array
|
|
138
141
|
assert_equal 0, multi.size
|
139
142
|
end
|
140
143
|
|
144
|
+
def test_3x3 # nested (multi-dimensional) array
|
145
|
+
board = Array_Array_3x3_Integer.new
|
146
|
+
pp board
|
147
|
+
|
148
|
+
assert_equal Integer, Array_3_Integer.klass_value
|
149
|
+
assert_equal 3, Array_3_Integer.klass_size
|
150
|
+
|
151
|
+
assert_equal Array_3_Integer, Array_Array_3x3_Integer.klass_value
|
152
|
+
assert_equal 3, Array_Array_3x3_Integer.klass_size
|
153
|
+
|
154
|
+
assert_equal 0, board[0][0]
|
155
|
+
assert_equal 0, board[0][1]
|
156
|
+
assert_equal 0, board[0][2]
|
157
|
+
assert_equal 0, board[1][0]
|
158
|
+
assert_equal 0, board[1][1]
|
159
|
+
assert_equal 0, board[1][2]
|
160
|
+
assert_equal 0, board[2][0]
|
161
|
+
assert_equal 0, board[2][1]
|
162
|
+
assert_equal 0, board[2][2]
|
163
|
+
|
164
|
+
board[0][0] = 1
|
165
|
+
board[0][1] = 2
|
166
|
+
board[0][2] = 1
|
167
|
+
board[1][0] = 2
|
168
|
+
pp board
|
169
|
+
|
170
|
+
assert_equal 1, board[0][0]
|
171
|
+
assert_equal 2, board[0][1]
|
172
|
+
assert_equal 1, board[0][2]
|
173
|
+
assert_equal 2, board[1][0]
|
174
|
+
|
175
|
+
board[0].clear
|
176
|
+
pp board[0]
|
177
|
+
assert_equal 3, board[0].size
|
178
|
+
assert_equal 0, board[0][0]
|
179
|
+
assert_equal 0, board[0][1]
|
180
|
+
assert_equal 0, board[0][2]
|
181
|
+
|
182
|
+
board.clear
|
183
|
+
assert_equal 3, board.size
|
184
|
+
assert_equal 3, board[0].size
|
185
|
+
assert_equal 3, board[1].size
|
186
|
+
assert_equal 3, board[2].size
|
187
|
+
|
188
|
+
assert_equal 0, board[0][0]
|
189
|
+
assert_equal 0, board[0][1]
|
190
|
+
assert_equal 0, board[0][2]
|
191
|
+
assert_equal 0, board[1][0]
|
192
|
+
assert_equal 0, board[1][1]
|
193
|
+
assert_equal 0, board[1][2]
|
194
|
+
assert_equal 0, board[2][0]
|
195
|
+
assert_equal 0, board[2][1]
|
196
|
+
assert_equal 0, board[2][2]
|
197
|
+
|
198
|
+
##############################################
|
199
|
+
## try Array.of convenience helper
|
200
|
+
|
201
|
+
board = Array.of( Array.of( Integer, 3 ), 3 )
|
202
|
+
pp board
|
203
|
+
|
204
|
+
assert_equal 0, board[0][0]
|
205
|
+
assert_equal 0, board[0][1]
|
206
|
+
assert_equal 0, board[0][2]
|
207
|
+
assert_equal 0, board[1][0]
|
208
|
+
assert_equal 0, board[1][1]
|
209
|
+
assert_equal 0, board[1][2]
|
210
|
+
assert_equal 0, board[2][0]
|
211
|
+
assert_equal 0, board[2][1]
|
212
|
+
assert_equal 0, board[2][2]
|
213
|
+
|
214
|
+
board[0][0] = 1
|
215
|
+
board[0][1] = 2
|
216
|
+
board[0][2] = 1
|
217
|
+
board[1][0] = 2
|
218
|
+
pp board
|
219
|
+
|
220
|
+
assert_equal 1, board[0][0]
|
221
|
+
assert_equal 2, board[0][1]
|
222
|
+
assert_equal 1, board[0][2]
|
223
|
+
assert_equal 2, board[1][0]
|
224
|
+
end
|
225
|
+
|
226
|
+
|
141
227
|
end # class TestArray
|
data/test/test_hash.rb
CHANGED
@@ -33,8 +33,9 @@ def test_integer
|
|
33
33
|
assert_equal 0, h.instance_variable_get('@h').size
|
34
34
|
assert_equal 0, h.instance_variable_get('@h').length
|
35
35
|
|
36
|
-
assert_equal Hash_X_Integer.zero
|
37
|
-
assert_equal Hash_X_Integer.zero
|
36
|
+
assert_equal true, Hash_X_Integer.zero == h
|
37
|
+
assert_equal true, Hash_X_Integer.zero == Hash_X_Integer.new
|
38
|
+
assert_equal true, Hash_X_Integer.zero == Hash_X_Integer.new_zero
|
38
39
|
|
39
40
|
pp Hash_X_Integer.zero
|
40
41
|
assert_equal true, Hash_X_Integer.zero.frozen?
|
@@ -79,8 +80,8 @@ def test_voter
|
|
79
80
|
pp h = Hash_X_Voter.new
|
80
81
|
|
81
82
|
assert_equal Voter, Hash_X_Voter.klass_value
|
82
|
-
assert_equal Voter.zero
|
83
|
-
assert_equal Voter.zero
|
83
|
+
assert_equal true, Voter.zero == h['0x1111']
|
84
|
+
assert_equal true, Voter.zero == h['0x2222']
|
84
85
|
|
85
86
|
h['0x1111'].voted = true
|
86
87
|
h['0x2222'].voted = true
|
data/test/test_struct.rb
CHANGED
@@ -19,37 +19,85 @@ Voter = SafeStruct.new( weight: 0, voted: false, vote: 0, delegate: '0x0000' )
|
|
19
19
|
Bet = SafeStruct.new( user: '0x0000', block: 0, cap: 0, amount: 0 )
|
20
20
|
|
21
21
|
|
22
|
+
|
23
|
+
### test with "real" enum
|
24
|
+
class Enum
|
25
|
+
def initialize( key, value )
|
26
|
+
@key = key
|
27
|
+
@value = value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Winner < Enum
|
32
|
+
NONE = new(:none, 0 )
|
33
|
+
DRAW = new(:draw, 1 )
|
34
|
+
HOST = new(:host, 2 )
|
35
|
+
CHALLENGER = new(:challenger, 3)
|
36
|
+
|
37
|
+
def self.none() NONE; end
|
38
|
+
def self.draw() DRAW; end
|
39
|
+
def self.host() HOST; end
|
40
|
+
def self.challenger() CHALLENGER; end
|
41
|
+
|
42
|
+
def none?() self == NONE; end
|
43
|
+
def draw?() self == DRAW; end
|
44
|
+
def host?() self == HOST; end
|
45
|
+
def challenger?() self == CHALLENGER; end
|
46
|
+
end
|
47
|
+
|
48
|
+
Board = Array_9_Integer = SafeArray.build_class( Integer, 3*3 )
|
49
|
+
|
50
|
+
## sig: [Address, Address, Address, Winner(Enum), Board(Array9)]
|
51
|
+
Game = SafeStruct.new( host: '0x0000',
|
52
|
+
challenger: '0x0000',
|
53
|
+
turn: '0x0000', ## address of host/ challenger
|
54
|
+
winner: Winner.none,
|
55
|
+
board: Board.zero
|
56
|
+
)
|
57
|
+
|
22
58
|
def test_voter
|
23
|
-
assert_equal Voter.zero
|
59
|
+
assert_equal true, Voter.zero.frozen?
|
60
|
+
assert_equal true, Voter.zero == Voter.zero
|
61
|
+
|
24
62
|
assert_equal '0x0000', Voter.zero.delegate
|
25
63
|
assert_equal false, Voter.zero.voted?
|
26
64
|
assert_equal 0, Voter.zero.weight
|
27
65
|
assert_equal 0, Voter.zero.vote
|
28
|
-
assert_equal true, Voter.zero.frozen?
|
29
66
|
|
30
67
|
voter = Voter.new_zero
|
31
68
|
|
32
69
|
assert_equal false, voter.frozen?
|
33
|
-
assert_equal Voter.zero
|
34
|
-
|
35
|
-
|
70
|
+
assert_equal true, Voter.zero == voter
|
71
|
+
assert_equal true, Voter.zero.eql?( voter )
|
72
|
+
assert_equal true, Voter.zero == Voter.new
|
73
|
+
assert_equal true, Voter.zero == Voter.new_zero
|
74
|
+
|
36
75
|
|
37
76
|
voter.delegate = '0x1111'
|
38
77
|
pp voter
|
39
78
|
|
40
|
-
|
79
|
+
assert_equal false, Voter.zero == voter
|
41
80
|
|
42
|
-
|
43
|
-
|
44
|
-
|
81
|
+
##############
|
82
|
+
# try a new voter
|
83
|
+
voter = Voter.new( 0, false, 0, '0x0000' )
|
84
|
+
assert_equal true, Voter.zero == voter
|
85
|
+
assert_equal false, voter.frozen?
|
45
86
|
end
|
46
87
|
|
88
|
+
|
47
89
|
def test_bet
|
48
90
|
pp Bet
|
91
|
+
assert_equal true, Bet.zero.frozen?
|
92
|
+
assert_equal true, Bet.zero == Bet.zero
|
93
|
+
|
94
|
+
|
49
95
|
bet = Bet.new_zero
|
50
96
|
pp bet
|
51
97
|
|
52
|
-
assert_equal Bet.zero
|
98
|
+
assert_equal true, Bet.zero == bet
|
99
|
+
assert_equal true, Bet.zero == Bet.new
|
100
|
+
assert_equal true, Bet.zero == Bet.new_zero
|
53
101
|
|
54
102
|
bet.cap = 20_000
|
55
103
|
bet.amount = 100
|
@@ -57,23 +105,69 @@ def test_bet
|
|
57
105
|
assert_equal false, bet.frozen?
|
58
106
|
assert_equal 20_000, bet.cap
|
59
107
|
assert_equal 100, bet.amount
|
60
|
-
|
108
|
+
assert_equal false, Bet.zero == bet
|
61
109
|
|
62
110
|
pp bet
|
63
111
|
|
64
112
|
pp Bet.zero
|
65
113
|
pp Bet.zero
|
66
114
|
|
67
|
-
assert_equal Bet.zero
|
115
|
+
assert_equal true, Bet.zero == Bet.zero
|
68
116
|
assert_equal '0x0000', Bet.zero.user
|
69
117
|
assert_equal 0, Bet.zero.block
|
70
118
|
assert_equal 0, Bet.zero.cap
|
71
119
|
assert_equal 0, Bet.zero.amount
|
72
120
|
assert_equal true, Bet.zero.frozen?
|
73
121
|
|
74
|
-
|
75
|
-
|
76
|
-
|
122
|
+
#############################
|
123
|
+
# try a new bet
|
124
|
+
bet = Bet.new( '0x0000', 0, 0, 0 )
|
125
|
+
assert_equal true, Bet.zero == bet
|
126
|
+
assert_equal false, bet.frozen?
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_game
|
130
|
+
pp Game
|
131
|
+
assert_equal true, Game.zero.frozen?
|
132
|
+
assert_equal true, Game.zero == Game.zero
|
133
|
+
|
134
|
+
game = Game.new_zero
|
135
|
+
pp game
|
136
|
+
|
137
|
+
assert_equal true, Game.zero == game
|
138
|
+
assert_equal true, Game.zero == Game.new
|
139
|
+
assert_equal true, Game.zero == Game.new_zero
|
140
|
+
assert_equal false, game.frozen?
|
141
|
+
|
142
|
+
assert_equal '0x0000', game.host
|
143
|
+
assert_equal '0x0000', game.challenger
|
144
|
+
assert_equal '0x0000', game.turn
|
145
|
+
assert_equal Winner.none, game.winner
|
146
|
+
assert_equal true, game.winner.none?
|
147
|
+
assert_equal Board.zero, game.board
|
148
|
+
assert_equal 9, game.board.size
|
149
|
+
assert_equal 0, game.board[0]
|
150
|
+
assert_equal 0, game.board[1]
|
151
|
+
assert_equal 0, game.board[2]
|
152
|
+
assert_equal 0, game.board[3]
|
153
|
+
assert_equal 0, game.board[4]
|
154
|
+
assert_equal 0, game.board[5]
|
155
|
+
assert_equal 0, game.board[6]
|
156
|
+
assert_equal 0, game.board[7]
|
157
|
+
assert_equal 0, game.board[8]
|
158
|
+
|
159
|
+
|
160
|
+
game.winner = Winner.host
|
161
|
+
assert_equal false, Game.zero == game
|
162
|
+
pp game
|
163
|
+
|
164
|
+
#####
|
165
|
+
# try a new game
|
166
|
+
game = Game.new
|
167
|
+
game.board[0] = 1
|
168
|
+
assert_equal false, Game.zero == game
|
169
|
+
pp game
|
77
170
|
end
|
78
171
|
|
172
|
+
|
79
173
|
end # class TestStruct
|