Skip to content

Commit

Permalink
ENH - added unit test files
Browse files Browse the repository at this point in the history
  • Loading branch information
schoffelen committed Nov 28, 2023
1 parent 41792a4 commit 3472e63
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 5 deletions.
45 changes: 45 additions & 0 deletions matlab/test/test_fiff_finish_writing_raw.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function test_suite=test_fiff_finish_writing_raw
try test_functions=localfunctions(); catch
end
initTestSuite;

function test_fiff_readwrite_raw_()
% Test a round trip of reading and writing raw data
% this function calls the following relevant m-files:
% - fiff_read_meas_info
% - fiff_setup_read_raw
% - fiff_read_raw_segment
% - fiff_start_writing_raw
% - fiff_write_int
% - fiff_write_raw_buffer
% - fiff_finish_writing_raw

FIFF = fiff_define_constants;

% read
pathstr = fileparts(mfilename('fullpath'));
fname = fullfile(pathstr, 'data', 'test_raw.fif');
info = fiff_read_meas_info(fname);
raw = fiff_setup_read_raw(fname);
[data, times] = fiff_read_raw_segment(raw);

% write -> note that the original data was in 24 chunks of 600 samples, the
% data is saved out as a single chunk of 14400 samples
fnamenew = strrep(fname, 'test_', 'testout_');
[outfid, cals] = fiff_start_writing_raw(fnamenew, info);
fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, raw.first_samp);
fiff_write_int(outfid, FIFF.FIFF_DATA_SKIP, 0);
fiff_write_raw_buffer(outfid, data, cals);
fiff_finish_writing_raw(outfid);

% read the new file
rawnew = fiff_setup_read_raw(fnamenew);
[datanew, timesnew] = fiff_read_raw_segment(rawnew);

% compare
assertEqual(data, datanew);
assertEqual(times, timesnew);

% clean up
delete(fnamenew);

45 changes: 45 additions & 0 deletions matlab/test/test_fiff_read_meas_info.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function test_suite=test_fiff_read_meas_info
try test_functions=localfunctions(); catch
end
initTestSuite;

function test_fiff_readwrite_raw_()
% Test a round trip of reading and writing raw data
% this function calls the following relevant m-files:
% - fiff_read_meas_info
% - fiff_setup_read_raw
% - fiff_read_raw_segment
% - fiff_start_writing_raw
% - fiff_write_int
% - fiff_write_raw_buffer
% - fiff_finish_writing_raw

FIFF = fiff_define_constants;

% read
pathstr = fileparts(mfilename('fullpath'));
fname = fullfile(pathstr, 'data', 'test_raw.fif');
info = fiff_read_meas_info(fname);
raw = fiff_setup_read_raw(fname);
[data, times] = fiff_read_raw_segment(raw);

% write -> note that the original data was in 24 chunks of 600 samples, the
% data is saved out as a single chunk of 14400 samples
fnamenew = strrep(fname, 'test_', 'testout_');
[outfid, cals] = fiff_start_writing_raw(fnamenew, info);
fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, raw.first_samp);
fiff_write_int(outfid, FIFF.FIFF_DATA_SKIP, 0);
fiff_write_raw_buffer(outfid, data, cals);
fiff_finish_writing_raw(outfid);

% read the new file
rawnew = fiff_setup_read_raw(fnamenew);
[datanew, timesnew] = fiff_read_raw_segment(rawnew);

% compare
assertEqual(data, datanew);
assertEqual(times, timesnew);

% clean up
delete(fnamenew);

48 changes: 43 additions & 5 deletions matlab/test/test_fiff_read_tag.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,46 @@
initTestSuite;

function test_fiff_read_tag_()
% Test fiff_read_tag.m
%
% This is a placeholder function, referencing the below test function,
% which should fail if fiff_read_tag does not work properly
test_fiff_open
% Test fiff_open.m

if ispc
% I don't know what happens with the unit testing actually, if the
% function returns without actually testing anything
return;
end

f = which('test_fiff_define_constants.m');
[p,f,e] = fileparts(f);

% load the reference trees
load(fullfile(p, 'data', 'test_fiftrees_ref.mat'));

fname = fullfile(p, 'data', 'test_raw.fif');
[fid, tree, dir] = fiff_open(fname);
fclose(fid);
assert(isequal(tree, trees.raw));

fname = fullfile(p, 'data', 'test_long_raw.fif');
[fid, tree, dir] = fiff_open(fname);
fclose(fid);
assert(isequal(tree, trees.longraw));

fname = fullfile(p, 'data', 'test_raw-eve.fif');
[fid, tree, dir] = fiff_open(fname);
fclose(fid);
assert(isequal(tree, trees.raweve));

fname = fullfile(p, 'data', 'test-eve.fif');
[fid, tree, dir] = fiff_open(fname);
fclose(fid);
assert(isequal(tree, trees.eve));

fname = fullfile(p, 'data', 'test-ave.fif');
[fid, tree, dir] = fiff_open(fname);
fclose(fid);
assert(isequal(tree, trees.ave));

