/*
 * Tests for putcolk.c - signed int write functions
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "fitsio.h"
#include "test_macros.h"

#define test_path "test_putcolk.fits"

static void
test_write_primary_array(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 5 };
	int data[] = { INT_MIN, -1000, 0, 1000, INT_MAX };
	int result[5];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 1, naxes);
	call_05(ffpprk, f, 1, 1, 5, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_07(ffgpvk, f, 1, 1, 5, 0, result, &anynull);
	fail_if(result[0] != INT_MIN);
	fail_if(result[1] != -1000);
	fail_if(result[2] != 0);
	fail_if(result[3] != 1000);
	fail_if(result[4] != INT_MAX);
	call_01(ffclos, f);
}

static void
test_write_primary_with_null(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 5 };
	int data[] = { 10, 20, -9999, 40, 50 };
	int result[5];
	char nularray[5];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 1, naxes);
	call_04(ffpkyj, f, "BLANK", -9999, NULL);
	call_06(ffppnk, f, 1, 1, 5, data, -9999);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_07(ffgpfk, f, 1, 1, 5, result, nularray, &anynull);
	fail_if(result[0] != 10);
	fail_if(result[1] != 20);
	fail_if(nularray[2] != 1);
	fail_if(result[3] != 40);
	fail_if(result[4] != 50);
	fail_if(anynull != 1);
	call_01(ffclos, f);
}

static void
test_write_2d_array(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 3, 2 };
	int data[] = { -3, -2, -1, 1, 2, 3 };
	int result[6];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 2, naxes);
	call_06(ffp2dk, f, 1, 3, 3, 2, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_08(ffg2dk, f, 1, 0, 3, 3, 2, result, &anynull);
	fail_if(result[0] != -3);
	fail_if(result[1] != -2);
	fail_if(result[2] != -1);
	fail_if(result[3] != 1);
	fail_if(result[4] != 2);
	fail_if(result[5] != 3);
	call_01(ffclos, f);
}

static void
test_write_3d_array(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 2, 2, 2 };
	int data[] = { -4, -3, -2, -1, 1, 2, 3, 4 };
	int result[8];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 3, naxes);
	call_08(ffp3dk, f, 1, 2, 2, 2, 2, 2, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_10(ffg3dk, f, 1, 0, 2, 2, 2, 2, 2, result, &anynull);
	fail_if(result[0] != -4);
	fail_if(result[7] != 4);
	call_01(ffclos, f);
}

static void
test_write_subsection(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 4, 4 };
	long fpixel[] = { 2, 2 };
	long lpixel[] = { 3, 3 };
	int data[] = { -100, -200, 100, 200 };
	int result[16];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 2, naxes);
	call_07(ffpssk, f, 1, 2, naxes, fpixel, lpixel, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_07(ffgpvk, f, 1, 1, 16, 0, result, &anynull);
	fail_if(result[5] != -100);
	fail_if(result[6] != -200);
	fail_if(result[9] != 100);
	fail_if(result[10] != 200);
	call_01(ffclos, f);
}

static void
test_write_group_parameters(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 4 };
	int pdata[] = { -10, 20 };
	int idata[] = { 1, 2, 3, 4 };
	int presult[2];

	call_02(ffinit, &f, "!" test_path);
	call_08(ffphpr, f, TRUE, LONG_IMG, 1, naxes, 2, 1, TRUE);
	call_05(ffpgpk, f, 1, 1, 2, pdata);
	call_05(ffpprk, f, 1, 1, 4, idata);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_05(ffggpk, f, 1, 1, 2, presult);
	fail_if(presult[0] != -10);
	fail_if(presult[1] != 20);
	call_01(ffclos, f);
}

static void
test_write_binary_table_column(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "KCOL" };
	char *tform[] = { "1J" };
	int data[] = { INT_MIN, 0, INT_MAX };
	int result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvk, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != INT_MIN);
	fail_if(result[1] != 0);
	fail_if(result[2] != INT_MAX);
	call_01(ffclos, f);
}

static void
test_write_column_with_null(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "KCOL" };
	char *tform[] = { "5J" };
	int data[] = { 10, 20, -9999, 40, 50 };
	int result[5];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 1, 1, ttype, tform, NULL, NULL);
	call_03(fftnul, f, 1, -9999);
	call_07(ffpcnk, f, 1, 1, 1, 5, data, -9999);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvk, f, 1, 1, 1, 5, 0, result, &anynull);
	fail_if(result[0] != 10);
	fail_if(result[1] != 20);
	fail_if(result[3] != 40);
	fail_if(result[4] != 50);
	call_01(ffclos, f);
}

static void
test_write_to_byte_column(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "BCOL" };
	char *tform[] = { "1B" };
	int data[] = { 0, 128, 255 };
	unsigned char result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvb, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != 0);
	fail_if(result[1] != 128);
	fail_if(result[2] != 255);
	call_01(ffclos, f);
}

static void
test_write_to_short_column(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "ICOL" };
	char *tform[] = { "1I" };
	int data[] = { SHRT_MIN, 0, SHRT_MAX };
	short result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvi, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != SHRT_MIN);
	fail_if(result[1] != 0);
	fail_if(result[2] != SHRT_MAX);
	call_01(ffclos, f);
}

static void
test_write_to_longlong_column(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "KCOL" };
	char *tform[] = { "1K" };
	int data[] = { INT_MIN, 0, INT_MAX };
	LONGLONG result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvjj, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != INT_MIN);
	fail_if(result[1] != 0);
	fail_if(result[2] != INT_MAX);
	call_01(ffclos, f);
}

static void
test_write_to_float_column(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "ECOL" };
	char *tform[] = { "1E" };
	int data[] = { -1000, 0, 1000 };
	float result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcve, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != -1000.0f);
	fail_if(result[1] != 0.0f);
	fail_if(result[2] != 1000.0f);
	call_01(ffclos, f);
}

static void
test_write_to_double_column(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "DCOL" };
	char *tform[] = { "1D" };
	int data[] = { -1000000, 0, 1000000 };
	double result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvd, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != -1000000.0);
	fail_if(result[1] != 0.0);
	fail_if(result[2] != 1000000.0);
	call_01(ffclos, f);
}

static void
test_write_to_ascii_table(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "NUMCOL" };
	char *tform[] = { "I15" };
	int data[] = { -1000000, 0, 1000000 };
	long result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, ASCII_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvj, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != -1000000);
	fail_if(result[1] != 0);
	fail_if(result[2] != 1000000);
	call_01(ffclos, f);
}

static void
test_write_multirow(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "KCOL" };
	char *tform[] = { "3J" };
	int data[] = { -4, -3, -2, -1, 0, 1, 2, 3, 4 };
	int result[9];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_06(ffpclk, f, 1, 1, 1, 9, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvk, f, 1, 1, 1, 9, 0, result, &anynull);
	fail_if(result[0] != -4);
	fail_if(result[4] != 0);
	fail_if(result[8] != 4);
	call_01(ffclos, f);
}

static void
test_write_variable_length_array(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "VARK" };
	char *tform[] = { "1PJ" };
	int data1[] = { -30, -20, -10 };
	int data2[] = { 10, 20 };
	int result[5];
	int anynull;
	long nelem;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 2, 1, ttype, tform, NULL, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data1);
	call_06(ffpclk, f, 1, 2, 1, 2, data2);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_05(ffgdes, f, 1, 1, &nelem, NULL);
	fail_if(nelem != 3);
	call_08(ffgcvk, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != -30);
	fail_if(result[1] != -20);
	fail_if(result[2] != -10);
	call_05(ffgdes, f, 1, 2, &nelem, NULL);
	fail_if(nelem != 2);
	call_08(ffgcvk, f, 1, 2, 1, 2, 0, &result[3], &anynull);
	fail_if(result[3] != 10);
	fail_if(result[4] != 20);
	call_01(ffclos, f);
}

static void
test_byte_overflow(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "BCOL" };
	char *tform[] = { "1B" };
	int data[] = { -1 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 1, 1, ttype, tform, NULL, NULL);
	ffpclk(f, 1, 1, 1, 1, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_short_overflow(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "ICOL" };
	char *tform[] = { "1I" };
	int data[] = { SHRT_MIN - 1 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 1, 1, ttype, tform, NULL, NULL);
	ffpclk(f, 1, 1, 1, 1, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_bad_status(void)
{
	fitsfile *f;
	int status = 1;
	int data[] = { 1 };
	ffpprk(f, 1, 1, 1, data, &status);
	fail_if(status != 1);
	ffppnk(f, 1, 1, 1, data, 0, &status);
	fail_if(status != 1);
	ffpclk(f, 1, 1, 1, 1, data, &status);
	fail_if(status != 1);
	ffpcnk(f, 1, 1, 1, 1, data, 0, &status);
	fail_if(status != 1);
}

static void
test_write_null_column_no_tnull(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "KCOL" };
	char *tform[] = { "5J" };
	int data[] = { 10, 20, -9999, 40, 50 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 1, 1, ttype, tform, NULL, NULL);
	ffpcnk(f, 1, 1, 1, 5, data, -9999, &status);
	fail_if(status != NO_NULL);
	status = 0;
	call_01(ffclos, f);
}

static void
test_write_3d_noncontiguous(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 3, 3, 2 };
	int data[32];
	int result[18];
	int anynull, i;

	for (i = 0; i < 32; i += 1) {
		data[i] = i + 1;
	}

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 3, naxes);
	/* ncols=4, nrows=4 but naxis1=3, naxis2=3 */
	call_08(ffp3dk, f, 1, 4, 4, 3, 3, 2, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_07(ffgpvk, f, 1, 1, 18, 0, result, &anynull);
	fail_if(result[0] != 1);
	fail_if(result[3] != 5);
	call_01(ffclos, f);
}

