comparison scripts/miscellaneous/compare_versions.m @ 12288:f07e6b4d34c7 release-3-4-x

Add function compare_versions to documentation. Update docstrings for surrounding functions in manual.
author Rik <octave@nomad.inbox5.com>
date Sat, 29 Jan 2011 16:54:14 -0800
parents fd0a3ac60b0e
children f96b9b9f141b
comparison
equal deleted inserted replaced
12287:0566e6fef0c0 12288:f07e6b4d34c7
64 ## @end itemize 64 ## @end itemize
65 ## 65 ##
66 ## Note that version "1.1-test2" will compare as greater than 66 ## Note that version "1.1-test2" will compare as greater than
67 ## "1.1-test10". Also, since the numeric part is compared first, "a" 67 ## "1.1-test10". Also, since the numeric part is compared first, "a"
68 ## compares less than "1a" because the second string starts with a 68 ## compares less than "1a" because the second string starts with a
69 ## numeric part even though double("a") is greater than double("1"). 69 ## numeric part even though @code{double("a")} is greater than
70 ## @code{double("1").}
70 ## @end deftypefn 71 ## @end deftypefn
71 72
72 ## Author: Bill Denney <denney@seas.upenn.edu> 73 ## Author: Bill Denney <denney@seas.upenn.edu>
73
74 ## FIXME?: This allows a single equal sign "=" to indicate equality, do
75 ## we want to require a double equal since that is the boolean operator?
76 74
77 function out = compare_versions (v1, v2, operator) 75 function out = compare_versions (v1, v2, operator)
78 76
79 if (nargin != 3) 77 if (nargin != 3)
80 print_usage (); 78 print_usage ();
81 endif 79 endif
82 80
83 ## Make sure that the version numbers are valid. 81 ## Make sure that the version numbers are valid.
84 if (! (ischar (v1) && ischar (v2))) 82 if (! (ischar (v1) && ischar (v2)))
85 error ("compare_versions: both version numbers must be strings"); 83 error ("compare_versions: both version numbers must be strings");
86 elseif (size (v1, 1) != 1 || size (v2, 1) != 1) 84 elseif (rows (v1) != 1 || rows (v2) != 1)
87 error ("compare_versions: version numbers must be a single row"); 85 error ("compare_versions: version numbers must be a single row");
88 endif 86 endif
89 87
90 ## check and make sure that the operator is valid 88 ## check and make sure that the operator is valid
91 if (! ischar (operator)) 89 if (! ischar (operator))
92 error ("compare_versions: OPERATOR must be a character string"); 90 error ("compare_versions: OPERATOR must be a character string");
93 elseif (numel (operator) > 2) 91 elseif (numel (operator) > 2)
94 error("compare_versions: OPERATOR cannot be more than 2 characters long"); 92 error("compare_versions: OPERATOR must be 1 or 2 characters long");
95 endif 93 endif
96 94
97 ## trim off any character data that is not part of a normal version 95 ## trim off any character data that is not part of a normal version
98 ## number 96 ## number
99 numbers = "0123456789."; 97 numbers = "0123456789.";
167 ## Make sure that we don't have conflicting operators. 165 ## Make sure that we don't have conflicting operators.
168 if (gt_op && lt_op) 166 if (gt_op && lt_op)
169 error ("compare_versions: OPERATOR cannot contain both greater and less than symbols"); 167 error ("compare_versions: OPERATOR cannot contain both greater and less than symbols");
170 elseif ((gt_op || lt_op) && not_op) 168 elseif ((gt_op || lt_op) && not_op)
171 error ("compare_versions: OPERATOR cannot contain not and greater than or less than symbols"); 169 error ("compare_versions: OPERATOR cannot contain not and greater than or less than symbols");
170 elseif (strcmp (operator, "="))
171 error ("compare_versions: equality OPERATOR is \"==\", not \"=\"");
172 elseif (! (equal_op || not_op || lt_op || gt_op))
173 error ("compare_versions: No valid OPERATOR specified");
172 endif 174 endif
173 175
174 ## Compare the versions (making sure that they're the same shape) 176 ## Compare the versions (making sure that they're the same shape)
175 vcmp = v1n(:) - v2n(:); 177 vcmp = v1n(:) - v2n(:);
176 vcmp = [vcmp; (v1c - v2c)(:)]; 178 vcmp = [vcmp; (v1c - v2c)(:)];
185 out = equal_op; 187 out = equal_op;
186 elseif (lt_op || gt_op) 188 elseif (lt_op || gt_op)
187 ## They're correctly less than or greater than. 189 ## They're correctly less than or greater than.
188 out = (vcmp(firstdiff) > 0); 190 out = (vcmp(firstdiff) > 0);
189 else 191 else
190 ## They're not correctly less than or greater than, and they're not 192 ## They're not correctly less than or greater than, and they're not equal.
191 ## equal.
192 out = false; 193 out = false;
193 endif 194 endif
194 195
195 ## Reverse the output if not is given. 196 ## Reverse the output if not is given.
196 out = xor (not_op, out); 197 if (not_op)
198 out = !out;
199 endif
197 200
198 endfunction 201 endfunction
199 202
200 ## tests 203 ## tests
201 ## test both equality symbols 204 ## test both equality symbols
202 %!assert(compare_versions("1", "1", "="), true)
203 ## test arbitrarily long equality 205 ## test arbitrarily long equality
204 %!assert(compare_versions("1.1.0.0.0", "1.1", "=="), true) 206 %!assert(compare_versions("1.1.0.0.0", "1.1", "=="), true)
205 %!assert(compare_versions("1", "1.1", "<"), true) 207 %!assert(compare_versions("1", "1.1", "<"), true)
206 %!assert(compare_versions("1.1", "1.1", "<="), true) 208 %!assert(compare_versions("1.1", "1.1", "<="), true)
207 %!assert(compare_versions("1.1", "1.1.1", "<="), true) 209 %!assert(compare_versions("1.1", "1.1.1", "<="), true)
222 %!assert(compare_versions("1.1.0a", "1.1.0b", "!="), true) 224 %!assert(compare_versions("1.1.0a", "1.1.0b", "!="), true)
223 %!assert(compare_versions("1.1.0test", "1.1.0b", "=="), false) 225 %!assert(compare_versions("1.1.0test", "1.1.0b", "=="), false)
224 %!assert(compare_versions("1.1.0test", "1.1.0test", "=="), true) 226 %!assert(compare_versions("1.1.0test", "1.1.0test", "=="), true)
225 227
226 ## make sure that it won't just give true output 228 ## make sure that it won't just give true output
227 %!assert(compare_versions("1", "0", "="), false) 229 %!assert(compare_versions("1", "0", "=="), false)
228 ## test arbitrarily long equality 230 ## test arbitrarily long equality
229 %!assert(compare_versions("1.1.1.0.0", "1.1", "=="), false) 231 %!assert(compare_versions("1.1.1.0.0", "1.1", "=="), false)
230 %!assert(compare_versions("1.1", "1", "<"), false) 232 %!assert(compare_versions("1.1", "1", "<"), false)
231 %!assert(compare_versions("2", "1.1", "<="), false) 233 %!assert(compare_versions("2", "1.1", "<="), false)
232 %!assert(compare_versions("1.1.1", "1.1", "<="), false) 234 %!assert(compare_versions("1.1.1", "1.1", "<="), false)
236 %!assert(compare_versions("0.0.0.2", "0.0.1", ">="), false) 238 %!assert(compare_versions("0.0.0.2", "0.0.1", ">="), false)
237 %!assert(compare_versions("0.0.20", "0.10.2", "=>"), false) 239 %!assert(compare_versions("0.0.20", "0.10.2", "=>"), false)
238 %!assert(compare_versions("0.1", "0.1", "!="), false) 240 %!assert(compare_versions("0.1", "0.1", "!="), false)
239 %!assert(compare_versions("0.1", "0.1", "~="), false) 241 %!assert(compare_versions("0.1", "0.1", "~="), false)
240 242
241 ## FIXME: how do we check to make sure that it gives errors when it 243 %% Test input validation
242 ## should 244 %!error(compare_versions(0.1, "0.1", "=="))
245 %!error(compare_versions("0.1", 0.1, "=="))
246 %!error(compare_versions(["0";".";"1"], "0.1", "=="))
247 %!error(compare_versions("0.1", ["0";".";"1"], "=="))
248 %!error(compare_versions("0.1", "0.1", "<>"))
249 %!error(compare_versions("0.1", "0.1", "!>"))
250 %!error(compare_versions("0.1", "0.1", "="))
251 %!error(compare_versions("0.1", "0.1", "aa"))
252
253