/******************************************************************************
 * Copyright (c) 2000-2025 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Raduly, Csaba
 *   Szabados, Kristof
 *
 ******************************************************************************/
module xmlTest_Functions_string
{

import from www_XmlTest_org_string all;
import from www_XmlTest_org_string_withEnum all;
import from www_XmlTest_org_string_withWhitespace all;
import from xmlTest_Shell all;
//==============================================================================
//  External functions
//==============================================================================

//***Name***
external function f_enc_NameA(in NameA pdu, out octetstring stream)
with { extension "prototype(fast) encode(XER:XER_EXTENDED)" }
//0: OK
//1: NOK

external function f_dec_NameA(in octetstring stream, out NameA pdu) return integer
with { extension "prototype(backtrack) decode(XER:XER_EXTENDED)" }

//***NameB***
external function f_enc_NameB(in NameB pdu, out octetstring stream)
with { extension "prototype(fast) encode(XER:XER_EXTENDED)" }

external function f_dec_NameB(in octetstring stream, out NameB pdu) return integer
with { extension "prototype(backtrack) decode(XER:XER_EXTENDED)" }

//***Non_empty_string***
external function f_enc_Non_empty_string(in Non_empty_string pdu, out octetstring stream)
with { extension "prototype(fast) encode(XER:XER_EXTENDED)" }

external function f_dec_Non_empty_string(in octetstring stream, out Non_empty_string pdu) return integer
with { extension "prototype(backtrack) decode(XER:XER_EXTENDED)" }

//***Type***
external function f_enc_Type(in Type pdu, out octetstring stream)
with { extension "prototype(fast) encode(XER:XER_EXTENDED)" }

external function f_dec_Type(in octetstring stream, out Type pdu) return integer
with { extension "prototype(backtrack) decode(XER:XER_EXTENDED)" }

//***StringWhiteSpaceP***
external function f_enc_StringWhiteSpaceP(in StringWhiteSpaceP pdu, out octetstring stream)
with { extension "prototype(fast) encode(XER:XER_EXTENDED)" }

external function f_dec_StringWhiteSpaceP(in octetstring stream, out StringWhiteSpaceP pdu) return integer
with { extension "prototype(backtrack) decode(XER:XER_EXTENDED)" }

//***StringWhiteSpaceR***
external function f_enc_StringWhiteSpaceR(in StringWhiteSpaceR pdu, out octetstring stream)
with { extension "prototype(fast) encode(XER:XER_EXTENDED)" }

external function f_dec_StringWhiteSpaceR(in octetstring stream, out StringWhiteSpaceR pdu) return integer
with { extension "prototype(backtrack) decode(XER:XER_EXTENDED)" }

//***StringWhiteSpaceC***
external function f_enc_StringWhiteSpaceC(in StringWhiteSpaceC pdu, out octetstring stream)
with { extension "prototype(fast) encode(XER:XER_EXTENDED)" }

external function f_dec_StringWhiteSpaceC(in octetstring stream, out StringWhiteSpaceC pdu) return integer
with { extension "prototype(backtrack) decode(XER:XER_EXTENDED)" }

//***StringEnum***
external function f_enc_StringEnum(in StringEnum pdu, out octetstring stream)
with { extension "prototype(fast) encode(XER:XER_EXTENDED)" }

external function f_dec_StringEnum(in octetstring stream, out StringEnum pdu) return integer
with { extension "prototype(backtrack) decode(XER:XER_EXTENDED)" }

//==============================================================================
//   Encoding decoding test
//==============================================================================
//==========================================
// f_encDecTest_Name
//==========================================
function f_encDecTest_NameA() runs on Shell_CT {
  var NameA vl_pdu:= 
     "BarackObama"
  ,vl_decodedPdu;
  var octetstring vl_expectedEncodedPdu:=char2oct("<NameA xmlns=\"http://www.XmlTest.org/string\">BarackObama</NameA>\n\n");
  var octetstring vl_stream:=''O;
  f_enc_NameA(vl_pdu,vl_stream);

  if( not match(vl_stream,vl_expectedEncodedPdu)) {
    log("Expected encoded value: ",vl_expectedEncodedPdu);
    log("Actual encoded value:   ", vl_stream);
    setverdict(fail);
  }

  //log("===Checking encoded value (xml) against the xsd file by xmllint ===");
  var boolean vl_success:=false;
  f_shell_validateXml(vl_stream,"XmlTest_string.xsd", c_shell_successWithWarning, vl_success);
  if(vl_success) {
    setverdict(pass);
  }else{
    log("Xml validation against xsd failed");
    setverdict(fail);
  }
  
  //log("===Checking value decoded from the encoded value vs original value===");
  if(f_dec_NameA(vl_stream, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }

  //log("===Checking value decoded from the expected value vs original value===");
  if( f_dec_NameA(vl_expectedEncodedPdu, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded from the expected value");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }
}
//==========================================
// f_encDecTest_NameB
//==========================================
function f_encDecTest_NameB() runs on Shell_CT {
  var NameB vl_pdu:= 
     "Barack Obama"
  ,vl_decodedPdu;
  var octetstring vl_expectedEncodedPdu:=char2oct("<nameB xmlns=\"http://www.XmlTest.org/string\">Barack Obama</nameB>\n\n");
  var octetstring vl_stream:=''O;
  f_enc_NameB(vl_pdu,vl_stream);

  if( not match(vl_stream,vl_expectedEncodedPdu)) {
    log("Expected encoded value: ",vl_expectedEncodedPdu);
    log("Actual encoded value:   ", vl_stream);
    setverdict(fail);
  }

  //log("===Checking encoded value (xml) against the xsd file by xmllint ===");
  var boolean vl_success:=false;
  f_shell_validateXml(vl_stream,"XmlTest_string.xsd", c_shell_successWithWarning, vl_success);
  if(vl_success) {
    setverdict(pass);
  }else{
    log("Xml validation against xsd failed");
    setverdict(fail);
  }

  //log("===Just for fun: expected versus xsd====");
  f_shell_validateXml(vl_expectedEncodedPdu,"XmlTest_string.xsd", c_shell_successWithoutWarningAndError, vl_success);
  if(vl_success) {
    setverdict(pass);
  }else{
    log("Xml validation against xsd failed");
    setverdict(fail);
  }
  
  //log("===Checking value decoded from the encoded value vs original value===");
  if(f_dec_NameB(vl_stream, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }

  //log("===Checking value decoded from the expected value vs original value===");
  if( f_dec_NameB(vl_expectedEncodedPdu, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded from the expected value");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }
}
//==========================================
// f_encDecTest_Non_empty_string
//==========================================
function f_encDecTest_Non_empty_string() {
  var Non_empty_string vl_pdu:= "dad"
  ,vl_decodedPdu;
  var octetstring vl_expectedEncodedPdu:=char2oct("<Non-empty-string>dad</Non-empty-string>\n\n");
  var octetstring vl_stream:=''O;
  f_enc_Non_empty_string(vl_pdu,vl_stream);

  if( not match(vl_stream,vl_expectedEncodedPdu)) {
    log("Expected encoded value: ",vl_expectedEncodedPdu);
    log("Actual encoded value:   ", vl_stream);
    setverdict(fail);
  }

  //log("===Checking value decoded from the encoded value vs original value===");
  if(f_dec_Non_empty_string(vl_stream, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }

  //log("===Checking value decoded from the expected value vs original value===");
  if( f_dec_Non_empty_string(vl_expectedEncodedPdu, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded from the expected value");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }
}

//==========================================
// f_encDecTest_Type
//==========================================
function f_encDecTest_Type() {
  var Type vl_pdu:= "bad"
  ,vl_decodedPdu;
  var octetstring vl_expectedEncodedPdu:=char2oct("<type>bad</type>\n\n");
  var octetstring vl_stream:=''O;
  f_enc_Type(vl_pdu,vl_stream);

  if( not match(vl_stream,vl_expectedEncodedPdu)) {
    log("Expected encoded value: ",vl_expectedEncodedPdu);
    log("Actual encoded value:   ", vl_stream);
    setverdict(fail);
  }

  //log("===Checking value decoded from the encoded value vs original value===");
  if(f_dec_Type(vl_stream, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }

  //log("===Checking value decoded from the expected value vs original value===");
  if( f_dec_Type(vl_expectedEncodedPdu, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded from the expected value");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }
}


//==========================================
//  f_encDecTest_StringWhiteSpaceP
//==========================================
function f_encDecTest_StringWhiteSpaceP() {
  var StringWhiteSpaceP vl_pdu:= "This text contains     spaces \ttabs and\n new lines  \t  \n"
  ,vl_decodedPdu;
  var octetstring vl_expectedEncodedPdu:=char2oct("<StringWhiteSpaceP>This text contains     spaces \ttabs and\n new lines  \t  \n</StringWhiteSpaceP>\n\n");
  var octetstring vl_stream:=''O;
  f_enc_StringWhiteSpaceP(vl_pdu,vl_stream);

  if( not match(vl_stream,vl_expectedEncodedPdu)) {
    log("Expected encoded value: ",vl_expectedEncodedPdu);
    log("Actual encoded value:   ", vl_stream);
    setverdict(fail);
  }

  //log("===Checking value decoded from the encoded value vs original value===");
  if(f_dec_StringWhiteSpaceP(vl_stream, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }

  //log("===Checking value decoded from the expected value vs original value===");
  if( f_dec_StringWhiteSpaceP(vl_expectedEncodedPdu, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded from the expected value");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }
}

//==========================================
//  f_encDecTest_StringWhiteSpaceR
//==========================================
function f_encDecTest_StringWhiteSpaceR() {
  var StringWhiteSpaceR vl_pdu:= "This text contains     spaces \ttabs and\n new lines  \t  \n",
               vl_expectedPdu:= "This text contains     spaces  tabs and  new lines      "    ,
               vl_decodedPdu;
  var octetstring vl_expectedEncodedPdu:=char2oct("<StringWhiteSpaceR>This text contains     spaces \ttabs and\n new lines  \t  \n</StringWhiteSpaceR>\n\n");
  var octetstring vl_stream:=''O;
  f_enc_StringWhiteSpaceR(vl_pdu,vl_stream);

  if( not match(vl_stream,vl_expectedEncodedPdu)) {
    log("Expected encoded value: ",vl_expectedEncodedPdu);
    log("Actual encoded value:   ", vl_stream);
    setverdict(fail);
  }

  //log("===Checking value decoded from the encoded value vs original value===");
  if(f_dec_StringWhiteSpaceR(vl_stream, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_expectedPdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded");
      log(match(vl_expectedPdu,vl_decodedPdu));
    }
  }

  //log("===Checking value decoded from the expected value vs original value===");
  if( f_dec_StringWhiteSpaceR(vl_expectedEncodedPdu, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_expectedPdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded from the expected value");
      log(match(vl_expectedPdu,vl_decodedPdu));
    }
  }
}

//==========================================
//  f_encDecTest_StringWhiteSpaceC
//==========================================
function f_encDecTest_StringWhiteSpaceC() {
  var StringWhiteSpaceC vl_pdu:= "This text contains     spaces \ttabs and\n new lines  \t  \n",
    vl_expectedPdu:= "This text contains spaces tabs and new lines ",
    vl_decodedPdu;
  var octetstring vl_expectedEncodedPdu:=char2oct("<StringWhiteSpaceC>This text contains     spaces \ttabs and\n new lines  \t  \n</StringWhiteSpaceC>\n\n");
  var octetstring vl_stream:=''O;
  f_enc_StringWhiteSpaceC(vl_pdu,vl_stream);

  if( not match(vl_stream,vl_expectedEncodedPdu)) {
    log("Expected encoded value: ",vl_expectedEncodedPdu);
    log("Actual encoded value:   ", vl_stream);
    setverdict(fail);
  }

  //log("===Checking value decoded from the encoded value vs original value===");
  if(f_dec_StringWhiteSpaceC(vl_stream, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }

  //log("===Checking value decoded from the expected value vs original value===");
  if( f_dec_StringWhiteSpaceC(vl_expectedEncodedPdu, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded from the expected value");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }
}
//==========================================
// f_encDecTest_StringEnum
//==========================================
function f_encDecTest_StringEnum() {
  var StringEnum vl_pdu:= hU
  ,vl_decodedPdu;
  var octetstring vl_expectedEncodedPdu:=char2oct("<StringEnum>HU</StringEnum>\n\n");
  var octetstring vl_stream:=''O;
  f_enc_StringEnum(vl_pdu,vl_stream);

  if( not match(vl_stream,vl_expectedEncodedPdu)) {
    log("Expected encoded value: ",vl_expectedEncodedPdu);
    log("Actual encoded value:   ", vl_stream);
    setverdict(fail);
  }

  //log("===Checking value decoded from the encoded value vs original value===");
  if(f_dec_StringEnum(vl_stream, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }

  //log("===Checking value decoded from the expected value vs original value===");
  if( f_dec_StringEnum(vl_expectedEncodedPdu, vl_decodedPdu)==0) {
    //log("The decoded pdu: ", vl_decodedPdu);
    if(match(vl_pdu,vl_decodedPdu)) {
      setverdict(pass);
    } else {
      log("Pdu could not decoded from the expected value");
      log(match(vl_pdu,vl_decodedPdu));
    }
  }
}

}//module
