Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Functions | Variables

compat_informix-dec_test.c File Reference

#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
#include <ecpg_informix.h>
#include <stdio.h>
#include <stdlib.h>
#include <pgtypes_numeric.h>
#include <pgtypes_error.h>
#include <decimal.h>
#include <sqltypes.h>
Include dependency graph for compat_informix-dec_test.c:

Go to the source code of this file.

Defines

#define ECPGdebug(X, Y)   ECPGdebug((X)+100,(Y))
#define BUFSIZE   200

Functions

static void check_errno (void)
int main (void)

Variables

char * decs []

Define Documentation

#define BUFSIZE   200

Definition at line 61 of file compat_informix-dec_test.c.

Referenced by main().

#define ECPGdebug (   X,
  Y 
)    ECPGdebug((X)+100,(Y))

Definition at line 9 of file compat_informix-dec_test.c.


Function Documentation

static void check_errno ( void   )  [static]

Definition at line 233 of file compat_informix-dec_test.c.

References ECPG_INFORMIX_NUM_OVERFLOW, ECPG_INFORMIX_NUM_UNDERFLOW, PGTYPES_NUM_BAD_NUMERIC, PGTYPES_NUM_DIVIDE_ZERO, PGTYPES_NUM_OVERFLOW, PGTYPES_NUM_UNDERFLOW, and strerror().

Referenced by main().

{
    switch(errno)
    {
        case 0:
            printf("(no errno set) - ");
            break;
        case ECPG_INFORMIX_NUM_OVERFLOW:
            printf("(errno == ECPG_INFORMIX_NUM_OVERFLOW) - ");
            break;
        case ECPG_INFORMIX_NUM_UNDERFLOW:
            printf("(errno == ECPG_INFORMIX_NUM_UNDERFLOW) - ");
            break;
        case PGTYPES_NUM_OVERFLOW:
            printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
            break;
        case PGTYPES_NUM_UNDERFLOW:
            printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
            break;
        case PGTYPES_NUM_BAD_NUMERIC:
            printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
            break;
        case PGTYPES_NUM_DIVIDE_ZERO:
            printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
            break;
        default:
            printf("(unknown errno (%d))\n", errno);
            printf("(libc: (%s)) ", strerror(errno));
            break;
    }
}

int main ( void   ) 

Definition at line 64 of file compat_informix-dec_test.c.

References buf, BUFSIZE, calloc, CDECIMALTYPE, check_errno(), decadd(), deccmp(), deccvasc(), deccvint(), deccvlong(), decdiv(), decmul(), decs, decsub(), dectoasc(), dectodbl(), dectoint(), dectolong(), ECPGdebug, free, i, PGTYPESdecimal_free(), PGTYPESdecimal_new(), realloc, risnull(), and rsetnull().

