KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TestCalcMD5.m
Go to the documentation of this file.
1 
2 
3 /* (Autoinserted by mtoc++)
4  * This source code has been filtered by the mtoc++ executable,
5  * which generates code that can be processed by the doxygen documentation tool.
6  *
7  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
8  * Except for the comments, the function bodies of your M-file functions are untouched.
9  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
10  * attached source files that are highly readable by humans.
11  *
12  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
13  * the correct locations in the source code browser.
14  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
15  */
16 
17 function TestCalcMD5(doSpeed) {
18 
19 
20 /* $JRev: R5.00g V:013 Sum:uNknB6D/Ksze Date:12-Dec-2009 00:14:15 $
21  * $File: CalcMD5\TestCalcMD5.m $ */
22 
23 /* Initialize: ==================================================================
24  * Global Interface: ------------------------------------------------------------ */
25 FuncName = " TestCalcMD5 "; /* $Managed by AutoFuncPath$ */
26 
27 
28 /* Initial values: -------------------------------------------------------------- */
29 if nargin == 0
30  doSpeed = true;
31 end
32 
33 /* Program Interface: -----------------------------------------------------------
34  * User Interface: --------------------------------------------------------------
35  * Do the work: ================================================================= */
36 disp([" ==== Test CalcMD5, ", datestr(now, 0)]);
37 
38 TestData = [, " d41d8cd98f00b204e9800998ecf8427e "; ...
39  " a ", " 0cc175b9c0f1b6a831c399e269772661 "; ...
40  " abc ", " 900150983cd24fb0d6963f7d28e17f72 "; ...
41  " message digest ", " f96b697d7cb7938d525a2f31aaf161d0 "; ...
42  " abcdefghijklmnopqrstuvwxyz ", " c3fcd3d76192e4007dfb496cca67e13b "; ...
43  " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ", ...
44  " d174ab98d277d9f5a5611c2c9f419d9f "; ...
45  [" 123456789012345678901234567890123456789012345678901234567890123456 ", ...
46  " 78901234567890 "], " 57edf4a22be3c955ac49da2e2107b67a "; ...
47  char(0:255), " e2c865db4162bed963bfaa9ef6ac18f0 "]; /* Not in RFC1321 */
48 
49 
50 fprintf(" Known answer test from RFC 1321 for strings and files: ");
51 TestFile = tempname;
52 
53 /* Loop over test data: */
54 for iTest = 1:size(TestData, 1)
55  /* Check string input: */
56  Str = CalcMD5(TestData[iTest, 1], " char ");
57  if strcmpi(Str, TestData[iTest, 2]) == 0
58  fprintf(" \n ");
59  error([" *** ", FuncName, " : Failed for string: ", ...
60  char(10), " [ ", TestData[iTest, 1], " ] "]);
61  end
62 
63  /* Check file input: */
64  FID = fopen(TestFile, " wb+ ");
65  if FID < 0
66  fprintf(" \n ");
67  error([" *** ", FuncName, " : Cannot open test file [ ", TestFile, " ] "]);
68  end
69  fwrite(FID, TestData[iTest, 1], " uchar ");
70  fclose(FID);
71 
72  Str2 = CalcMD5(TestFile, " file ");
73  if strcmpi(Str2, TestData[iTest, 2]) == 0
74  fprintf(" \n ");
75  error([" *** ", FuncName, " : Failed for file: ", ...
76  char(10), " [ ", TestData[iTest, 1], " ] "]);
77  end
78 end
79 fprintf(" ok\n ");
80 delete(TestFile);
81 
82 /* Check different output types: */
83 N = 1000;
84 fprintf(" %d random tests with hex, HEX, dec and base64 output: ", N);
85 for i = 1:N
86  data = uint8(fix(rand(1, 1 + fix(rand * 100)) * 256));
87  lowHexOut = CalcMD5(data, " char ", " hex ");
88  upHexOut = CalcMD5(data, " char ", " HEX ");
89  decOut = CalcMD5(data, " char ", " Dec ");
90  b64Out = CalcMD5(data, " char ", " Base64 ");
91 
92  if not(strcmpi(lowHexOut, upHexOut) && ...
93  isequal(sscanf(lowHexOut, " %2x "), decOut(:)) && ...
94  isequal(Base64decode(b64Out), decOut))
95  fprintf(" \n ");
96  error([" *** ", FuncName, " : Different results for output types. "]);
97  end
98 
99  /* Check unicode, if the data length is a multiple of 2: */
100  if rem(length(data), 2) == 0
101  doubleData = double(data);
102  uniData = char(doubleData(1:2:end) + 256 * doubleData(2:2:end));
103  uniOut = CalcMD5(uniData, " unicode ", " dec ");
104  if not(isequal(uniOut, decOut))
105  fprintf(" \n ");
106  error([" *** ", FuncName, " : Different results for unicode input. "]);
107  end
108  end
109 end
110 fprintf(" ok\n ");
111 fprintf(" Unicode input: ok\n\n ");
112 
113 /* Speed test: ------------------------------------------------------------------ */
114 if doSpeed
115  disp(" == Test speed: ");
116  disp(" (Short data: mainly the overhead of calling the function) ");
117  Delay = 2;
118 
119  for Len = [10, 100, 1000, 10000, 1e5, 1e6, 1e7]
120  [Number, Unit] = UnitPrint(Len);
121  fprintf(" Data length: %s %s:\n ", Number, Unit);
122  data = uint8(fix(rand(1, Len) * 256));
123 
124  /* Measure java time: */
125  iniTime = cputime;
126  finTime = iniTime + Delay;
127  javaLoop = 0;
128  while cputime < finTime
129  x = java.security.MessageDigest.getInstance(" MD5 ");
130  x.update(data);
131  javaHash = double(typecast(x.digest, " uint8 "));
132  javaLoop = javaLoop + 1;
133  end
134  javaLoopPerSec = javaLoop / (cputime - iniTime);
135  [Number, Unit] = UnitPrint(javaLoopPerSec * Len);
136  fprintf(" java: %6s %s/sec\n ", Number, Unit);
137 
138  /* Measure Mex time: */
139  iniTime = cputime;
140  finTime = iniTime + Delay;
141  mexLoop = 0;
142  while cputime < finTime
143  mexHash = CalcMD5(data, " char ", " dec ");
144  mexLoop = mexLoop + 1;
145  end
146  mexLoopPerSec = mexLoop / (cputime - iniTime);
147  [Number, Unit] = UnitPrint(mexLoopPerSec * Len);
148  fprintf(" mex: %6s %s/sec: %.1f times faster\n ", ...
149  Number, Unit, mexLoopPerSec / javaLoopPerSec);
150 
151  /* Compare the results: */
152  if ~isequal(javaHash(:), mexHash(:))
153  error([" *** ", FuncName, " : Different results from java and Mex. "]);
154  end
155  end
156 end
157 
158 fprintf(" \nCalcMD5 seems to work well.\n ");
159 
160 return;
161 
162 /* ****************************************************************************** */
163 }
188 function Out = TestCalcMD5>Base64decode(In) {
189 
190 /* Initialize: ================================================================== */
191 Pool = [65:90, 97:122, 48:57, 43, 47]; /* [0:9, a:z, A:Z, +, /] */
192 
193 v8 = [128, 64, 32, 16, 8, 4, 2, 1];
194 v6 = [32; 16; 8; 4; 2; 1];
195 
196 /* Do the work: ================================================================= */
197 In = reshape(In, 1, []);
198 Table = zeros(1, 256);
199 Table(Pool) = 1:64;
200 Value = Table(In) - 1;
201 
202 X = rem(floor(Value(ones(6, 1), :) ./ v6(:, ones(length(In), 1))), 2);
203 Out = v8 * reshape(X(1:fix(numel(X) / 8) * 8), 8, []);
204 
205 return;
206 
207 /* ****************************************************************************** */
208 }
209 
210 function [Number , Unit ] = TestCalcMD5>UnitPrint(N) {
211 
212 if N < 1000
213  Number = sprintf(" %d ", round(N));
214  Unit = " Byte ";
215 elseif N < 1e6
216  Number = sprintf(" %.1f ", N / 1000);
217  Unit = " kB ";
218 else
219  Number = sprintf(" %.1f ", N / 1e6);
220  Unit = " MB ";
221 end
222 
223 return;
224 
225 }
226 
function varargout = typecast(varargin)
Definition: typecast.m:17
function MD5 = CalcMD5(Data, InClass, OutClass)
128 bit MD5 checksum: file, string, byte stream [MEX] This function calculates a 128 bit checksum for...
Definition: CalcMD5.m:17
A double value.
function TestCalcMD5(doSpeed)
Automatic test: CalcMD5 (Mex) This is a routine for automatic testing. It is not needed for processin...
Definition: TestCalcMD5.m:17
#define X(i, j)
function [ Number , Unit ] = TestCalcMD5>UnitPrint(N)
Definition: TestCalcMD5.m:210
A MatLab character array.
function Out = TestCalcMD5>Base64decode(In)
Definition: TestCalcMD5.m:188