function res = testStructRepMeas_hC(SR, varargin)
% Each measure, for each measure parameter, for each partition a set of hC
% vectors is tested: direct measure calculation versus StructRep measure
% calculation.

% only one measure!!

%% Input Handling

[hC_tests, p_search, measure, verbose, summarized, extra_tests] = mapOptions(varargin, ...
    'tests', 10, ...
    'search_partition', 1,...
    'measures', {''}, ...
    'verbose', 1,...
    'summarized', 0, ...
    'extra_tests', [1 1]);

% if ~iscell(measure)
%     measure = {measure};
% end

%% Initialization
rand('seed', 0);

if isempty(measure)
    res = struct;
    res.relmin = [];
    res.relmax = [];
    res.time_SR = [];
    res.time_dir = [];
    return
end

meas = SR.meas.(measure);
par = meas.par;
i_max = hC_tests*max(1,meas.ispar)*size(SR.simplex.se_partitions, 1);

% init results
res.hC_tests  = hC_tests;
res.parameter = zeros([1 i_max]);
res.partition = zeros([1 i_max]);
res.hC        = cell([1 i_max]);
res.rel_err   = zeros([1 i_max]);
res.meas_SR   = zeros([1 i_max]);
res.meas_dir  = zeros([1 i_max]);
res.time_SR   = [0 0];
res.time_dir  = 0;


%% Generate data
for i_par = (1:max(size(par, 1), 1))
    for i_partition = (1:length(SR.simplex.hC_partitions))
        i_now = (i_par-1) * size(SR.simplex.se_partitions, 1) + (i_partition-1);
        i_now = i_now * hC_tests;
        p = i_partition;
        pu = SR.simplex_unified_map(p);
        % we have to use the first partition of a union, the
        % second partition may have been deleted!
        p = find(SR.simplex_unified_map == pu, 1, 'first');
        
        % generate hC-vectors
        hC = zeros([hC_tests, size(SR.se, 2)]);
        for j = 1:hC_tests
            hC(j, :) = (SR.se(SR.simplex.se_partitions(i_partition,:), :)' ...
                * (rand([size(SR.se, 2) 1]) + 0.1))';
        end
 
        % store results
        for j = (1:size(hC, 1))
            res.parameter(i_now + j) = i_par * meas.ispar;
            res.partition(i_now + j) = p;
            res.hC{i_now + j} = hC(j,:)';
        end
    end
end

%% Generate SR measures (without knowing the partition)
tic
for et = 1:extra_tests(1)
for i = (1:length(res.hC))
    if meas.ispar
        res.meas_SR(i) = getMeasureSR(SR, 'hC', res.hC{i},...
            measure, res.parameter(i));
    else
        res.meas_SR(i) = getMeasureSR(SR, 'hC', res.hC{i}, measure);
    end
end
end
res.time_SR(1) = toc;

%% Generate SR measures (with knowing the partition)
% results not stored
tic
for et = 1:extra_tests(2)
for i = (1:length(res.hC))
        if meas.ispar
            getMeasureSR(SR, 'hC', {res.partition(i), res.hC{i}}, measure, res.parameter(i), [], 1);
        else
            getMeasureSR(SR, 'hC', {res.partition(i), res.hC{i}}, measure, [], [], 1);
        end
end
end
res.time_SR(2) = toc;

%% Generate direct measures
tic
for i = (1:length(res.hC))
    if meas.ispar
        res.meas_dir(i) = meas.handle(res.hC{i}, par(res.parameter(i),:));
    else
        res.meas_dir(i) = meas.handle(res.hC{i});
    end
    % store results
    res.rel_err(i) = (res.meas_SR(i) - res.meas_dir(i))...
        / res.meas_dir(i); 
end
res.time_dir = toc;

if summarized
    if isempty(res.hC)
        res = struct;
        res.relmin = [];
        res.relmax = [];
        res.time_SR = [];
        %res.time_SRp = [];
        res.time_dir = [];
    else
        res2.relmin = min(res.rel_err);
        res2.relmax = max(res.rel_err);
        res2.time_SR = res.time_SR / max(size(res.partition));
        res2.time_SR(1) = res2.time_SR(1)/extra_tests(1);
        res2.time_SR(2) = res2.time_SR(2)/extra_tests(2);
        %res2.time_SRp = res.time_SRp / length(res.hC);
        res2.time_dir = res.time_dir / max(size(res.partition));
        res = res2;
    end
end
    
%plot(res.rel_err)
%min(res.rel_err)
%max(res.rel_err)