/******************************************************************************
 * 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
 *   Baranyi, Botond
 *
 ******************************************************************************/
// This module tests the following functions for UTF-32:
// oct2unichar
// unichar2oct
// get_stringencoding
module UTF32Testcases {

import from Common all;

//=========================================================================
// Testcases
//=========================================================================

// Insert test cases here if applicable!
// You can use the test_case skeleton!

testcase tc_converter_test_temp() runs on MTC {
  var octetstring o := '010203040a0b0c0d'O
  
  f_changeOrderUTF32(o)
  log(o);
  if(o=='040302010d0c0b0a'O){
    setverdict(pass);
  } else {
    setverdict(fail);
  }
}


//==== The next 4 testcase groups uses the Uncode standard 5.0 / Chapter 3/ Table 3-9 ======

//===1===
testcase tc_oct2unichar_UTF32_1() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '0000004D'O
  var universal charstring u_expected := char(0,0,0,77);
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32",
    expectedUCS:= u_expected,
    expectedEncType:="UTF-32BE",
    expectedOs := o
  )
}

testcase tc_oct2unichar_UTF32BE_1() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '0000004D'O
  var universal charstring u_expected := char(0,0,0,77);
  
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32BE",
    expectedUCS:= u_expected,
    expectedEncType:="UTF-32BE",
    expectedOs := o
  )
}

testcase tc_oct2unichar_UTF32LE_1() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '0000004D'O
  var universal charstring u_expected :=  char(0,0,0,77);
  
  f_changeOrderUTF32(o);
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32LE",
    expectedUCS:= u_expected,
    expectedEncType:="UTF-32LE",
    expectedOs := o
  )
}

//===2===
testcase tc_oct2unichar_UTF32_2() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00000430'O
  var universal charstring u_expected := char(0,0,4,48);
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32",
    expectedUCS:= u_expected,
    expectedEncType:="UTF-32BE",
    expectedOs := o
  )
}

testcase tc_oct2unichar_UTF32BE_2() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00000430'O
  var universal charstring u_expected := char(0,0,4,48);
  
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32BE",
    expectedUCS:= u_expected,
    expectedEncType:="UTF-32BE",
    expectedOs := o
  )
}



testcase tc_oct2unichar_UTF32LE_2() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00000430'O
  var universal charstring u_expected := char(0,0,4,48);
  
  f_changeOrderUTF32(o);
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32LE",
    expectedUCS:= u_expected,
    expectedEncType:="UTF-32LE",
    expectedOs := o
  )
}

//===3===
testcase tc_oct2unichar_UTF32_3() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00004E8C'O
  var universal charstring u_expected := char(0,0,78,140);
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32",
    expectedUCS:= u_expected,
    expectedEncType:="UTF-32BE",
    expectedOs := o
  )
}

testcase tc_oct2unichar_UTF32BE_3() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00004E8C'O
  var universal charstring u_expected := char(0,0,78,140);
  
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32BE",
    expectedUCS:= u_expected,
    expectedEncType:="UTF-32BE",
    expectedOs := o
  )
}

testcase tc_oct2unichar_UTF32LE_3() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00004E8C'O
  var universal charstring u_expected := char(0,0,78,140);
  
  f_changeOrderUTF32(o);
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32LE",
    expectedUCS:= u_expected,
    expectedEncType:="UTF-32LE",
    expectedOs := o
  )
}


//==4==
testcase tc_oct2unichar_UTF32_4() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00010302'O
  var universal charstring u_expected := char(0,1,3,2);
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32",
    expectedUCS:=char(0,1,3,2),
    expectedEncType:="UTF-32BE",
    expectedOs := o
  )
}

testcase tc_oct2unichar_UTF32BE_4() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00010302'O
  var universal charstring u_expected := char(0,1,3,2);
  
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32BE",
    expectedUCS:=char(0,1,3,2),
    expectedEncType:="UTF-32BE",
    expectedOs := o
  )
}



testcase tc_oct2unichar_UTF32LE_4() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00010302'O
  
  var universal charstring u_expected := char(0,1,3,2);
  
  f_changeOrderUTF32(o);
  
  f_oct2unichar_positivetest(
    inputOs:=o,
    encType:="UTF-32LE",
    expectedUCS:=char(0,1,3,2),
    expectedEncType:="UTF-32LE",
    expectedOs := o
  )
}

