/******************************************************************************
 * 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:
 *   Baranyi, Botond – initial implementation
 *
 ******************************************************************************/
module RealTests {

import from Types all;
import from Tools all;

external function enc_real(in Real x) return bitstring with { extension "prototype(convert) encode(PER)" }

external function dec_real(in bitstring x) return Real with { extension "prototype(convert) decode(PER)" }

testcase tc_per_real_encode() runs on CT {
  var Real x1 := -13.7796215;
  var bitstring enc_exp := '00001111000000110010110100110001001100110011011100110111001110010011011000110010001100010011010100101110010001010010110100110111'B;
  var bitstring enc := enc_real(x1);
  if (enc != enc_exp) {
    setverdict(fail, "Encoding real #1 failed. Got: ", enc, ", expected: ", enc_exp);
  }
  var Real x2 := 19891.6e-25;
  enc_exp := '00001100000000110011000100111001001110000011100100110001001101100010111001000101001011010011001000110110'B;
  enc := enc_real(x2);
  if (enc != enc_exp) {
    setverdict(fail, "Encoding real #2 failed. Got: ", enc, ", expected: ", enc_exp);
  }
  setverdict(pass);
}

testcase tc_per_real_decode() runs on CT {
  var bitstring enc := '00001111000000110010110100110001001100110011011100110111001110010011011000110010001100010011010100101110010001010010110100110111'B;
  var Real x1 := dec_real(enc);
  var Real x1_exp := -13.7796215;
  if (x1 != x1_exp) {
    setverdict(fail, "Decoding real #1 failed. Got: ", x1, ", expected: ", x1_exp);
  }
  enc := '00001100000000110011000100111001001110000011100100110001001101100010111001000101001011010011001000110110'B;
  var Real x2 := dec_real(enc);
  var Real x2_exp := 19891.6e-25;
  if (x2 != x2_exp) {
    setverdict(fail, "Decoding real #2 failed. Got: ", x2, ", expected: ", x2_exp);
  }
  setverdict(pass);
}

testcase tc_per_real_negtest() runs on CT {
  var Real x;
  var bitstring buf;
  @try {
    buf := enc_real(x);
    setverdict(fail, "Real #1 encoded successfully.");
  }
  @catch (msg) {
    if (not match(msg, pattern "*Encoding an unbound REAL value.")) {
      setverdict(fail, "Invalid error message received (#1): ", msg);
    }
  }
  buf := ''B;
  @try {
    x := dec_real(buf);
    setverdict(fail, "Real #2 decoded successfully.");
  }
  @catch (msg) {
    if (not match(msg, pattern "*Buffer overflow while PER-decoding. Requested bits: 1, remaining bits in buffer: 0")) {
      setverdict(fail, "Invalid error message received (#2): ", msg);
    }
  }
  setverdict(pass);
}
  
control {
  execute(tc_per_real_encode());
  execute(tc_per_real_decode());
  execute(tc_per_real_negtest());
}

}
