webget_ruby_ramp 1.7.3 → 1.7.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  # Integer extensions
2
4
 
3
5
  class Integer
@@ -19,4 +21,26 @@ class Integer
19
21
  (0...self).map{|item| yield item}
20
22
  end
21
23
 
24
+
25
+ # Return true if the number is even
26
+ #
27
+ # ==Example
28
+ # 2.even? => true
29
+ # 3.even? => false
30
+
31
+ def even?
32
+ self % 2 == 0
33
+ end
34
+
35
+
36
+ # Return true if the number is odd
37
+ #
38
+ # ==Example
39
+ # 2.odd? => false
40
+ # 3.odd? => true
41
+
42
+ def odd?
43
+ self %2 == 1
44
+ end
45
+
22
46
  end
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  # IO extensions
2
4
 
3
5
  class IO
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  require 'pp'
2
4
  require 'stringio'
3
5
 
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  # Math extensions
2
4
 
3
5
  module Math
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  # NilClass provides an instantiatable object
2
4
  # suitable for more careful thorough programming.
3
5
 
@@ -1,8 +1,9 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  # Numeric extensions
2
4
 
3
5
  class Numeric
4
6
 
5
-
6
7
  # Return 0 if the given flag is any of: nil, false, 0, [], {}
7
8
  #
8
9
  # ==Example
@@ -11,8 +12,7 @@ class Numeric
11
12
  #
12
13
 
13
14
  def if(flag)
14
- # inline for speed
15
- return (flag==nil or flag==false or flag==0 or flag==[] or flag=={}) ? 0 : self
15
+ if [nil,false,0,[],{}].include?(flag) then 0 else self end
16
16
  end
17
17
 
18
18
 
@@ -23,8 +23,7 @@ class Numeric
23
23
  # 3.unless(false) => 3
24
24
 
25
25
  def unless(flag)
26
- # inline for speed
27
- return (flag==nil or flag==false or flag==0 or flag==[] or flag=={}) ? self : 0
26
+ if [nil,false,0,[],{}].include?(flag) then self else 0 end
28
27
  end
29
28
 
30
29
 
@@ -51,7 +50,7 @@ class Numeric
51
50
 
52
51
  # Return self / 10^6
53
52
  def mega
54
- self/100000
53
+ self/1000000
55
54
  end
56
55
 
57
56
  # Return self / 10^3
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  # Object extensions
2
4
 
3
5
  class Object
@@ -1,4 +1,6 @@
1
- # Process extensions to help debug Ruby programs.
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # = Process extensions to help debug Ruby programs.
2
4
  #
3
5
  # ==Examples
4
6
  #
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  # String extensions
2
4
 
3
5
  class String
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  # Symbol extensions
2
4
 
3
5
  class Symbol
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  # Time extensions
2
4
 
3
5
  class Time
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  require 'rexml/document'
2
4
 
3
5
  # XML extensions
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  require 'yaml'
2
4
 
3
5
  # YAML extensions
@@ -4,98 +4,171 @@ require 'webget_ruby_ramp'
4
4
 
5
5
  class ArrayTest < Test::Unit::TestCase
6
6
 
7
- def test_join
8
- a=['a','b','c']
9
- assert_equal('abc',a.join)
10
- assert_equal('abc',a.join(''))
11
- assert_equal('a*b*c',a.join('*'))
12
- assert_equal('[a]*[b]*[c]',a.join('*','[',']'))
13
- assert_equal('[a][b][c]',a.join('[',']'))
14
- assert_raise(ArgumentError){ a.join('','','','') }
7
+ A=['a','b','c','d']
8
+ B=['w','x','y','z']
9
+ SLICEABLE=['a','b','c','d','e','f','g','h']
10
+
11
+ def test_join_with_blank
12
+ assert_equal('abcd', A.join(''))
13
+ end
14
+
15
+ def test_join_with_infix
16
+ assert_equal('a*b*c*d', A.join('*'))
15
17
  end
16
18
 
