/* **********************************************************************
 * *                                                                    *
 * *   STRUCTURE SIZE TESTING UTILITY                                   *
 * *                                                                    *
 * **********************************************************************
 * File: libs/user/test_c.c
 *
 *         COPYRIGHT (c) 1992, 1998, 1999, 2001, 2002, 2004 BY
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */
#include <stdio.h>
#include <stdlib.h>
#include "sigtypes.h"
#include "projection.h"
#include "dsp.h"
#include "setup.h"
#include "ascope.h"

/* ==========================================================================
 * History:
 *  29 Jul 1992   jmh   Init
 */
int main( int iargc, char **sargv)
{
  int idesired_size, iactual_size, err, exitstatus=0 ;

#define NOCHECK (-1)
#define CHECK( name, structure, size ) \
  iactual_size  = sizeof( struct structure ) ; \
  idesired_size = size ; \
  if( (idesired_size == NOCHECK) || (idesired_size == iactual_size) ) { \
    printf( "  %s : OK.    Size=%d", name, iactual_size ) ; \
    if( idesired_size == NOCHECK ) printf( " (unchecked)" ) ; \
    printf( "\n" ) ; \
  } else { \
    printf( "  %s : ERROR. Desired=%d  Actual=%d\n", name, \
      idesired_size, iactual_size ) ; err++ ; \
  }

  err = 0 ;

/* --------------------------------------------------
 * The following structures are from setup.h
 * --------------------------------------------------
 */
  printf( "Structures from setup.h...\n" ) ;
  CHECK( "catch_inventory8     ", catch_inventory8     , NOCHECK              ) ;
  CHECK( "one_protected_region ", one_protected_region , ONE_PROT_REGION_SIZE ) ;
  CHECK( "overlay_inventory8   ", overlay_inventory8   , NOCHECK              ) ;
  CHECK( "protect_setup        ", protect_setup        , PROTECT_SETUP_SIZE   ) ;
  printf( "\n" ) ;

/* --------------------------------------------------
 * The following structures are from sigtypes.h
 * --------------------------------------------------
 */
  printf( "Structures from sigtypes.h...\n" ) ;
  CHECK( "ymds_time            ", ymds_time           , NOCHECK             ) ;
  printf( "\n" ) ;

/* --------------------------------------------------
 * The following structures are from projection.h
 * --------------------------------------------------
 */
  printf( "Structures from projection.h...\n" ) ;
  CHECK( "layer_dir            ", layer_dir           , NOCHECK             ) ;
  CHECK( "overlay_file_header8 ", overlay_file_header8, NOCHECK             ) ;
  CHECK( "overlay_gif8         ", overlay_gif8        , NOCHECK             ) ;
  CHECK( "overlay_point8       ", overlay_point8      , OVERLAY_POINT_SIZE  ) ;
  CHECK( "overlay_text8        ", overlay_text8       , OVERLAY_TEXT_SIZE   ) ;
  printf( "\n" ) ;

/* --------------------------------------------------
 * The following structures are from ascope.h
 * --------------------------------------------------
 */
  printf( "Structures from ascope.h...\n" ) ;
  CHECK( "asc_stats            ", asc_stats          , ASC_STATS_SIZE      ) ;
  CHECK( "asc_ray_header       ", asc_ray_header     , ASC_RAY_HEADER_SIZE ) ;
  printf( "\n" ) ;

  if( err ) {
    fprintf( stdout, "*** Errors in %d of the structures ***\a\n", err ) ;
    fprintf( stderr, "*** Errors in %d of the structures ***\a\n", err ) ;
    exitstatus = 1 ;
  } else {
    printf( "All structures have correct size.\n" ) ;
  }

  /* ==================================================
   * Check that the internal integer declarations all work properly.
   */
  err = 0 ;
  if( 0x000000FF != (int)  (UINT1)0xFFFFFFFF      ) { printf("UINT1 ") ; err++ ; }
  if( 0x0000FFFF != (int)  (UINT2)0xFFFFFFFF      ) { printf("UINT2 ") ; err++ ; }
  if( 0x0FFFFFF0 != (int)(((UINT4)0xFFFFFF00)>>4) ) { printf("UINT4 ") ; err++ ; }
  if( 0xFFFFFFFF != (int)  (SINT1)0x000000FF      ) { printf("SINT1 ") ; err++ ; }
  if( 0xFFFFFFFF != (int)  (SINT2)0x0000FFFF      ) { printf("SINT2 ") ; err++ ; }
  if( 0xFFFFFFF0 != (int)(((SINT4)0xFFFFFF00)>>4) ) { printf("SINT4 ") ; err++ ; }
  if( err ) {
    fprintf( stdout, "typedefs are working improperly.\a\n" ) ;
    fprintf( stderr, "typedefs are working improperly.\a\n" ) ;
    exitstatus = 1 ;
  } else {
    printf( "All internal typdefs are working properly.\n" ) ;
  }

  /* ==================================================
   * Check the size of the C enumeration type.
   */
  {
    enum { yes, no } enum_test ; SINT4 iesize = sizeof( enum_test ) ;
    if( 4 != iesize ) {
      fprintf( stdout, "Enumeration type is %d (rather than 4) bytes.\a\n", iesize ) ;
      fprintf( stderr, "Enumeration type is %d (rather than 4) bytes.\a\n", iesize ) ;
      exitstatus = 1 ;
    } else {
      printf( "Enumeration type has correct 4-byte length.\n" ) ;
    }
  }

  /* ==================================================
   * Report on Byte Swappedness
   */
  {
    union { UINT2 word ; UINT1 chars[2] ; } utest ;
    utest.word = 0x1234 ;
    printf( "Byte Order: " ) ;
    if     ( utest.chars[0] == 0x12 ) printf( "Big Endian (SGI/HP/IBM, etc)\n" ) ;
    else if( utest.chars[0] == 0x34 ) printf( "Little Endian (DEC/Intel, etc)\n" ) ;
    else                              printf( "Unknown!\n" ) ;
  }

  exit( exitstatus ) ;
}

