-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1_spec.rb
65 lines (59 loc) · 2.32 KB
/
part1_spec.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
# frozen_string_literal: true
require_relative '../lib/ruby_intro'
describe 'Ruby intro part 1' do
describe '#sum' do
it 'should be defined' do
expect { sum([1, 3, 4]) }.not_to raise_error
end
it 'returns correct sum [1 point]', points: 1 do
expect(sum([1, 2, 3, 4, 5])).to be_a_kind_of Integer
expect(sum([1, 2, 3, 4, 5])).to eq(15)
expect(sum([1, 2, 3, 4, -5])).to eq(5)
expect(sum([1, 2, 3, 4, -5, 5, -100])).to eq(-90)
end
it 'works on the empty array [2 points]', points: 2 do
expect { sum([]) }.not_to raise_error
expect(sum([])).to be_zero
end
end
describe '#max_2_sum' do
it 'should be defined' do
expect { max_2_sum([1, 2, 3]) }.not_to raise_error
end
it 'returns the correct sum [1 point]', points: 1 do
expect(max_2_sum([1, 2, 3, 4, 5])).to be_a_kind_of Integer
expect(max_2_sum([1, -2, -3, -4, -5])).to eq(-1)
end
it 'works even if 2 largest values are the same [1 point]', points: 1 do
expect(max_2_sum([1, 2, 3, 3])).to eq(6)
end
it 'returns zero if array is empty [1 point]', points: 1 do
expect(max_2_sum([])).to be_zero
end
it 'returns value of the element if just one element [1 point]', points: 1 do
expect(max_2_sum([3])).to eq(3)
end
end
describe '#sum_to_n' do
it 'should be defined' do
expect { sum_to_n?([1, 2, 3], 4) }.not_to raise_error
end
it 'returns true when any two elements sum to the second argument [2 points]', points: 2 do
expect(sum_to_n?([1, 2, 3, 4, 5], 5)).to be true # 2 + 3 = 5
expect(sum_to_n?([3, 0, 5], 5)).to be true # 0 + 5 = 5
expect(sum_to_n?([-1, -2, 3, 4, 5, -8], -3)).to be true # handles negative sum
expect(sum_to_n?([-1, -2, 3, 4, 5, -8], 12)).to be false # 3 + 4 + 5 = 12 (not 3 elements)
expect(sum_to_n?([-1, -2, 3, 4, 6, -8], 12)).to be false # no two elements that sum
end
it 'returns false for any single element array [1 point]', points: 1 do
expect(sum_to_n?([0], 0)).to be false
expect(sum_to_n?([1], 1)).to be false
expect(sum_to_n?([-1], -1)).to be false
expect(sum_to_n?([-3], 0)).to be false
end
it 'returns false for an empty array [1 point]', points: 1 do
expect(sum_to_n?([], 0)).to be false
expect(sum_to_n?([], 7)).to be false
end
end
end