Build out MATLAB test framework and core integration coverage

This commit is contained in:
Silas Oettinghaus
2026-03-24 17:23:02 +01:00
parent 9446dfb888
commit a592ebefda
154 changed files with 2281 additions and 1305 deletions

View File

@@ -1,11 +1,79 @@
classdef Electricalsignal_test < matlab.unittest.TestCase
% Test class for Electricalsignal
classdef Electricalsignal_test < IMDDTestCase
methods (Test, TestTags = {'unit', 'fast', 'signals', 'electricalsignal'})
function constructorStoresSignalFsAndLogbook(testCase)
seed = Signal(1, "fs", 2);
seed = seed.logbookentry("electrical seed", seed);
methods (Test)
function testExample(testCase)
% Example test case for Electricalsignal
% Add your test code here
testCase.verifyTrue(true);
elec = Electricalsignal([1; -2; 3], "fs", 80e9, "logbook", seed.logbook);
testCase.verifyClass(elec, 'Electricalsignal');
testCase.verifyEqual(elec.signal, [1; -2; 3]);
testCase.verifyEqual(elec.fs, 80e9);
testCase.verifyEqual(elec.logbook, seed.logbook);
end
function inheritedArithmeticKeepsElectricalsignalTypeAndMetadata(testCase)
seed = Signal(0, "fs", 1);
seed = seed.logbookentry("lhs metadata", seed);
lhs = Electricalsignal([1; 2; 3], "fs", 64e9, "logbook", seed.logbook);
rhs = Electricalsignal([4; 5; 6], "fs", 32e9, "logbook", table());
sumSig = lhs + rhs;
testCase.verifyClass(sumSig, 'Electricalsignal');
testCase.verifyEqual(sumSig.signal, [5; 7; 9]);
testCase.verifyEqual(sumSig.fs, lhs.fs);
testCase.verifyEqual(sumSig.logbook, lhs.logbook);
end
function informationCastPreservesSignalFsAndLogbook(testCase)
seed = Signal(3, "fs", 4);
seed = seed.logbookentry("electrical cast", seed);
elec = Electricalsignal([2; 0; -2], "fs", 96e9, "logbook", seed.logbook);
info = elec.Informationsignal("fs", elec.fs, "logbook", elec.logbook);
testCase.verifyClass(info, 'Informationsignal');
testCase.verifyEqual(info.signal, elec.signal);
testCase.verifyEqual(info.fs, elec.fs);
testCase.verifyEqual(info.logbook, elec.logbook);
end
function power50MatchesInheritedElectricalPowerDefinition(testCase)
elec = Electricalsignal([1; -1; 1; -1], "fs", 1, "logbook", table());
expectedPowerDbm = 10 * log10(mean(abs(elec.signal).^2) / 50) + 30;
testCase.verifyEqual(elec.power50(), expectedPowerDbm, "AbsTol", 1e-12);
testCase.verifyEqual(elec.power50(), elec.power(), "AbsTol", 1e-12);
end
function csprUsesCarrierToTotalPowerRatioInDb(testCase)
elec = Electricalsignal([1; 3], "fs", 1, "logbook", table());
carrierPowerDbm = pow2db(abs(mean(elec.signal)).^2) + 30;
expectedCsprDb = carrierPowerDbm - elec.power();
testCase.verifyEqual(elec.cspr(), expectedCsprDb, "AbsTol", 1e-12);
end
function normalizeMilliwattSetsPowerToZeroDbmInto50Ohm(testCase)
elec = Electricalsignal([2; -2; 2; -2], "fs", 1, "logbook", table());
elec = elec.normalize("mode", normalization_mode.milliwatt);
testCase.verifyEqual(elec.power50(), 0, "AbsTol", 1e-12);
testCase.verifyClass(elec, 'Electricalsignal');
end
function setPowerSupportsMilliwattAndDbwTargets(testCase)
elec = Electricalsignal([1; -1; 1; -1], "fs", 1, "logbook", table());
elecMilliwatt = elec.setPower(2, power_notation.mW);
elecDbw = elec.setPower(-20, power_notation.dBW);
testCase.verifyEqual(elecMilliwatt.power50(), 10 * log10(2), "AbsTol", 1e-12);
testCase.verifyEqual(elecDbw.power50(), 10, "AbsTol", 1e-12);
end
end
end

View File

