Skip to content

Commit

Permalink
Create test_cut_dat.m
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanharvey1 committed Nov 6, 2024
1 parent 93f7bdf commit fbb61a9
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/test_cut_dat.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function tests = test_cut_dat
tests = functiontests(localfunctions);
end

function testFileRenamingAndProcessing(testCase)
% Setup: Create mock .dat files to simulate ephys data files
mockFiles = {'amplifier.dat', 'auxiliary.dat', 'digitalin.dat'};
nChannels = 32; % Updated channel count
sampleRate = 20000; % Hz
originalDuration = 2; % seconds
cutDuration = 1; % seconds (to test trimming)
nSamples = originalDuration * sampleRate * nChannels; % Total samples for 2 seconds

for i = 1:length(mockFiles)
fid = fopen(mockFiles{i}, 'w');
fwrite(fid, ones(1, nSamples, 'int16'), 'int16'); % Write 2 seconds of sample data
fclose(fid);
end

% Run the function with maxTime set to 1 second
cut_dat(nChannels, cutDuration, 'sample_rate', sampleRate);

% Verify: Check that _original.dat files were created and cut correctly
for i = 1:length(mockFiles)
originalFile = [mockFiles{i}(1:end - 4), '_original.dat'];
newFile = mockFiles{i};

% Check existence of original and trimmed files
verifyTrue(testCase, isfile(originalFile), ['Missing file: ', originalFile]);
verifyTrue(testCase, isfile(newFile), ['Processed file not found: ', newFile]);

% Check that the new file has data corresponding to 1 second
newFileInfo = dir(newFile);
expectedSize = cutDuration * sampleRate * nChannels * 2; % 1 sec of int16 data with 32 channels
% Set a tolerance of 64 bytes
tolerance = 64;
verifyTrue(testCase, abs(newFileInfo.bytes-expectedSize) <= tolerance, ...
sprintf('File size mismatch for: %s (within tolerance of %d bytes)', newFile, tolerance));
end

% Cleanup: Remove generated files
for i = 1:length(mockFiles)
delete(mockFiles{i});
delete([mockFiles{i}(1:end - 4), '_original.dat']);
end
end

0 comments on commit fbb61a9

Please sign in to comment.