static void
test_write_3d_bad_dimen(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 5, 5, 2 };
	int data[50];

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 3, naxes);
	/* ncols < naxis1 should fail */
	ffp3dk(f, 1, 3, 3, 5, 5, 2, data, &status);
	fail_if(status != BAD_DIMEN);
	status = 0;
	call_01(ffclos, f);
}

static void
test_subsection_bad_naxis(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 4 };
	long fpixel[] = { 1 };
	long lpixel[] = { 4 };
	int data[] = { 1, 2, 3, 4 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 1, naxes);
	/* naxis = 0 should fail */
	ffpssk(f, 1, 0, naxes, fpixel, lpixel, data, &status);
	fail_if(status != BAD_DIMEN);
	status = 0;
	/* naxis = 8 should fail */
	ffpssk(f, 1, 8, naxes, fpixel, lpixel, data, &status);
	fail_if(status != BAD_DIMEN);
	status = 0;
	call_01(ffclos, f);
}

static void
test_write_with_scaling(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "ICOL" };
	char *tform[] = { "1I" };
	int data[] = { 100, 200, 300 };
	double result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 2.0, 15, NULL);
	call_05(ffpkyd, f, "TZERO1", 10.0, 15, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvd, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != 100.0);
	fail_if(result[1] != 200.0);
	fail_if(result[2] != 300.0);
	call_01(ffclos, f);
}