17
- def test_size
18
- assert_equal(false,[].size?)
19
- assert_equal(true,[nil].size?)
19
+ def test_join_with_infix_and_prefix_and_suffix
20
+ assert_equal('[a]*[b]*[c]*[d]', A.join('*','[',']'))
21
+ end
22
+
23
+ def test_join_with_prefix_and_suffix
24
+ assert_equal('[a][b][c][d]', A.join('[',']'))
25
+ end
26
+
27
+ def test_join_with_no_ops
28
+ assert_equal('abcd',A.join)
29
+ end
30
+
31
+ def test_join_with_too_many_ops
32
+ assert_raise(ArgumentError){ A.join('','','','') }
33
+ end
34
+
35
+ def test_size_with_one_item
20
36
  assert_equal(true,[1].size?)
37
+ end
38
+
39
+ def test_size_with_two_items
21
40
  assert_equal(true,[1,2].size?)
41
+ end
42
+
43
+ def test_size_with_empty
44
+ assert_equal(false,[].size?)
45
+ end
46
+
47
+ def test_size_with_nil
48
+ assert_equal(true,[nil].size?)
49
+ end
50
+
51
+ def test_size_with_one_empty
22
52
  assert_equal(true,[[]].size?)
23
53
  end
24
54
 
25
55
  def test_rotate
26
- a=[1,2,3,4]
56
+ a=A.dup
57
+ a.rotate!
58
+ assert_equal(['b','c','d','a'],a)
27
59
  a.rotate!
28
- assert_equal([2,3,4,1],a)
60
+ assert_equal(['c','d','a','b'],a)
29
61
  a.rotate!
30
- assert_equal([3,4,1,2],a)
62
+ assert_equal(['d','a','b','c'],a)
31
63
  a.rotate!
64
+ assert_equal(['a','b','c','d'],a)
65
+ end
66
+
67
+ def test_rotate_with_empty
32
68
  a=[]
33
69
  a.rotate!
34
70
  assert_equal([],a)
35
71
  end
36
72
 
37
73
  def test_choice
38
- a=[1,2,3,4]
39
74
  5.times {
40
- c=a.choice
41
- assert_equal(Fixnum,c.class)
42
- assert(a.include?(c))
75
+ c=A.choice
76
+ assert_equal(String,c.class)
77
+ assert(A.include?(c))
43
78
  }
44
79
  end
45
80
 
46
81
  def test_choices
47
- a=[1,2,3,4]
48
82
  5.times {
49
- c=a.choices(2)
83
+ c=A.choices(2)
50
84
  assert_equal(2,c.size)
51
- assert(a.include?(c[0]))
52
- assert(a.include?(c[1]))
53
- c=a.choices(3)
85
+ assert(A.include?(c[0]))
86
+ assert(A.include?(c[1]))
87
+ c=A.choices(3)
54
88
  assert_equal(3,c.size)
55
- assert(a.include?(c[0]))
56
- assert(a.include?(c[1]))
57
- assert(a.include?(c[2]))
89
+ assert(A.include?(c[0]))
90
+ assert(A.include?(c[1]))
91
+ assert(A.include?(c[2]))
58
92
  }
59
93
  end
60
94
 
61
95
  def test_onto
62
- foo=[:a,:b,:c]
63
- goo=[:x,:y,:z]
64
- assert_equal({:a=>:x, :b=>:y, :c=>:z},foo.onto(goo))
65
- assert_raise(ArgumentError){ foo.onto([]) }
66
- end
67
-
68
- def test_slices
69
- a=[1,2,3,4,5,6,7,8]
70
- assert_equal([[1,2],[3,4],[5,6],[7,8]],a.slices(2))
71
- assert_equal([[1,2,3,4],[5,6,7,8]], a.slices(4))
72
- assert_equal([[1,2,3],[4,5,6],[7,8]],a.slices(3))
73
- assert_equal([[1,2,3,4,5],[6,7,8]],a.slices(5))
96
+ assert_equal({'a'=>'w', 'b'=>'x', 'c'=>'y', 'd'=>'z'},A.onto(B))
97
+ end
98
+
99
+ def test_onto_with_empty
100
+ assert_raise(ArgumentError){ A.onto([]) }
101
+ end
102
+
103
+ def test_slices_with_balanced_results
104
+ assert_equal([['a','b'],['c','d'],['e','f'],['g','h']], SLICEABLE.slices(2))
105
+ assert_equal([['a','b','c','d'],['e','f','g','h']], SLICEABLE.slices(4))
106
+ end
107
+
108
+ def test_slices_with_unbalanced_results
109
+ assert_equal([['a','b','c'],['d','e','f'],['g','h']], SLICEABLE.slices(3))
110
+ assert_equal([['a','b','c','d','e'],['f','g','h']], SLICEABLE.slices(5))
111
+ end
112
+
113
+ def test_slices_with_empty
74
114
  assert_equal([],[].slices(1))