@@ -1,11 +1,82 @@
classdef Informationsignal_test < matlab.unittest.TestCase
% Test class for Informationsignal
classdef Informationsignal_test < IMDDTestCase
methods (Test, TestTags = {'unit', 'fast', 'signals', 'informationsignal'})
function constructorWithoutOptionsUsesSignalDefaults(testCase)
info = Informationsignal([0; 1; 1; 0]);
methods (Test)
function testExample(testCase)
% Example test case for Informationsignal
% Add your test code here
testCase.verifyTrue(true);
testCase.verifyClass(info, 'Informationsignal');
testCase.verifyEqual(info.signal, [0; 1; 1; 0]);
testCase.verifyEmpty(info.fs);
testCase.verifyEmpty(info.logbook);
end
function constructorWithOptionsCopiesFsAndLogbook(testCase)
seed = Signal(5, "fs", 2);
seed = seed.logbookentry("seed entry", seed);
info = Informationsignal([1; 0; 1], "fs", 32e9, "logbook", seed.logbook);
testCase.verifyEqual(info.signal, [1; 0; 1]);
testCase.verifyEqual(info.fs, 32e9);
testCase.verifyEqual(info.logbook, seed.logbook);
end
function inheritedArithmeticKeepsInformationsignalTypeAndMetadata(testCase)
lhs = Informationsignal([1; 2; 3], "fs", 64e9, "logbook", table());
rhs = Informationsignal([4; 5; 6], "fs", 1, "logbook", table());
sumSig = lhs + rhs;
testCase.verifyClass(sumSig, 'Informationsignal');
testCase.verifyEqual(sumSig.signal, [5; 7; 9]);
testCase.verifyEqual(sumSig.fs, lhs.fs);
testCase.verifyEqual(sumSig.logbook, lhs.logbook);
end
function electricalsignalCastUsesRequestedFsAndLogbook(testCase)
seed = Signal(1, "fs", 4);
seed = seed.logbookentry("cast logbook", seed);
info = Informationsignal([0; 1; 0; 1]);
elec = info.Electricalsignal("fs", 128e9, "logbook", seed.logbook);
testCase.verifyClass(elec, 'Electricalsignal');
testCase.verifyEqual(elec.signal, info.signal);
testCase.verifyEqual(elec.fs, 128e9);
testCase.verifyEqual(elec.logbook, seed.logbook);
end
function opticalsignalCastFailsForInformationsignal(testCase)
info = Informationsignal([0; 1; 0; 1]);
didError = false;
try
info.Opticalsignal( ...
"fs", 64e9, ...
"logbook", info.logbook, ...
"lambda", 1310e-9, ...
"nase", 0, ...
"polrot", 0);
catch ME
didError = true;
testCase.verifySubstring(ME.message, ...
"Cannot convert from information- to opticalsignal.");
end
testCase.verifyTrue(didError, ...
"Informationsignal -> Opticalsignal should reject direct conversion.");
end
function electricalToInformationCastPreservesSignalAndMetadata(testCase)
seed = Signal(2, "fs", 8);
seed = seed.logbookentry("from electrical", seed);
elec = Electricalsignal([3; 1; 4], "fs", 96e9, "logbook", seed.logbook);
info = elec.Informationsignal("fs", elec.fs, "logbook", elec.logbook);
testCase.verifyClass(info, 'Informationsignal');
testCase.verifyEqual(info.signal, elec.signal);
testCase.verifyEqual(info.fs, elec.fs);
testCase.verifyEqual(info.logbook, elec.logbook);
end
end
end

View File

@@ -1,11 +1,10 @@
classdef Opticalsignal_test < matlab.unittest.TestCase
% Test class for Opticalsignal
classdef Opticalsignal_test < IMDDTestCase
% Auto-generated placeholder for Opticalsignal.
% Target: C:/Users/Silas/Documents/MATLAB/imdd_simulation/Classes/00_signals/Opticalsignal.m
methods (Test)
function testExample(testCase)
% Example test case for Opticalsignal
% Add your test code here
testCase.verifyTrue(true);
methods (Test, TestTags = {'placeholder', 'todo'})
function testNotImplemented(testCase)
testCase.assumeFail("Tests for Opticalsignal are not implemented yet.");
end
end
end

View File

