%

% Copyright 2012 The MathWorks, Inc.

classdef SymmetricConductor < coder.rtiostreamtest.TestDecorator
%SYMMETRICCONDUCTOR Conducts a set of symmetric tests
% Conducts a series of symmetric tests using the enclosed test and
% returns the average of the result
%
% Parameters:
%
% default_chunks -
% An array containing the numbers that would be used for
% PC/target receive or send chunks for each tests performed.
% For example [1 2 10] means that there would be three tests.
% The three tests would be data being transmitted between the
% target and the PC where the PC would do 1 chunk at a time
% and the target would do 1 chunk at a time, then 2 chunks,
% then 10 chunks.

% Available parameters defined here
properties (Constant, GetAccess = 'public')
param_defaultChunks = 'default_chunks';

% Determines how much data to send. 32 would mean that it will
% transmit 32 times the chunk size being tested
data_multiplier = 8;
end

methods

function this = SymmetricConductor(tester)
% TESTER must have exactly one output

this = this@coder.rtiostreamtest.TestDecorator(tester);
end

function result = runTest(this, connection, parameters)

import coder.rtiostreamtest.*;

% Pass the array of desired test chunk sizes
raw_chunks = Test.getSetting(parameters, this.param_defaultChunks);

% Eliminate the ones that will overflow the target's buffer
chunks = raw_chunks(raw_chunks <= connection.targetBufferSize);

% This class must never receive chunk sizes that cannot fit in
% the buffer. This must be handled by the class that is calling
% this one. This error for example will be triggered if a
% symmetric test is done with a server that has a bigger buffer
% size than the precompiled one and the user explicitly wants
% to test big sizes. If this assertion was not here in this
% case the results of the bar chart wouldn't be accurate since
% the two tests would run with different chunk size values!
assert(isequal(chunks, raw_chunks), Test.message('ErrorBigSymmetTests'));

% Shorthand for the number of iterations to run
tests = numel(chunks);

% Initialize array for averaging
results = [];

% Run the test with the different parameters
for i = 1 : tests
result = this.test.runTest(connection, [parameters {...
BandwidthOutgoingTest.param_pcSize, chunks(i), ...
BandwidthOutgoingTest.param_targetSize, chunks(i), ...
BandwidthOutgoingTest.param_size, chunks(i)*this.data_multiplier, ...
}]);
if result > 0, results = [results result]; end; %#ok<AGROW>
end

if isempty(results)
coder.rtiostreamtest.Test.error('NoValidResults');
end

% Calculate the mean - this is our result
result = mean(results);
end

end

end