Skip to content

Commit

Permalink
Add support for GoogleTest
Browse files Browse the repository at this point in the history
  • Loading branch information
octave-user committed May 12, 2024
1 parent 2759dd7 commit 783cca2
Show file tree
Hide file tree
Showing 58 changed files with 900 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ fntests.log
# GNU Octave
*.oct
*.o
src/gtest-octave-cli
src/.deps

# octave-forge.m4
oct-alt-includes.h
Expand Down
6 changes: 6 additions & 0 deletions inst/addpath_full_001.tst
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## addpath_full.m:01
%!test
%! try
%! dirname = ".";
%! addpath_full(dirname);
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
6 changes: 6 additions & 0 deletions inst/addpath_full_002.tst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## addpath_full.m:02
%!demo
%! try
%! addpath_full("~");
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
24 changes: 16 additions & 8 deletions inst/assert_simple.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Copyright (C) 2023(-2023) Reinhard <octave-user@a1.net>
## Copyright (C) 2023(-2024) Reinhard <octave-user@a1.net>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -66,10 +66,18 @@
## @end deftypefn

function assert_simple(varargin)
arg_names = cell(nargin, 1);

for i=1:nargin
arg_names{i} = inputname(i, false);
endfor

argin = ["(" strjoin(arg_names, ",") ")"];

switch (nargin)
case {2, 3}
otherwise
real_assert(varargin{:});
real_assert(argin, varargin);
return
endswitch

Expand Down Expand Up @@ -100,14 +108,14 @@ function assert_simple(varargin)
tol_test = tolerance >= 0;

if (~(scalar_test && tol_test && size_test && class_test && numeric_test && sparse_test && finite_test))
real_assert(varargin{:});
real_assert(argin, varargin);
return;
endif

difference = really_max(abs(observed - expected));

if (difference > tolerance)
error("Abs err %.5g exceeds tol %.5g", difference, tolerance);
error("Abs err %.5g exceeds tol %.5g\nassert_simple %s failed", difference, tolerance, argin);
endif
endfunction

Expand All @@ -127,16 +135,16 @@ function assert_simple(varargin)
endwhile
endfunction

function real_assert(varargin)
function real_assert(argin, args)
err = [];

try
assert(varargin{:});
assert(args{:});
catch
err = lasterror();
end_try_catch

if (~isempty(err))
rethrow(err);
error("%s\tassert_simple %s failed", err.message, argin);
endif
endfunction

