package nxm.sys.test;

import nxm.sys.lib.Native;
import nxm.sys.lib.Shell;
import nxm.sys.lib.Table;

/** Test case to verify the fixed width sizes of the typedefs in $NMROOT/nxm/sys/inc/cdefs.h
    (e.g. int_1, int_2, int_4, int_8, real_4, real_8, etc.) and the standard
    C native types (size_t, char, short, int, long, long long, float, and double).
    @author  Neon Ngo
    @since   NeXtMidas 2.9.0
 */
public class test_cdefs {

  /** Indicates whether the native library has been loaded properly. */
  private static boolean loaded = Shell.loadLibrary ("sys","test","test_cdefs");

  /** This constructor was automatically provided by Java,
   *  but it is unneeded as all the methods are static methods.
   *  @deprecated now that Javadocs are required, it is temporarily
   *              being explicitely provided but deprecated
   */
  @Deprecated
  public test_cdefs () {}

  /** Returns String form of Table described in {@link #getSizeofNativeTypes()}
      @return Returns a String representing the size of Native types
  */
  public static native String sizeofNativeTypes ();

  /** Get a Table of all the sizes (in bytes) of the fixed withd typedefs in cdefs.h
      along with the primitive types where the key is the typedef (i.e. int_1, int_u2,
      real_4, size_t, char, short, int, long, etc.) and the corresponding value is
      the sizeof the type in bytes. If a type has a space such as "long long", then the
      space will be replaced with underscore as the KEY in the table.
      There will also be a "NATIVE" key to signal if native code was available (true) or not (false).
      @return Table of all the fixed width typedefs in cdefs.h
              or a table with only one "NATIVE=FALSE" entry if native code cannot be loaded.
   */
  public static Table getSizeofNativeTypes () {
    Table tbl = new Table();
    boolean useNative = (Native.useOther && loaded);
    if (useNative) {
      String tblstr = sizeofNativeTypes();
      tbl.fromBytes(tblstr.getBytes());
    } else {
      tbl.put("NATIVE", false); // native code not available
    }
    return tbl;
  }
}
