function [errs warns] = unit_grow_struct()

% Unit test for GROW_STRUCT.M. % % [ERRS WARNS] = UNIT_GROW_STRUCT()

errs = {}; warns = {};

% positive tests [errs warns] = basic_case(errs,warns); [errs warns] = empty_into(errs,warns); % we also need a case for cell arrays (which do work)

% all these should raise exceptions [errs warns] = extra_into_field(errs,warns); [errs warns] = extra_from_field(errs,warns); [errs warns] = diff_fnames(errs,warns); [errs warns] = diff_into_lengths(errs,warns); [errs warns] = diff_from_lengths(errs,warns); [errs warns] = empty_from(errs,warns); [errs warns] = empty_into(errs,warns); % need a case for non-vector inputs

% alert the user if there are any problems [errs warns] = alert_unit_errors(errs,warns);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [errs warns] = basic_case(errs,warns)

% the standard example. INTO has some data, FROM has some % data. concatenate, all should be well. into.f1 = [1 2 3]; into.f2 = [10 20 30]; into.f3 = [100 200 300]; from.f1 = [4 5]; from.f2 = [40 50]; from.f3 = [400 500]; actual = grow_struct(into, from); desired.f1 = [1 2 3 4 5]; desired.f2 = [10 20 30 40 50]; desired.f3 = [100 200 300 400 500]; if ~compare_struct(actual,desired) errs{end+1} = ‘Basic test didn”t work right’; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [errs warns] = empty_into(errs,warns)

% the first time we call it, the INTO will be empty into = []; from.f1 = [4 5]; from.f2 = [40 50]; from.f3 = [400 500]; actual = grow_struct(into, from); desired.f1 = [4 5]; desired.f2 = [40 50]; desired.f3 = [400 500]; if ~compare_struct(actual,desired) errs{end+1} = ‘Empty INTO test didn”t work right’; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [errs warns] = extra_into_field(errs,warns)

into.f1 = [1 2 3]; into.f2 = [10 20 30]; into.f3 = [100 200 300]; from.f1 = [4 5]; from.f2 = [40 50]; try actual = grow_struct(into, from); errs{end+1} = ‘Should fail with extra INTO field’; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [errs warns] = extra_from_field(errs,warns)

into.f1 = [1 2 3]; into.f2 = [10 20 30]; from.f1 = [4 5]; from.f2 = [40 50]; from.f3 = [400 500]; try actual = grow_struct(into, from); errs{end+1} = ‘Should fail with extra FROM field’; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [errs warns] = diff_fnames(errs,warns)

into.f1 = [1 2 3]; into.f2 = [10 20 30]; from.f1 = [4 5]; from.f4 = [400 500]; try actual = grow_struct(into, from); errs{end+1} = ‘Should fail with different field names’; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [errs warns] = diff_into_lengths(errs,warns)

into.f1 = [1 2 3 3]; into.f2 = [10 20 30]; into.f3 = [100 200 300]; from.f1 = [4 5]; from.f2 = [40 50]; from.f3 = [400 500]; try actual = grow_struct(into, from); errs{end+1} = ‘Should fail if INTO fields are different lengths’; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [errs warns] = diff_from_lengths(errs,warns)

into.f1 = [1 2 3]; into.f2 = [10 20 30]; into.f3 = [100 200 300]; from.f1 = [4 5 5]; from.f2 = [40 50]; from.f3 = [400 500]; try actual = grow_struct(into, from); errs{end+1} = ‘Should fail if FROM fields are different lengths’; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [errs warns] = empty_from(errs,warns)

% unlike ‘empty into’, this is aberrant

into.f1 = [1 2 3]; into.f2 = [10 20 30]; into.f3 = [100 200 300]; from = []; try actual = grow_struct(into, from); errs{end+1} = ‘Should fail with empty FROM’; end

into.f1 = [1 2 3]; into.f2 = [10 20 30]; into.f3 = [100 200 300]; from = []; try actual = grow_struct(into, from); errs{end+1} = ‘Should fail if FROM fields are empty’; end

Home - Blog - Startups - Software - Research - Writing

Email me - updated 31 Dec 2012