testcase tc_oct2unichar_UTF32_4_negative() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00010302'O
  var universal charstring u_expected := char(0,1,3,2);
  
  f_changeOrderUTF32(o);
  
  f_oct2unichar_negativetest(
    inputOs:=o,
    encType:="UTF-32",
    expectedErrorMsg:="Any UTF-32 code \(0xFFFE0000\) greater than 0x0010FFFF is ill-formed"
  )
}

testcase tc_oct2unichar_UTF32BE_4_negative() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00010302'O
  var universal charstring u_expected := char(0,1,3,2);
  
  f_changeOrderUTF32(o);
  
  f_oct2unichar_negativetest(
    inputOs:=o,
    encType:="UTF-32BE",
    expectedErrorMsg:="Any UTF-32 code \(0xFFFE0000\) greater than 0x0010FFFF is ill-formed"
  )
}

testcase tc_oct2unichar_UTF32LE_4_negative() runs on MTC {
  var octetstring o := c_BOM_UTF32BE & '00010302'O
  var universal charstring u_expected := char(0,1,3,2);
  
  f_oct2unichar_negativetest(
    inputOs:=o,
    encType:="UTF-32LE",
    expectedErrorMsg:="Any UTF-32 code \(0xFFFE0000\) greater than 0x0010FFFF is ill-formed"
  )
}


// ==== General test ===

testcase tc_oct2unichar_UTF32BE_BMP_low() runs on MTC {
  
  var integer L := 0;
  var integer N := oct2int('D7FF'O)
  
  for(var integer i:=L;i<=N;i:=i+2049) {   
    
    for(var integer  j:=0;j<4;j:=j+1) {
      f_oct2unichar_positivetest(
        inputOs :=  c_BOM_UTF32BE & int2oct(i+j,4),
        encType := "UTF-32BE",
        expectedUCS := int2unichar(i+j),
        expectedEncType := "UTF-32BE"
      )
    }
    
  }
}

testcase tc_oct2unichar_UTF32BE_BMP_hi() runs on MTC {
  
  var integer L := oct2int('E000'O)
  var integer N := oct2int('FFFF'O)

  for(var integer i:=L;i<=N;i:=i+2049) {   
    
    for(var integer  j:=0;j<4;j:=j+1) {
      log(">>=====i: ",i," j: ",j,"=======")
      f_oct2unichar_positivetest(
        inputOs :=  c_BOM_UTF32BE & int2oct(i+j,4),
        encType := "UTF-32BE",
        expectedUCS := int2unichar(i+j),
        expectedEncType := "UTF-32BE"
      )
    }
    
  }
}

testcase tc_oct2unichar_UTF32LE_BMP_low() runs on MTC {
  
  var integer L := 0;
  var integer N := oct2int('D7FF'O)
  var octetstring o;
  
  for(var integer i:=L;i<=N;i:=i+2049) {   
    
    for(var integer  j:=0;j<4;j:=j+1) {
      o:=c_BOM_UTF32BE & int2oct(i+j,4);
      f_changeOrderUTF32(o);
      f_oct2unichar_positivetest(
        inputOs :=  o,
        encType := "UTF-32LE",
        expectedUCS := int2unichar(i+j),
        expectedEncType := "UTF-32LE"
      )
    }
    
  }
}

testcase tc_oct2unichar_UTF32LE_BMP_hi() runs on MTC {
  
  var integer L := oct2int('E000'O)
  var integer N := oct2int('FFFF'O)
  var octetstring o;
  
  for(var integer i:=L;i<=N;i:=i+2049) {      
    for(var integer  j:=0;j<4;j:=j+1) {
      o:=c_BOM_UTF32BE & int2oct(i+j,4);
      f_changeOrderUTF32(o);
      f_oct2unichar_positivetest(
        inputOs :=  o,
        encType := "UTF-32LE",
        expectedUCS := int2unichar(i+j),
        expectedEncType := "UTF-32LE"
      )
    }
    
  }
}

//=== higher planes ====
 
testcase tc_oct2unichar_UTF32() runs on MTC {
  
  var integer L := oct2int('010000'O);
  var integer N := oct2int('10FFFF'O)
  
  for(var integer i:=L;i<=N;i:=i+25007) {   
    
    for(var integer j:=0;j<4;j:=j+1) {
      log("=====i: ",i," j: ",j,"=======")
      f_oct2unichar_positivetest(
        inputOs :=  c_BOM_UTF32BE & int2oct(i+j,4),
        encType := "UTF-32",
        expectedUCS := int2unichar(i+j),
        expectedEncType := "UTF-32BE"
      )
    }
    
  }
}

