Skip to content

Commit

Permalink
Add helper method for amplitude range
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmacphersonmusic committed May 19, 2015
1 parent 3683bbf commit af1e321
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions test/note_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def setup
end

def test_samples_are_identical
assert compare_sample_arrays(@test_samples, @basic_note.samples) < (0.1 * (10 ** -10))
assert sample_arrays_diff(@test_samples, @basic_note.samples) < (0.1 * (10 ** -10))
end

def test_number_of_samples_match
Expand All @@ -25,27 +25,29 @@ def test_number_of_frames_matches_number_of_samples
end

def test_amplitude_range
# this doesn't look very DRY - private helper method?
# helper should take care of array examination and return one assertion
@basic_note.samples.each do |sample|
assert sample.abs <= 1
end
@note_220.samples.each do |sample|
assert sample.abs <= 1
end
@distorted_note.samples.each do |sample|
assert sample.abs <= 1
end
assert each_array_item_less_than_1(@basic_note.samples)
assert each_array_item_less_than_1(@note_220.samples)
assert each_array_item_less_than_1(@distorted_note.samples)
end

private

def compare_sample_arrays(array_1, array_2)
def sample_arrays_diff(array_1, array_2)
diff = 0
zipped = array_1.zip(array_2)
zipped.each do |item|
diff += (item[0] - item[1]).abs
end
diff
end

def each_array_item_less_than_1(samples_array)
less_than_1 = true
samples_array.each do |sample|
if sample.abs > 1
return less_than_1 = false
end
end
less_than_1
end
end

0 comments on commit af1e321

Please sign in to comment.