10 changes: 10 additions & 0 deletions inst/assert_simple_001.tst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## assert_simple.m:01
%!test
%! try
%! N = 1000;
%! for i=1:100
%! A = rand(N, N);
Expand All @@ -9,6 +10,11 @@
%! endif
%! fail("assert_simple(A, B, 0)");
%! endfor
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!error assert_simple ([])
%!error assert_simple ("")
%!error assert_simple ({})
Expand Down Expand Up @@ -51,3 +57,7 @@
%!assert ([1,2;3,4],[1,2;3,4])
%!error assert_simple ([1,4;3,4],[1,2;3,4])
%!error <Dimensions don't match> assert_simple ([1,3;2,4;3,5],[1,2;3,4])
%!error assert_simple(2 == 1);
%!error assert_simple(2, 1);
%!error assert_simple(2, 1, eps);
%!error assert_simple(ones(3,3), zeros(3,3), eps);
13 changes: 13 additions & 0 deletions inst/assert_simple_002.tst
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
## assert_simple.m:02
%!test # 2-D matrix
%! try
%! A = [1 2 3]'*[1,2];
%! assert_simple (A, A);
%! fail ("assert_simple (A.*(A!=2),A)");
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
13 changes: 13 additions & 0 deletions inst/assert_simple_003.tst
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
## assert_simple.m:03
%!test # N-D matrix
%! try
%! X = zeros (2,2,3);
%! Y = X;
%! Y(1,2,3) = 1.5;
%! fail ("assert_simple (X,Y)");
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!assert (100+100*eps, 100, -2*eps)
%!assert (100, 100+100*eps, -2*eps)
%!error <Rel err .* exceeds tol> assert_simple (100+300*eps, 100, -2*eps)
%!error <Rel err .* exceeds tol> assert_simple (100, 100+300*eps, -2*eps)
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
16 changes: 15 additions & 1 deletion inst/assert_simple_004.tst
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## assert_simple.m:04
%!test assert_simple (0.1+eps, 0.1, 2*eps);
%!test
%! try
%! assert_simple (0.1+eps, 0.1, 2*eps);
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!error <Rel err 2.2204e-0?15 exceeds tol> assert_simple (0.1+eps, 0.1, -2*eps)
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
16 changes: 15 additions & 1 deletion inst/assert_simple_005.tst
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## assert_simple.m:05
%!test assert_simple (100+100*eps, 100, -2*eps);
%!test
%! try
%! assert_simple (100+100*eps, 100, -2*eps);
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!error <Abs err 2.8422e-0?14 exceeds tol> assert_simple (100+100*eps, 100, 2*eps)
%!error <Abs err 2 exceeds tol 0.1> assert_simple (2, 0, -0.1)
%!error <Class single != double> assert_simple (single (1), 1)
Expand All @@ -11,3 +18,10 @@
%!assert ([NaN, NA, Inf, -Inf, 1+eps, eps], [NaN, NA, Inf, -Inf, 1, 0], eps)
%!error <'NaN' mismatch> assert_simple (NaN, 1)
%!error <'NaN' mismatch> assert_simple ([NaN 1], [1 NaN])
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
13 changes: 13 additions & 0 deletions inst/assert_simple_006.tst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## assert_simple.m:06
%!test
%! try
%! try
%! assert_simple ([NaN 1], [1 NaN]);
%! catch
%! errmsg = lasterr ();
Expand All @@ -12,5 +13,17 @@
%! error ("Abs err reported for NaN assert");
%! endif
%! end_try_catch
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!error <'NA' mismatch> assert_simple (NA, 1)
%!error assert_simple ([NA 1]', [1 NA]')
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
13 changes: 13 additions & 0 deletions inst/assert_simple_007.tst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## assert_simple.m:07
%!test
%! try
%! try
%! assert_simple ([NA 1]', [1 NA]');
%! catch
%! errmsg = lasterr ();
Expand All @@ -12,6 +13,18 @@
%! error ("Abs err reported for NA assert");
%! endif
%! end_try_catch
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!error assert_simple ([(complex (NA, 1)) (complex (2, NA))], [(complex (NA, 2)) 2])
%!error <'Inf' mismatch> assert_simple (-Inf, Inf)
%!error <'Inf' mismatch> assert_simple ([-Inf Inf], [Inf -Inf])
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
13 changes: 13 additions & 0 deletions inst/assert_simple_008.tst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## assert_simple.m:08
%!test
%! try
%! try
%! assert_simple (complex (Inf, 0.2), complex (-Inf, 0.2 + 2*eps), eps);
%! catch
%! errmsg = lasterr ();
Expand All @@ -10,6 +11,11 @@
%! error ("Abs err reported for Inf assert");
%! endif
%! end_try_catch
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!error <Abs err> assert (complex (Inf, 0.2), complex (Inf, 0.2 + 2*eps), eps)
%!assert ("dog", "dog")
%!error <Strings don't match> assert_simple ("dog", "cat")
Expand All @@ -19,3 +25,10 @@
%!error <Expected string, but observed struct> assert_simple (struct ("dog", 3), "dog")
%!error <Expected cell, but observed double> assert_simple (1, {1})
%!error <Dimensions don't match> assert_simple (cell (1,2,3), cell (3,2,1))
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
13 changes: 13 additions & 0 deletions inst/assert_simple_009.tst
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
## assert_simple.m:09
%!test
%! try
%! x = {{{1}}, 2}; # cell with multiple levels
%! y = x;
%! assert_simple (x,y);
%! y{1}{1}{1} = 3;
%! fail ("assert_simple (x,y)");
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!assert (@sin, @sin)
%!error <Function handles don't match> assert_simple (@sin, @cos)
%!error <Expected function handle, but observed double> assert_simple (pi, @cos)
%!error <Class function_handle != double> assert_simple (@sin, pi)
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
13 changes: 13 additions & 0 deletions inst/assert_simple_010.tst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## assert_simple.m:10
%!test
%! try
%! x = {[3], [1,2,3]; 100+100*eps, "dog"};
%! y = x;
%! assert_simple (x, y);
Expand All @@ -13,6 +14,11 @@
%! fail ("assert_simple (x, y)");
%! y = x; y(1,1) = [2]; y(1,2) = [0, 2, 3]; y(2,1) = 101; y(2,2) = "cat";
%! fail ("assert_simple (x, y)");
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!error <Expected struct, but observed double> assert_simple (1, struct ("a", 1))
%!error <Structure sizes don't match>
%! x(1,2,3).a = 1;
Expand All @@ -31,3 +37,10 @@
%! x.b = 1;
%! y.a = 1;
%! assert_simple (x,y);
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
13 changes: 13 additions & 0 deletions inst/assert_simple_011.tst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## assert_simple.m:11
%!test
%! try
%! x.a = 1; x.b=[2, 2];
%! y.a = 1; y.b=[2, 2];
%! assert_simple (x, y);
Expand All @@ -11,3 +12,15 @@
%! x = resize (x, 0, 1);
%! y = resize (y, 0, 1);
%! assert_simple (x, y);
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
13 changes: 13 additions & 0 deletions inst/assert_simple_012.tst
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
## assert_simple.m:12
%!test
%! try
%! x = [-40:0];
%! y1 = (10.^x).*(10.^x);
%! y2 = 10.^(2*x);
%! ## Increase tolerance from eps (y1) to 4*eps (y1) because of an upstream bug
%! ## in mingw-w64: https://sourceforge.net/p/mingw-w64/bugs/466/
%! assert_simple (y1, y2, 4*eps (y1));
%! fail ("assert_simple (y1, y2 + eps*1e-70, eps (y1))");
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
%!test
%! try
%! catch
%! gtest_error = lasterror();
%! gtest_fail(gtest_error, evalin("caller", "__file"));
%! rethrow(gtest_error);
%! end_try_catch
Loading

0 comments on commit 783cca2

Please sign in to comment.