testcase tc_oct2unichar_UTF32BE() runs on MTC {
  
  var integer L := oct2int('010000'O);
  var integer N := oct2int('10FFFF'O)
  
  for(var integer i:=L;i<=N;i:=i+256*64) {   
    
    for(var integer  j:=0;j<4;j:=j+1) {
      f_oct2unichar_positivetest(
        inputOs :=  c_BOM_UTF32BE & int2oct(i+j,4),
        encType := "UTF-32BE",
        expectedUCS := int2unichar(i+j),
        expectedEncType := "UTF-32BE"
      )
    }
    
  }
}


testcase tc_oct2unichar_UTF32LE() runs on MTC {
  
  var integer L := oct2int('010000'O);
  var integer N := oct2int('10FFFF'O)
  var octetstring o;
  
  for(var integer i:=L;i<=N;i:=i+256*64) {
    
    for(var integer j:=0;j<4;j:=j+1) {
      o:=c_BOM_UTF32BE & int2oct(i+j,4);
      f_changeOrderUTF32(o);
      f_oct2unichar_positivetest(
        inputOs :=  o,
        encType := "UTF-32LE",
        expectedUCS := int2unichar(i+j),
        expectedEncType := "UTF-32LE"
      )
    }
    
  }
}

testcase tc_unichar2oct_utf32_asciistring() runs on MTC {
  var universal charstring us1 := c_arvizturo; //"ÃrvÃ­ztÅ±rÅ tÃ¼kÃ¶rfÃºrÃ³gÃ©p"
  var universal charstring us2 :="Steve Jobs likes the Apple"
  //2
  var octetstring os_expected := f_ucharstring2utf32BE(us1);

  f_unichar2oct_positivetest(
    u := us1,
    encodingType:="UTF-32",
    expectedResult :=  os_expected
  )
  
  os_expected := f_ucharstring2utf32BE(us2);

  f_unichar2oct_positivetest(
    u := us2,
    encodingType:="UTF-32",
    expectedResult :=  os_expected
  )
  
  
  
}

testcase tc_unichar2oct_utf32BE_asciistring() runs on MTC {

  var universal charstring us1 := c_arvizturo; //"ÃrvÃ­ztÅ±rÅ tÃ¼kÃ¶rfÃºrÃ³gÃ©p"
  var universal charstring us2 :="Steve Jobs likes the Apple"
  
  var octetstring os_expected := f_ucharstring2utf32BE(us1);

  f_unichar2oct_positivetest(
    u := us1,
    encodingType:="UTF-32BE",
    expectedResult :=  os_expected
  )
  
  os_expected := f_ucharstring2utf32BE(us2);

  f_unichar2oct_positivetest(
    u := us2,
    encodingType:="UTF-32BE",
    expectedResult :=  os_expected
  )
  
}


testcase tc_unichar2oct_utf32LE_asciistring() runs on MTC {
 
  var universal charstring us1 := c_arvizturo; //"ÃrvÃ­ztÅ±rÅ tÃ¼kÃ¶rfÃºrÃ³gÃ©p"
  var universal charstring us2 :="Steve Jobs likes the Apple"

  var octetstring os_expected := f_ucharstring2utf32BE(us1);
  if( not f_changeOrderUTF32(os_expected) ) { setverdict(fail, "Odd length is not expected") }
  f_unichar2oct_positivetest(
    u := us1,
    encodingType:="UTF-32LE",
    expectedResult :=  os_expected
  )
  
  os_expected := f_ucharstring2utf32BE(us2);
  if( not f_changeOrderUTF32(os_expected) ) { setverdict(fail, "Odd length is not expected") }
  f_unichar2oct_positivetest(
    u := us2,
    encodingType:="UTF-32LE",
    expectedResult :=  os_expected
  )
  
}


testcase tc_unichar2oct_utf32_upperplane_longer() runs on MTC {
  var RoUCS us := { 
    char(0,1,0,0) & char(0,1,0,175) & char(0,1,255,255),
    char(0,2,0,0) & char(0,2,0,175) & char(0,2,255,255),
    char(0,3,0,0) & char(0,3,0,175) & char(0,3,255,255),
    char(0,15,0,0)& char(0,15,0,175)& char(0,15,255,255),
    char(0,16,0,0)& char(0,16,0,175)& char(0,16,255,255),
    char(0,16,0,0) & char(0,0,0,1) & " "
    };
  var octetstring os_expected;
  var integer len:=lengthof(us);

  for(var integer i:=0;i<len;i:=i+1) {
    os_expected := f_ucharstring2utf32BE(us[i]);
    f_unichar2oct_positivetest(
      u := us[i],
      encodingType:="UTF-32",
      expectedResult :=  os_expected
    )
  }
}

