1// Copyright (C) 2008-2009 - INRIA - Michael Baudin 2// Copyright (C) 2010 - 2011 - DIGITEO - Michael Baudin 3// 4// Copyright (C) 2012 - 2016 - Scilab Enterprises 5// 6// This file is hereby licensed under the terms of the GNU GPL v2.0, 7// pursuant to article 5.3.4 of the CeCILL v.2.1. 8// This file was originally licensed under the terms of the CeCILL v2.1, 9// and continues to be available under such terms. 10// For more information, see the COPYING file which you should have received 11// along with this program. 12 13function [flag,errmsg] = assert_checkfilesequal ( varargin ) 14 // Check two files are equal. 15 16 [lhs,rhs]=argn() 17 if ( and ( rhs <> [ 2 3 ] ) ) then 18 errmsg = sprintf ( gettext ( "%s: Wrong number of input arguments: %d to %d expected.") , "assert_checkfilesequal" , 2 , 3 ) 19 error(errmsg) 20 end 21 // 22 // Get input arguments 23 filecomp = varargin(1) 24 fileref = varargin(2) 25 if ( rhs <= 2 ) then 26 compfun = [] 27 else 28 compfun = varargin(3) 29 end 30 // 31 // Check types of variables 32 if ( typeof(filecomp) <> "string" ) then 33 errmsg = sprintf ( gettext ( "%s: Wrong type for input argument #%d: Matrix of strings expected.\n") , "assert_checkfilesequal" , 1 ) 34 error(errmsg) 35 end 36 if ( typeof(fileref) <> "string" ) then 37 errmsg = sprintf ( gettext ( "%s: Wrong type for input argument #%d: Matrix of strings expected.\n") , "assert_checkfilesequal" , 2 ) 38 error(errmsg) 39 end 40 if ( compfun <> [] ) then 41 if ( and ( typeof(compfun) <> [ "function" "list" ] ) ) then 42 errmsg = sprintf ( gettext ( "%s: Expected type ""%s"" or ""%s"" for input argument %s #%d, but got %s instead.") , "assert_checkfilesequal" , "function" , "list" , "compfun" , 3 , typeof(compfun) ) 43 error(errmsg) 44 end 45 end 46 // 47 // Check sizes of variables 48 if ( size(filecomp,"*") <> 1 ) then 49 errmsg = sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkfilesequal" , 1 , 1 , 1 ) 50 error(errmsg) 51 end 52 if ( size(fileref,"*") <> 1 ) then 53 errmsg = sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkfilesequal" , 2 , 1 , 1 ) 54 error(errmsg) 55 end 56 // 57 // Test if both files exist on disk 58 if ( fileinfo(filecomp) == [] ) then 59 flag = %f 60 errmsg = sprintf ( gettext ( "%s: The file %s does not exist.\n") , "assert_checkfilesequal" , filecomp ) 61 if ( lhs < 2 ) then 62 assert_generror ( errmsg ) 63 else 64 return 65 end 66 end 67 if ( fileinfo(fileref) == [] ) then 68 flag = %f 69 errmsg = sprintf ( gettext ( "%s: The file %s does not exist.\n") , "assert_checkfilesequal" , fileref ) 70 if ( lhs < 2 ) then 71 assert_generror ( errmsg ) 72 else 73 return 74 end 75 end 76 // 77 // Open files 78 [fdc,err] = mopen(filecomp,"r") 79 if ( err <> 0 ) then 80 flag = %f 81 errmsg = sprintf ( gettext ( "%s: Cannot open file %s.\n") , "assert_checkfilesequal" , filecomp ) 82 if ( lhs < 2 ) then 83 assert_generror ( errmsg ) 84 else 85 return 86 end 87 end 88 [fdr,err] = mopen(fileref,"r") 89 if ( err <> 0 ) then 90 flag = %f 91 errmsg = sprintf ( gettext ( "%s: Cannot open file %s.\n") , "assert_checkfilesequal" , fileref ) 92 if ( lhs < 2 ) then 93 assert_generror ( errmsg ) 94 else 95 return 96 end 97 end 98 // 99 // Get contents 100 txtcomp = mgetl(fdc) 101 txtref = mgetl(fdr) 102 // 103 // Compare contents 104 if ( compfun <> [] ) then 105 if ( typeof(compfun) == "function" ) then 106 areeq = compfun ( txtcomp , txtref ) 107 else 108 // compfun is a list 109 cf = compfun(1) 110 areeq = cf ( txtcomp , txtref , compfun(2:$) ) 111 end 112 else 113 areeq = ( txtcomp == txtref ) 114 end 115 if ( areeq ) then 116 flag = %t 117 errmsg = "" 118 else 119 flag = %f 120 errmsg = msprintf(gettext("%s: The content of computed file ""%s"" is different from the content of reference file ""%s""."), .. 121 "assert_checkfilesequal",filecomp,fileref) 122 // Do not generate the error now: let us close the files before! 123 end 124 // 125 // Close the files 126 err=mclose(fdc) 127 if ( err <> 0 ) then 128 flag = %f 129 errmsg = sprintf ( gettext ( "%s: Cannot close file %s.\n") , "assert_checkfilesequal" , filecomp ) 130 // It may happen that we overwrite the content of the errmsg varaiable. 131 // For example, we are there, while the file contents were different. 132 // We consider that that not being able to close the file is a bigger issue, 133 end 134 err=mclose(fdr) 135 if ( err <> 0 ) then 136 errmsg = sprintf ( gettext ( "%s: Cannot close file %s.\n") , "assert_checkfilesequal" , fileref ) 137 // It may happen that we overwrite the content of the errmsg varaiable. 138 // For example, we are there, while the file contents were different. 139 // We consider that that not being able to close the file is a bigger issue, 140 end 141 142 if ( ~flag & lhs < 2 ) then 143 // If no output variable is given, generate an error 144 assert_generror ( errmsg ) 145 end 146endfunction 147 148