% Testing combination of ARCH parameters of both fopen and fread.
% Related to Octave anomaly report #45687.
% The file 'test_octave_double_01.hex' is required in excution directory.
% This script is intened to run also on Matlab so I had to make it as
% function to get the nested function works ...
%
% Emmanuel Goua de Baix <emmanuel.goua-de-baix@wnavontime.com> 2015-08-06
function rpt_arch_combinations()

% NOTE : I don't care about the correctness of the value, I just want to produce
% an error of fread() and catch it

% initial test without any ARCH param and check tested file
fid=fopen('test_octave_double_01.hex', 'r');
if fid<0
  error('Test file not found !');
end
fprintf('fopen ARCH nothing | fread ARCH nothing => ');
fread(fid, 1, 'double');fprintf('OK\n'); % should be OK no try/catch ...
fclose(fid);

% nested function that make the test for a given combination of ARCH
% to test without ARCH, use 'nothing'
function test_archs(fopen_arch, fread_arch)
  fprintf('fopen ARCH % 7s | fread ARCH % 7s => ', fopen_arch, fread_arch);
  % open the file with ARCH 
  if strcmpi(fopen_arch, 'nothing') == 0
    fid=fopen('test_octave_double_01.hex', 'r', fopen_arch);
  else
    fid=fopen('test_octave_double_01.hex', 'r');
  end
  try
    if strcmpi(fread_arch, 'nothing') == 0
      fread(fid, 1, 'double', 0, fread_arch)
    else 
      fread(fid, 1, 'double')
    end
    %fprintf('OK\n');
  catch
    fprintf('ERROR !\n');
  end
  fclose(fid);
end

% fopen 'ieee-le' , fread no ARCH
test_archs('ieee-le', 'nothing');
% fopen 'ieee-be' , fread no ARCH 
test_archs('ieee-be', 'nothing');
% fopen 'b' , fread no ARCH 
test_archs('b', 'nothing');
% fopen no ARCH , fread 'native'
test_archs('nothing', 'native');
% fopen 'ieee-le' , fread 'native'
test_archs('ieee-le', 'native');
% fopen 'ieee-be' , fread 'native'
test_archs('ieee-be', 'native');
% fopen 'b' , fread 'native'
test_archs('b', 'native');
% fopen no ARCH , fread 'ieee-be'
test_archs('nothing', 'ieee-be');
% fopen 'ieee-le' , fread 'ieee-be'
test_archs('ieee-le', 'ieee-be');
% fopen 'ieee-be' , fread 'ieee-be'
test_archs('ieee-be', 'ieee-be');
% fopen 'b' , fread 'ieee-be'
test_archs('b', 'ieee-be');
% fopen no ARCH , fread 'ieee-le'
test_archs('nothing', 'ieee-le');
% fopen 'ieee-le' , fread 'ieee-le'
test_archs('ieee-le', 'ieee-le');
% fopen 'ieee-be' , fread 'ieee-le'
test_archs('ieee-be', 'ieee-le');
% fopen 'b' , fread 'ieee-le'
test_archs('b', 'ieee-le');
end