fname = fullfile(p, 'data', 'test-cov.fif');
[fid, tree, dir] = fiff_open(fname);
fclose(fid);
assert(isequal(tree, trees.cov));
45 changes: 45 additions & 0 deletions matlab/test/test_fiff_start_writing_raw.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function test_suite=test_fiff_start_writing_raw
try test_functions=localfunctions(); catch
end
initTestSuite;

function test_fiff_readwrite_raw_()
% Test a round trip of reading and writing raw data
% this function calls the following relevant m-files:
% - fiff_read_meas_info
% - fiff_setup_read_raw
% - fiff_read_raw_segment
% - fiff_start_writing_raw
% - fiff_write_int
% - fiff_write_raw_buffer
% - fiff_finish_writing_raw

FIFF = fiff_define_constants;

% read
pathstr = fileparts(mfilename('fullpath'));
fname = fullfile(pathstr, 'data', 'test_raw.fif');
info = fiff_read_meas_info(fname);
raw = fiff_setup_read_raw(fname);
[data, times] = fiff_read_raw_segment(raw);

% write -> note that the original data was in 24 chunks of 600 samples, the
% data is saved out as a single chunk of 14400 samples
fnamenew = strrep(fname, 'test_', 'testout_');
[outfid, cals] = fiff_start_writing_raw(fnamenew, info);
fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, raw.first_samp);
fiff_write_int(outfid, FIFF.FIFF_DATA_SKIP, 0);
fiff_write_raw_buffer(outfid, data, cals);
fiff_finish_writing_raw(outfid);

% read the new file
rawnew = fiff_setup_read_raw(fnamenew);
[datanew, timesnew] = fiff_read_raw_segment(rawnew);

% compare
assertEqual(data, datanew);
assertEqual(times, timesnew);

% clean up
delete(fnamenew);

45 changes: 45 additions & 0 deletions matlab/test/test_fiff_write_int.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function test_suite=test_fiff_write_int
try test_functions=localfunctions(); catch
end
initTestSuite;

function test_fiff_readwrite_raw_()
% Test a round trip of reading and writing raw data
% this function calls the following relevant m-files:
% - fiff_read_meas_info
% - fiff_setup_read_raw
% - fiff_read_raw_segment
% - fiff_start_writing_raw
% - fiff_write_int
% - fiff_write_raw_buffer
% - fiff_finish_writing_raw

FIFF = fiff_define_constants;

% read
pathstr = fileparts(mfilename('fullpath'));
fname = fullfile(pathstr, 'data', 'test_raw.fif');
info = fiff_read_meas_info(fname);
raw = fiff_setup_read_raw(fname);
[data, times] = fiff_read_raw_segment(raw);

% write -> note that the original data was in 24 chunks of 600 samples, the
% data is saved out as a single chunk of 14400 samples
fnamenew = strrep(fname, 'test_', 'testout_');
[outfid, cals] = fiff_start_writing_raw(fnamenew, info);
fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, raw.first_samp);
fiff_write_int(outfid, FIFF.FIFF_DATA_SKIP, 0);
fiff_write_raw_buffer(outfid, data, cals);
fiff_finish_writing_raw(outfid);

% read the new file
rawnew = fiff_setup_read_raw(fnamenew);
[datanew, timesnew] = fiff_read_raw_segment(rawnew);

% compare
assertEqual(data, datanew);
assertEqual(times, timesnew);

% clean up
delete(fnamenew);

45 changes: 45 additions & 0 deletions matlab/test/test_fiff_write_raw_buffer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function test_suite=test_fiff_write_raw_buffer
try test_functions=localfunctions(); catch
end
initTestSuite;

function test_fiff_readwrite_raw_()
% Test a round trip of reading and writing raw data
% this function calls the following relevant m-files:
% - fiff_read_meas_info
% - fiff_setup_read_raw
% - fiff_read_raw_segment
% - fiff_start_writing_raw
% - fiff_write_int
% - fiff_write_raw_buffer
% - fiff_finish_writing_raw

FIFF = fiff_define_constants;

% read
pathstr = fileparts(mfilename('fullpath'));
fname = fullfile(pathstr, 'data', 'test_raw.fif');
info = fiff_read_meas_info(fname);
raw = fiff_setup_read_raw(fname);
[data, times] = fiff_read_raw_segment(raw);

% write -> note that the original data was in 24 chunks of 600 samples, the
% data is saved out as a single chunk of 14400 samples
fnamenew = strrep(fname, 'test_', 'testout_');
[outfid, cals] = fiff_start_writing_raw(fnamenew, info);
fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, raw.first_samp);
fiff_write_int(outfid, FIFF.FIFF_DATA_SKIP, 0);
fiff_write_raw_buffer(outfid, data, cals);
fiff_finish_writing_raw(outfid);

% read the new file
rawnew = fiff_setup_read_raw(fnamenew);
[datanew, timesnew] = fiff_read_raw_segment(rawnew);

% compare
assertEqual(data, datanew);
assertEqual(times, timesnew);

% clean up
delete(fnamenew);

0 comments on commit 3472e63

Please sign in to comment.