//******************************************************************************
//  Description: Test case for MUNIQUE's okio field for BUG 2375 and other bugs.
//
//  BUG: 2375 XBC: does not support okio field in Munique structure
//  BUG: 2547 XBC incorrectly implements M$GRAB support
//
//  Author : Neon Ngo
//  Version: $Id: test_mu_okio.cc,v 1.2 2012/01/20 14:33:17 ntn Exp $
//
//******************************************************************************
#include <primitive.h>
//#define _DEBUG_NXM_TEST // uncomment to enable test framework debugging
#include "nxm_test.hh"    // XBC C++ test framework

// functions declarations
void test_munique_okio(CPHEADER&);

//******************************************************************************
// main C++ host primitive entry point
//******************************************************************************
void mainroutine () {
  CPHEADER HIN;
  nxm_assert_init("DEBUG","STATUS");

  // open and test input file for testing BUG 2547
  m_init(HIN, "nxm.sys.dat.test1000_ieee.prm", "", "", 0);
  m_inok(HIN);

  // signal end of preamble
  m_sync();

  test_munique_okio(HIN);

  nxm_assert_auto_report();
}

//******************************************************************************
// test case for Mu.okio field (BUG 2375) and M$GRAB BUG 2547
//******************************************************************************
void test_munique_okio (CPHEADER& HCB) {
  int_4  loopcount = 0;
  int_4  xfer = 2;
  int_4  ldo;
  int_4  ngot;
  real_4 inbuf[xfer]; // input buffer

  nxm_assert("MUNIQUE.okio is true", (Mu->okio == true));

  m_grab(HCB, inbuf, 1.0, xfer, ngot); // read using M$GRAB to start test for BUG 2547
  nxm_assert_eq("m_grab got xfer element(s)", xfer, ngot);

  // as part of testing BUG 2547, it should go into the while loop below one time
  while (m_do(xfer, ldo) && (loopcount < 3)) {
    loopcount++;
    Mu->okio = false;
    nxm_assert("MUNIQUE.okio set to false", (Mu->okio == false));
  }
  nxm_assert_eq("m_do(..) executed once & returns false when Mu.okio is false", 1, loopcount);

} // end test_munique_okio()