static void
test_write_to_byte_column_scaled(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "BCOL" };
	char *tform[] = { "1B" };
	int data[] = { 100, 200, 300 };
	double result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 2.0, 15, NULL);
	call_05(ffpkyd, f, "TZERO1", 50.0, 15, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvd, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != 100.0);
	fail_if(result[1] != 200.0);
	fail_if(result[2] != 300.0);
	call_01(ffclos, f);
}

static void
test_write_to_long_column_scaled(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "JCOL" };
	char *tform[] = { "1J" };
	int data[] = { 100, 200, 300 };
	double result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 2.0, 15, NULL);
	call_05(ffpkyd, f, "TZERO1", 50.0, 15, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvd, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != 100.0);
	fail_if(result[1] != 200.0);
	fail_if(result[2] != 300.0);
	call_01(ffclos, f);
}

static void
test_write_to_longlong_column_scaled(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "KCOL" };
	char *tform[] = { "1K" };
	int data[] = { 100, 200, 300 };
	double result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 2.0, 15, NULL);
	call_05(ffpkyd, f, "TZERO1", 50.0, 15, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvd, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != 100.0);
	fail_if(result[1] != 200.0);
	fail_if(result[2] != 300.0);
	call_01(ffclos, f);
}

static void
test_write_to_float_column_scaled(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "ECOL" };
	char *tform[] = { "1E" };
	int data[] = { 100, 200, 300 };
	double result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 2.0, 15, NULL);
	call_05(ffpkyd, f, "TZERO1", 50.0, 15, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvd, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != 100.0);
	fail_if(result[1] != 200.0);
	fail_if(result[2] != 300.0);
	call_01(ffclos, f);
}

