1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
function success = Oneliners_Test_Structs
success = true;
% create some test variables
a.test = 4;
a.yar = 'test';
b.test = 'collision';
c.der = 42;
d.test = 4;
d.c = {3,'true',false};
e.c = {3,'true',false};
notstruct = 'nope, not a struct';
f.r = 4;
f(3).t = 5;
f(4).e = [];
g.r = 4;
g.e = 34;
g(2).r = 5;
g(3).r = 4;
data(1).field = 23;
data(2).field = 56;
data2(1).test = 23;
data2(2).test = 'd';
data3(1).cells = 23;
data3(2).cells = [42 45];
data4(1).struc.a = 24;
data4(2).struc.a = [42 45];
try
% test: AddStructs
qCaught = false;
try
% this should generate an error
out = AddStructs(a,b);
catch
qCaught = true;
end
if ~qCaught
success = false;
fprintf('AddStructs didn''t flag collision when merging two structs that contain the same field\n');
end
clear out, clear qCaught
qCaught = false;
try
% this should generate an error
out = AddStructs(a,notstruct);
catch
qCaught = true;
end
if ~qCaught
success = false;
fprintf('AddStructs didn''t flag that one of the inputs was not a struct\n');
end
clear out, clear qCaught
out = AddStructs(a,c);
fields = fieldnames(out);
if length(fields)>3 || ~any(strcmp('test',fields)) || ~any(strcmp('yar',fields)) || ~any(strcmp('der',fields))
success = false;
fprintf('AddStructs fieldnames in result not correct\ngot fields:\n');
fprintf(' %s\n',fields{:});
elseif out.test~=4 || ~strcmp(out.yar,'test') || out.der~=42
success = false;
fprintf('AddStructs values in fields not correct\n');
end
clear out, clear fields
% test: AreStructsEqualOnFields
out = AreStructsEqualOnFields(a,b,'test');
if out
success = false;
fprintf('AreStructsEqualOnFields these two structs should not compare equal on the field ''test''\n');
end
out = AreStructsEqualOnFields(a,d,'test');
if ~out
success = false;
fprintf('AreStructsEqualOnFields these two structs should compare equal on the field ''test''\n');
end
out = AreStructsEqualOnFields(d,e,'c');
if ~out
success = false;
fprintf('AreStructsEqualOnFields these two structs should compare equal on the field ''test''\n');
end
clear out
% test: CleanStruct
out = CleanStruct(f);
if ~(length(out)==2 && out(1).r==4 && isempty(out(1).t) && out(2).t==5 && isempty(out(2).r))
success = false;
fprintf('CleanStruct did not remove empty structs from struct array\n');
elseif isfield(out,'e')
success = false;
fprintf('CleanStruct did not remove empty field from struct array\n');
end
clear out
% test: FillEmptyFields
out = FillEmptyFields(f,'-');
if ~(all(strcmp({out.e},'-')) && out(2).r=='-' && out(3).r=='-') % not going to test all, if these work rest will too
success = false;
fprintf('FillEmptyFields did not fill in empty fields in struct array correctly\n');
end
out = FillEmptyFields({'a',[],4,[]},'-');
if ~(out{1}=='a' && out{2}=='-' && out{3}==4 && out{4}=='-')
success = false;
fprintf('FillEmptyFields did not fill in empty elements in cell array correctly\n');
end
clear out
% test: GroupStructArrayByFields
out = GroupStructArrayByFields(g,'r');
if ~(length(out{1})==2 && isscalar(out{2}))
success = false;
fprintf('GroupStructArrayByFields did not group structs correctly\n');
end
clear out
% test: Struct2Vect
out = Struct2Vect(data,'field');
if ~(isnumeric(out) && all(out==[23 56]))
success = false;
fprintf('Struct2Vect did not concatenate two scalar numerics correctly\n');
end
out = Struct2Vect(data2,'test');
if ~(iscell(out) && out{1}==23 && out{2}=='d')
success = false;
fprintf('Struct2Vect did not concatenate a scalar numeric and a char correctly\n');
end
out = Struct2Vect(data3,'cells');
if ~(iscell(out) && out{1}==23 && all(out{2}==[42 45]))
success = false;
fprintf('Struct2Vect did not concatenate a scalar numeric and numeric array correctly\n');
end
out = Struct2Vect(data4,'struc');
if ~(isstruct(out) && out(1).a==24 && all(out(2).a==[42 45]))
success = false;
fprintf('Struct2Vect did not concatenate scalar structs correctly\n');
end
clear out
catch me
success = false;
fprintf('Unit test %s failed, error ocurred:\n%s\n',mfilename,me.getReport());
end
|