%%%% Testscript for empty assignment, Octave bug 31287, Olaf Till %%%%

lhs_e = [];
lhs_2d = ones (2, 2);
lhs_3d = ones (2, 2, 2);
ea1 = ones (0, 1);
ea2 = ones (3, 2, 0, 2);
fprintf ('The following variables will be used:\nlhs_e = [];\nlhs_2d = ones (2, 2);\nlhs_3d = ones (2, 2, 2);\nea1 = ones (0, 1);\nea2 = ones (3, 2, 0, 2);\n\n');

fprintf ('There should be no messages after this one.\n');

%%%% After this, there should be no messages. Verified with Matlab 2009b. %%%%

%%%%%%%%%%%%%% "null assignment" %%%%%%%%%%%%%%%
idc3dls = {'(false)', '(false, [])', '(false, [], 1)'};
%%DISPUTABLE
idc3dlf = {'(false, 1)', '(false, false)'};
idcs = {'([], [])', '(1, [])', '([], 1, [])', '(1, [], 1, 1)', '([], 1, 1)', ...
        '(1, ea2)', '([], 1, ea2)', '(1, ea2, ea2)', '(1, ea2, 1)', ...
        '(false, 1)', '(1, [], false)', '(false, false)', '(false, [], false)', ...
        '([], false, false, false)', '(1, [], false, false)', ...
        '(:, false)', '(false, :)', '(false, :, [], 1)', '(:, [], false)'};
%%DISPUTABLE
idcf = {'(1, 1, [])', '(false, false, 1)', '(false, false, [])', '(false, false, [], false)', ...
        '(1, false, [], false)','(:, false, 1)'};
%%
idce = {'([])', '(:)', '(false)', '([], false)', '([], false, :)'};
for k = 1 : length (idc3dls)
  cmd = sprintf ('lhs_3d%s = [];', idc3dls{k});
  try
    eval (cmd);
  catch
    fprintf ('\nshould succeed, but did not:\n%s\n%s\n', cmd, lasterr);
  end
end
for k = 1 : length (idc3dlf)
  cmd = sprintf ('lhs_3d%s = [];', idc3dlf{k});
  try
    eval (cmd);
    fprintf ('\nDISPUTABLE: should not succeed, but did:\n%s\n', cmd);
  catch
  end
end
for k = 1 : length (idcs)
  cmd = sprintf ('lhs_2d%s = [];', idcs{k});
  try
    eval (cmd);
  catch
    fprintf ('\nshould succeed, but did not:\n%s\n%s\n', cmd, lasterr);
  end
end
for k = 1 : length (idcf)
  cmd = sprintf ('lhs_2d%s = [];', idcf{k});
  try
    eval (cmd);
    fprintf ('\nDISPUTABLE: should not succeed, but did:\n%s\n', cmd);
  catch
  end
end
%% lhs []
for k = 1 : length (idce)
  cmd = sprintf ('lhs_2d%s = [];', idce{k});
  try
    eval (cmd);
  catch
    fprintf ('\nshould succeed, but did not:\n%s\n%s\n', cmd, lasterr);
  end
end

%%%%%%%%%%% no "null assignment", lhs and rhs <= 2D and #indices <= 2 %%%%%%%%%%%%%%%%
%% use strings and evaluate them later, so we can also use them in messages
rhs = {'zeros (0, 2)', 'zeros (2, 0)'};
i_lhs = {'lhs_2d([])', 'lhs_2d(ea2)', 'lhs_2d(false)', ...
         'lhs_2d(1, [])', 'lhs_2d(1, ea2)', 'lhs_2d(1, false)', ...
         'lhs_2d(false, false)'};
for k = 1 : length (i_lhs)
  for l = 1 : length (rhs)
    cmd = sprintf ('%s = %s;', i_lhs{k}, rhs{l});
    try
      eval (cmd);
    catch
      fprintf ('\nshould succeed, but did not:\n%s\n%s\n', cmd, lasterr);
    end
  end
end

%%%%%%%%%%% no "null assignment", lhs or rhs > 2D or #indices > 2 %%%%%%%%%%%%%%%%
%% #indices > 2
cmd = 'lhs_2d(1, false, 1) = zeros (0, 2)';
try
  eval (cmd);
  fprintf ('\nshould not succeed, but did:\n%s\n', cmd);
catch
end
%% lhs > 2D
cmd = 'lhs_3d(false, false) = zeros (0, 2);';
try
  eval (cmd);
  fprintf ('\nshould not succeed, but did:\n%s\n', cmd);
catch
end
%% rhs > 2D
cmd = 'lhs_2d(false, false) = zeros (0, 2, 0);';
try
  eval (cmd);
  fprintf ('\nshould not succeed, but did:\n%s\n', cmd);
catch
end
%% some other cases, left column: index, right column: rhs
as = {'(:, :, [])', 'zeros (2, 2, 0)'; ...
      '(:, [], :)', 'zeros (2, 0, 2)'; ...
      '(:, [], :, [])', 'zeros (2, 0, 2, 0)'; ...
      '(1, :, 1, 1, [], [], [], 1, 1)', 'zeros (2, 0, 0, 0)'; ...
      '(:, [], :, [])', 'zeros (1, 1, 2, 1, 0, 1, 1, 2, 1, 0, 1, 1, 1)'; ...
      '(1, :, 1, 1, [], [], [], 1, 1)', 'zeros (1, 1, 2, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1)'};
af = {'(:, :, [])', 'zeros (2, 0, 0)'; ...
      '(:, [], :)', 'zeros (0, 2, 2)'; ...
      '(:, [], :, [])', 'zeros (2, 0, 0, 2)'; ...
      '(1, :, 1, 1, [], [], [], 1, 1)', 'zeros (0, 2, 0, 0)'; ...
      '(:, [], :, [])', 'zeros (1, 1, 2, 1, 0, 1, 1, 2, 1, 1, 1, 1)'; ...
      '(1, :, 1, 1, [], [], 1, 1)', 'zeros (1, 1, 2, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1)'};
for k = 1 : length (as)
  cmd = sprintf ('lhs_3d%s = %s;', as{k, 1}, as{k, 2});
  try
    eval (cmd);
  catch
    fprintf ('\nshould succeed, but did not:\n%s\n%s\n', cmd, lasterr);
  end
end
for k = 1 : length (af)
  cmd = sprintf ('lhs_3d%s = %s;', af{k, 1}, af{k, 2});
  try
    eval (cmd);
    fprintf ('\nshould not succeed, but did:\n%s\n', cmd);
  catch
  end
end
