put11.gms : Put_Utility without file handle

Description

Before GAMS 25.0 the Put_Utility statement required an open file handle. This
test makes sure, that the different utilities also work without one.

Contributor: Lutz Westermann, December 2017


Small Model of Type : GAMS


Category : GAMS Test library


Main file : put11.gms

$title Put_Utility without file handle (PUT11,SEQ=741)

$ontext
Before GAMS 25.0 the Put_Utility statement required an open file handle. This
test makes sure, that the different utilities also work without one.

Contributor: Lutz Westermann, December 2017
$offtext


$call rm -f dummy.txt shell.txt mySave.g00

put_utility 'exec' / 'touch dummy.txt';
execute '=test -e dummy.txt';
abort$(errorlevel) 'Problem with "exec"';

$echo shell > shellWant.txt
put_utility 'shell' / 'echo shell > shell.txt';
execute "diff -bw shellWant.txt shell.txt";
abort$(errorlevel) 'Problem with "shell"';

put_utility 'save' / 'mySave';
execute '=test -e mySave.g00';
abort$(errorlevel) 'Problem with "save"';

Set       j          / 2005*2007 /;
Parameter randoms(j)              ;
Scalar    random                  ;

loop(j,
   put_utility 'gdxOut' / 'data' j.tl:0;
   random = uniform(0,1);
   randoms(j) = random;
   execute_unload random;
);

loop(j,
   put_utility 'gdxIn' / 'data' j.tl:0 ;
   execute_load random;
   abort$(randoms(j)<>random) 'Problem with "gdxout/gdxin"';
);

* These are hard to test for correctness, just make sure that nothing crashes
put_utility 'click' / 'shell.txt' ;
put_utility 'log' / 'Hi log!';
put_utility 'msg' / 'Hi lst!';
put_utility 'msgLog' / 'Hi lstlog!';
put_utility 'title' / 'New Title';
$ifi not %system.fileSys%==UNIX
   put_utility 'winMsg' / 'New Title' / "Some Message";

abort$(execerror<>0) 'There should be no errors until here';

* These actually need an open file handle, but not for put_utility itself, but for the action perfomed
put_utility 'ren' / 'one.txt';
abort$(execerror=0) 'This "ren" should have failed';
execerror = 0;

put_utility 'inc' / 'external.txt' ;
abort$(execerror=0) 'This "inc" should have failed';
execerror = 0;

set slv / cplex, conopt /;
variable v;equation e; e.. v=e=0; model m /e/;
loop(slv,
   put_utility 'solver' / 'lp' / slv.tl:0;
   solve m us lp min v;
   if (sameas(slv,'cplex'),
      put_utility 'shell' / 'echo "' system.SString '" | grep CPLEX  > %system.nullFile%';
   else
      put_utility 'shell' / 'echo "' system.SString '" | grep CONOPT > %system.nullFile%';
   );
   abort$errorlevel 'requested solver name not in audit string';   
);

singleton set someText / 1 /;
put_utility 'assignText' / 'someText' / 'We assign some text to the label';
put_utility 'shell' / 'echo "' someText.te(someText) '" | grep "We assign some text to the label" > %system.nullFile%';
abort$errorlevel 'text expected and stored are different';

$echo zero > zeroWant.txt
$echo one  > oneWant.txt
file fx /'zero.txt'/; putclose fx 'zero';
put_utility 'ren' / 'one.txt'; putclose 'one';
execute "diff -bw zeroWant.txt zero.txt";
abort$(errorlevel) 'Problem with "ren zero"';
execute "diff -bw oneWant.txt one.txt";
abort$(errorlevel) 'Problem with "ren one"';

$echo Here we include content from an external file: > includedWant.txt
$echo I am the external content                     >> includedWant.txt
$echo I am the external content > external.txt
put_utility 'ren' / 'included.txt';
put "Here we include content from an external file:";
put_utility 'inc' / 'external.txt' ; putclose;
execute "diff -bw includedWant.txt included.txt";
abort$(errorlevel) 'Problem with "inc"';

* There was a bug in 25.0.1 which caused this to fail
file f1, f2;
put f1                 '1';
put f2                 '2';
put_utility f1 'log' / '3';

* There was a bug in 25.0.2 which caused this to fail
$echo trnsport > trnsportWant.txt
File fy /'trnsport.txt'/; fy.lCase = 1;

Singleton Set s / TRNSPORT /;
put_utility fy 'log' / s.tl:0;
putClose               s.tl:0;
execute "diff -bw trnsportWant.txt trnsport.txt";
abort$(errorlevel) 'Problem with "Trnsport lCase"';