From 692d8654b894c53f0d48be38595c95360ccdf1ea Mon Sep 17 00:00:00 2001 From: Ray Zimmerman Date: Tue, 13 Feb 2024 10:08:32 -0700 Subject: [PATCH] Add more tests for table subscripting. Especially for the subsref() and subasgn() for the following: T{*, } T{*, } T(*, ) T(*, ) --- inst/t/t_01_table.m | 137 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 3 deletions(-) diff --git a/inst/t/t_01_table.m b/inst/t/t_01_table.m index 470d0b2d..69ea14f4 100644 --- a/inst/t/t_01_table.m +++ b/inst/t/t_01_table.m @@ -21,7 +21,7 @@ skip_rowname_autogen = true; n_classes = numel (table_classes); -nt = 160 + (8 * n_classes); +nt = 229 + (8 * n_classes); t_begin (n_classes * nt, quiet); @@ -57,6 +57,8 @@ [nr, nz] = size (T); t_is ([nr, nz], [6 6], 12, [t '[nr, nz] = size (T)']); t_ok (isempty (T.Properties.RowNames), [t 'RowNames'] ); + t_ok (isequal (T.Properties.VariableNames, ... + {'var1', 'var2', 'var3', 'var4', 'Var5', 'var6'}), [t 'VariableNames'] ); t_ok (isequal (T.Properties.DimensionNames, {'Row', 'Variables'}), ... [t 'DimensionNames'] ); @@ -164,9 +166,18 @@ t_ok (isequal (T{:, 5}, v5), [t 'T{:, 5} == v5']); t_ok (isequal (T{:, 6}, v6), [t 'T{:, 6} == v6']); + t_ok (isequal (T{:, 4:6}, [v4 v5 v6]), [t 'T{:, j1:jN}']); + t_ok (isequal (T{:, 2:2:6}, [v2 v4 v6]), [t 'T{:, j1:2:jN}']); + + t_ok (isequal (T{:, 'igr'}, v1), [t 'T{:, ''igr''} == v1']); + t_ok (isequal (T{:, "flt"}, v2), [t 'T{:, "flt"} == v2']); + t_ok (isequal (T{:, 'str'}, v3), [t 'T{:, ''str''} == v3']); + t_ok (isequal (T{:, 'dbl'}, v4), [t 'T{:, ''dbl''} == v4']); + t_ok (isequal (T{:, 'boo'}, v5), [t 'T{:, ''boo''} == v5']); + t_ok (isequal (T{:, 'mat'}, v6), [t 'T{:, ''mat''} == v6']); + t_ok (isequal (T{2, 1}, v1(2)), [t 'T{i, j} == v(i)']); t_ok (isequal (T{4, 6}, v6(4, :)), [t 'T{i, j} == v(i)']); - t_ok (isequal (T{end, end}, v6(end,:)), [t 'T{end, end} == v(end,:)']); t_ok (isequal (T{1:3, 2}, v2(1:3)), [t 'T{i1:iN, j} == v(i1:iN)']); t_ok (isequal (T{1:3, 6}, v6(1:3, :)), [t 'T{i1:iN, j} == v(i1:iN, :)']); @@ -177,6 +188,17 @@ t_ok (isequal (T{:, [4;1]}, [v4 v1]), [t 'T{:, jj} == [v v ...]']); t_ok (isequal (T{:, [4;6;1]}, [v4 v6 v1]), [t 'T{:, jj} == [v v ...]']); + t_ok( isequal (T{2, 'igr'}, v1(2)), [t 'T{i, } == v(i)']); + t_ok( isequal (T{4, 'mat'}, v6(4, :)), [t 'T{i, } == v(i)']); + t_ok( isequal (T{1:3, 'flt'}, v2(1:3)), [t 'T{i1:iN, } == v(i1:iN)']); + t_ok( isequal (T{1:3, 'mat'}, v6(1:3, :)), [t 'T{i1:iN, } == v(i1:iN, :)']); + t_ok( isequal (T{[6;3], 'boo'}, v5([6;3])), [t 'T{ii, } == v(ii)']); + t_ok( isequal (T{[6;3], 'mat'}, v6([6;3], :)), [t 'T{ii, } == v(ii, :)']); + t_ok( isequal (T{6:-1:3, {'flt', 'dbl', 'boo'}}, [v2(6:-1:3) v4(6:-1:3) v5(6:-1:3)]), [t 'T{iN:-1:i1, }']); + t_ok( isequal (T{6:-1:3, {'flt', 'dbl', 'mat'}}, [v2(6:-1:3) v4(6:-1:3) v6(6:-1:3, :)]), [t 'T{iN:-1:i1, }']); + t_ok( isequal (T{:, {'dbl', 'igr'}}, [v4 v1]), [t 'T{:, } == [v v ...]']); + t_ok( isequal (T{:, {'dbl', 'mat', 'igr'}}, [v4 v6 v1]), [t 'T{:, } == [v v ...]']); + t = sprintf ('%s : subsasgn {} : ', cls); T{:, 1} = var1; t_is (T.igr, var1, 12, [t 'T{:, 1} = var1']); @@ -191,6 +213,37 @@ T{:, 6} = var6; t_is (T.mat, var6, 12, [t 'T{:, 6} = var6']); + T{:, 4:6} = [v4 v5 v6]; + t_is (T{:, [2;4;5;6]}, [var2 v4 v5 v6], 12, [t 'T{:, j1:jN} = ']); + T{:, 2:2:6} = [v2 var4 var6]; + t_is (T{:, [2;4;5;6]}, [v2 var4 v5 var6], 12, [t 'T{:, j1:2:jN} = ']); + T{:, [2 5]} = [var2 var5]; # reset values + + T2 = table_class([1;2;3], [4;5;6], [7;8;9]); + T2{2, :} = T2{2, :} * 10; + t_is (T2{:, :}, [1 4 7; 20 50 80; 3 6 9], 12, [t 'T{i, :} = ']); + T2{[1 3], :} = T2{[1 3], :} * -1; + t_is (T2{:, :}, [-1 -4 -7; 20 50 80; -3 -6 -9], 12, [t 'T{ii, :} = ']); + T2{:, :} = [1 2 3; 4 5 6; 7 8 9]; + t_is (T2{:, :}, [1 2 3; 4 5 6; 7 8 9], 12, [t 'T{:, :} = ']); + + T{:, 'igr'} = v1; + t_is (T.igr, v1, 12, [t 'T{:, ''igr''} = v1']); + T{:, 'flt'} = v2; + t_is (T.flt, v2, 12, [t 'T{:, ''flt''} = v2']); + T{:, 'str'} = v3; + t_ok (isequal (T.str, v3), [t 'T{:, ''str''} = v3']); + T{:, 'dbl'} = v4; + t_is (T.dbl, v4, 12, [t 'T{:, ''dbl''} = v4']); + T{:, 'boo'} = v5; + t_is (T.boo, v5, 12, [t 'T{:, ''boo''} = v5']); + T{:, 'mat'} = v6; + t_is (T.mat, v6, 12, [t 'T{:, ''mat''} = v6']); + T{:, {'str'}} = var3; + t_ok (isequal(T{:, 3}, var3), [t 'T{:, } = var3']); + T{:, {'igr', 'mat', 'dbl', 'flt', 'boo'}} = [var1 var6 var4 var2 var5]; + t_is (T{:, [1;2;4;5;6]}, [var1 var2 var4 var5 var6], 12, [t 'T{:, } = ']); + T{2, 1} = v1(2); t_ok (isequal (T{2, 1}, v1(2)), [t 'T{i, j} = v(i)']); T{4, 6} = v6(4, :); @@ -211,6 +264,28 @@ t_ok (isequal (T{6:-1:3, [2;4;6;5]}, [v2(6:-1:3) v4(6:-1:3) v6(6:-1:3, :) v5(6:-1:3)]), [t 'T{iN:-1:i1, jj}']); T{:, [6;4;1]} = [v6 v4 v1]; t_ok (isequal (T{:, [6;4;1]}, [v6 v4 v1]), [t 'T{:, jj} = [v v ...]']); + + T{2, 'igr'} = var1(2); + t_ok (isequal (T{2, 1}, var1(2)), [t 'T{i, } = var(i)']); + T{4, 'mat'} = var6(4, :); + t_ok (isequal (T{4, 6}, var6(4, :)), [t 'T{i, } = var(i, :)']); + T{1:3, 'flt'} = var2(1:3); + t_ok (isequal (T.flt(1:3), var2(1:3)), [t 'T{i1:iN, } = var(i1:iN)']); + T{1:3, 'mat'} = var6(1:3, :); + t_ok (isequal (T.mat(1:3, :), var6(1:3, :)), [t 'T{i1:iN, } = var(i1:iN, :)']); + T{1:3, 'mat'} = var6(1:3, [2; 1]); + t_ok (isequal (T.mat(1:3, :), var6(1:3, [2; 1])), [t 'T{i1:iN, } = var(i1:iN, jj)']); + T{[6;3], 'boo'} = var5([6;3]); + t_ok (isequal (T.boo([6;3]), var5([6;3])), [t 'T{ii, } = var(ii)']); + T{[6;3], 'mat'} = var6([6;3], :); + t_ok (isequal (T.mat([6;3], :), var6([6;3], :)), [t 'T{ii, } = var(ii, :)']); + T{[6;3], 'mat'} = var6([6;3], [2;1]); + t_ok (isequal (T.mat([6;3], :), var6([6;3], [2;1])), [t 'T{ii, } = v(ii, jj)']); + T{6:-1:3, {'flt', 'dbl', 'mat', 'boo'}} = [var2(6:-1:3) var4(6:-1:3) var6(6:-1:3, :) var5(6:-1:3)]; + t_ok (isequal (T{6:-1:3, [2;4;6;5]}, [var2(6:-1:3) var4(6:-1:3) var6(6:-1:3, :) var5(6:-1:3)]), [t 'T{iN:-1:i1, }']); + T{:, {'mat', 'dbl', 'igr'}} = [var6 var4 var1]; + t_ok (isequal (T{:, [6;4;1]}, [var6 var4 var1]), [t 'T{:, } = [var var ...]']); + T{:, 1} = v1; T{:, 2} = v2; T{:, 3} = v3; @@ -228,6 +303,15 @@ t_ok (isequal (T2.Properties.RowNames, row_names(ii)), [t 'RowNames']); t_ok (isequal (T2.Properties.DimensionNames, T.Properties.DimensionNames), [t 'DimensionNames']); + t = sprintf ('%s : subsref () : T(ii,) : ', cls); + ii = [2;4]; jj = [1;3;4;6]; cc = {'igr', 'str', 'dbl', 'mat'}; + T2 = T(ii,cc); + t_is (size (T2), [2 4], 12, [t 'size']); + t_ok (isequal (T2.Properties.VariableNames, var_names(jj)), [t 'VariableNames']); + t_ok (isequal (table_values(T2), {v1(ii), v3(ii), v4(ii), v6(ii, :)}), [t 'VariableValues']); + t_ok (isequal (T2.Properties.RowNames, row_names(ii)), [t 'RowNames']); + t_ok (isequal (T2.Properties.DimensionNames, T.Properties.DimensionNames), [t 'DimensionNames']); + t = sprintf ('%s : subsref () : T(:,j1:s:jN) : ', cls); jj = [2:2:6]; T2 = T(:,2:2:6); @@ -260,6 +344,20 @@ t_ok (isequal (T2.Properties.RowNames, row_names(3)), [t 'RowNames']); t_ok (isequal (T2.Properties.DimensionNames, T.Properties.DimensionNames), [t 'DimensionNames']); + t = sprintf ('%s : subsref () : T(i,) : ', cls); + T2 = T(5,'dbl'); + t_is (size (T2), [1 1], 12, [t 'size']); + t_ok (isequal (T2.Properties.VariableNames, var_names(4)), [t 'VariableNames']); + t_ok (isequal (table_values(T2), {v4(5)}), [t 'VariableValues']); + t_ok (isequal (T2.Properties.RowNames, row_names(5)), [t 'RowNames']); + t_ok (isequal (T2.Properties.DimensionNames, T.Properties.DimensionNames), [t 'DimensionNames']); + T2 = T(3,'mat'); + t_is (size (T2), [1 1], 12, [t 'size']); + t_ok (isequal (T2.Properties.VariableNames, var_names(6)), [t 'VariableNames']); + t_ok (isequal (table_values(T2), {v6(3, :)}), [t 'VariableValues']); + t_ok (isequal (T2.Properties.RowNames, row_names(3)), [t 'RowNames']); + t_ok (isequal (T2.Properties.DimensionNames, T.Properties.DimensionNames), [t 'DimensionNames']); + t = sprintf ('%s : subsref () : T(end, end) : ', cls); T2 = T(end, end); t_is (size (T2), [1 1], 12, [t 'size']); @@ -283,6 +381,18 @@ t_ok (isequal (T2.Properties.DimensionNames, T.Properties.DimensionNames), [t 'DimensionNames']); T(ii0, jj) = T2; # restore + t = sprintf ('%s : subsasgn () : T(ii,) : ', cls); + ii0 = [2;4]; jj = [1;3;4;6]; cc = {'igr', 'str', 'dbl', 'mat'}; + T3 = T(ii0,jj); + ii = [4;2]; + T(ii,cc) = T3; + T2 = T(ii,jj); + t_ok (isequal (T2.Properties.VariableNames, var_names(jj)), [t 'VariableNames']); + t_ok (isequal (table_values(T2), {v1(ii0), v3(ii0), v4(ii0), v6(ii0, :)}), [t 'VariableValues']); + t_ok (isequal (T2.Properties.RowNames, row_names(ii)), [t 'RowNames']); + t_ok (isequal (T2.Properties.DimensionNames, T.Properties.DimensionNames), [t 'DimensionNames']); + T(ii0, jj) = T2; # restore + t = sprintf ('%s : subsasgn () : T(:,j1:s:jN) : ', cls); jj = [2:2:6]; T3 = T(end:-1:1,2:2:6); @@ -324,7 +434,28 @@ t_ok (isequal (T2.Properties.VariableNames, var_names(6)), [t 'VariableNames']); t_ok (isequal (table_values(T2), {[111 222]}), [t 'VariableValues']); t_ok (isequal (T2.Properties.RowNames, row_names(3)), [t 'RowNames']); - t_ok (isequal (T2.Properties.DimensionNames, dim_names), [t 'DimensionNames']); + t_ok (isequal (T2.Properties.DimensionNames, T.Properties.DimensionNames), [t 'DimensionNames']); + T{3,6} = [3 0.3]; + + t = sprintf ('%s : subsref () : T(i,) : ', cls); + T3 = T(5,4); + T3{1,1} = exp(1); + T(5,4) = T3; + T2 = T(5,'dbl'); + t_ok (isequal (T2.Properties.VariableNames, var_names(4)), [t 'VariableNames']); + t_ok (isequal (table_values(T2), {exp(1)}), [t 'VariableValues']); + t_ok (isequal (T2.Properties.RowNames, row_names(5)), [t 'RowNames']); + t_ok (isequal (T2.Properties.DimensionNames, T.Properties.DimensionNames), [t 'DimensionNames']); + T{5,4} = pi; + + T3 = T(3,6); + T3{1,1} = [111 222]; + T(3,6) = T3; + T2 = T(3,'mat'); + t_ok (isequal (T2.Properties.VariableNames, var_names(6)), [t 'VariableNames']); + t_ok (isequal (table_values(T2), {[111 222]}), [t 'VariableValues']); + t_ok (isequal (T2.Properties.RowNames, row_names(3)), [t 'RowNames']); + t_ok (isequal (T2.Properties.DimensionNames, T.Properties.DimensionNames), [t 'DimensionNames']); T{3,6} = [3 0.3]; endif