echo 0
//=============================================================
// testing the compile-command
// Following issues are tested:
//   * one function/procedure in one file (1st, 2nd)
//   * several functions/procedures in one file (3rd, 4th, 5th, 6th, 7th, 8th)
//   * a function calling a function (2nd, 3rd)
//   * a function calling a procedure (5th)
//   * a procedure calling a procedure (7th)
//   * a procedure calling a function (8th)
//   * recompiling a function (2nd)/procedure (7th)
//   * a procedure calling a builtin procedure (6th, pi())
// nr. | what? | in file
// 1st | func  | compilefirst.dpuser 
// 2nd | func  | compilesecond.dpuser
// 3rd | func  | compilethird.dpuser
// 4th | proc  |    "
// 5th | func  |    "
// 6th | proc  |    "
// 7th | proc  | compileseventh.dpuser
// 8th | proc  |    "
//
//
// At the end c-code-compiling is tested
//
result_unit = ""
result_tmp = ""
cd "test_compile"
@../test_functions/compare_types.dpuser

//=============================================================

// compile 1st: function
compile "compilefirst.dpuser"
res = compilefirst(45)
if (comparedbl(res, 0.707107, 0.01) != 1) {
   result_tmp += "[compiling function compilefirst: res = " + res + "]"
}


// compile 2nd: function
compile "compilesecond.dpuser"
res = compilesecond(45)
if (comparedbl(res, 200.707, 0.01) != 1) {
   result_tmp += "[compiling function compilesecond: res = " + res + "]"
}

// compile 3rd, 4th, 5th &  6th: functions & procedures in one file
compile "compilethird.dpuser"
res = compilethird(45)
if (comparedbl(res, 3200.71, 0.01) != 1) {
   result_tmp += "[compiling function compilethird: res = " + res + "]"
}

// test 4th: procedure
res = 5
compilefourth res
if (res != 2000) {
   result_tmp += "[compiling procedure compilefourth: res = " + res + "]"
}

// test 5th: function
res = compilefifth(1000)
if (res != -1000) {
   result_tmp += "[compiling function compilefifth: res = " + res + "]"
}

// test 6th: procedure
res = 5
compilesixth res
if (comparedbl(res, 3.14159, 0.01) != 1) {
   result_tmp += "[compiling procedure compilesixth: res = " + res + "]"
}

// change 2nd: function
tmpstrarr = import("compilesecond.dpuser")
strarr = stringarray(3)
strarr[1] = tmpstrarr[1]
strarr[2] = "    compilesecond = 990 + compilefirst(y)"
strarr[3] = tmpstrarr[3]
export "compilesecond.dpuser", strarr

//=============================================================
// recompile 2nd: function
compile "compilesecond.dpuser"
res = compilethird(45)
if (comparedbl(res, 3990.71, 0.01) != 1) {
    result_tmp += "[recompiling function compilesecond: res = " + res + "]"
}
// reset 2nd: function
export "compilesecond.dpuser", tmpstrarr

// compile 7th & 8th: procedure
compile "compileseventh.dpuser"

// test 7th: procedure 
res = 4
compileseventh res
if (res != 2000) {
    result_tmp += "[compiling procedure compileseventh: res = " + res + "]"
}

// test 8th: function
res = 10
compileeigth res
if (res != -1990) {
    result_tmp += "[compiling function compileeigth: res = " + res + "]"
}

// recompile 7th & 8th: function
compile "compileseventh.dpuser"

//=============================================================
// re-testing all functions
res = "" + compilefirst(45)
if (comparedbl(res, 0.707107, 0.01) != 1) {
    result_tmp += "[function compilefirst corrupt (2nd run): res = " + res + "]"
}

res = compilesecond(45)
if (comparedbl(res, 990.707, 0.01) != 1) {
    result_tmp += "[function compilesecond corrupt (2nd run): res = " + res + "]"
}

res = compilethird(45)
if (comparedbl(res, 3990.71, 0.01) != 1) {
    result_tmp += "[function compilethird corrupt (2nd run): res = " + res + "]"
}