testcase tc_unichar2oct_utf32BE_upperplane_longer() runs on MTC {
  var RoUCS us := { 
    char(0,1,0,0) & char(0,1,0,175) & char(0,1,255,255),
    char(0,2,0,0) & char(0,2,0,175) & char(0,2,255,255),
    char(0,3,0,0) & char(0,3,0,175) & char(0,3,255,255),
    char(0,15,0,0)& char(0,15,0,175)& char(0,15,255,255),
    char(0,16,0,0)& char(0,16,0,175)& char(0,16,255,255),
    char(0,16,0,0) & char(0,0,0,1) & " "
    };
  var octetstring os_expected;
  var integer len:=lengthof(us);

  for(var integer i:=0;i<len;i:=i+1) {
    os_expected := f_ucharstring2utf32BE(us[i]);
    f_unichar2oct_positivetest(
      u := us[i],
      encodingType:="UTF-32BE",
      expectedResult :=  os_expected
    )
  }
}

testcase tc_unichar2oct_utf32LE_upperplane_longer() runs on MTC {
  var RoUCS us := { 
    char(0,1,0,0) & char(0,1,0,175) & char(0,1,255,255),
    char(0,2,0,0) & char(0,2,0,175) & char(0,2,255,255),
    char(0,3,0,0) & char(0,3,0,175) & char(0,3,255,255),
    char(0,15,0,0)& char(0,15,0,175)& char(0,15,255,255),
    char(0,16,0,0)& char(0,16,0,175)& char(0,16,255,255),
    char(0,16,0,0) & char(0,0,0,1) & " "
    };
  var octetstring os_expected;
  var integer len:=lengthof(us);

  for(var integer i:=0;i<len;i:=i+1) {
    os_expected := f_ucharstring2utf32BE(us[i]);
    if( not f_changeOrderUTF32(os_expected) ) { setverdict(fail, "Odd length is not expected") }
    f_unichar2oct_positivetest(
      u := us[i],
      encodingType:="UTF-32LE",
      expectedResult :=  os_expected
    )
  }
}
//=========================================================================
// Control
//=========================================================================

control{
  execute(tc_converter_test_temp())
  execute(tc_oct2unichar_UTF32_1());
  execute(tc_oct2unichar_UTF32BE_1());
  execute(tc_oct2unichar_UTF32LE_1());
  execute(tc_oct2unichar_UTF32_2());
  execute(tc_oct2unichar_UTF32BE_2());
  execute(tc_oct2unichar_UTF32LE_2());
  execute(tc_oct2unichar_UTF32_3());
  execute(tc_oct2unichar_UTF32BE_3());
  execute(tc_oct2unichar_UTF32LE_3());
  execute(tc_oct2unichar_UTF32_4());
  execute(tc_oct2unichar_UTF32BE_4());
  execute(tc_oct2unichar_UTF32LE_4());
  execute(tc_oct2unichar_UTF32_4_negative());
  execute(tc_oct2unichar_UTF32BE_4_negative());
  execute(tc_oct2unichar_UTF32LE_4_negative());
  execute(tc_oct2unichar_UTF32BE_BMP_low());
  execute(tc_oct2unichar_UTF32BE_BMP_hi());
  execute(tc_oct2unichar_UTF32LE_BMP_low());
  execute(tc_oct2unichar_UTF32LE_BMP_hi());
  execute(tc_oct2unichar_UTF32());
  execute(tc_oct2unichar_UTF32BE());
  execute(tc_oct2unichar_UTF32LE());
  execute(tc_unichar2oct_utf32_asciistring());
  execute(tc_unichar2oct_utf32BE_asciistring());
  execute(tc_unichar2oct_utf32LE_asciistring());
  execute(tc_unichar2oct_utf32_upperplane_longer());
  execute(tc_unichar2oct_utf32BE_upperplane_longer());
  execute(tc_unichar2oct_utf32LE_upperplane_longer());
}

}  // end of module
