Mercurial > octave
comparison scripts/general/isequal.m @ 31202:434b5a1b9498
isequal.m: Add ability to compare Java objects (bug #62930)
* isequal.m: Add elseif clause to detect Java objects and use built-in Java
"equals" method for comparison. Add BIST test for Java object comparison.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 25 Aug 2022 15:35:25 -0700 |
parents | 83f9f8bda883 |
children | 4c2b83516eb7 |
comparison
equal
deleted
inserted
replaced
31201:a8b0acc018a2 | 31202:434b5a1b9498 |
---|---|
168 endwhile | 168 endwhile |
169 | 169 |
170 elseif (is_function_handle (x)) | 170 elseif (is_function_handle (x)) |
171 ## function type. Use '==' operator which is overloaded. | 171 ## function type. Use '==' operator which is overloaded. |
172 tf = (x == y); | 172 tf = (x == y); |
173 | |
174 elseif (isjava (x)) | |
175 try | |
176 tf = x.equals (y); | |
177 catch | |
178 error ('isequal: Java object does not implement "equals" function'); | |
179 end_try_catch | |
173 | 180 |
174 else | 181 else |
175 error ("isequal: Impossible to reach code. File a bug report."); | 182 error ("isequal: Impossible to reach code. File a bug report."); |
176 | 183 |
177 endif | 184 endif |
542 | 549 |
543 ## Matlab compatibility | 550 ## Matlab compatibility |
544 %!assert (isequal (sparse (1), sparse (1)), true) | 551 %!assert (isequal (sparse (1), sparse (1)), true) |
545 %!assert (isequal (sparse (1), sparse (1)), sparse (1), true) | 552 %!assert (isequal (sparse (1), sparse (1)), sparse (1), true) |
546 | 553 |
554 ## Java objects | |
555 %!test <*62930> | |
556 %! int1 = javaObject ("java.lang.Integer", 1.0); | |
557 %! int2 = javaObject ("java.lang.Integer", 2.0); | |
558 %! assert (isequal (int1, int1)); | |
559 %! assert (! isequal (int1, 1.0)); | |
560 %! assert (! isequal (int1, int2)); | |
561 | |
547 ## test input validation | 562 ## test input validation |
548 %!error <Invalid call> isequal () | 563 %!error <Invalid call> isequal () |
549 %!error <Invalid call> isequal (1) | 564 %!error <Invalid call> isequal (1) |