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 == [];
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