res = 5
compilefourth res
if (res != 2000) {
    result_tmp += "[procedure compilefourth corrupt (2nd run): res = " + res + "]"
}

res = compilefifth(1000)
if (res != -1000) {
    result_tmp += "[function compilefifth corrupt (2nd run): res = " + res + "]"
}

res = 5
compilesixth res
if (comparedbl(res, 3.14159, 0.01) != 1) {
    result_tmp += "[procedure compilesixth corrupt (2nd run): res = " + res + "]"
}

res = 4
compileseventh res
if (res != 2000) {
    result_tmp += "[compiling compileseventh corrupt (2nd run): res = " + res + "]"
}

res = 10
compileeigth res
if (res != -1990) {
    result_tmp += "[compiling function compileeigth (2nd run): res = " + res + "]"
}

//=============================================================
// testing c-code-compiling
compile "test_c_code.cpp", /c
res = 2
testProc res
if (res != 222) {
    result_tmp += "[procedure testProc corrupt: res = " + res + "]"
}

res = testFunc1(2,3)
if (res != 9) {
    result_tmp += "[function testFunc1 corrupt: res = " + res + "]"
}

res = testFunc2()
if (res != 9) {
    result_tmp += "[function testFunc2 corrupt: res = " + res + "]"
}

res = testFunc3()
if (res != 2) {
    result_tmp += "[function testFunc3 corrupt: res = " + res + "]"
}

//=============================================================
// testing if functions and procedures compiled after ones without any arguments
// compile correctly
compile "compileninth.dpuser"
res = compileninth()
if (res != 333) {
    result_tmp += "[function compileninth corrupt: res = " + res + "]"
}

compiletenth
// nothing to test here...

res = compileeleventh(2)
if (res != 666) {
    result_tmp += "[function compileeleventh corrupt: res = " + res + "]"
}

//=============================================================
// testing if passing a expression as argument works (e.g. 2+2)
// works only for dpuserType and not for dpuserType& (reference))
// compile correctly
compile "compile_expression.dpuser"
a = 0
res = no(2+2, a)
if ((res != 16) || (a != 64)) {
    result_tmp += "[function no corrupt: res = " + res + "]"
}

a = 0
res = no(a, 2+2)
if ((res != 0) || (a != 0)) {
    result_tmp += "[function no corrupt: res = " + res + "]"
}

res1 = 0
res2 = 0
testno, res1, res2
if ((res1 != 4) || (res2 != 2)) {
    result_tmp += "[function testno corrupt: res1 = " + res1 + ", res2 = " + res2 + "]"
}

//=============================================================
// print unit-test results
if (strlen(result_tmp) != 0) {
   result_unit += "FAILED: " + result_tmp + " in unit-test test_compile.dpuser!"
   print result_unit
   result_unit += "\n"
} else {
   if (quiet == 0) {
      result_unit = "SUCCESS: unit-test test_compile.dpuser ok"
      print result_unit
      result_unit += "\n"
   }
   !rm -f compilefirst.cpp
   !rm -f compilefirst.dll
   !rm -f compilesecond.cpp
   !rm -f compilesecond.dll
   !rm -f compilethird.cpp
   !rm -f compilethird.dll
   !rm -f compilefourth.cpp
   !rm -f compilefourth.dll
   !rm -f compilefifth.cpp
   !rm -f compilefifth.dll
   !rm -f compilesixth.cpp
   !rm -f compilesixth.dll
   !rm -f compileseventh.cpp
   !rm -f compileseventh.dll
   !rm -f compileeigth.cpp
   !rm -f compileeigth.dll
   !rm -f testFunc1.cpp
   !rm -f testFunc1.dll
   !rm -f testFunc2.cpp
   !rm -f testFunc2.dll
   !rm -f testFunc3.cpp
   !rm -f testFunc3.dll
   !rm -f testno.cpp
   !rm -f testno.dll
   !rm -f testProc.cpp
   !rm -f testProc.dll
   !rm -f no.cpp
   !rm -f no.dll
}

cd ".."
