webget_ruby_ramp 1.7.2
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/lib/webget_ruby_ramp.rb +250 -0
- data/lib/webget_ruby_ramp/active_record.rb +119 -0
- data/lib/webget_ruby_ramp/active_record/connection_adapters/abstract/schema_statements.rb +24 -0
- data/lib/webget_ruby_ramp/active_record/save_extensions.rb +35 -0
- data/lib/webget_ruby_ramp/array.rb +370 -0
- data/lib/webget_ruby_ramp/csv.rb +53 -0
- data/lib/webget_ruby_ramp/date.rb +90 -0
- data/lib/webget_ruby_ramp/enumerable.rb +385 -0
- data/lib/webget_ruby_ramp/file.rb +15 -0
- data/lib/webget_ruby_ramp/hash.rb +223 -0
- data/lib/webget_ruby_ramp/integer.rb +22 -0
- data/lib/webget_ruby_ramp/io.rb +65 -0
- data/lib/webget_ruby_ramp/kernel.rb +36 -0
- data/lib/webget_ruby_ramp/math.rb +20 -0
- data/lib/webget_ruby_ramp/nil.rb +17 -0
- data/lib/webget_ruby_ramp/numeric.rb +98 -0
- data/lib/webget_ruby_ramp/object.rb +20 -0
- data/lib/webget_ruby_ramp/process.rb +153 -0
- data/lib/webget_ruby_ramp/string.rb +221 -0
- data/lib/webget_ruby_ramp/symbol.rb +11 -0
- data/lib/webget_ruby_ramp/time.rb +11 -0
- data/lib/webget_ruby_ramp/xml.rb +193 -0
- data/lib/webget_ruby_ramp/yaml.rb +34 -0
- data/test/webget_ruby_ramp/active_record/connection_adapters/abstract/schema_statements_test.rb +9 -0
- data/test/webget_ruby_ramp/active_record/save_extensions_test.rb +7 -0
- data/test/webget_ruby_ramp/active_record_test.rb +64 -0
- data/test/webget_ruby_ramp/array_test.rb +171 -0
- data/test/webget_ruby_ramp/csv_test.rb +18 -0
- data/test/webget_ruby_ramp/date_test.rb +60 -0
- data/test/webget_ruby_ramp/enumerable_test.rb +275 -0
- data/test/webget_ruby_ramp/file_test.rb +15 -0
- data/test/webget_ruby_ramp/hash_test.rb +105 -0
- data/test/webget_ruby_ramp/integer_test.rb +19 -0
- data/test/webget_ruby_ramp/io_test.rb +31 -0
- data/test/webget_ruby_ramp/io_test.txt +1 -0
- data/test/webget_ruby_ramp/kernel_test.rb +15 -0
- data/test/webget_ruby_ramp/math_test.rb +17 -0
- data/test/webget_ruby_ramp/nil_test.rb +15 -0
- data/test/webget_ruby_ramp/numeric_test.rb +28 -0
- data/test/webget_ruby_ramp/object_test.rb +12 -0
- data/test/webget_ruby_ramp/process_test.rb +24 -0
- data/test/webget_ruby_ramp/string_test.rb +125 -0
- data/test/webget_ruby_ramp/symbol_test.rb +26 -0
- data/test/webget_ruby_ramp/time_test.rb +12 -0
- data/test/webget_ruby_ramp/xml_test.rb +93 -0
- data/test/webget_ruby_ramp/xml_test_1.xml +5 -0
- data/test/webget_ruby_ramp/xml_test_2.xml +5 -0
- data/test/webget_ruby_ramp/xml_test_msword_clean.html +1 -0
- data/test/webget_ruby_ramp/xml_test_msword_dirty.html +148 -0
- data/test/webget_ruby_ramp/yaml_test.rb +32 -0
- data/test/webget_ruby_ramp/yaml_test_1.yml +38 -0
- data/test/webget_ruby_ramp/yaml_test_2.yml +38 -0
- metadata +128 -0
- metadata.gz.sig +1 -0
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
# YAML extensions
|
4
|
+
|
5
|
+
module YAML
|
6
|
+
|
7
|
+
# Specify one or more directory patterns and pass each YAML file in the matching directories to a block.
|
8
|
+
#
|
9
|
+
# See [Dir#glob](http://www.ruby-doc.org/core/classes/Dir.html#M002347) for pattern details.
|
10
|
+
#
|
11
|
+
# ==Example
|
12
|
+
# YAML.load_dir('/tmp/*.yaml'){|yaml_document|
|
13
|
+
# #...whatever you want to do with each yaml document
|
14
|
+
# }
|
15
|
+
#
|
16
|
+
# ==Example to load documents in files ending in ".yaml" and ".yml"
|
17
|
+
# YAML.load_dir('/tmp/*.yaml','/tmp/*.yml','){|yaml_document|
|
18
|
+
# #...whatever you want to do with the yaml document
|
19
|
+
# }
|
20
|
+
|
21
|
+
def YAML.load_dir(*dirpaths)
|
22
|
+
dirpaths=[*dirpaths.flatten]
|
23
|
+
dirpaths.each do |dirpath|
|
24
|
+
Dir[dirpath].each do |filename|
|
25
|
+
File.open(filename) do |file|
|
26
|
+
YAML.load_documents(file) do |doc|
|
27
|
+
yield doc
|
28
|
+
end #load
|
29
|
+
end #file
|
30
|
+
end #dir
|
31
|
+
end #each
|
32
|
+
end #def
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'webget_ruby_ramp'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'sqlite3'
|
7
|
+
rescue
|
8
|
+
# noop because sqlite may already be available,
|
9
|
+
# e.g. in JRuby on Ubuntu you can install it:
|
10
|
+
# apt-get install libsqlite3-ruby
|
11
|
+
end
|
12
|
+
|
13
|
+
begin
|
14
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
15
|
+
rescue
|
16
|
+
raise ""+
|
17
|
+
"ActiveRecord cannot establish connection to sqlite3 database memory.\n" +
|
18
|
+
"Please verify that you have the sqlite3 database installed for testing.\n" +
|
19
|
+
"To install it for Ruby typically do: sudo gem install sqlite3-ruby\n" +
|
20
|
+
"To install it for JRuby + Ubuntu do: sudo apt-get install libsqlite3-ruby"
|
21
|
+
end
|
22
|
+
|
23
|
+
ActiveRecord::Schema.define(:version => 1) do
|
24
|
+
create_table :foos do |t|
|
25
|
+
t.string :a
|
26
|
+
t.string :b
|
27
|
+
t.string :c
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Foo < ActiveRecord::Base
|
32
|
+
end
|
33
|
+
|
34
|
+
class ActiveRecordTest < Test::Unit::TestCase
|
35
|
+
|
36
|
+
def test_prelim_count
|
37
|
+
assert_equal(0,Foo.count)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_prelim_create
|
41
|
+
f=Foo.new
|
42
|
+
f.a='aa'
|
43
|
+
f.b='bb'
|
44
|
+
f.c='cc'
|
45
|
+
f.save
|
46
|
+
assert_equal(1,Foo.count)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_seed_with_create
|
50
|
+
n1=Foo.count
|
51
|
+
Foo.seed(:a,{:a=>'xxx',:b=>'yyy'})
|
52
|
+
n2=Foo.count
|
53
|
+
assert_equal(n1+1,n2)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_seed_with_update
|
57
|
+
n1=Foo.count
|
58
|
+
f1=Foo.find(:first)
|
59
|
+
Foo.seed(:a,{:a=>f1.a,:b=>'bbb'})
|
60
|
+
n2=Foo.count
|
61
|
+
assert_equal(n1,n2)
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'csv'
|
3
|
+
require 'webget_ruby_ramp'
|
4
|
+
|
5
|
+
class ArrayTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_join
|
8
|
+
a=['a','b','c']
|
9
|
+
assert_equal('abc',a.join)
|
10
|
+
assert_equal('a*b*c',a.join('*'))
|
11
|
+
assert_equal('[a]*[b]*[c]',a.join('*','[',']'))
|
12
|
+
assert_equal('[a][b][c]',a.join('[',']'))
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_size
|
16
|
+
assert_equal(false,[].size?)
|
17
|
+
assert_equal(true,[1].size?)
|
18
|
+
assert_equal(true,[1,2].size?)
|
19
|
+
assert_equal(true,[[]].size?)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_rotate
|
23
|
+
a=[1,2,3,4]
|
24
|
+
a.rotate!
|
25
|
+
assert_equal([2,3,4,1],a)
|
26
|
+
a.rotate!
|
27
|
+
assert_equal([3,4,1,2],a)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_choice
|
31
|
+
a=[1,2,3,4]
|
32
|
+
c=a.choice
|
33
|
+
assert_equal(Fixnum,c.class)
|
34
|
+
assert(a.include?(c))
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_choices
|
38
|
+
a=[1,2,3,4]
|
39
|
+
c=a.choices(2)
|
40
|
+
assert_equal(2,c.size)
|
41
|
+
assert(a.include?(c[0]))
|
42
|
+
assert(a.include?(c[1]))
|
43
|
+
c=a.choices(3)
|
44
|
+
assert_equal(3,c.size)
|
45
|
+
assert(a.include?(c[0]))
|
46
|
+
assert(a.include?(c[1]))
|
47
|
+
assert(a.include?(c[2]))
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_onto
|
51
|
+
foo=[:a,:b,:c]
|
52
|
+
goo=[:x,:y,:z]
|
53
|
+
assert_equal({:a=>:x, :b=>:y, :c=>:z},foo.onto(goo))
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_slices
|
57
|
+
a=[1,2,3,4,5,6,7,8]
|
58
|
+
assert_equal([[1,2],[3,4],[5,6],[7,8]],a.slices(2))
|
59
|
+
assert_equal([[1,2,3,4],[5,6,7,8]], a.slices(4))
|
60
|
+
assert_equal([[1,2,3],[4,5,6],[7,8]],a.slices(3))
|
61
|
+
assert_equal([[1,2,3,4,5],[6,7,8]],a.slices(5))
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_divvy_evenly_divisible
|
65
|
+
assert_equal([[1,2,3],[4,5]],[1,2,3,4,5].divvy(2),'division with equal slices')
|
66
|
+
assert_equal([[1,2,3],[4,5,6],[7]],[1,2,3,4,5,6,7].divvy(3),'division with remainer slice')
|
67
|
+
assert_equal([[1,2],[3,4],[5,6]],[1,2,3,4,5,6].divvy(4),'division with small numbers')
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_union
|
71
|
+
a=[[2,3,4],[4,5,6],[6,7,8]]
|
72
|
+
assert_equal([2,3,4,5,6,7,8],a.union)
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_intersect
|
76
|
+
a=[[2,3,4],[4,3,5],[3,4,7]]
|
77
|
+
assert_equal([3,4],a.intersect)
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_shifted
|
81
|
+
a=['a','b','c']
|
82
|
+
assert_equal([ 'b','c'],a.shifted)
|
83
|
+
assert_equal(['a','b','c'],a.shifted(0))
|
84
|
+
assert_equal([ 'b','c'],a.shifted(1))
|
85
|
+
assert_equal([ 'c'],a.shifted(2))
|
86
|
+
assert_equal([ ],a.shifted(3))
|
87
|
+
assert_equal(nil ,a.shifted(4))
|
88
|
+
end
|
89
|
+
|
90
|
+
# test_cdr must be idential to test_shifted
|
91
|
+
# because cdr is an alias for shifted
|
92
|
+
def test_cdr
|
93
|
+
a=['a','b','c']
|
94
|
+
assert_equal([ 'b','c'],a.cdr)
|
95
|
+
assert_equal(['a','b','c'],a.cdr(0))
|
96
|
+
assert_equal([ 'b','c'],a.cdr(1))
|
97
|
+
assert_equal([ 'c'],a.cdr(2))
|
98
|
+
assert_equal([ ],a.cdr(3))
|
99
|
+
assert_equal(nil ,a.cdr(4))
|
100
|
+
end
|
101
|
+
|
102
|
+
# test_rest must be idential to test_shifted
|
103
|
+
# because rest is an alias for shifted
|
104
|
+
def test_rest
|
105
|
+
a=['a','b','c']
|
106
|
+
assert_equal([ 'b','c'],a.rest)
|
107
|
+
assert_equal(['a','b','c'],a.rest(0))
|
108
|
+
assert_equal([ 'b','c'],a.rest(1))
|
109
|
+
assert_equal([ 'c'],a.rest(2))
|
110
|
+
assert_equal([ ],a.rest(3))
|
111
|
+
assert_equal(nil ,a.rest(4))
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_shifted_bang
|
115
|
+
a=['a','b','c']; a.shifted!; assert_equal([ 'b','c'],a)
|
116
|
+
a=['a','b','c']; a.shifted!(0); assert_equal(['a','b','c'],a)
|
117
|
+
a=['a','b','c']; a.shifted!(1); assert_equal([ 'b','c'],a)
|
118
|
+
a=['a','b','c']; a.shifted!(2); assert_equal([ 'c'],a)
|
119
|
+
a=['a','b','c']; a.shifted!(3); assert_equal([ ],a)
|
120
|
+
a=['a','b','c']; a.shifted!(4); assert_equal([ ],a)
|
121
|
+
end
|
122
|
+
|
123
|
+
# alias: test_cdr_bang must be identical to test_shifted_bang
|
124
|
+
def test_cdr_bang
|
125
|
+
a=['a','b','c']; a.cdr!; assert_equal([ 'b','c'],a)
|
126
|
+
a=['a','b','c']; a.cdr!(0); assert_equal(['a','b','c'],a)
|
127
|
+
a=['a','b','c']; a.cdr!(1); assert_equal([ 'b','c'],a)
|
128
|
+
a=['a','b','c']; a.cdr!(2); assert_equal([ 'c'],a)
|
129
|
+
a=['a','b','c']; a.cdr!(3); assert_equal([ ],a)
|
130
|
+
a=['a','b','c']; a.cdr!(4); assert_equal([ ],a)
|
131
|
+
end
|
132
|
+
|
133
|
+
# alias: test_rest_band must be identical to test_shifted_bang
|
134
|
+
def test_rest_bang
|
135
|
+
a=['a','b','c']; a.rest!; assert_equal([ 'b','c'],a)
|
136
|
+
a=['a','b','c']; a.rest!(0); assert_equal(['a','b','c'],a)
|
137
|
+
a=['a','b','c']; a.rest!(1); assert_equal([ 'b','c'],a)
|
138
|
+
a=['a','b','c']; a.rest!(2); assert_equal([ 'c'],a)
|
139
|
+
a=['a','b','c']; a.rest!(3); assert_equal([ ],a)
|
140
|
+
a=['a','b','c']; a.rest!(4); assert_equal([ ],a)
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_car
|
144
|
+
a=['a','b','c']
|
145
|
+
assert_equal('a',a.car)
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_to_csv_one_dimensional
|
149
|
+
a=["a","b","c"]
|
150
|
+
assert_equal("a,b,c\n",a.to_csv)
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_to_csv_multi_dimensional
|
154
|
+
a=[["a","b","c"], ["d","e","f"],["g","h","i"]]
|
155
|
+
assert_equal("a,b,c\nd,e,f\ng,h,i\n",a.to_csv)
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_to_tsv
|
159
|
+
a=[["a", "b"], ["c", "d"], ["e", "f"]]
|
160
|
+
assert_equal("a\tb\nc\td\ne\tf\n",a.to_tsv)
|
161
|
+
end
|
162
|
+
|
163
|
+
# alias: test_to_tdf must be identical to test_to_tsv
|
164
|
+
def test_to_tdf
|
165
|
+
a=[["a", "b"], ["c", "d"], ["e", "f"]]
|
166
|
+
assert_equal("a\tb\nc\td\ne\tf\n",a.to_tdf)
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'webget_ruby_ramp'
|
3
|
+
|
4
|
+
class CSVTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_http_headers
|
7
|
+
h=CSV.http_headers
|
8
|
+
assert_equal('text/csv',h["Content-Type"])
|
9
|
+
assert_equal("attachment; filename=\"data.csv\"",h["Content-Disposition"])
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_http_headers_with_filename
|
13
|
+
h=CSV.http_headers(:filename=>'foo')
|
14
|
+
assert_equal('text/csv',h["Content-Type"])
|
15
|
+
assert_equal("attachment; filename=\"foo\"",h["Content-Disposition"])
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'webget_ruby_ramp'
|
3
|
+
|
4
|
+
class DateTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
|
7
|
+
def test_weekday
|
8
|
+
# Start on Monday, January 1, 2007
|
9
|
+
assert( Date.new(2007,1,1).weekday?)
|
10
|
+
assert( Date.new(2007,1,2).weekday?)
|
11
|
+
assert( Date.new(2007,1,3).weekday?)
|
12
|
+
assert( Date.new(2007,1,4).weekday?)
|
13
|
+
assert( Date.new(2007,1,5).weekday?)
|
14
|
+
assert(!Date.new(2007,1,6).weekday?)
|
15
|
+
assert(!Date.new(2007,1,7).weekday?)
|
16
|
+
assert( Date.new(2007,1,8).weekday?)
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def test_weekend
|
21
|
+
# Start on Monday, January 1, 2007
|
22
|
+
assert(!Date.new(2007,1,1).weekend?)
|
23
|
+
assert(!Date.new(2007,1,2).weekend?)
|
24
|
+
assert(!Date.new(2007,1,3).weekend?)
|
25
|
+
assert(!Date.new(2007,1,4).weekend?)
|
26
|
+
assert(!Date.new(2007,1,5).weekend?)
|
27
|
+
assert( Date.new(2007,1,6).weekend?)
|
28
|
+
assert( Date.new(2007,1,7).weekend?)
|
29
|
+
assert(!Date.new(2007,1,8).weekend?)
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def test_to_sql
|
34
|
+
assert('2007-12-31',Date.new(2008,12,31))
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
# for test_age_years and test_age_days
|
39
|
+
BIRTHDATE = Date.new(1980,10,31)
|
40
|
+
VALENTINES = Date.new(2008,02,14)
|
41
|
+
HALLOWEEN = Date.new(2008,10,31)
|
42
|
+
NEW_YEARS_EVE = Date.new(2008,12,31)
|
43
|
+
|
44
|
+
|
45
|
+
def test_age_years
|
46
|
+
assert_equal(27,BIRTHDATE.age_years(VALENTINES), '< birthday')
|
47
|
+
assert_equal(28,BIRTHDATE.age_years(HALLOWEEN), '= birthday')
|
48
|
+
assert_equal(28,BIRTHDATE.age_years(NEW_YEARS_EVE), '> birthday')
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def test_age_days
|
53
|
+
assert_equal( 9967,BIRTHDATE.age_days(VALENTINES), '< birthday')
|
54
|
+
assert_equal(10227,BIRTHDATE.age_days(HALLOWEEN), '= birthday')
|
55
|
+
assert_equal(10288,BIRTHDATE.age_days(NEW_YEARS_EVE), '> birthday')
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,275 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'webget_ruby_ramp'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
class EnumerableTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
ITEMS = ['a','b','c']
|
8
|
+
MAPTEST = [123,"456"]
|
9
|
+
RGB = ["red","green","blue"]
|
10
|
+
|
11
|
+
|
12
|
+
########################################################################
|
13
|
+
#
|
14
|
+
# typecast
|
15
|
+
#
|
16
|
+
########################################################################
|
17
|
+
|
18
|
+
|
19
|
+
def test_to_h_with_unique_keys
|
20
|
+
a=[[:a,:b],[:c,:d],[:e,:f]]
|
21
|
+
assert_equal({:a=>:b, :c=>:d, :e=>:f}, a.to_h)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_to_h_with_duplicate_keys
|
25
|
+
a=[[:a,:b],[:a,:c],[:a,:d]]
|
26
|
+
assert_equal({:a=>[:b, :c, :d]}, a.to_h)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_index_by
|
30
|
+
assert_equal({3=>"red",4=>"blue",5=>"green"},RGB.index_by{|x| x.size})
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_hash_by
|
34
|
+
assert_equal({3=>"Red",4=>"Blue",5=>"Green"},RGB.hash_by{|x| [x.size,x.titlecase]})
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
########################################################################
|
39
|
+
#
|
40
|
+
# map
|
41
|
+
#
|
42
|
+
########################################################################
|
43
|
+
|
44
|
+
|
45
|
+
class Mock #:nodoc: all
|
46
|
+
attr_accessor :id
|
47
|
+
|
48
|
+
def initialize(id)
|
49
|
+
@id=id
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_a
|
53
|
+
[self]
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
A = Mock.new('a')
|
60
|
+
B = Mock.new('b')
|
61
|
+
C = Mock.new('c')
|
62
|
+
|
63
|
+
|
64
|
+
def test_map_id
|
65
|
+
assert_equal(['a','b','c'],[A,B,C].map_id)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_map_to_a
|
69
|
+
assert_equal([[123],["456"]],MAPTEST.to_set.map_to_a)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_map_to_f
|
73
|
+
assert_equal([123.0,456.0],MAPTEST.map_to_f)
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_map_to_i
|
77
|
+
assert_equal([123,456],MAPTEST.map_to_i)
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_map_to_s
|
81
|
+
assert_equal(["123","456"],MAPTEST.map_to_s)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_map_to_sym
|
85
|
+
assert_equal([:a,:b,:c],ITEMS.map_to_sym)
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_map_with_index
|
89
|
+
assert_equal(['a0','b1','c2'],ITEMS.map_with_index{|x,i| "#{x}#{i}"})
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
########################################################################
|
94
|
+
#
|
95
|
+
# select
|
96
|
+
#
|
97
|
+
########################################################################
|
98
|
+
|
99
|
+
|
100
|
+
def test_select_while
|
101
|
+
assert_equal([ ],ITEMS.select_while{|x| x < 'a' },'< a')
|
102
|
+
assert_equal(['a' ],ITEMS.select_while{|x| x < 'b' },'< b')
|
103
|
+
assert_equal(['a','b' ],ITEMS.select_while{|x| x < 'c' },'< c')
|
104
|
+
assert_equal(['a','b','c'],ITEMS.select_while{|x| x < 'd' },'< d')
|
105
|
+
assert_equal(['a','b','c'],ITEMS.select_while{|x| x < 'e' },'< e')
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
def test_select_until_condition
|
110
|
+
assert_equal([ ],ITEMS.select_until{|x| x == 'a' },'a')
|
111
|
+
assert_equal(['a' ],ITEMS.select_until{|x| x == 'b' },'b')
|
112
|
+
assert_equal(['a','b' ],ITEMS.select_until{|x| x == 'c' },'c')
|
113
|
+
assert_equal(['a','b','c'],ITEMS.select_until{|x| x == 'd' },'d')
|
114
|
+
assert_equal(['a','b','c'],ITEMS.select_until{|x| x == 'e' },'e')
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
def test_select_with_index
|
119
|
+
assert_equal([ ],ITEMS.select_with_index{|x,i| i < 0 },'i < 0')
|
120
|
+
assert_equal(['a' ],ITEMS.select_with_index{|x,i| i < 1 },'i < 1')
|
121
|
+
assert_equal(['a','b' ],ITEMS.select_with_index{|x,i| i < 2 },'i < 2')
|
122
|
+
assert_equal(['a','b','c'],ITEMS.select_with_index{|x,i| i < 3 },'i < 3')
|
123
|
+
assert_equal(['a','b','c'],ITEMS.select_with_index{|x,i| i < 4 },'i < 4')
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
########################################################################
|
128
|
+
#
|
129
|
+
# bisect
|
130
|
+
#
|
131
|
+
########################################################################
|
132
|
+
|
133
|
+
def test_bisect
|
134
|
+
assert_equal([[ ],['a','b','c']],ITEMS.bisect{|x| x < 'a' },'< a')
|
135
|
+
assert_equal([['a' ],[ 'b','c']],ITEMS.bisect{|x| x < 'b' },'< b')
|
136
|
+
assert_equal([['a','b' ],[ 'c']],ITEMS.bisect{|x| x < 'c' },'< c')
|
137
|
+
assert_equal([['a','b','c'],[ ]],ITEMS.bisect{|x| x < 'd' },'< d')
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
########################################################################
|
142
|
+
#
|
143
|
+
# nitems
|
144
|
+
#
|
145
|
+
########################################################################
|
146
|
+
|
147
|
+
|
148
|
+
def test_nitems_while
|
149
|
+
assert_equal(0,ITEMS.nitems_while{|x| x < 'a' },'< a')
|
150
|
+
assert_equal(1,ITEMS.nitems_while{|x| x < 'b' },'< b')
|
151
|
+
assert_equal(2,ITEMS.nitems_while{|x| x < 'c' },'< c')
|
152
|
+
assert_equal(3,ITEMS.nitems_while{|x| x < 'd' },'< d')
|
153
|
+
assert_equal(3,ITEMS.nitems_while{|x| x < 'e' },'< e')
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
def test_nitems_until
|
158
|
+
assert_equal(0,ITEMS.nitems_until{|x| x == 'a' },'a')
|
159
|
+
assert_equal(1,ITEMS.nitems_until{|x| x == 'b' },'b')
|
160
|
+
assert_equal(2,ITEMS.nitems_until{|x| x == 'c' },'c')
|
161
|
+
assert_equal(3,ITEMS.nitems_until{|x| x == 'd' },'d')
|
162
|
+
assert_equal(3,ITEMS.nitems_until{|x| x == 'e' },'e')
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
def test_nitems_with_index
|
167
|
+
assert_equal(0,ITEMS.nitems_with_index{|x,i| i < 0 },'i < 0')
|
168
|
+
assert_equal(1,ITEMS.nitems_with_index{|x,i| i < 1 },'i < 1')
|
169
|
+
assert_equal(2,ITEMS.nitems_with_index{|x,i| i < 2 },'i < 2')
|
170
|
+
assert_equal(3,ITEMS.nitems_with_index{|x,i| i < 3 },'i < 3')
|
171
|
+
assert_equal(3,ITEMS.nitems_with_index{|x,i| i < 4 },'i < 4')
|
172
|
+
end
|
173
|
+
|
174
|
+
|
175
|
+
########################################################################
|
176
|
+
#
|
177
|
+
# strings
|
178
|
+
#
|
179
|
+
########################################################################
|
180
|
+
|
181
|
+
|
182
|
+
def test_join_0
|
183
|
+
a=['a','b','c']
|
184
|
+
assert_equal("abc",a.join,"join()")
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_join_1
|
188
|
+
a=['a','b','c']
|
189
|
+
assert_equal("a+b+c",a.join('+'),"join('+')")
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_join_2
|
193
|
+
a=['a','b','c']
|
194
|
+
assert_equal("+a-+b-+c-",a.join("+","-"),"join('+','-')")
|
195
|
+
end
|
196
|
+
|
197
|
+
|
198
|
+
########################################################################
|
199
|
+
#
|
200
|
+
# set math
|
201
|
+
#
|
202
|
+
########################################################################
|
203
|
+
|
204
|
+
def test_intersect_true
|
205
|
+
assert_equal(true,['a','b'].intersect?(['b','c']))
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_intersect_true_with_same
|
209
|
+
assert_equal(true,['a','b'].intersect?(['a','b']))
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_intersect_false
|
213
|
+
assert_equal(false,['a','b'].intersect?(['c','d']))
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_intersect_false_with_none
|
217
|
+
assert_equal(false,['a','b','c'].intersect?([]))
|
218
|
+
end
|
219
|
+
|
220
|
+
CARTESIAN_PRODUCT_EXPECT=[["a", "d", "g"],
|
221
|
+
["a", "d", "h"],
|
222
|
+
["a", "d", "i"],
|
223
|
+
["a", "e", "g"],
|
224
|
+
["a", "e", "h"],
|
225
|
+
["a", "e", "i"],
|
226
|
+
["a", "f", "g"],
|
227
|
+
["a", "f", "h"],
|
228
|
+
["a", "f", "i"],
|
229
|
+
["b", "d", "g"],
|
230
|
+
["b", "d", "h"],
|
231
|
+
["b", "d", "i"],
|
232
|
+
["b", "e", "g"],
|
233
|
+
["b", "e", "h"],
|
234
|
+
["b", "e", "i"],
|
235
|
+
["b", "f", "g"],
|
236
|
+
["b", "f", "h"],
|
237
|
+
["b", "f", "i"],
|
238
|
+
["c", "d", "g"],
|
239
|
+
["c", "d", "h"],
|
240
|
+
["c", "d", "i"],
|
241
|
+
["c", "e", "g"],
|
242
|
+
["c", "e", "h"],
|
243
|
+
["c", "e", "i"],
|
244
|
+
["c", "f", "g"],
|
245
|
+
["c", "f", "h"],
|
246
|
+
["c", "f", "i"]]
|
247
|
+
|
248
|
+
def test_cartesian_product_class_method
|
249
|
+
a1=['a','b','c']
|
250
|
+
a2=['d','e','f']
|
251
|
+
a3=['g','h','i']
|
252
|
+
actual=Enumerable.cartesian_product(a1,a2,a3)
|
253
|
+
assert_equal(CARTESIAN_PRODUCT_EXPECT,actual,'class method')
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_cartesian_product_instance_method
|
257
|
+
a1=['a','b','c']
|
258
|
+
a2=['d','e','f']
|
259
|
+
a3=['g','h','i']
|
260
|
+
actual=a1.cartesian_product(a2,a3)
|
261
|
+
assert_equal(CARTESIAN_PRODUCT_EXPECT,actual,'instance method')
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_power_set
|
265
|
+
a=['a','b','c']
|
266
|
+
expect=[[],['a'],['a','b'],['a','b','c'],['a','c'],['b'],['b','c'],['c']]
|
267
|
+
actual=a.power_set.sort
|
268
|
+
assert_equal(expect,actual)
|
269
|
+
end
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
|