static void
test_write_to_double_column_scaled(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "DCOL" };
	char *tform[] = { "1D" };
	int data[] = { 100, 200, 300 };
	double result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 2.0, 15, NULL);
	call_05(ffpkyd, f, "TZERO1", 50.0, 15, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvd, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != 100.0);
	fail_if(result[1] != 200.0);
	fail_if(result[2] != 300.0);
	call_01(ffclos, f);
}

static void
test_write_to_ascii_table_scaled(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "NUMCOL" };
	char *tform[] = { "F15.2" };
	int data[] = { 100, 200, 300 };
	double result[3];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, ASCII_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 2.0, 15, NULL);
	call_05(ffpkyd, f, "TZERO1", 50.0, 15, NULL);
	call_06(ffpclk, f, 1, 1, 1, 3, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvd, f, 1, 1, 1, 3, 0, result, &anynull);
	fail_if(result[0] != 100.0);
	fail_if(result[1] != 200.0);
	fail_if(result[2] != 300.0);
	call_01(ffclos, f);
}

static void
test_byte_overflow_with_scaling(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "BCOL" };
	char *tform[] = { "1B" };
	int data[] = { 50, 200, 100 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 0.5, 15, NULL);
	/* 200 / 0.5 = 400 > 255, should overflow */
	ffpclk(f, 1, 1, 1, 3, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_byte_underflow_with_scaling(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "BCOL" };
	char *tform[] = { "1B" };
	int data[] = { -100, 100, 200 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TZERO1", 50.0, 15, NULL);
	/* -100 - 50 = -150 < 0, should underflow */
	ffpclk(f, 1, 1, 1, 3, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_short_underflow_with_scaling(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "ICOL" };
	char *tform[] = { "1I" };
	int data[] = { INT_MIN, 100, 200 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TZERO1", 50000.0, 15, NULL);
	/* INT_MIN - 50000 < -32768, should underflow */
	ffpclk(f, 1, 1, 1, 3, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_short_overflow_with_scaling(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "ICOL" };
	char *tform[] = { "1I" };
	int data[] = { 1000, 20000, 5000 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 0.5, 15, NULL);
	/* 20000 / 0.5 = 40000 > 32767, should overflow */
	ffpclk(f, 1, 1, 1, 3, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_long_underflow_with_scaling(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "JCOL" };
	char *tform[] = { "1J" };
	int data[] = { INT_MIN, 100, 200 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TZERO1", 3.0e9, 15, NULL);
	/* INT_MIN - 3e9 < -2147483648, should underflow */
	ffpclk(f, 1, 1, 1, 3, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_long_overflow_with_scaling(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "JCOL" };
	char *tform[] = { "1J" };
	int data[] = { 100, INT_MAX, 200 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 0.5, 15, NULL);
	/* INT_MAX / 0.5 > 2147483647, should overflow */
	ffpclk(f, 1, 1, 1, 3, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_longlong_underflow_with_scaling(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "KCOL" };
	char *tform[] = { "1K" };
	int data[] = { INT_MIN, 100, 200 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TZERO1", 1.0e19, 15, NULL);
	/* INT_MIN - 1e19 < LONGLONG_MIN, should underflow */
	ffpclk(f, 1, 1, 1, 3, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_longlong_overflow_with_scaling(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "KCOL" };
	char *tform[] = { "1K" };
	int data[] = { 100, INT_MAX, 200 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_05(ffpkyd, f, "TSCAL1", 1.0e-10, 15, NULL);
	/* INT_MAX / 1e-10 > LONGLONG_MAX, should overflow */
	ffpclk(f, 1, 1, 1, 3, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_subsection_3d(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 4, 4, 2 };
	long fpixel[] = { 2, 2, 1 };
	long lpixel[] = { 3, 3, 2 };
	int data[8];
	int result[32];
	int anynull, i;

	for (i = 0; i < 8; i += 1) {
		data[i] = i + 100;
	}

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 3, naxes);
	call_07(ffpssk, f, 1, 3, naxes, fpixel, lpixel, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_07(ffgpvk, f, 1, 1, 32, 0, result, &anynull);
	fail_if(result[5] != 100);
	call_01(ffclos, f);
}

static void
test_4d_subsection(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 3, 3, 2, 2 };
	long fpixel[] = { 1, 1, 1, 1 };
	long lpixel[] = { 2, 2, 2, 2 };
	int data[16];
	int result[36];
	int anynull, i;

	for (i = 0; i < 16; i += 1) {
		data[i] = i + 1;
	}

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 4, naxes);
	call_07(ffpssk, f, 1, 4, naxes, fpixel, lpixel, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_07(ffgpvk, f, 1, 1, 36, 0, result, &anynull);
	fail_if(result[0] != 1);
	call_01(ffclos, f);
}

static void
test_5d_subsection(void)
{
	fitsfile *f;
	int status = 0;
	long naxes[] = { 2, 2, 2, 2, 2 };
	long fpixel[] = { 1, 1, 1, 1, 1 };
	long lpixel[] = { 2, 2, 2, 2, 2 };
	int data[32];
	int result[32];
	int anynull, i;

	for (i = 0; i < 32; i += 1) {
		data[i] = i + 1;
	}

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, LONG_IMG, 5, naxes);
	call_07(ffpssk, f, 1, 5, naxes, fpixel, lpixel, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_07(ffgpvk, f, 1, 1, 32, 0, result, &anynull);
	fail_if(result[0] != 1);
	fail_if(result[31] != 32);
	call_01(ffclos, f);
}

static void
test_write_nulls_at_end(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "VAL" };
	char *tform[] = { "1J" };
	int data[] = { 10, 20, -9999, -9999 };
	long result[4];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 4, 1, ttype, tform, NULL, NULL);
	call_03(fftnul, f, 1, -9999);
	call_07(ffpcnk, f, 1, 1, 1, 4, data, -9999);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	call_08(ffgcvj, f, 1, 1, 1, 4, 0, result, &anynull);
	fail_if(result[0] != 10);
	fail_if(result[1] != 20);
	fail_if(result[2] != -9999);
	fail_if(result[3] != -9999);
	call_01(ffclos, f);
}

static void
test_overflow_in_null_write(void)
{
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "BCOL" };
	char *tform[] = { "1B" };
	int data[] = { 100, 300, -999 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 3, 1, ttype, tform, NULL, NULL);
	call_03(fftnul, f, 1, 123);
	/* 300 overflows byte - tests overflow path in ffpcnk */
	ffpcnk(f, 1, 1, 1, 3, data, -999, &status);
	/* Expect NUM_OVERFLOW because 300 > 255 */
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_short_positive_overflow(void)
{
	/* Test writing INT_MAX to a short column */
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "ICOL" };
	char *tform[] = { "1I" };
	int data[] = { SHRT_MAX + 1 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 1, 1, ttype, tform, NULL, NULL);
	ffpclk(f, 1, 1, 1, 1, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_negative_dvalue_short(void)
{
	/* Test negative dvalue rounding in short conversion */
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "ICOL" };
	char *tform[] = { "1I" };
	int data[] = { 0 };
	short result[1];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 1, 1, ttype, tform, NULL, NULL);
	/* TSCAL=1, TZERO=100: stored = (0 - 100) / 1 = -100 */
	call_05(ffpkyd, f, "TZERO1", 100.0, 15, NULL);
	call_06(ffpclk, f, 1, 1, 1, 1, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	/* Read raw value without scaling */
	call_04(fftscl, f, 1, 1.0, 0.0);
	call_08(ffgcvi, f, 1, 1, 1, 1, 0, result, &anynull);
	fail_if(result[0] != -100);
	call_01(ffclos, f);
}

static void
test_negative_dvalue_int32(void)
{
	/* Test negative dvalue rounding in INT32BIT conversion */
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "JCOL" };
	char *tform[] = { "1J" };
	int data[] = { 0 };
	long result[1];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 1, 1, ttype, tform, NULL, NULL);
	/* TSCAL=1, TZERO=100: stored = (0 - 100) / 1 = -100 */
	call_05(ffpkyd, f, "TZERO1", 100.0, 15, NULL);
	call_06(ffpclk, f, 1, 1, 1, 1, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	/* Read raw value without scaling */
	call_04(fftscl, f, 1, 1.0, 0.0);
	call_08(ffgcvj, f, 1, 1, 1, 1, 0, result, &anynull);
	fail_if(result[0] != -100);
	call_01(ffclos, f);
}

static void
test_write_to_unsigned_longlong(void)
{
	/* Test writing signed int to unsigned longlong */
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "KCOL" };
	char *tform[] = { "1K" };
	int data[] = { 100, -1 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 2, 1, ttype, tform, NULL, NULL);
	/* Set TZERO to make this an unsigned longlong column */
	call_05(ffpkyd, f, "TZERO1", 9223372036854775808.0, 20, NULL);
	/* Negative value should cause overflow */
	ffpclk(f, 1, 1, 1, 2, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_negative_dvalue_longlong(void)
{
	/* Test negative dvalue rounding in LONGLONG conversion */
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "KCOL" };
	char *tform[] = { "1K" };
	int data[] = { 0 };
	LONGLONG result[1];
	int anynull;

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 1, 1, ttype, tform, NULL, NULL);
	/* TSCAL=1, TZERO=100: stored = (0 - 100) / 1 = -100 */
	call_05(ffpkyd, f, "TZERO1", 100.0, 15, NULL);
	call_06(ffpclk, f, 1, 1, 1, 1, data);
	call_01(ffclos, f);

	call_03(ffopen, &f, test_path, READONLY);
	call_03(ffmahd, f, 2, NULL);
	/* Read raw value without scaling */
	call_04(fftscl, f, 1, 1.0, 0.0);
	call_08(ffgcvjj, f, 1, 1, 1, 1, 0, result, &anynull);
	fail_if(result[0] != -100);
	call_01(ffclos, f);
}

static void
test_ascii_table_overflow(void)
{
	/* Test overflow in ASCII table write */
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "NUM" };
	char *tform[] = { "I3" };  /* 3-character integer column */
	int data[] = { 10000 };    /* 5 digits won't fit in 3 chars */

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, ASCII_TBL, 1, 1, ttype, tform, NULL, NULL);
	ffpclk(f, 1, 1, 1, 1, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_ascii_table_overflow_scaled(void)
{
	/* Test overflow in ASCII table write with scaling */
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "NUM" };
	char *tform[] = { "I3" };  /* 3-character integer column */
	int data[] = { 1 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, ASCII_TBL, 1, 1, ttype, tform, NULL, NULL);
	/* Scaling makes the stored value huge: (1-0)/0.0001 = 10000 */
	call_05(ffpkyd, f, "TSCAL1", 0.0001, 15, NULL);
	ffpclk(f, 1, 1, 1, 1, data, &status);
	fail_if(status != NUM_OVERFLOW);
	status = 0;
	call_01(ffclos, f);
}

static void
test_vla_with_nulls(void)
{
	/* Test VLA null handling in ffpcnk */
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "VLA" };
	char *tform[] = { "1PJ" };  /* Variable length array of J */
	int data[] = { 10, 20, 30 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 1, 1, ttype, tform, NULL, NULL);
	call_03(fftnul, f, 1, -999);
	/* Write VLA with null value handling */
	call_07(ffpcnk, f, 1, 1, 1, 3, data, -999);
	call_01(ffclos, f);
}

static void
test_write_to_ascii_string_column(void)
{
	/* Test writing to ASCII string column - should fail with BAD_ATABLE_FORMAT */
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "STR" };
	char *tform[] = { "A10" };  /* String column format */
	int data[] = { 42 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, ASCII_TBL, 1, 1, ttype, tform, NULL, NULL);
	/* Try to write int to string column - should fail */
	ffpclk(f, 1, 1, 1, 1, data, &status);
	fail_if(status != BAD_ATABLE_FORMAT);
	status = 0;
	call_01(ffclos, f);
}

static void
test_write_to_binary_string_column(void)
{
	/* Test writing to binary string column - should fail with BAD_BTABLE_FORMAT */
	fitsfile *f;
	int status = 0;
	char *ttype[] = { "STR" };
	char *tform[] = { "10A" };  /* String column format */
	int data[] = { 42 };

	call_02(ffinit, &f, "!" test_path);
	call_04(ffphps, f, BYTE_IMG, 0, NULL);
	call_08(ffcrtb, f, BINARY_TBL, 1, 1, ttype, tform, NULL, NULL);
	/* Try to write int to string column - should fail */
	ffpclk(f, 1, 1, 1, 1, data, &status);
	fail_if(status != BAD_BTABLE_FORMAT);
	status = 0;
	call_01(ffclos, f);
}

int
main(void)
{
	test_write_primary_array();
	test_write_primary_with_null();
	test_write_2d_array();
	test_write_3d_array();
	test_write_subsection();
	test_write_group_parameters();
	test_write_binary_table_column();
	test_write_column_with_null();
	test_write_to_byte_column();
	test_write_to_short_column();
	test_write_to_longlong_column();
	test_write_to_float_column();
	test_write_to_double_column();
	test_write_to_ascii_table();
	test_write_multirow();
	test_write_variable_length_array();
	test_byte_overflow();
	test_short_overflow();
	test_bad_status();
	test_write_null_column_no_tnull();
	test_write_3d_noncontiguous();
	test_write_3d_bad_dimen();
	test_subsection_bad_naxis();
	test_write_with_scaling();
	test_write_to_byte_column_scaled();
	test_write_to_long_column_scaled();
	test_write_to_longlong_column_scaled();
	test_write_to_float_column_scaled();
	test_write_to_double_column_scaled();
	test_write_to_ascii_table_scaled();
	test_byte_overflow_with_scaling();
	test_byte_underflow_with_scaling();
	test_short_underflow_with_scaling();
	test_short_overflow_with_scaling();
	test_long_underflow_with_scaling();
	test_long_overflow_with_scaling();
	test_longlong_underflow_with_scaling();
	test_longlong_overflow_with_scaling();
	test_subsection_3d();
	test_4d_subsection();
	test_5d_subsection();
	test_write_nulls_at_end();
	test_overflow_in_null_write();
	test_short_positive_overflow();
	test_negative_dvalue_short();
	test_negative_dvalue_int32();
	test_write_to_unsigned_longlong();
	test_negative_dvalue_longlong();
	test_ascii_table_overflow();
	test_ascii_table_overflow_scaled();
	test_vla_with_nulls();
	test_write_to_ascii_string_column();
	test_write_to_binary_string_column();
	remove(test_path);

	return 0;
}
