# include "ohana.h"

enum
{
    OHANA_LITTLE_ENDIAN = 0x03020100ul,
    OHANA_BIG_ENDIAN    = 0x00010203ul,
};

static const union { 
    unsigned char bytes[4]; 
    int value; 
} ohana_host_order = { { 0, 1, 2, 3 } };

# define OHANA_HOST_ORDER (ohana_host_order.value)

// we need to verify some type-related issues:
int main (int argc, char **argv) {

    int status;

    status = 0;

    // 1) have we defined BYTE_SWAP correctly?

# ifdef BYTE_SWAP
    if (OHANA_HOST_ORDER == OHANA_BIG_ENDIAN) {
	fprintf (stderr, "ERROR: BYTE_SWAP is defined on a big endian machine\n");
	fprintf (stderr, "(see libohana/include/ohana.h)\n");
	status = 1;
    } else {
	fprintf (stderr, "BYTE_SWAP is small endian\n");
    }	
# else
    if (OHANA_HOST_ORDER == OHANA_BIG_ENDIAN) {
	fprintf (stderr, "NOT_BYTE_SWAP is big endian\n");
    } else {
	fprintf (stderr, "ERROR: BYTE_SWAP not defined on a small endian machine\n");
	fprintf (stderr, "(see libohana/include/ohana.h)\n");
	status = 1;
    }	
# endif 

    // 2) have we defined NAN correctly?
    {
	float fvalue;
	double dvalue;
    
	fvalue = NAN;
	dvalue = NAN;

	if (isfinite(fvalue)) {
	    fprintf (stderr, "ERROR: NAN definition fails for float\n");
	    fprintf (stderr, "fvalue: %e\n", fvalue);
	    status = 1;
	}
	if (isfinite(dvalue)) {
	    fprintf (stderr, "ERROR: NAN definition fails for double\n");
	    fprintf (stderr, "dvalue: %e\n", dvalue);
	    status = 1;
	}

	fprintf (stderr, "fvalue: %e (isfinite: %d)\n", fvalue, isfinite(fvalue));
	fprintf (stderr, "dvalue: %e (isfinite: %d)\n", dvalue, isfinite(dvalue));

# ifdef __STDC_VERSION__
	fprintf (stderr, "STDC_VERSION: %ld\n", __STDC_VERSION__);
# else
	fprintf (stderr, "STDC_VERSION is not set\n");
# endif
    }

    // 3) have we defined OFF_T_FMT correctly?
    off_t big_value;

    big_value = 10;
    fprintf (stderr, "this is a big int: "OFF_T_FMT" n'est pas?\n", big_value);

    // we could move the % out of the FMT and allow constructions like this:
    // # define OFF_T_FMT "jd"
    // fprintf (stderr, "this is a big int: %06"OFF_T_FMT_ALT" n'est pas?\n", big_value);
    // still kind of ugly...

    exit (status);
}
