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: Pavel Hofman
Subject: Re: Sourcing global variables in unit tests
Date: Tue, 16 Apr 2019 14:10:43 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1

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.



reply via email to

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