help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sourcing global variables in unit tests


From: Kai Torben Ohlhus
Subject: Re: Sourcing global variables in unit tests
Date: Tue, 16 Apr 2019 22:34:06 +0900

On Tue, Apr 16, 2019 at 9:10 PM Pavel Hofman <address@hidden> wrote:
Hi Kai,

Dne 16. 04. 19 v 5:26 Kai Torben Ohlhus napsal(a):
>
>
> Can you provide a small, comprehensible, and complete test within a ZIP
> file?   Maybe this already works with Octave 5.1.0.

This very simple example - first test works, the second one fails.

function [A, B] = f1Direct()
   global A;
   global B;
endfunction

%!test
%! global A;
%! A = 2;
%! global B;
%! B = 3;
%! [o1, o2] = f1Direct();
%! expected = [2, 3];
%! assert([o1, o2], expected);

%!test
%! global A = 1;
%! global B = 2;
%! [o1, o2] = f1Direct();
%! expected = [1, 2];
%! assert([o1, o2], expected);


 >> test f1Direct.m
***** test
  global A = 1;
  global B = 2;
  [o1, o2] = f1Direct();
  expected = [1, 2];
  assert([o1, o2], expected);
!!!!! test failed
ASSERT errors for:  assert ([o1, o2],expected)

   Location  |  Observed  |  Expected  |  Reason
     (1)           2            1         Abs err 1 exceeds tol 0
     (2)           3            2         Abs err 1 exceeds tol 0


>
> Otherwise, why don't you call a function "my_consts" on the workspace,
> providing the necessary test constants?

My consts.m defines tens of constants, all used throughout my project
https://github.com/pavhofman/nonlinear-compensation/blob/master/octave/consts.m
.

I can imagine returning a struct with all the constants

const().CONST1
const().CONST2
const().CONST3

Using this would avoid the "global" call overhead at the same time. But
on the other hand it introduces the overhead with returning/accessing a
large struct.

Thanks,

Pavel.


Dear Pavel,

Thank you for the example.  This makes your problem clearer to me.  You should avoid the syntax

   global A = 1;

This is a convenient Octave extension, that does not exist in Matlab.  The problem is, that if a global variable A already exists, any consecutive initializations will be ignored (see [1]):

  global A = 1;
  global A = 2;  % ignored A == 1

or

  global A;
  global A = 3; % ignored A == [];

Thus avoid this Octave extension where not appropriate and you function (and https://github.com/pavhofman/nonlinear-compensation/blob/master/octave/consts.m) must be changed to a global declaration line and a second initialization line in consts.m.  For example:

function [A, B] = f1Direct()
   global A;
   global B;
end

%!test
%! global A;
%! A = 2;
%! global B;
%! B = 3;
%! [o1, o2] = f1Direct();
%! expected = [2, 3];
%! assert([o1, o2], expected);

%!test
%! global A B;
%! A = 1;
%! B = 2;
%! [o1, o2] = f1Direct();
%! expected = [1, 2];
%! assert([o1, o2], expected);

Best,
Kai

[1] https://octave.org/doc/v4.2.2/Global-Variables.html#Global-Variables

reply via email to

[Prev in Thread] Current Thread [Next in Thread]