/******************************************************************************
 * 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
 *
 ******************************************************************************/
module EnumTest {

modulepar boolean EnumTest_verbose := false;
#define verbose EnumTest_verbose

#include "../macros.ttcnin"

type enumerated PrimesUnder31 { int2(2), int3(3), int5(5), int7(7), int11(11),
  int13(13), int17(17), int19(19), int23(23), int29(29) }

type enumerated NumbersUnder31 { int2(2), int3(3), int5(5), int7(7), int11(11),
  int13(13), int17(17), int19(19), int23(23), int29(29) }
with {
  variant "useNumber"
}

type record of PrimesUnder31  Primes;
type record of NumbersUnder31 Numbers;

type record of PrimesUnder31  PrimesList  with { variant "list" };
type record of NumbersUnder31 NumbersList with { variant "list" };

type component SAP {}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

DECLARE_XER_ENCODERS(PrimesUnder31, p31)
DECLARE_EXER_ENCODERS(PrimesUnder31, p31)

DECLARE_XER_ENCODERS(Primes, p)
DECLARE_EXER_ENCODERS(Primes, p)

DECLARE_XER_ENCODERS(PrimesList, pl)
DECLARE_EXER_ENCODERS(PrimesList, pl)

DECLARE_XER_ENCODERS(NumbersUnder31, n31)
DECLARE_EXER_ENCODERS(NumbersUnder31, n31)

DECLARE_XER_ENCODERS(Numbers, n)
DECLARE_EXER_ENCODERS(Numbers, n)

DECLARE_XER_ENCODERS(NumbersList, nrl)
DECLARE_EXER_ENCODERS(NumbersList, nrl)

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

const PrimesUnder31  p7 := int7;
const NumbersUnder31 n23 := int23;

const universal charstring p7_str_b :=
"<PrimesUnder31><int7/></PrimesUnder31>\n\n";

const universal charstring p7_str :=
"<PrimesUnder31>int7</PrimesUnder31>\n\n";

const universal charstring n23_str_b :=
"<NumbersUnder31><int23/></NumbersUnder31>\n\n";

const universal charstring n23_str :=
"<NumbersUnder31>23</NumbersUnder31>\n\n";

testcase bxer_enc_enum() runs on SAP
{
  CHECK_METHOD(bxer_enc_p31, p7 , p7_str_b);
  CHECK_METHOD(bxer_enc_n31, n23, n23_str_b);
}

testcase bxer_dec_enum() runs on SAP
{
  CHECK_DECODE(bxer_dec_p31, p7_str_b , PrimesUnder31 , p7);
  CHECK_DECODE(bxer_dec_n31, n23_str_b, NumbersUnder31, n23);
}

testcase exer_enc_enum() runs on SAP
{
  CHECK_METHOD(exer_enc_p31, p7 , p7_str);
  CHECK_METHOD(exer_enc_n31, n23, n23_str);
}

testcase exer_dec_enum() runs on SAP
{
  CHECK_DECODE(exer_dec_p31, p7_str , PrimesUnder31 , p7);
  CHECK_DECODE(exer_dec_n31, n23_str, NumbersUnder31, n23);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

const Primes  p0 := {}
const Numbers n0 := {}

const Primes  p10 := { int2, int5 }
const Numbers n42 := { int2, int3, int7 }

const universal charstring p10_str_b :=
"<Primes>\n" &
"\t<int2/><int5/>\n" &
"</Primes>\n\n";

const universal charstring p10_str :=
"<Primes>\n" &
"\t<PrimesUnder31>int2</PrimesUnder31>\n" &
"\t<PrimesUnder31>int5</PrimesUnder31>\n" &
"</Primes>\n\n";

const universal charstring p10_str_ex :=
"<Primes>\n" &
"\t<PrimesUnder31>\n" &
"\t\tint2\n" &
"\t</PrimesUnder31>\n" &
"\t<PrimesUnder31>int5</PrimesUnder31>\n" &
"</Primes>\n\n";


/*
"\n" &
*/

const universal charstring n42_str_b :=
"<Numbers>\n" &
"\t<int2/><int3/><int7/>\n" &
"</Numbers>\n\n";

const universal charstring n42_str :=
"<Numbers>\n" &
"\t<NumbersUnder31>2</NumbersUnder31>\n" &
"\t<NumbersUnder31>3</NumbersUnder31>\n" &
"\t<NumbersUnder31>7</NumbersUnder31>\n" &
"</Numbers>\n\n";

testcase bxer_enc_recofenum() runs on SAP
{
  CHECK_METHOD(bxer_enc_p, p10, p10_str_b);
  CHECK_METHOD(bxer_enc_n, n42, n42_str_b);
}

testcase bxer_dec_recofenum() runs on SAP
{
  CHECK_DECODE(bxer_dec_p, p10_str_b, Primes , p10);
  CHECK_DECODE(bxer_dec_n, n42_str_b, Numbers, n42);
}

testcase exer_enc_recofenum() runs on SAP
{
  CHECK_METHOD(exer_enc_p, p10, p10_str);
  CHECK_METHOD(exer_enc_n, n42, n42_str);
}

testcase exer_dec_recofenum() runs on SAP
{
  CHECK_DECODE(exer_dec_p, p10_str, Primes , p10);
//can't do it yet: CHECK_DECODE(exer_dec_p, p10_str_ex, Primes , p10);
  CHECK_DECODE(exer_dec_n, n42_str, Numbers, n42);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

const PrimesList  pl10 := { int2, int5 }
const NumbersList nl42 := { int2, int3, int7 }

const universal charstring pl10_str_b :=
"<PrimesList>\n" &
"\t<int2/><int5/>\n" &
"</PrimesList>\n\n";

const universal charstring nl42_str_b :=
"<NumbersList>\n" &
"\t<int2/><int3/><int7/>\n" &
"</NumbersList>\n\n";

testcase bxer_enc_listofenum() runs on SAP
{
  CHECK_METHOD(bxer_enc_pl, pl10, pl10_str_b);
  CHECK_METHOD(bxer_enc_nrl, nl42, nl42_str_b);
}

testcase bxer_dec_listofenum() runs on SAP
{
  CHECK_DECODE(bxer_dec_p, p10_str_b, Primes , p10);
  CHECK_DECODE(bxer_dec_n, n42_str_b, Numbers, n42);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

control {
  execute(bxer_enc_enum());
  execute(bxer_dec_enum());

  execute(exer_enc_enum());
  execute(exer_dec_enum());

  execute(bxer_enc_recofenum());
  execute(bxer_dec_recofenum());

  execute(exer_enc_recofenum());
  execute(exer_dec_recofenum());

  execute(bxer_enc_listofenum());
  execute(bxer_dec_listofenum());
}

}
with { encode "XML" }

