Skip to content

Commit 6f9de2a

Browse files
Merge pull request #3212 from isabel-lombardi/add-test-m1-011
[FEAT] add test m1 011
2 parents 9ae6cd2 + 5e03899 commit 6f9de2a

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed

projects/m1/011-note-to-frequency/README.md

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,35 @@ with their frequencies.
1414
| B4 | 493.88 |
1515

1616

17-
Begin by writing a program that reads the name of a note from the user and displays the note’s frequency.
18-
Your program should support all of the notes listed previously.
17+
Begin by writing a program that **reads the name of a note** from the user and **displays the note’s frequency**.
18+
Your program should support all the notes listed previously.
1919

20-
Once you have your program working correctly for the notes listed previously you should add support for all of the notes from C0 to C8.
21-
While this could be done by adding many additional cases to your if statement, such a solution is cumbersome, inelegant and unacceptable for the purposes of this exercise.
20+
Once you have your program working correctly for the notes listed previously
21+
you should add support for all the notes from C0 to C8.
22+
While this could be done by adding many additional cases to your if statement, such a solution is cumbersome,
23+
inelegant and unacceptable for the purposes of this exercise.
2224
Instead, you should exploit the relationship between notes in adjacent octaves.
23-
In partic- ular, the frequency of any note in octave n is half the frequency of the corre- sponding note in octave n + 1.
24-
By using this relationship, you should be able to add support for the additional notes without adding additional cases to your if statement.
25+
In particular, **the frequency of any note in octave n is half the frequency of the corresponding note in octave n + 1.**
26+
By using this relationship, you should be able to add support for the additional notes without
27+
adding additional cases to your if statement.
2528

26-
Hint: You will want to access the characters in the note entered by the user individually when completing this exercise.
27-
Begin by separating the letter from the octave.
28-
Then compute the frequency for that letter in the fourth octave using the data in the table above. Once you have this frequency you should divide it by 2<sup>4−x</sup> ,
29-
where x is the octave number entered by the user. This will halve or double the frequency the correct number of times.
29+
Hint:
30+
You will want to access the characters in the note entered by the user individually when completing this exercise.
31+
Begin by separating the letter from the octave.
32+
Then compute the frequency for that letter in the fourth octave using the data in the table above.
33+
Once you have this frequency you should divide it by 2<sup>4−x</sup> ,
34+
where x is the octave number entered by the user.
35+
This will halve or double the frequency the correct number of times.
3036

37+
Example:
38+
Input = F6
39+
Output = 1396.9
3140

41+
42+
Input = B0
43+
Output = 30.8
44+
45+
https://pages.mtu.edu/~suits/notefreqs.html
3246
# Documentation
3347

3448
For this project solution you may use:
@@ -38,6 +52,16 @@ For this project solution you may use:
3852
- Iteration
3953
- Strings
4054

55+
56+
# Test
57+
58+
Execute the test to validate your solution.
59+
60+
- **Python**: `python -m unittest python/test_note_to_frequency.py`
61+
62+
https://pages.mtu.edu/~suits/notefreqs.html
63+
64+
4165
# Deadline
4266

4367
This project requires to be completed in a maximum of **3 hours**

projects/m1/011-note-to-frequency/__init__.py

Whitespace-only changes.

projects/m1/011-note-to-frequency/python/__init__.py

Whitespace-only changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import importlib
2+
import io
3+
import os
4+
import re
5+
import sys
6+
from pathlib import Path
7+
from unittest import TestCase, skipIf
8+
from unittest.mock import patch
9+
10+
file_path = Path(__file__).absolute().parent / 'main.py'
11+
is_file_empty = os.stat(file_path).st_size == 0
12+
13+
14+
@skipIf(is_file_empty, 'Empty file. Test 011 Skipped')
15+
class TestNoteToFrequency(TestCase):
16+
17+
def setUp(self) -> None:
18+
self.module_name = 'projects.m1.011-note-to-frequency.python.main'
19+
20+
@patch('builtins.input')
21+
def test_ok(self, mock_input):
22+
"""
23+
Check if return the expected result
24+
"""
25+
26+
mock_input.return_value = 'C7'
27+
28+
with patch('sys.stdout', new_callable=io.StringIO) as mock_print:
29+
sys.modules.pop(self.module_name, None)
30+
importlib.import_module(name=self.module_name, package='files')
31+
32+
result = float(re.findall(r'\d+\.\d', mock_print.getvalue())[-1])
33+
self.assertEqual(2093.0, result)
34+
35+
@patch('builtins.input')
36+
def test_ok_2(self, mock_input):
37+
"""
38+
Check if return the expected result
39+
"""
40+
41+
mock_input.return_value = 'A0'
42+
43+
with patch('sys.stdout', new_callable=io.StringIO) as mock_print:
44+
sys.modules.pop(self.module_name, None)
45+
importlib.import_module(name=self.module_name, package='files')
46+
47+
result = float(re.findall(r'\d+\.\d', mock_print.getvalue())[-1])
48+
self.assertEqual(27.5, result)

0 commit comments

Comments
 (0)