{
    decimal *dec, *din;
    char buf[BUFSIZE];
    long l;
    int i, j, k, q, r, count = 0;
    double dbl;
    decimal **decarr = (decimal **) calloc(1, sizeof(decimal));

    ECPGdebug(1, stderr);

    for (i = 0; decs[i]; i++)
    {
        dec = PGTYPESdecimal_new();
        r = deccvasc(decs[i], strlen(decs[i]), dec);
        if (r)
        {
            check_errno();
            printf("dec[%d,0]: r: %d\n", i, r);
            PGTYPESdecimal_free(dec);
            continue;
        }
        decarr = realloc(decarr, sizeof(decimal *) * (count + 1));
        decarr[count++] = dec;

        r = dectoasc(dec, buf, BUFSIZE-1, -1);
        if (r < 0) check_errno();
        printf("dec[%d,1]: r: %d, %s\n", i, r, buf);

        r = dectoasc(dec, buf, BUFSIZE-1, 0);
        if (r < 0) check_errno();
        printf("dec[%d,2]: r: %d, %s\n", i, r, buf);
        r = dectoasc(dec, buf, BUFSIZE-1, 1);
        if (r < 0) check_errno();
        printf("dec[%d,3]: r: %d, %s\n", i, r, buf);
        r = dectoasc(dec, buf, BUFSIZE-1, 2);
        if (r < 0) check_errno();
        printf("dec[%d,4]: r: %d, %s\n", i, r, buf);

        din = PGTYPESdecimal_new();
        r = dectoasc(din, buf, BUFSIZE-1, 2);
        if (r < 0) check_errno();
        printf("dec[%d,5]: r: %d, %s\n", i, r, buf);

        r = dectolong(dec, &l);
        if (r) check_errno();
        printf("dec[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
        if (r == 0)
        {
            r = deccvlong(l, din);
            if (r) check_errno();
            dectoasc(din, buf, BUFSIZE-1, 2);
            q = deccmp(dec, din);
            printf("dec[%d,7]: %s (r: %d - cmp: %d)\n", i, buf, r, q);
        }

        r = dectoint(dec, &k);
        if (r) check_errno();
        printf("dec[%d,8]: %d (r: %d)\n", i, r?0:k, r);
        if (r == 0)
        {
            r = deccvint(k, din);
            if (r) check_errno();
            dectoasc(din, buf, BUFSIZE-1, 2);
            q = deccmp(dec, din);
            printf("dec[%d,9]: %s (r: %d - cmp: %d)\n", i, buf, r, q);
        }

        if (i != 6)
        {
            /* underflow does not work reliable on several archs, so not testing it here */
            /* this is a libc problem since we only call strtod() */
            r = dectodbl(dec, &dbl);
            if (r) check_errno();
            printf("dec[%d,10]: %g (r: %d)\n", i, r?0.0:dbl, r);
        }

        PGTYPESdecimal_free(din);
        printf("\n");
    }

    /* add a NULL value */
    dec = PGTYPESdecimal_new();
    decarr = realloc(decarr, sizeof(decimal *) * (count + 1));
    decarr[count++] = dec;

    rsetnull(CDECIMALTYPE, (char *) decarr[count-1]);
    printf("dec[%d]: %sNULL\n", count-1,
        risnull(CDECIMALTYPE, (char *) decarr[count-1]) ? "" : "NOT ");
    printf("dec[0]: %sNULL\n",
        risnull(CDECIMALTYPE, (char *) decarr[0]) ? "" : "NOT ");

    r = dectoasc(decarr[3], buf, -1, -1);
    check_errno(); printf("dectoasc with len == -1: r: %d\n", r);
    r = dectoasc(decarr[3], buf, 0, -1);
    check_errno(); printf("dectoasc with len == 0: r: %d\n", r);

    for (i = 0; i < count; i++)
    {
        for (j = 0; j < count; j++)
        {
            decimal a, s, m, d;
            int c;
            c = deccmp(decarr[i], decarr[j]);
            printf("dec[c,%d,%d]: %d\n", i, j, c);

            r = decadd(decarr[i], decarr[j], &a);
            if (r)
            {
                check_errno();
                printf("r: %d\n", r);
            }
            else
            {
                dectoasc(&a, buf, BUFSIZE-1, -1);
                printf("dec[a,%d,%d]: %s\n", i, j, buf);
            }

            r = decsub(decarr[i], decarr[j], &s);
            if (r)
            {
                check_errno();
                printf("r: %d\n", r);
            }
            else
            {
                dectoasc(&s, buf, BUFSIZE-1, -1);
                printf("dec[s,%d,%d]: %s\n", i, j, buf);
            }

            r = decmul(decarr[i], decarr[j], &m);
            if (r)
            {
                check_errno();
                printf("r: %d\n", r);
            }
            else
            {
                dectoasc(&m, buf, BUFSIZE-1, -1);
                printf("dec[m,%d,%d]: %s\n", i, j, buf);
            }

            r = decdiv(decarr[i], decarr[j], &d);
            if (r)
            {
                check_errno();
                printf("r: %d\n", r);
            }
            else
            {
                dectoasc(&d, buf, BUFSIZE-1, -1);
                printf("dec[d,%d,%d]: %s\n", i, j, buf);
            }
        }
    }

    for (i = 0; i < count; i++)
    {
        dectoasc(decarr[i], buf, BUFSIZE-1, -1);
        printf("%d: %s\n", i, buf);

        PGTYPESdecimal_free(decarr[i]);
    }
    free(decarr);

    return (0);
}


Variable Documentation

char* decs[]
Initial value:
 { "2E394", "-2", ".794", "3.44", "592.49E21", "-32.84e4",
                 "2E-394", ".1E-2", "+.0", "-592.49E-07", "+32.84e-4",
                 ".500001", "-.5000001",
                 "1234567890123456789012345678.91", 
                 "1234567890123456789012345678.921", 
                 "not a number",
                 NULL}

Definition at line 47 of file compat_informix-dec_test.c.

Referenced by main().