@@ -1,11 +1,117 @@
classdef Signal_test < matlab.unittest.TestCase
% Test class for Signal
classdef Signal_test < IMDDTestCase
methods (Test, TestTags = {'unit', 'fast', 'signals'})
function constructorStoresSignalAndSamplingRate(testCase)
sig = Signal([1; 2; 3], "fs", 64e9);
methods (Test)
function testExample(testCase)
% Example test case for Signal
% Add your test code here
testCase.verifyTrue(true);
testCase.verifyEqual(sig.signal, [1; 2; 3]);
testCase.verifyEqual(sig.fs, 64e9);
testCase.verifyEmpty(sig.logbook);
end
function arithmeticOperatorsSupportSignalOperands(testCase)
x = Signal([1; 2; 3]);
y = Signal([4; 5; 6]);
sumSig = x + y;
diffSig = y - x;
prodSig = x .* y;
testCase.verifyEqual(sumSig.signal, [5; 7; 9]);
testCase.verifyEqual(diffSig.signal, [3; 3; 3]);
testCase.verifyEqual(prodSig.signal, [4; 10; 18]);
end
function arithmeticOperatorsSupportNumericOperands(testCase)
x = Signal([1; 2; 3]);
sumSig = x + 2;
diffSig = x - 1;
prodSig = x .* 3;
testCase.verifyEqual(sumSig.signal, [3; 4; 5]);
testCase.verifyEqual(diffSig.signal, [0; 1; 2]);
testCase.verifyEqual(prodSig.signal, [3; 6; 9]);
end
function lengthReturnsSignalLength(testCase)
sig = Signal(zeros(11, 1));
testCase.verifyEqual(sig.length(), 11);
end
function normalizeRmsScalesSignalToUnitPower(testCase)
sig = Signal([1; -1; 1; -1]);
sig = sig.normalize("mode", normalization_mode.rms);
testCase.verifyEqual(mean(abs(sig.signal).^2), 1, "AbsTol", 1e-12);
end
function normalizeOneOneMapsSignalToMinusOneToOne(testCase)
sig = Signal([2; 4; 6]);
sig = sig.normalize("mode", normalization_mode.oneone);
testCase.verifyEqual(sig.signal, [-1; 0; 1], "AbsTol", 1e-12);
end
function logbookEntryAppendsRowAndDescription(testCase)
sig = Signal([1; 0; -1], "fs", 1);
sig = sig.logbookentry("unit-test entry", sig);
testCase.verifyEqual(height(sig.logbook), 1);
testCase.verifyEqual(string(sig.logbook.SignalType(1)), "Signal");
testCase.verifyEqual(string(sig.logbook.Description(1)), "unit-test entry");
testCase.verifyEqual(string(sig.logbook.ModifierName(1)), "Signal");
end
function resampleWithIdenticalRatesKeepsSignalAndFs(testCase)
original = [1; 2; 3; 4];
sig = Signal(original, "fs", 8);
sig = sig.resample("fs_in", 8, "fs_out", 8);
testCase.verifyEqual(sig.signal, original);
testCase.verifyEqual(sig.fs, 8);
testCase.verifyEqual(height(sig.logbook), 1);
end
function resampleToNewRateUpdatesSamplingRate(testCase)
sig = Signal((1:8).', "fs", 8);
sig = sig.resample("fs_in", 8, "fs_out", 16);
testCase.verifyEqual(sig.fs, 16);
testCase.verifyNotEmpty(sig.signal);
testCase.verifyEqual(height(sig.logbook), 1);
end
function castingRoundTripPreservesSignalThroughAllDomains(testCase)
infoIn = Informationsignal([0; 1; 1; 0]);
elecFromInfo = infoIn.Electricalsignal("fs", 64e9, "logbook", infoIn.logbook);
optFromElec = elecFromInfo.Opticalsignal( ...
"fs", elecFromInfo.fs, ...
"logbook", elecFromInfo.logbook, ...
"lambda", 1310e-9, ...
"nase", 0, ...
"polrot", 0);
elecFromOpt = optFromElec.Electricalsignal("fs", optFromElec.fs, "logbook", optFromElec.logbook);
infoOut = elecFromOpt.Informationsignal("fs", elecFromOpt.fs, "logbook", elecFromOpt.logbook);
testCase.verifyClass(elecFromInfo, 'Electricalsignal');
testCase.verifyClass(optFromElec, 'Opticalsignal');
testCase.verifyClass(elecFromOpt, 'Electricalsignal');
testCase.verifyClass(infoOut, 'Informationsignal');
testCase.verifyEqual(infoOut.signal, infoIn.signal);
testCase.verifyEqual(elecFromInfo.fs, 64e9);
testCase.verifyEqual(optFromElec.fs, 64e9);
testCase.verifyEqual(elecFromOpt.fs, 64e9);
testCase.verifyEqual(infoOut.fs, 64e9);
testCase.verifyEqual(optFromElec.lambda, 1310e-9);
testCase.verifyEqual(optFromElec.nase, 0);
end
end
end