75
- assert_raise(ArgumentError){ a.slices(-1) }
76
- assert_raise(ArgumentError){ a.slices("") }
77
- assert_raise(ArgumentError){ a.slices(0.123) }
115
+ end
116
+
117
+ def test_slices_with_negative_count
118
+ assert_raise(ArgumentError){ [].slices(-1) }
119
+ end
120
+
121
+ def test_slices_with_non_integer_count
122
+ assert_raise(ArgumentError){ [].slices(0.123) }
123
+ end
124
+
125
+ def test_slices_with_non_numeric_count
126
+ assert_raise(ArgumentError){ [].slices("") }
127
+ end
128
+
129
+ def test_divvy_with_one_slice
130
+ assert_equal([[1,2,3,4,5,6]],[1,2,3,4,5,6].divvy(1))
131
+ end
132
+
133
+ def test_divvy_with_equal_slices
134
+ assert_equal([[1,2,3],[4,5]],[1,2,3,4,5].divvy(2))
135
+ end
136
+
137
+ def test_divvy_with_remainer_slice
138
+ assert_equal([[1,2,3],[4,5,6],[7]],[1,2,3,4,5,6,7].divvy(3))
78
139
  end
79
140
 
80
- def test_divvy
81
- assert_equal([[1,2,3,4,5,6]],[1,2,3,4,5,6].divvy(1),'division into one slice')
82
- assert_equal([[1,2,3],[4,5]],[1,2,3,4,5].divvy(2),'division with equal slices')
83
- assert_equal([[1,2,3],[4,5,6],[7]],[1,2,3,4,5,6,7].divvy(3),'division with remainer slice')
84
- assert_equal([[1,2],[3,4],[5,6]],[1,2,3,4,5,6].divvy(4),'division with small numbers')
141
+ def test_divvy_with_small_number
142
+ assert_equal([[1,2],[3,4],[5,6]],[1,2,3,4,5,6].divvy(4))
143
+ end
144
+
145
+ def test_divvy_with_negative_count
85
146
  assert_raise(ArgumentError){ [].divvy(-1) }
86
- assert_raise(ArgumentError){ [].divvy("") }
147
+ end
148
+
149
+ def test_divvy_with_non_integer_count
87
150
  assert_raise(ArgumentError){ [].divvy(0.123) }
88
151
  end
89
152
 
153
+ def test_divvy_with_non_numeric_count
154
+ assert_raise(ArgumentError){ [].divvy("") }
155
+ end
156
+
90
157
  def test_union
91
158
  a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
92
159
  assert_equal([1,2,3,4,5,6],a.union)
160
+ end
161
+
162
+ def test_union_with_empty
93
163
  assert_equal([],[].union)
94
164
  end
95
165
 
96
166
  def test_intersect
97
167
  a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
98
168
  assert_equal([3,4],a.intersect)
169
+ end
170
+
171
+ def test_intersect_with_empty
99
172
  assert_equal([],[].intersect)
100
173
  end
101
174
 
@@ -107,13 +180,21 @@ class ArrayTest < Test::Unit::TestCase
107
180
  assert_equal([ 'c'],a.shifted(2))
108
181
  assert_equal([ ],a.shifted(3))
109
182
  assert_equal(nil ,a.shifted(4))
110
- assert_raise(ArgumentError){ a.shifted(-1) }
111
- assert_raise(ArgumentError){ a.shifted("") }
112
- assert_raise(ArgumentError){ a.shifted(0.123) }
113
183
  end
114
184
 
115
- # test_cdr must be idential to test_shifted
116
- # because cdr is an alias for shifted
185
+ def test_shifted_with_negative_count
186
+ assert_raise(ArgumentError){ [].shifted(-1) }
187
+ end
188
+
189
+ def test_shifted_with_non_integer_count
190
+ assert_raise(ArgumentError){ [].shifted(0.123) }
191
+ end
192
+
193
+ def test_shifted_with_non_numeric_count
194
+ assert_raise(ArgumentError){ [].shifted("") }
195
+ end
196
+
197
+ # alias: test_cdr must be idential to test_shifted
117
198
  def test_cdr
