int
main(void)
{
	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;
}
int
main(void)
{
	char *text="error\n";
	char *endptr;
	numeric *num, *nin;
	decimal *dec;
	long l;
	int i, j, k, q, r, count = 0;
	double d;
	numeric **numarr = (numeric **) calloc(1, sizeof(numeric));

	ECPGdebug(1, stderr);

	for (i = 0; nums[i]; i++)
	{
		num = PGTYPESnumeric_from_asc(nums[i], &endptr);
		if (!num) check_errno();
		if (endptr != NULL)
		{
			printf("endptr of %d is not NULL\n", i);
			if (*endptr != '\0')
				printf("*endptr of %d is not \\0\n", i);
		}
		if (!num) continue;

		numarr = realloc(numarr, sizeof(numeric *) * (count + 1));
		numarr[count++] = num;

		text = PGTYPESnumeric_to_asc(num, -1);
		if (!text) check_errno();
		printf("num[%d,1]: %s\n", i, text); free(text);
		text = PGTYPESnumeric_to_asc(num, 0);
		if (!text) check_errno();
		printf("num[%d,2]: %s\n", i, text); free(text);
		text = PGTYPESnumeric_to_asc(num, 1);
		if (!text) check_errno();
		printf("num[%d,3]: %s\n", i, text); free(text);
		text = PGTYPESnumeric_to_asc(num, 2);
		if (!text) check_errno();
		printf("num[%d,4]: %s\n", i, text); free(text);

		nin = PGTYPESnumeric_new();
		text = PGTYPESnumeric_to_asc(nin, 2);
		if (!text) check_errno();
		printf("num[%d,5]: %s\n", i, text); free(text);

		r = PGTYPESnumeric_to_long(num, &l);
		if (r) check_errno();
		printf("num[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
		if (r == 0)
		{
			r = PGTYPESnumeric_from_long(l, nin);
			if (r) check_errno();
			text = PGTYPESnumeric_to_asc(nin, 2);
			q = PGTYPESnumeric_cmp(num, nin);
			printf("num[%d,7]: %s (r: %d - cmp: %d)\n", i, text, r, q);
			free(text);
		}

		r = PGTYPESnumeric_to_int(num, &k);
		if (r) check_errno();
		printf("num[%d,8]: %d (r: %d)\n", i, r?0:k, r);
		if (r == 0)
		{
			r = PGTYPESnumeric_from_int(k, nin);
			if (r) check_errno();
			text = PGTYPESnumeric_to_asc(nin, 2);
			q = PGTYPESnumeric_cmp(num, nin);
			printf("num[%d,9]: %s (r: %d - cmp: %d)\n", i, text, r, q);
			free(text);
		}

		r = PGTYPESnumeric_to_double(num, &d);
		if (r) check_errno();
		printf("num[%d,10]: %g (r: %d)\n", i, r?0.0:d, r);
		/* do not test double to numeric because
		 * - extra digits are different on different architectures
		 * - PGTYPESnumeric_from_double internally calls PGTYPESnumeric_from_asc anyway
		 */

		dec = PGTYPESdecimal_new();
		r = PGTYPESnumeric_to_decimal(num, dec);
		if (r) check_errno();
		/* we have no special routine for outputting decimal, it would
		 * convert to a numeric anyway */
		printf("num[%d,11]: - (r: %d)\n", i, r);
		if (r == 0)
		{
			r = PGTYPESnumeric_from_decimal(dec, nin);
			if (r) check_errno();
			text = PGTYPESnumeric_to_asc(nin, 2);
			q = PGTYPESnumeric_cmp(num, nin);
			printf("num[%d,12]: %s (r: %d - cmp: %d)\n", i, text, r, q);
			free(text);
		}

		PGTYPESdecimal_free(dec);
		PGTYPESnumeric_free(nin);
		printf("\n");
	}

	for (i = 0; i < count; i++)
	{
		for (j = 0; j < count; j++)
		{
			numeric* a = PGTYPESnumeric_new();
			numeric* s = PGTYPESnumeric_new();
			numeric* m = PGTYPESnumeric_new();
			numeric* d = PGTYPESnumeric_new();
			r = PGTYPESnumeric_add(numarr[i], numarr[j], a);
			if (r)
			{
				check_errno();
				printf("r: %d\n", r);
			}
			else
			{
				text = PGTYPESnumeric_to_asc(a, 10);
				printf("num[a,%d,%d]: %s\n", i, j, text);
				free(text);
			}
			r = PGTYPESnumeric_sub(numarr[i], numarr[j], s);
			if (r)
			{
				check_errno();
				printf("r: %d\n", r);
			}
			else
			{
				text = PGTYPESnumeric_to_asc(s, 10);
				printf("num[s,%d,%d]: %s\n", i, j, text);
				free(text);
			}
			r = PGTYPESnumeric_mul(numarr[i], numarr[j], m);
			if (r)
			{
				check_errno();
				printf("r: %d\n", r);
			}
			else
			{
				text = PGTYPESnumeric_to_asc(m, 10);
				printf("num[m,%d,%d]: %s\n", i, j, text);
				free(text);
			}
			r = PGTYPESnumeric_div(numarr[i], numarr[j], d);
			if (r)
			{
				check_errno();
				printf("r: %d\n", r);
			}
			else
			{
				text = PGTYPESnumeric_to_asc(d, 10);
				printf("num[d,%d,%d]: %s\n", i, j, text);
				free(text);
			}
		}
	}

	for (i = 0; i < count; i++)
	{
		text = PGTYPESnumeric_to_asc(numarr[i], -1);
		printf("%d: %s\n", i, text);
		free(text);
	}

	return (0);
}