Skip to content

Commit

Permalink
#1 Write a simple test for file I/O.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzamecnik committed Oct 19, 2016
1 parent a2317f7 commit 5d47e5c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
test:
nosetests

install:
pip install tfr

Expand Down
Empty file added tests/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions tests/files_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np
import os
import tempfile

# Note that nose prepends the project directory to the sys.path so that we
# do not import from the system-wide package!

from tfr import files


def test_array_should_be_saved_to_a_file():
fs = 1000
x = sine_wave(5.0, fs)
with tempfile.NamedTemporaryFile(delete=True) as tmpfile:
file_name = tmpfile.name
files.save_wav(x, file_name, fs=fs)
assert os.path.exists(file_name)

def test_array_should_be_loaded_from_a_file():
fs = 1000
x = sine_wave(5.0, fs)
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
file_name = tmpfile.name
files.save_wav(x, file_name, fs=fs)
x_loaded, fs_loaded = files.load_wav(file_name)

assert fs_loaded == fs
# 16-bit quantization error is OK
assert np.allclose(x_loaded, x, atol=(1 / (((2**15)) - 1)))

def sine_wave(duration, fs):
return np.sin(np.linspace(0, duration, duration * fs))

0 comments on commit 5d47e5c

Please sign in to comment.