118
199
  a=['a','b','c']
119
200
  assert_equal([ 'b','c'],a.cdr)
@@ -122,13 +203,24 @@ class ArrayTest < Test::Unit::TestCase
122
203
  assert_equal([ 'c'],a.cdr(2))
123
204
  assert_equal([ ],a.cdr(3))
124
205
  assert_equal(nil ,a.cdr(4))
125
- assert_raise(ArgumentError){ a.cdr(-1) }
126
- assert_raise(ArgumentError){ a.cdr("") }
127
- assert_raise(ArgumentError){ a.cdr(0.123) }
128
206
  end
129
207
 
130
- # test_rest must be idential to test_shifted
131
- # because rest is an alias for shifted
208
+ # alias: test_cdr must be idential to test_shifted
209
+ def test_cdr_with_negative_count
210
+ assert_raise(ArgumentError){ [].cdr(-1) }
211
+ end
212
+
213
+ # alias: test_cdr must be idential to test_shifted
214
+ def test_cdr_with_non_integer_count
215
+ assert_raise(ArgumentError){ [].cdr(0.123) }
216
+ end
217
+
218
+ # alias: test_cdr must be idential to test_shifted
219
+ def test_cdr_with_non_numeric_count
220
+ assert_raise(ArgumentError){ [].cdr("") }
221
+ end
222
+
223
+ # alias: test_rest must be idential to test_shifted
132
224
  def test_rest
133
225
  a=['a','b','c']
134
226
  assert_equal([ 'b','c'],a.rest)
@@ -137,9 +229,21 @@ class ArrayTest < Test::Unit::TestCase
137
229
  assert_equal([ 'c'],a.rest(2))
138
230
  assert_equal([ ],a.rest(3))
139
231
  assert_equal(nil ,a.rest(4))
140
- assert_raise(ArgumentError){ a.rest(-1) }
141
- assert_raise(ArgumentError){ a.rest("") }
142
- assert_raise(ArgumentError){ a.rest(0.123) }
232
+ end
233
+
234
+ # alias: test_rest must be idential to test_shifted
235
+ def test_rest_with_negative_count
236
+ assert_raise(ArgumentError){ [].rest(-1) }
237
+ end
238
+
239
+ # alias: test_rest must be idential to test_shifted
240
+ def test_rest_with_non_integer_count
241
+ assert_raise(ArgumentError){ [].rest(0.123) }
242
+ end
243
+
244
+ # alias: test_rest must be idential to test_shifted
245
+ def test_rest_with_non_numeric_count
246
+ assert_raise(ArgumentError){ [].rest("") }
143
247
  end
144
248
 
145
249
  def test_shifted_bang
@@ -149,10 +253,18 @@ class ArrayTest < Test::Unit::TestCase
149
253
  a=['a','b','c']; a.shifted!(2); assert_equal([ 'c'],a)
150
254
  a=['a','b','c']; a.shifted!(3); assert_equal([ ],a)
151
255
  a=['a','b','c']; a.shifted!(4); assert_equal([ ],a)
152
- a=[]
153
- assert_raise(ArgumentError){ a.shifted!(-1) }
154
- assert_raise(ArgumentError){ a.shifted!("") }
155
- assert_raise(ArgumentError){ a.shifted!(0.123) }
256
+ end
257
+
258
+ def test_shifted_bang_with_negative_count
259
+ assert_raise(ArgumentError){ [].shifted!(-1) }
260
+ end
261
+
262
+ def test_shifted_bang_with_non_integer_count
263
+ assert_raise(ArgumentError){ [].shifted!(0.123) }
264
+ end
265
+
266
+ def test_shifted_bang_with_non_numeric_count
267
+ assert_raise(ArgumentError){ [].shifted!("") }
156
268
  end
157
269
 
158
270
  # alias: test_cdr_bang must be identical to test_shifted_bang
@@ -163,13 +275,24 @@ class ArrayTest < Test::Unit::TestCase
163
275
  a=['a','b','c']; a.cdr!(2); assert_equal([ 'c'],a)
164
276
  a=['a','b','c']; a.cdr!(3); assert_equal([ ],a)
