gdxencoding1.gms : Simple gdxencoding test

Description

This test tests the gdxencoding functionality on a simple example.

Contributor: Michael Bussieck, December 2022


Small Model of Type : GAMS


Category : GAMS Test library


Main file : gdxencoding1.gms

$title Simple gdxencoding test (GDXENCODING1,SEQ=922)

$onText
This test tests the gdxencoding functionality on a simple example.

Contributor: Michael Bussieck, December 2022
$offText
 
 
* UTF-8 encoding
Set c "côte d'ivoire-3" / "côte d'ivoire-3" , "cote d'ivoire-3" /;
Scalar numConv /0/;

$gdxOut c.gdx
$unload c
$gdxOut
$gdxOut corg.gdx
$unload c
$gdxOut

$onMulti
$callTool.checkErrorLevel gdxservice.gdxEncoding c.gdx -encodingIn=utf_8 -encodingOut=latin_1 -numConv=numConv -trace=1
$ifE numConv<>1 $abort 'expect 1 conversion utf_8 -> latin_1'

$callTool.checkErrorLevel gdxservice.gdxEncoding c.gdx -numConv=numConv
$ifE numConv<>1 $abort 'expect 1 conversion latin_1 -> utf_8'
$call.checkErrorLevel gdxdiff c.gdx corg.gdx > %system.nullFile%

* gdxEncoding only changes the encoding of the labels, not the symbol text nor 
* the set element text. In such a case the encoding of all elements (label, symbol,
* and set element text) of a GDX file can be changed with rewriting the text file 
* generated by gdxdump from one encoding to another one. The following example
* demonstrates this:
$call.checkErrorLevel gdxdump c.gdx > c.org
$onEmbeddedCode Python:
with open('c.org', 'r', encoding='utf_8') as f_in:
    with open('c.latin_1', 'w',  encoding='latin_1') as f_out:
        for l in f_in:
            f_out.write(l)
$offEmbeddedCode
$onEmbeddedCode Python:
with open('c.latin_1', 'r', encoding='latin_1') as f_in:
    with open('c.utf_8', 'w',  encoding='utf_8') as f_out:
        for l in f_in:
            f_out.write(l)
$offEmbeddedCode
$call.checkErrorLevel diff -q c.org c.utf_8 > %system.nullFile%

* Recreate the GDX file and compare with original
$call.checkErrorLevel gams c.utf_8 lo=2 gdx=c.gdx
$call.checkErrorLevel gdxdiff c.gdx corg.gdx > %system.nullFile%