-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_test.rb
156 lines (115 loc) · 3.45 KB
/
solution_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
require 'minitest/autorun'
require_relative 'solution'
class SolutionTest < Minitest::Test
class Collection
include MyEnumerable
def initialize(*data)
@data = data
end
def each(&block)
@data.each(&block)
end
def ==(otherCollection)
@data == otherCollection.data
end
def get(index)
return @data[index]
end
end
def test_map
collection = Collection.new(*1..5)
assert_equal [2, 3, 4, 5, 6], collection.map(&:succ)
end
def test_filter
collection = Collection.new(*1..10)
assert_equal [1, 3, 5, 7, 9], collection.filter(&:odd?)
end
def test_reject
collection = Collection.new(*1..10)
assert_equal [1, 3, 5, 7, 9], collection.reject(&:even?)
end
def test_reduce
collection = Collection.new(*1..10)
assert_equal 55, collection.reduce(0) { |sum, n| sum + n }
end
def test_include?
collection = Collection.new(*1..10)
assert_equal true, collection.include?(5)
end
def test_any?
collection = Collection.new(*1..10)
assert collection.any?(&:even?)
end
def test_all?
collection = Collection.new(*1..10)
assert collection.all? { |x| x > 0 }
end
def test_size
collection = Collection.new(*1..10)
assert_equal 10, collection.size
end
def test_count_with_element_nil
collection = Collection.new(*1..10)
assert_equal 10, collection.count
end
def test_count_with_non_nil_element
collection = Collection.new(*1..10)
assert_equal 1, collection.count(10)
end
def test_min_with_numbers
collection = Collection.new(*1..10)
assert_equal 1, collection.min
end
def test_min_by
collection = Collection.new(*['apple', 'orange', 'horse', 'ruby'])
assert_equal 'ruby', collection.min_by { |x| x.length }
end
def test_max
collection = Collection.new(*1..10)
assert_equal 10, collection.max
end
def test_max_by
collection = Collection.new(*['apples', 'oranges', 'horse', 'ruby'])
assert_equal 'oranges', collection.max_by { |x| x.length }
end
def test_take_in_standard_case
collection = Collection.new(*1..10)
assert_equal [1, 2, 3, 4, 5], collection.take(5)
end
def test_take_with_0
collection = Collection.new(*1..10)
assert_equal [], collection.take(0)
end
def test_take_with_larger_than_size_number
collection = Collection.new(*1..2)
assert_equal [1, 2], collection.take(5)
end
def test_take_while_with_some_elements_matching
collection = Collection.new(*[0, 0, 0, 1, 2, 3])
assert_equal [0, 0, 0], collection.take_while { |x| x.zero? }
end
def test_take_with_no_elements_matching
collection = Collection.new(*[2, 4, 6, 3])
assert_equal [], collection.take_while {|x| x.odd? }
end
def test_drop_in_standard_case
collection = Collection.new(*1..10)
assert_equal [6, 7, 8, 9, 10], collection.drop(5)
end
def test_drop_with_0
collection = Collection.new(*1..10)
assert_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], collection.drop(0)
end
def test_take_with_larger_than_size_number
collection = Collection.new(*1..2)
assert_equal [1, 2], collection.take(5)
end
def test_drop_while_with_some_elements_matching
collection = Collection.new(*[0, 0, 0, 1, 2, 3])
assert_equal [1, 2, 3], collection.drop_while { |x| x.zero? }
end
def test_drop_with_no_elements_matching
collection = Collection.new(*[2, 4, 6, 3])
assert_equal [2, 4, 6, 3], collection.drop_while {|x| x.odd? }
end
end