165
277
  a=['a','b','c']; a.cdr!(4); assert_equal([ ],a)
166
- a=[]
167
- assert_raise(ArgumentError){ a.cdr!(-1) }
168
- assert_raise(ArgumentError){ a.cdr!("") }
169
- assert_raise(ArgumentError){ a.cdr!(0.123) }
170
278
  end
171
279
 
172
- # alias: test_rest_band must be identical to test_shifted_bang
280
+ # alias: test_cdr_bang must be identical to test_shifted_bang
281
+ def test_cdr_bang_with_negative_count
282
+ assert_raise(ArgumentError){ [].cdr!(-1) }
283
+ end
284
+
285
+ # alias: test_cdr_bang must be identical to test_shifted_bang
286
+ def test_cdr_bang_with_non_integer_count
287
+ assert_raise(ArgumentError){ [].cdr!(0.123) }
288
+ end
289
+
290
+ # alias: test_cdr_bang must be identical to test_shifted_bang
291
+ def test_cdr_bang_with_non_numeric_count
292
+ assert_raise(ArgumentError){ [].cdr!("") }
293
+ end
294
+
295
+ # alias: test_rest_bang must be identical to test_shifted_bang
173
296
  def test_rest_bang
174
297
  a=['a','b','c']; a.rest!; assert_equal([ 'b','c'],a)
175
298
  a=['a','b','c']; a.rest!(0); assert_equal(['a','b','c'],a)
@@ -177,10 +300,21 @@ class ArrayTest < Test::Unit::TestCase
177
300
  a=['a','b','c']; a.rest!(2); assert_equal([ 'c'],a)
178
301
  a=['a','b','c']; a.rest!(3); assert_equal([ ],a)
179
302
  a=['a','b','c']; a.rest!(4); assert_equal([ ],a)
180
- a=[]
181
- assert_raise(ArgumentError){ a.rest!(-1) }
182
- assert_raise(ArgumentError){ a.rest!("") }
183
- assert_raise(ArgumentError){ a.rest!(0.123) }
303
+ end
304
+
305
+ # alias: test_rest_bang must be identical to test_shifted_bang
306
+ def test_rest_bang_with_negative_count
307
+ assert_raise(ArgumentError){ [].rest!(-1) }
308
+ end
309
+
310
+ # alias: test_rest_bang must be identical to test_shifted_bang
311
+ def test_rest_bang_with_non_integer_count
312
+ assert_raise(ArgumentError){ [].rest!(0.123) }
313
+ end
314
+
315
+ # alias: test_rest_bang must be identical to test_shifted_bang
316
+ def test_rest_bang_with_non_numeric_count
317
+ assert_raise(ArgumentError){ [].rest!("") }
184
318
  end
185
319
 
186
320
  def test_car
@@ -208,8 +342,10 @@ class ArrayTest < Test::Unit::TestCase
208
342
  end
209
343
 
210
344
  def test_to_csv_one_dimensional
211
- a=["a","b","c"]
212
- assert_equal("a,b,c\n",a.to_csv)
345
+ assert_equal("a,b,c,d\n",A.to_csv)
346
+ end
347
+
348
+ def test_to_csv_with_empty
213
349
  assert_equal("",[].to_csv)
214
350
  end
215
351
 
@@ -221,6 +357,9 @@ class ArrayTest < Test::Unit::TestCase
221
357
  def test_to_tsv
222
358
  a=[["a", "b"], ["c", "d"], ["e", "f"]]
223
359
  assert_equal("a\tb\nc\td\ne\tf\n",a.to_tsv)
360
+ end
361
+
362
+ def test_to_tsv_with_empty
224
363
  assert_equal("",[].to_tsv)
225
364
  end
226
365
 
@@ -228,6 +367,10 @@ class ArrayTest < Test::Unit::TestCase
228
367
  def test_to_tdf
229
368
  a=[["a", "b"], ["c", "d"], ["e", "f"]]
230
369
  assert_equal("a\tb\nc\td\ne\tf\n",a.to_tdf)
370
+ end
371
+
372
+ # alias: test_to_tdf must be identical to test_to_tsv
373
+ def test_to_tdf_with_empty
231
374
  assert_equal("",[].to_tdf)
232
375
  end
233
376