embpy01.gms : Test for embedded code facility

Description

Test functionality of the GAMS embedded code facility for Python during compilation time

Contributor: Clemens Westphal, July 2017


Small Model of Type : GAMS


Category : GAMS Test library


Main file : embpy01.gms

$title 'Test for embedded code facility' (EMBPY01,SEQ=735)

$onText
Test functionality of the GAMS embedded code facility for Python during compilation time

Contributor: Clemens Westphal, July 2017
$offText


$log --- Using Python library %sysEnv.GMSPYTHONLIB%


Set i       / i1 text for i1
              i2 text for i2 /
    iout(i)
    j       / j1*j3 /
    ij(i,j) / #i.#j /
    i_want(i) / i1, i2 /
    i_want_ex(i) / i1 text 1, i2 text 2 /
    ij_want(i,j) / i1.j1, i1.j2 /
    ij_want_merge(i,j) / i1.j1, i1.j2, i1.j3 /
    ij_want_ex(i,j) / i1.j1 text 1, i1.j2 text 2/;

Scalar p0 /3.14/
       p0_want / 3.14 /;

Parameter p1(i)   / #i 3.14 /
          p2(i,j) / #i.#j 3.14 /
          p1_want(i) / #i 3.14 /
          p2_want(i,j) / i1.j1 3.14, i1.j2 3.14 /
          p2_want_merge(i,j) / i1.j1 3.14, i1.j2 3.14, i1.j3 3.14 /;

Variable v0      / fx 3.14 /
         v1(i)   / #i.fx 3.14 /
         v2(i,j) / #i.#j.fx 3.14 /
         v0_want / l 3.14, m 0, lo 0, up 10, scale 1 /
         v1_want(i) / i1.(l 3.14, m 0, lo 0, up 10, scale 1),
                      i2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         v2_want(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                        i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         v2_want_merge(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                              i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1),
                              i1.j3.(l 3.14, m 0, lo 0, up 10, scale 1) /;

equation e0      / fx 3.14 /
         e1(i)   / #i.fx 3.14 /
         e2(i,j) / #i.#j.fx 3.14 /
         e0_want / l 3.14, m 0, lo 0, up 10, scale 1 /
         e1_want(i) / i1.(l 3.14, m 0, lo 0, up 10, scale 1),
                      i2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         e2_want(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                        i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         e2_want_merge(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                              i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1),
                              i1.j3.(l 3.14, m 0, lo 0, up 10, scale 1) /;

$call rm -f embpy01_log.txt
$onEchoV > checkData
$gdxOut have.gdx
$unLoad %1
$gdxOut
$call gdxdiff wanted.gdx have.gdx >> embpy01_log.txt
$if errorlevel 1 $abort %2
$offEcho

*** Test that an empty code block works

$onEmbeddedCode Python:
$offEmbeddedCode

*** Test that an code block with comments only works

$onEmbeddedCode Python:
#
$offEmbeddedCode


$onEmbeddedCode Python:
#
  #
$offEmbeddedCode

$onEmbeddedCode Python:
#

  #
$offEmbeddedCode

*** Test default list behavior ***

*Test 1.1: Test default list behavior of scalars
$onEmbeddedCode Python:
  p0 = list(gams.get('p0'))
  assert p0 == [3.14], \
  "Test 1.1 failed in Python"
$offEmbeddedCode

*Test 1.2: Test default list behavior of one dimensional parameters
$onEmbeddedCode Python:
  p1 = list(gams.get('p1'))
  assert p1 == [('i1', 3.14), ('i2', 3.14)], \
  "Test 1.2 failed in Python"
$offEmbeddedCode

*Test 1.3: Test default list behavior of multi dimensional parameters
$onEmbeddedCode Python:
  p2 = list(gams.get('p2'))
  assert p2 == [(('i1', 'j1'), 3.14), \
                (('i1', 'j2'), 3.14), \
                (('i1', 'j3'), 3.14), \
                (('i2', 'j1'), 3.14), \
                (('i2', 'j2'), 3.14), \
                (('i2', 'j3'), 3.14)], \
  "Test 1.3 failed in Python"
$offEmbeddedCode

*Test 1.4: Test default list behavior of one dimensional sets
$onEmbeddedCode Python:
  i = list(gams.get('i'))
  assert i == ['i1', 'i2'], \
  "Test 1.4 failed in Python"
$offEmbeddedCode

*Test 1.5: Test default list behavior of multi dimensional sets
$onEmbeddedCode Python:
  ij = list(gams.get('ij'))
  assert ij == [('i1', 'j1'), ('i1', 'j2'), \
                ('i1', 'j3'), ('i2', 'j1'), \
                ('i2', 'j2'), ('i2', 'j3')], \
  "Test 1.5 failed in Python"
$offEmbeddedCode

*Test 1.6: Test default list behavior of scalar variables
$onEmbeddedCode Python:
  v0 = list(gams.get('v0'))
  assert v0 == [(3.14, 0, 3.14, 3.14, 1)], \
  "Test 1.6 failed in Python"
$offEmbeddedCode

*Test 1.7: Test default list behavior of one dimensional variables
$onEmbeddedCode Python:
  v1 = list(gams.get('v1'))
  assert v1 == [('i1', (3.14, 0, 3.14, 3.14, 1)), \
                ('i2', (3.14, 0, 3.14, 3.14, 1))], \
  "Test 1.7 failed in Python"
$offEmbeddedCode

*Test 1.8: Test default list behavior of multi dimensional variables
$onEmbeddedCode Python:
  v2 = list(gams.get('v2'))
  assert v2 == [(('i1', 'j1'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i1', 'j2'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i1', 'j3'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j1'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j2'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j3'), (3.14, 0, 3.14, 3.14, 1))], \
  "Test 1.8 failed in Python"
$offEmbeddedCode

*Test 1.9: Test default list behavior of scalar equations
$onEmbeddedCode Python:
  e0 = list(gams.get('e0'))
  assert e0 == [(3.14, 0, 3.14, 3.14, 1)], \
  "Test 1.9 failed in Python"
$offEmbeddedCode

*Test 1.10: Test default list behavior of one dimensional equations
$onEmbeddedCode Python:
  e1 = list(gams.get('e1'))
  assert e1 == [('i1', (3.14, 0, 3.14, 3.14, 1)), \
                ('i2', (3.14, 0, 3.14, 3.14, 1))], \
  "Test 1.10 failed in Python"
$offEmbeddedCode

*Test 1.11: Test default list behavior of multi dimensional equations
$onEmbeddedCode Python:
  e2 = list(gams.get('e2'))
  assert e2 == [(('i1', 'j1'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i1', 'j2'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i1', 'j3'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j1'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j2'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j3'), (3.14, 0, 3.14, 3.14, 1)) ], \
  "Test 1.11 failed in Python"
$offEmbeddedCode

*** Test different aspects of list behavior ***

*Test 2.1: Test list behavior of explanatory text
$onEmbeddedCode Python:
  i = list(gams.get('i', valueFormat=ValueFormat.FLAT))
  assert i == [('i1', 'text for i1'), ('i2', 'text for i2')], \
  "Test 2.1a failed in Python"
$offEmbeddedCode

*Test 2.1: Test list behavior of explanatory text
$onEmbeddedCode Python:
  i = list(gams.get('i', valueFormat=ValueFormat.TUPLE))
  assert i == [('i1', ('text for i1',)), ('i2', ('text for i2',))], \
  "Test 2.1b failed in Python"
$offEmbeddedCode

*** Test default list behavior using label indexes instead of strings ***

*Test 3.1: Test list behavior of scalars using label indexes
$onEmbeddedCode Python:
  p0 = list(gams.get('p0', KeyType.INT))
  assert p0 == [3.14], \
  "Test 3.1 failed in Python"
$offEmbeddedCode

*Test 3.2: Test list behavior of one dimensional parameters using label indexes
$onEmbeddedCode Python:
  p1 = list(gams.get('p1', KeyType.INT))
  assert p1 == [(1, 3.14), (2, 3.14)], \
  "Test 3.2 failed in Python"
$offEmbeddedCode

*Test 3.3: Test list behavior of multi dimensional parameters using label indexes
$onEmbeddedCode Python:
  p2 = list(gams.get('p2', KeyType.INT))
  assert p2 == [((1, 3), 3.14), \
                ((1, 4), 3.14), \
                ((1, 5), 3.14), \
                ((2, 3), 3.14), \
                ((2, 4), 3.14), \
                ((2, 5), 3.14)], \
  "Test 3.3 failed in Python"
$offEmbeddedCode

*Test 3.4: Test list behavior of one dimensional sets using label indexes
$onEmbeddedCode Python:
  i = list(gams.get('i', KeyType.INT))
  assert i == [1, 2], \
  "Test 3.4 failed in Python"
$offEmbeddedCode

*Test 3.5: Test list behavior of multi dimensional sets using label indexes
$onEmbeddedCode Python:
  ij = list(gams.get('ij', KeyType.INT))
  assert ij == [(1, 3), (1, 4), \
                (1, 5), (2, 3), \
                (2, 4), (2, 5)], \
  "Test 3.5 failed in Python"
$offEmbeddedCode

*Test 3.6: Test list behavior of scalar variables using label indexes
$onEmbeddedCode Python:
  v0 = list(gams.get('v0', KeyType.INT))
  assert v0 == [(3.14, 0, 3.14, 3.14, 1)], \
  "Test 3.6 failed in Python"
$offEmbeddedCode

*Test 3.7: Test list behavior of one dimensional variables using label indexes
$onEmbeddedCode Python:
  v1 = list(gams.get('v1', KeyType.INT))
  assert v1 == [(1, (3.14, 0, 3.14, 3.14, 1)), \
                (2, (3.14, 0, 3.14, 3.14, 1))], \
  "Test 3.7 failed in Python"
$offEmbeddedCode

*Test 3.8: Test list behavior of multi dimensional variables using label indexes
$onEmbeddedCode Python:
  v2 = list(gams.get('v2', KeyType.INT))
  assert v2 == [((1, 3), (3.14, 0, 3.14, 3.14, 1)), \
                ((1, 4), (3.14, 0, 3.14, 3.14, 1)), \
                ((1, 5), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 3), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 4), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 5), (3.14, 0, 3.14, 3.14, 1))], \
  "Test 3.8 failed in Python"
$offEmbeddedCode

*Test 3.9: Test list behavior of scalar equations using label indexes
$onEmbeddedCode Python:
  e0 = list(gams.get('e0', KeyType.INT))
  assert e0 == [(3.14, 0, 3.14, 3.14, 1)], \
  "Test 3.9 failed in Python"
$offEmbeddedCode

*Test 3.10: Test list behavior of one dimensional equations using label indexes
$onEmbeddedCode Python:
  e1 = list(gams.get('e1', KeyType.INT))
  assert e1 == [(1, (3.14, 0, 3.14, 3.14, 1)), \
                (2, (3.14, 0, 3.14, 3.14, 1))], \
  "Test 3.10 failed in Python"
$offEmbeddedCode

*Test 3.11: Test list behavior of multi dimensional equations using label indexes
$onEmbeddedCode Python:
  e2 = list(gams.get('e2', KeyType.INT))
  assert e2 == [((1, 3), (3.14, 0, 3.14, 3.14, 1)), \
                ((1, 4), (3.14, 0, 3.14, 3.14, 1)), \
                ((1, 5), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 3), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 4), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 5), (3.14, 0, 3.14, 3.14, 1)) ], \
  "Test 3.11 failed in Python"
$offEmbeddedCode


*** Test writing symbol data using labels ***
$onMultiR
*Test 4.1: Test writing of scalars
$gdxOut wanted.gdx
$unLoad p0_want=p0
$gdxOut

$clear p0
$onEmbeddedCode Python:
  data = [3.14]
  gams.set('p0', data)
$offEmbeddedCode p0
$batInclude checkData p0 'Test 4.1a failed in GAMS'

$clear p0
$onEmbeddedCode Python:
  data = [(3.14,)]
  gams.set('p0', data)
$offEmbeddedCode p0
$batInclude checkData p0 'Test 4.1b failed in GAMS'

*Test 4.2: Test writing of one dimensional parameters
$gdxOut wanted.gdx
$unLoad p1_want=p1
$gdxOut

$clear p1
$onEmbeddedCode Python:
  data = [("i1", 3.14), ("i2", 3.14)]
  gams.set('p1', data)
$offEmbeddedCode p1
$batInclude checkData p1 'Test 4.2a failed in GAMS'

$clear p1
$onEmbeddedCode Python:
  data = [(("i1",), 3.14), (("i2",), 3.14)]
  gams.set('p1', data)
$offEmbeddedCode p1
$batInclude checkData p1 'Test 4.2b failed in GAMS'

$clear p1
$onEmbeddedCode Python:
  data = [("i1", (3.14,)), ("i2", (3.14,))]
  gams.set('p1', data)
$offEmbeddedCode p1
$batInclude checkData p1 'Test 4.2c failed in GAMS'

$clear p1
$onEmbeddedCode Python:
  data = [(("i1",), (3.14,)), (("i2",), (3.14,))]
  gams.set('p1', data)
$offEmbeddedCode p1
$batInclude checkData p1 'Test 4.2d failed in GAMS'

*Test 4.3: Test writing of multi dimensional parameters
$gdxOut wanted.gdx
$unLoad p2_want=p2
$gdxOut

$clear p2
$onEmbeddedCode Python:
  data = [('i1', 'j1', 3.14), ('i1', 'j2', 3.14)]
  gams.set('p2', data)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 4.3a failed in GAMS'

$clear p2
$onEmbeddedCode Python:
  data = [(('i1', 'j1'), 3.14), (('i1', 'j2'), 3.14)]
  gams.set('p2', data)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 4.3b failed in GAMS'

$clear p2
$onEmbeddedCode Python:
  data = [('i1', 'j1', (3.14,)), ('i1', 'j2', (3.14,))]
  gams.set('p2', data)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 4.3c failed in GAMS'

$clear p2
$onEmbeddedCode Python:
  data = [(('i1', 'j1'), (3.14,)), (('i1', 'j2'), (3.14,))]
  gams.set('p2', data)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 4.3d failed in GAMS'

*Test 4.4: Test writing of one dimensional sets
$gdxOut wanted.gdx
$unLoad i_want=iout
$gdxOut

$clear iout
$onEmbeddedCode Python:
  data = ['i1', 'i2']
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 4.4a failed in GAMS'

$clear iout
$onEmbeddedCode Python:
  data = [('i1',), ('i2',)]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 4.4b failed in GAMS'

$gdxOut wanted.gdx
$unLoad i_want_ex=iout
$gdxOut

$clear iout
$onEmbeddedCode Python:
  data = [('i1', "text 1"), ('i2', "text 2")]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 4.4c failed in GAMS'

$clear iout
$onEmbeddedCode Python:
  data = [(('i1',), "text 1"), (('i2',), "text 2")]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 4.4d failed in GAMS'

$clear iout
$onEmbeddedCode Python:
  data = [('i1', ("text 1",)), ('i2', ("text 2",))]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 4.4e failed in GAMS'

$clear iout
$onEmbeddedCode Python:
  data = [(('i1',), ("text 1",)), (('i2',), ("text 2",))]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 4.4f failed in GAMS'

*Test 4.5: Test writing of multi dimensional sets
$gdxOut wanted.gdx
$unLoad ij_want=ij
$gdxOut

$clear ij
$onEmbeddedCode Python:
  data = [('i1', 'j1'), ('i1', 'j2')]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 4.5a failed in GAMS'

$clear ij
$onEmbeddedCode Python:
  data = [(('i1', 'j1'),), (('i1', 'j2'),)]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 4.5b failed in GAMS'

$gdxOut wanted.gdx
$unLoad ij_want_ex=ij
$gdxOut

$clear ij
$onEmbeddedCode Python:
  data = [('i1', 'j1', 'text 1'), ('i1', 'j2', 'text 2')]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 4.5c failed in GAMS'

$clear ij
$onEmbeddedCode Python:
  data = [(('i1', 'j1'), 'text 1'), (('i1', 'j2'), 'text 2')]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 4.5d failed in GAMS'

$clear ij
$onEmbeddedCode Python:
  data = [('i1', 'j1', ('text 1',)), ('i1', 'j2', ('text 2',))]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 4.5e failed in GAMS'

$clear ij
$onEmbeddedCode Python:
  data = [(('i1', 'j1'), ('text 1',)), (('i1', 'j2'), ('text 2',))]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 4.5f failed in GAMS'

*Test 4.6: Test writing of scalar variable
$gdxOut wanted.gdx
$unLoad v0_want=v0
$gdxOut

$clear v0
$onEmbeddedCode Python:
  data = [(3.14, 0, 0, 10, 1)]
  gams.set('v0', data)
$offEmbeddedCode v0
$batInclude checkData v0 'Test 4.6a failed in GAMS'

*Test 4.7: Test writing of one dimensonal variables
$gdxOut wanted.gdx
$unLoad v1_want=v1
$gdxOut

$clear v1
$onEmbeddedCode Python:
  data = [("i1", 3.14, 0, 0, 10, 1), ("i2", 3.14, 0, 0, 10, 1)]
  gams.set('v1', data)
$offEmbeddedCode v1
$batInclude checkData v1 'Test 4.7a failed in GAMS'

$clear v1
$onEmbeddedCode Python:
  data = [(("i1",), 3.14, 0, 0, 10, 1), (("i2",), 3.14, 0, 0, 10, 1)]
  gams.set('v1', data)
$offEmbeddedCode v1
$batInclude checkData v1 'Test 4.7b failed in GAMS'

$clear v1
$onEmbeddedCode Python:
  data = [("i1", (3.14, 0, 0, 10, 1)), ("i2", (3.14, 0, 0, 10, 1))]
  gams.set('v1', data)
$offEmbeddedCode v1
$batInclude checkData v1 'Test 4.7c failed in GAMS'

$clear v1
$onEmbeddedCode Python:
  data = [(("i1",), (3.14, 0, 0, 10, 1)), (("i2",), (3.14, 0, 0, 10, 1))]
  gams.set('v1', data)
$offEmbeddedCode v1
$batInclude checkData v1 'Test 4.7d failed in GAMS'

*Test 4.8: Test writing of multi dimensonal variables
$gdxOut wanted.gdx
$unLoad v2_want=v2
$gdxOut

$clear v2
$onEmbeddedCode Python:
  data = [("i1", "j1", 3.14, 0, 0, 10, 1), ("i1", "j2", 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
$offEmbeddedCode v2
$batInclude checkData v2 'Test 4.8a failed in GAMS'

$clear v2
$onEmbeddedCode Python:
  data = [(("i1", "j1"), 3.14, 0, 0, 10, 1), (("i1", "j2"), 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
$offEmbeddedCode v2
$batInclude checkData v2 'Test 4.8b failed in GAMS'

$clear v2
$onEmbeddedCode Python:
  data = [("i1", "j1", (3.14, 0, 0, 10, 1)), ("i1", "j2", (3.14, 0, 0, 10, 1))]
  gams.set('v2', data)
$offEmbeddedCode v2
$batInclude checkData v2 'Test 4.8c failed in GAMS'

$clear v2
$onEmbeddedCode Python:
  data = [(("i1", "j1"), (3.14, 0, 0, 10, 1)), (("i1", "j2"), (3.14, 0, 0, 10, 1))]
  gams.set('v2', data)
$offEmbeddedCode v2
$batInclude checkData v2 'Test 4.8d failed in GAMS'

*Test 4.9: Test writing of scalar equations
$gdxOut wanted.gdx
$unLoad e0_want=e0
$gdxOut

$clear e0
$onEmbeddedCode Python:
  data = [(3.14, 0, 0, 10, 1)]
  gams.set('e0', data)
$offEmbeddedCode e0
$batInclude checkData e0 'Test 4.9 failed in GAMS'


*Test 4.10: Test writing of one dimensonal equations
$gdxOut wanted.gdx
$unLoad e1_want=e1
$gdxOut

$clear e1
$onEmbeddedCode Python:
  data = [("i1", 3.14, 0, 0, 10, 1), ("i2", 3.14, 0, 0, 10, 1)]
  gams.set('e1', data)
$offEmbeddedCode e1
$batInclude checkData e1 'Test 4.10a failed in GAMS'

$clear e1
$onEmbeddedCode Python:
  data = [(("i1",), 3.14, 0, 0, 10, 1), (("i2",), 3.14, 0, 0, 10, 1)]
  gams.set('e1', data)
$offEmbeddedCode e1
$batInclude checkData e1 'Test 4.10b failed in GAMS'

$clear e1
$onEmbeddedCode Python:
  data = [("i1", (3.14, 0, 0, 10, 1)), ("i2", (3.14, 0, 0, 10, 1))]
  gams.set('e1', data)
$offEmbeddedCode e1
$batInclude checkData e1 'Test 4.10c failed in GAMS'

$clear e1
$onEmbeddedCode Python:
  data = [(("i1",), (3.14, 0, 0, 10, 1)), (("i2",), (3.14, 0, 0, 10, 1))]
  gams.set('e1', data)
$offEmbeddedCode e1
$batInclude checkData e1 'Test 4.10d failed in GAMS'

*Test 4.11: Test writing of multi dimensonal equations
$gdxOut wanted.gdx
$unLoad e2_want=e2
$gdxOut

$clear e2
$onEmbeddedCode Python:
  data = [("i1", "j1", 3.14, 0, 0, 10, 1), ("i1", "j2", 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
$offEmbeddedCode e2
$batInclude checkData e2 'Test 4.11a failed in GAMS'

$clear e2
$onEmbeddedCode Python:
  data = [(("i1", "j1"), 3.14, 0, 0, 10, 1), (("i1", "j2"), 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
$offEmbeddedCode e2
$batInclude checkData e2 'Test 4.11b failed in GAMS'

$clear e2
$onEmbeddedCode Python:
  data = [("i1", "j1", (3.14, 0, 0, 10, 1)), ("i1", "j2", (3.14, 0, 0, 10, 1))]
  gams.set('e2', data)
$offEmbeddedCode e2
$batInclude checkData e2 'Test 4.11c failed in GAMS'

$clear e2
$onEmbeddedCode Python:
  data = [(("i1", "j1"), (3.14, 0, 0, 10, 1)), (("i1", "j2"), (3.14, 0, 0, 10, 1))]
  gams.set('e2', data)
$offEmbeddedCode e2
$batInclude checkData e2 'Test 4.11d failed in GAMS'


*** Test writing symbol data using label indexes instead of strings ***

*Test 5.1: Test writing of scalars using label indexes
$gdxOut wanted.gdx
$unLoad p0_want=p0
$gdxOut

$clear p0
$onEmbeddedCode Python:
  data = [3.14]
  gams.set('p0', data)
$offEmbeddedCode p0
$batInclude checkData p0 'Test 5.1a failed in GAMS'

$clear p0
$onEmbeddedCode Python:
  data = [(3.14,)]
  gams.set('p0', data)
$offEmbeddedCode p0
$batInclude checkData p0 'Test 5.1b failed in GAMS'

*Test 5.2: Test writing of one dimensional parameters using label indexes
$gdxOut wanted.gdx
$unLoad p1_want=p1
$gdxOut

$clear p1
$onEmbeddedCode Python:
  data = [(1, 3.14), (2, 3.14)]
  gams.set('p1', data)
$offEmbeddedCode p1
$batInclude checkData p1 'Test 5.2a failed in GAMS'

$clear p1
$onEmbeddedCode Python:
  data = [((1,), 3.14), ((2,), 3.14)]
  gams.set('p1', data)
$offEmbeddedCode p1
$batInclude checkData p1 'Test 5.2b failed in GAMS'

$clear p1
$onEmbeddedCode Python:
  data = [(1, (3.14,)), (2, (3.14,))]
  gams.set('p1', data)
$offEmbeddedCode p1
$batInclude checkData p1 'Test 5.2c failed in GAMS'

$clear p1
$onEmbeddedCode Python:
  data = [((1,), (3.14,)), ((2,), (3.14,))]
  gams.set('p1', data)
$offEmbeddedCode p1
$batInclude checkData p1 'Test 5.2d failed in GAMS'

*Test 5.3: Test writing of multi dimensional parameters using label indexes
$gdxOut wanted.gdx
$unLoad p2_want=p2
$gdxOut

$clear p2
$onEmbeddedCode Python:
  data = [(1, 3, 3.14), (1, 4, 3.14)]
  gams.set('p2', data)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 5.3a failed in GAMS'

$clear p2
$onEmbeddedCode Python:
  data = [((1, 3), 3.14), ((1, 4), 3.14)]
  gams.set('p2', data)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 5.3b failed in GAMS'

$clear p2
$onEmbeddedCode Python:
  data = [(1, 3, (3.14,)), (1, 4, (3.14,))]
  gams.set('p2', data)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 5.3c failed in GAMS'

$clear p2
$onEmbeddedCode Python:
  data = [((1, 3), (3.14,)), ((1, 4), (3.14,))]
  gams.set('p2', data)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 5.3d failed in GAMS'



*Test 5.4: Test writing of one dimensional sets using label indexes
$gdxOut wanted.gdx
$unLoad i_want=iout
$gdxOut

$clear iout
$onEmbeddedCode Python:
  data = [1, 2]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 5.4a failed in GAMS'

$clear iout
$onEmbeddedCode Python:
  data = [(1,), (2,)]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 5.4b failed in GAMS'

$gdxOut wanted.gdx
$unLoad i_want_ex=iout
$gdxOut

$clear iout
$onEmbeddedCode Python:
  data = [(1, "text 1"), (2, "text 2")]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 5.4c failed in GAMS'

$clear iout
$onEmbeddedCode Python:
  data = [((1,), "text 1"), ((2,), "text 2")]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 5.4d failed in GAMS'

$clear iout
$onEmbeddedCode Python:
  data = [(1, ("text 1",)), (2, ("text 2",))]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 5.4e failed in GAMS'

$clear iout
$onEmbeddedCode Python:
  data = [((1,), ("text 1",)), ((2,), ("text 2",))]
  gams.set('iout', data)
$offEmbeddedCode iout
$batInclude checkData iout 'Test 5.4f failed in GAMS'

*Test 5.5: Test writing of multi dimensional sets using label indexes
$gdxOut wanted.gdx
$unLoad ij_want=ij
$gdxOut

$clear ij
$onEmbeddedCode Python:
  data = [(1, 3), (1, 4)]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 5.5a failed in GAMS'

$clear ij
$onEmbeddedCode Python:
  data = [((1, 3),), ((1, 4),)]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 5.5b failed in GAMS'

$gdxOut wanted.gdx
$unLoad ij_want_ex=ij
$gdxOut

$clear ij
$onEmbeddedCode Python:
  data = [(1, 3, 'text 1'), (1, 4, 'text 2')]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 5.5c failed in GAMS'

$clear ij
$onEmbeddedCode Python:
  data = [((1, 3), 'text 1'), ((1, 4), 'text 2')]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 5.5d failed in GAMS'

$clear ij
$onEmbeddedCode Python:
  data = [(1, 3, ('text 1',)), (1, 4, ('text 2',))]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 5.5e failed in GAMS'

$clear ij
$onEmbeddedCode Python:
  data = [((1, 3), ('text 1',)), ((1, 4), ('text 2',))]
  gams.set('ij', data)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 5.5f failed in GAMS'

*Test 5.6: Test writing of scalar variable using label indexes
$gdxOut wanted.gdx
$unLoad v0_want=v0
$gdxOut

$clear v0
$onEmbeddedCode Python:
  data = [(3.14, 0, 0, 10, 1)]
  gams.set('v0', data)
$offEmbeddedCode v0
$batInclude checkData v0 'Test 5.6a failed in GAMS'

*Test 5.7: Test writing of one dimensonal variables using label indexes
$gdxOut wanted.gdx
$unLoad v1_want=v1
$gdxOut

$onEmbeddedCode Python:
  data = [(1, 3.14, 0, 0, 10, 1), (2, 3.14, 0, 0, 10, 1)]
  gams.set('v1', data)
$offEmbeddedCode v1
$batInclude checkData v1 'Test 5.7a failed in GAMS'

$clear v1
$onEmbeddedCode Python:
  data = [((1,), 3.14, 0, 0, 10, 1), ((2,), 3.14, 0, 0, 10, 1)]
  gams.set('v1', data)
$offEmbeddedCode v1
$batInclude checkData v1 'Test 5.7b failed in GAMS'

$clear v1
$onEmbeddedCode Python:
  data = [(1, (3.14, 0, 0, 10, 1)), (2, (3.14, 0, 0, 10, 1))]
  gams.set('v1', data)
$offEmbeddedCode v1
$batInclude checkData v1 'Test 5.7c failed in GAMS'

$clear v1
$onEmbeddedCode Python:
  data = [((1,), (3.14, 0, 0, 10, 1)), ((2,), (3.14, 0, 0, 10, 1))]
  gams.set('v1', data)
$offEmbeddedCode v1
$batInclude checkData v1 'Test 5.7d failed in GAMS'

*Test 5.8: Test writing of multi dimensonal variables using label indexes
$gdxOut wanted.gdx
$unLoad v2_want=v2
$gdxOut

$clear v2
$onEmbeddedCode Python:
  data = [(1, 3, 3.14, 0, 0, 10, 1), (1, 4, 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
$offEmbeddedCode v2
$batInclude checkData v2 'Test 5.8a failed in GAMS'

$clear v2
$onEmbeddedCode Python:
  data = [((1, 3), 3.14, 0, 0, 10, 1), ((1, 4), 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
$offEmbeddedCode v2
$batInclude checkData v2 'Test 5.8b failed in GAMS'

$clear v2
$onEmbeddedCode Python:
  data = [(1, 3, (3.14, 0, 0, 10, 1)), (1, 4, (3.14, 0, 0, 10, 1))]
  gams.set('v2', data)
$offEmbeddedCode v2
$batInclude checkData v2 'Test 5.8c failed in GAMS'

$clear v2
$onEmbeddedCode Python:
  data = [((1, 3), (3.14, 0, 0, 10, 1)), ((1, 4), (3.14, 0, 0, 10, 1))]
  gams.set('v2', data)
$offEmbeddedCode v2
$batInclude checkData v2 'Test 5.8d failed in GAMS'

*Test 5.9: Test writing of scalar equations using label indexes
$gdxOut wanted.gdx
$unLoad e0_want=e0
$gdxOut

$clear e0
$onEmbeddedCode Python:
  data = [(3.14, 0, 0, 10, 1)]
  gams.set('e0', data)
$offEmbeddedCode e0
$batInclude checkData e0 'Test 5.9 failed in GAMS'

*Test 5.10: Test writing of one dimensonal equations using label indexes
$gdxOut wanted.gdx
$unLoad e1_want=e1
$gdxOut

$clear e1
$onEmbeddedCode Python:
  data = [(1, 3.14, 0, 0, 10, 1), (2, 3.14, 0, 0, 10, 1)]
  gams.set('e1', data)
$offEmbeddedCode e1
$batInclude checkData e1 'Test 5.10a failed in GAMS'

$clear e1
$onEmbeddedCode Python:
  data = [((1,), 3.14, 0, 0, 10, 1), ((2,), 3.14, 0, 0, 10, 1)]
  gams.set('e1', data)
$offEmbeddedCode e1
$batInclude checkData e1 'Test 5.10b failed in GAMS'

$clear e1
$onEmbeddedCode Python:
  data = [(1, (3.14, 0, 0, 10, 1)), (2, (3.14, 0, 0, 10, 1))]
  gams.set('e1', data)
$offEmbeddedCode e1
$batInclude checkData e1 'Test 5.10c failed in GAMS'

$clear e1
$onEmbeddedCode Python:
  data = [((1,), (3.14, 0, 0, 10, 1)), ((2,), (3.14, 0, 0, 10, 1))]
  gams.set('e1', data)
$offEmbeddedCode e1
$batInclude checkData e1 'Test 5.10d failed in GAMS'

*Test 5.11: Test writing of multi dimensonal equations using label indexes
$gdxOut wanted.gdx
$unLoad e2_want=e2
$gdxOut

$clear e2
$onEmbeddedCode Python:
  data = [(1, 3, 3.14, 0, 0, 10, 1), (1, 4, 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
$offEmbeddedCode e2
$batInclude checkData e2 'Test 5.11a failed in GAMS'

$clear e2
$onEmbeddedCode Python:
  data = [((1, 3), 3.14, 0, 0, 10, 1), ((1, 4), 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
$offEmbeddedCode e2
$batInclude checkData e2 'Test 5.11b failed in GAMS'

$clear e2
$onEmbeddedCode Python:
  data = [(1, 3, (3.14, 0, 0, 10, 1)), (1, 4, (3.14, 0, 0, 10, 1))]
  gams.set('e2', data)
$offEmbeddedCode e2
$batInclude checkData e2 'Test 5.11c failed in GAMS'

$clear e2
$onEmbeddedCode Python:
  data = [((1, 3), (3.14, 0, 0, 10, 1)), ((1, 4), (3.14, 0, 0, 10, 1))]
  gams.set('e2', data)
$offEmbeddedCode e2
$batInclude checkData e2 'Test 5.11d failed in GAMS'


*** Test adding/retrieving labels/label indexes

*Test 6.1: Test retrieving label indexes from label
$onEmbeddedCode Python:
  label = gams.getUel(1)
  assert label == 'i1', \
  "Test 6.1 failed in Python"
  label = gams.getUel(3)
  assert label == 'j1', \
  "Test 6.1 failed in Python"
$offEmbeddedCode

*Test 6.2: Test merging/adding of labels
$onEmbeddedCode Python:
  nrUels = gams.getUelCount()
  assert nrUels == 5, \
  "Test 6.2 failed in Python"

  labelIndex = gams.mergeUel("i2")
  assert labelIndex == 2, \
  "Test 6.2 failed in Python"
  nrUels = gams.getUelCount()
  assert nrUels == 5, \
  "Test 6.2 failed in Python"

  labelIndex = gams.mergeUel("new1")
  assert labelIndex == 6, \
  "Test 6.2 failed in Python"
  nrUels = gams.getUelCount()
  assert nrUels == 6, \
  "Test 6.2 failed in Python"

  label = gams.getUel(6)
  assert label == 'new1', \
  "Test 6.2 failed in Python"
$offEmbeddedCode

*** Test set() method using mergeType=MergeType.MERGE using labels

*Test 7.1: Test mergeType=MergeType.MERGE with parameters
$gdxOut wanted.gdx
$unLoad p2_want_merge=p2
$gdxOut
$clear p2
$onEmbeddedCode Python:
  data = [('i1', 'j1', 3.14), ('i1', 'j2', 3.14)]
  gams.set('p2', data)
$offEmbeddedCode p2
$onEmbeddedCode Python:
  data = [('i1', 'j2', 3.14), ('i1', 'j3', 3.14)]
  gams.set('p2', data, mergeType=MergeType.MERGE)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 7.1 failed in GAMS'

*Test 7.2: Test mergeType=MergeType.MERGE with sets
$gdxOut wanted.gdx
$unLoad ij_want_merge=ij
$gdxOut
$clear ij
$onEmbeddedCode Python:
  data = [('i1', 'j1'), ('i1', 'j2')]
  gams.set('ij', data)
$offEmbeddedCode ij
$onEmbeddedCode Python:
  data = [('i1', 'j2'), ('i1', 'j3')]
  gams.set('ij', data, mergeType=MergeType.MERGE)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 7.2 failed in GAMS'

*Test 7.3: Test mergeType=MergeType.MERGE with variables
$gdxOut wanted.gdx
$unLoad v2_want_merge=v2
$gdxOut
$clear v2
$onEmbeddedCode Python:
  data = [("i1", "j1", 3.14, 0, 0, 10, 1), ("i1", "j2", 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
$offEmbeddedCode v2
$onEmbeddedCode Python:
  data = [("i1", "j2", 3.14, 0, 0, 10, 1), ("i1", "j3", 3.14, 0, 0, 10, 1)]
  gams.set('v2', data, mergeType=MergeType.MERGE)
$offEmbeddedCode v2
$batInclude checkData v2 'Test 7.3 failed in GAMS'

*Test 7.4: Test mergeType=MergeType.MERGE with equations
$gdxOut wanted.gdx
$unLoad e2_want_merge=e2
$gdxOut
$clear e2
$onEmbeddedCode Python:
  data = [("i1", "j1", 3.14, 0, 0, 10, 1), ("i1", "j2", 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
$offEmbeddedCode e2
$onEmbeddedCode Python:
  data = [("i1", "j2", 3.14, 0, 0, 10, 1), ("i1", "j3", 3.14, 0, 0, 10, 1)]
  gams.set('e2', data, mergeType=MergeType.MERGE)
$offEmbeddedCode e2
$batInclude checkData e2 'Test 7.4 failed in GAMS'


*** Test set() method using mergeType=MergeType.MERGE using label indexes instead of strings

*Test 8.1: Test mergeType=MergeType.MERGE with parameters using label indexes
$gdxOut wanted.gdx
$unLoad p2_want_merge=p2
$gdxOut
$clear p2
$onEmbeddedCode Python:
  data = [(1, 3, 3.14), (1, 4, 3.14)]
  gams.set('p2', data)
$offEmbeddedCode p2
$onEmbeddedCode Python:
  data = [(1, 4, 3.14), (1, 5, 3.14)]
  gams.set('p2', data, mergeType=MergeType.MERGE)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 8.1 failed in GAMS'

*Test 8.2: Test mergeType=MergeType.MERGE with sets using label indexes
$gdxOut wanted.gdx
$unLoad ij_want_merge=ij
$gdxOut
$clear ij
$onEmbeddedCode Python:
  data = [(1, 3), (1, 4)]
  gams.set('ij', data)
$offEmbeddedCode ij
$onEmbeddedCode Python:
  data = [(1, 4), (1, 5)]
  gams.set('ij', data, mergeType=MergeType.MERGE)
$offEmbeddedCode ij
$batInclude checkData ij 'Test 8.2 failed in GAMS'

*Test 8.3: Test mergeType=MergeType.MERGE with variables using label indexes
$gdxOut wanted.gdx
$unLoad v2_want_merge=v2
$gdxOut
$clear v2
$onEmbeddedCode Python:
  data = [(1, 3, 3.14, 0, 0, 10, 1), (1, 4, 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
$offEmbeddedCode v2
$onEmbeddedCode Python:
  data = [(1, 4, 3.14, 0, 0, 10, 1), (1, 5, 3.14, 0, 0, 10, 1)]
  gams.set('v2', data, mergeType=MergeType.MERGE)
$offEmbeddedCode v2
$batInclude checkData v2 'Test 8.3 failed in GAMS'

*Test 8.4: Test mergeType=MergeType.MERGE with equations using label indexes
$gdxOut wanted.gdx
$unLoad e2_want_merge=e2
$gdxOut
$clear e2
$onEmbeddedCode Python:
  data = [(1, 3, 3.14, 0, 0, 10, 1), (1, 4, 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
$offEmbeddedCode e2
$onEmbeddedCode Python:
  data = [(1, 4, 3.14, 0, 0, 10, 1), (1, 5, 3.14, 0, 0, 10, 1)]
  gams.set('e2', data, mergeType=MergeType.MERGE)
$offEmbeddedCode e2
$batInclude checkData e2 'Test 8.4 failed in GAMS'


*** Miscellaneous tests

*Test 9.1: Test reading of empty symbols
$clear p2
$onEmbeddedCode Python:
  p2 = list(gams.get('p2'))
  assert p2 == [], \
  "Test 9.1 failed in Python"
$offEmbeddedCode

*Test 9.2: Test writing of empty symbols
$clear p2_want
$gdxOut wanted.gdx
$unLoad p2_want=p2
$gdxOut
$onEmbeddedCode Python:
  data = []
  gams.set('p2', data)
$offEmbeddedCode p2
$batInclude checkData p2 'Test 9.2 failed in GAMS'

*Test 9.3: Test writing of empty scalars
$clear p0_want
$gdxOut wanted.gdx
$unLoad p0_want=p0
$gdxOut
$onEmbeddedCode Python:
  data = []
  gams.set('p0', data)
$offEmbeddedCode p0
$batInclude checkData p0 'Test 9.3 failed in GAMS'


*Test 9.4: Test clear behavior of scalar symbols and their default values
$clear p0_want
$clear e0_want
positive variable xpos / l 0 /;
negative variable xneg / l 0 /;

$gdxOut wanted.gdx
$unLoad p0_want=p0 e0_want=e0 xpos xneg
$gdxOut

$onEmbeddedCode Python:
  gams.db['p0'].clear()
  gams.db['e0'].clear()
  gams.db['xpos'].clear()
  gams.db['xneg'].clear()
$offEmbeddedCode p0, e0, xpos, xneg
$batInclude checkData 'p0 e0 xpos xneg' 'Test 9.4 failed in GAMS'


*Test 9.5: Test special values
Set svIdx / myUNDF, myNA, myPINF, myMINF, myEPS /;
$onUNDF 
Parameter sv_want(svIdx) / myUNDF UNDF, myNA NA, myPINF INF, myMINF -INF, myEPS EPS /
          svX(svIdx), svY(svIdx) 

$onEmbeddedCode Python:
gams.epsAsZero = False
sv = dict(gams.get('sv_want'))
if sv['myUNDF']!=GMS_SV_UNDEF:
  raise ValueError('Test 9.5 failed in GAMS: Expect UNDEF')
if sv['myNA']==sv['myNA']:
  raise ValueError('Test 9.5 failed in GAMS: Expect NAN')
if sv['myPINF']!=float('inf'):
  raise ValueError('Test 9.5 failed in GAMS: Expect INF')
if sv['myMINF']!=float('-inf'):
  raise ValueError('Test 9.5 failed in GAMS: Expect -INF')
if sv['myEPS']!= gams._eps:
  raise ValueError('Test 9.5 failed in GAMS: Expect EPS')
gams.db['sv_want'].copy_symbol(gams.db['svX'])
gams.set('svY',[('myUNDF',GMS_SV_UNDEF),('myNA',float('nan')),('myPINF',float('inf')),('myMINF',float('-inf')),('myEPS',gams._eps)])
$offEmbeddedCode svX svY

$offUNDF

$gdxOut wanted.gdx
$unLoad sv_want=svX
$gdxOut
$batInclude checkData 'svX' 'Test 9.5 failed in GAMS (svX)'

$gdxOut wanted.gdx
$unLoad sv_want=svY
$gdxOut
$batInclude checkData 'svY' 'Test 9.5 failed in GAMS (svY)'


*Test 9.6: Test special values (EPS)
Parameter svZeroToEps(svIdx), svZeroToEps_want(svIdx) / myEPS EPS/;
Parameter svZeroToZero(svIdx), svZeroToZero_want(svIdx);

$onEps
$onEmbeddedCode Python:
gams.db['svZeroToEPS'].add_record('myEPS').value = 0
$offEmbeddedCode svZeroToEPS
$gdxOut wanted.gdx
$unLoad svZeroToEPS_want=svZeroToEPS
$gdxOut
$batInclude checkData 'svZeroToEPS' 'Test 9.6 failed in GAMS (svZeroToEPS)'

$offEps
$onEmbeddedCode Python:
gams.db['svZeroToZero'].add_record('myEPS').value = 0
$offEmbeddedCode svZeroToZero
$gdxOut wanted.gdx
$unLoad svZeroToZero_want=svZeroToZero
$gdxOut
$batInclude checkData 'svZeroToZero' 'Test 9.6 failed in GAMS (svZeroToZero)'

parameter witheps(*) / i1 1, i2 EPS /;
$onEmbeddedCode Python:
gams.ws.my_eps = 0
p = [ r.value for r in gams.db['witheps'] ]
assert p == [1.0, 0], \
  "Test 9.7 failed in Python"
$offEmbeddedCode

$onEmbeddedCode Python:
p = [ r.value for r in gams.db['witheps'] ]
assert p == [1.0, 4.94066E-324], \
  "Test 9.8 failed in Python"
$offEmbeddedCode

* Tests for long strings and auto indentation behavior

*Test 10.1: Test long strings : Skip of auto-indentation
$onEmbeddedCode Python:
s = """some
long
  string
""" + '''and another''' + """ long
string
  to
be added
"""
assert s == "some\nlong\n  string\nand another long\nstring\n  to\nbe added\n", \
"Test 10.1 failed in Python"
$offEmbeddedCode


*Test 10.2: Test long strings : Skip of auto-indentation
$onEmbeddedCode Python:
s = '''some
long
  string
''' + """and another""" + ''' long
string
  to
be added
'''
assert s == "some\nlong\n  string\nand another long\nstring\n  to\nbe added\n", \
"Test 10.2 failed in Python"
$offEmbeddedCode


*Test 10.3: Test long strings : embed """ in '''
$onEmbeddedCode Python:
s = '''some
long
  string
""" + """and another""" + """ long
string
  to
be added
'''
assert s == '''some\nlong\n  string\n""" + """and another""" + """ long\nstring\n  to\nbe added\n''', \
"Test 10.3 failed in Python"
$offEmbeddedCode


*Test 10.4: Test long strings : embed ''' in """
$onEmbeddedCode Python:
s = """some
long
  string
''' + '''and another''' + ''' long
string
  to
be added
"""
assert s == """some\nlong\n  string\n''' + '''and another''' + ''' long\nstring\n  to\nbe added\n""", \
"Test 10.4 failed in Python"
$offEmbeddedCode


*Test 10.5: Test behavior of line continuation character
$onEmbeddedCode Python:
s = "some string\
with line continuation\
characters"
assert s == "some string  with line continuation  characters", \
"Test 10.5 failed in Python"
$offEmbeddedCode


*Test 10.6: Mix line continuation character with long strings
$onEmbeddedCode Python:
s = """some string\
with line continuation
characters"""
assert s == "some stringwith line continuation\ncharacters", \
"Test 10.6 failed in Python"
$offEmbeddedCode

*Test 11: Run in a directory with long path name
$call mkdir 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
$onEcho > 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890%system.dirsep%ec11.gms
$onEmbeddedCode Python:
pass
$offEmbeddedCode
$offEcho
$call.checkErrorLevel gams ec11 lo=2 cdir=123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890


*Test 12: Test for specifying source encoding

* UTF-8 is the default
$onEmbeddedCode Python:
with open('ec_utf8.inc', 'w', encoding='utf-8') as f:
    f.write("$onEmbeddedCode Python:\n")
    f.write("s = 'öäü'\n")
    f.write("$offEmbeddedCode")
$offEmbeddedCode
$include ec_utf8.inc

* Change encoding to cp1252
$onEmbeddedCode Python:
with open('ec_cp1252.inc', 'w', encoding='cp1252') as f:
    f.write("$onEmbeddedCode Python:\n")
    f.write("# coding=cp1252\n")
    f.write("s = 'öäü'\n")
    f.write("$offEmbeddedCode")
$offEmbeddedCode
$include ec_cp1252.inc

* cp1252 is still set from the previous embedded code section
$onEmbeddedCode Python:
with open('ec_cp1252_2.inc', 'w', encoding='cp1252') as f:
    f.write("$onEmbeddedCode Python:\n")
    f.write("s = 'öäü'\n")
    f.write("$offEmbeddedCode")
$offEmbeddedCode
$include ec_cp1252_2.inc

* need to revert to UTF-8
$onEmbeddedCode Python:
with open('ec_utf8_2.inc', 'w', encoding='utf-8') as f:
    f.write("$onEmbeddedCode Python:\n")
    f.write("# coding=utf-8\n")
    f.write("s = 'öäü'\n")
    f.write("$offEmbeddedCode")
$offEmbeddedCode
$include ec_utf8_2.inc

*Test 13: Test for correct alias to universe
$onEcho > ec13.gms
Set s(*); Alias (*,uni);
$echo Alias (uni, *); > unialias_expect.txt
$onEmbeddedCode Python:
gams.wsWorkingDir = '.'
gams.db.export('unialias.gdx')
$offEmbeddedCode
$call.checkErrorLevel gdxdump unialias.gdx | grep Alias > unialias.txt
$call.checkErrorLevel diff unialias.txt unialias_expect.txt > %system.NullFile%
$offEcho
$call.checkErrorLevel gams ec13.gms lo=2

*Test 14: Test gams.printLog with lo=0
$onEcho > test_printlog.gms
$onEmbeddedCode Python:
gams.printLog("some log")
$offEmbeddedCode
$offEcho

$call.checkErrorLevel gams test_printlog.gms lo=0

*Test 15: Test printLog with "%"
$onEchoV > print_log_percent.gms
$onEmbeddedCode Python:
gams.printLog("%file% was not specified")
$offEmbeddedCode
$offEcho
$call.checkErrorLevel gams print_log_percent.gms > print_log_percent.log
$onEmbeddedCode Python:
with open("print_log_percent.log", "r")as f:
  if f.read().count("%file%") != 1:
    raise Exception('Expected "%file%" to be contained in log output once')
$offEmbeddedCode

*Test 16: Test gams.printLog end parameter
$onEcho > test_printlog_newline.gms
$onEmbeddedCode Python:
gams.printLog("s1")
gams.printLog("s2")
gams.printLog("s3", end="\n")
gams.printLog("s4", end="")
gams.printLog("s5", end="x")
$offEmbeddedCode
$offEcho

$call.checkErrorLevel gams test_printlog_newline.gms > log_newline.txt

$onEmbeddedCode Python:
with open("log_newline.txt", "r") as f:
    s = f.read()
if "s1\ns2\ns3\ns4s5x" not in s:
    raise Exception("Unexpected log content.")
$offEmbeddedCode

*Test 17: Test gams.transfer with alias domain
$onEcho > gt14.gms
set i /1/; alias (i,j);
$gdxUnload xxx.gdx

$onEmbeddedCode Python:
import gams.transfer as gt
from gams import GamsWorkspace
ws = GamsWorkspace(working_directory='.', system_directory=r'%gams.sysDir% '.strip())
db = ws.add_database_from_gdx('xxx.gdx')
m = gt.Container(db, system_directory=r'%gams.sysDir% '.strip())
$offEmbeddedCode
$offEcho

$call.checkErrorLevel gams gt14.gms lo=0

*Test 18: Test EC arguments containing quotes
$onEmbeddedCode Python: argument containing 'single' and "double" quotes
args = gams.arguments
if args != '''argument containing 'single' and "double" quotes''':
  raise Exception("gams.arguments not as expected")
$offEmbeddedCode