/*
 */

#include "sm/generic.h"
SM_RCSID("@(#)$Id: t-rcb-4.c,v 1.4 2007/01/31 05:25:15 ca Exp $")

#include "sm/assert.h"
#include "sm/magic.h"
#include "sm/error.h"
#include "sm/memops.h"
#include "sm/rpool.h"
#include "sm/test.h"
#include "sm/rcb.h"

#include <stdio.h>

#define INITLEN	16
#define SMAXLEN	260
#define VLEN	8
#define RT	0x404
#define V1	123
#define V2	279
#define V3	842

static int
check_v(sm_rcb_P s)
{
	sm_ret_T r;
#if MTA_USE_RCBV_INTN
	uint32_t un, ui;
	uint32_t uiv[VLEN];

	r = sm_rcb_open_dec(s);
	SM_TEST_E(SM_SUCCESS == r);
	r = sm_rcb_getuint32(s, &ui);	/* total record size */
	SM_TEST(SM_SUCCESS == r);
	SM_TEST(24 == ui);
	r = sm_rcb_getauint32(s, VLEN, &un, &ui, uiv);
	SM_TEST(SM_SUCCESS == r);
	SM_TEST_E(RT == ui);
	SM_TEST_E(3 == un);
	SM_TEST_E(uiv[0] == V1);
	SM_TEST_E(uiv[1] == V2);
	SM_TEST_E(uiv[2] == V3);
	SM_TEST(SM_RCB_ISEOB(s));
	r = sm_rcb_close_dec(s);
	SM_TEST_E(SM_SUCCESS == r);
#else /* MTA_USE_RCBV_INTN */
	r = SM_SUCCESS;
#endif /* MTA_USE_RCBV_INTN */

  error:
	return r;
}

static int
check_v2(sm_rcb_P s)
{
	sm_ret_T r;
#if MTA_USE_RCBV_INTN
	uint32_t un, ui;
	uint32_t uiv[VLEN];

	r = sm_rcb_open_dec(s);
	SM_TEST_E(SM_SUCCESS == r);
	r = sm_rcb_getuint32(s, &ui);	/* total record size */
	SM_TEST_E(SM_SUCCESS == r);
	SM_TEST_E(24 == ui);

	r = sm_rcb_getuint32(s, &ui);	/* size of entry */
	SM_TEST_E(SM_SUCCESS == r);
	SM_TEST_E(12 == ui);
	un = ui / 4;

	r = sm_rcb_getuint32(s, &ui);	/* record type */
	SM_TEST_E(SM_SUCCESS == r);
	SM_TEST_E(RT == ui);

	r = sm_rcb_getauint32(s, un, NULL, NULL, uiv);
	SM_TEST_E(SM_SUCCESS == r);
	SM_TEST_E(uiv[0] == V1);
	SM_TEST_E(uiv[1] == V2);
	SM_TEST_E(uiv[2] == V3);
	SM_TEST(SM_RCB_ISEOB(s));
	r = sm_rcb_close_dec(s);
	SM_TEST_E(SM_SUCCESS == r);
#else /* MTA_USE_RCBV_INTN */
	r = SM_SUCCESS;
#endif /* MTA_USE_RCBV_INTN */

  error:
	return r;
}

static void
test_harness(sm_rpool_P a)
{
#if MTA_USE_RCBV_INTN
	sm_rcb_P s;
	uint32_t ui, ui1, ui2;
	uint32_t uiv[VLEN];
	sm_ret_T r;

	s = sm_rcb_new(a, INITLEN, SMAXLEN);
	SM_TEST_E(s != NULL);

	r = sm_rcb_open_enc(s, SMAXLEN);
	SM_TEST_E(SM_SUCCESS == r);
	r = sm_rcb_putuint32(s, 0);	/* dummy for record size */
	SM_TEST(SM_SUCCESS == r);

	ui = V1;
	ui1 = V2;
	ui2 = V3;
	r = sm_rcb_putnuint32(s, RT, 3, ui, ui1, ui2);
	SM_TEST(SM_SUCCESS == r);

	r = sm_rcb_close_enc(s);
	SM_TEST_E(SM_SUCCESS == r);

	r = sm_rcb_open_dec(s);
	SM_TEST_E(SM_SUCCESS == r);
	r = sm_rcb_getuint32(s, &ui);	/* total record size */
	SM_TEST(SM_SUCCESS == r);
	SM_TEST(24 == ui);

	r = sm_rcb_getuint32(s, &ui);	/* size of entry */
	SM_TEST(SM_SUCCESS == r);
	SM_TEST(12 == ui);

	r = sm_rcb_getuint32(s, &ui);	/* record type */
	SM_TEST(SM_SUCCESS == r);
	SM_TEST(RT == ui);

	r = sm_rcb_getuint32(s, &ui);
	SM_TEST(SM_SUCCESS == r);
	SM_TEST(V1 == ui);

	r = sm_rcb_getuint32(s, &ui);
	SM_TEST(SM_SUCCESS == r);
	SM_TEST(V2 == ui);

	r = sm_rcb_getuint32(s, &ui);
	SM_TEST(SM_SUCCESS == r);
	SM_TEST(V3 == ui);

	SM_TEST(SM_RCB_ISEOB(s));
	r = sm_rcb_close_dec(s);
	SM_TEST_E(SM_SUCCESS == r);

	r = check_v(s);
	SM_TEST_E(SM_SUCCESS == r);
	check_v2(s);
	SM_TEST_E(SM_SUCCESS == r);

	r = sm_rcb_open_enc(s, SMAXLEN);
	SM_TEST_E(SM_SUCCESS == r);
	r = sm_rcb_putuint32(s, 0);	/* dummy for record size */
	SM_TEST(SM_SUCCESS == r);
	uiv[0] = V1;
	uiv[1] = V2;
	uiv[2] = V3;
	r = sm_rcb_putauint32(s, RT, 3, uiv);
	SM_TEST(SM_SUCCESS == r);
	r = sm_rcb_close_enc(s);
	SM_TEST_E(SM_SUCCESS == r);

	r = check_v(s);
	SM_TEST_E(SM_SUCCESS == r);
	check_v2(s);
	SM_TEST_E(SM_SUCCESS == r);

	uiv[0] = V1;
	uiv[1] = V2;
	uiv[2] = V3;
	r = sm_rcb_putrec(s, RCB_PUTR_DFLT, 0, SMAXLEN,
			SM_RCBV_INTA, RT, 3, uiv,
			SM_RCBV_END);
	SM_TEST_E(SM_SUCCESS == r);

	r = check_v(s);
	SM_TEST_E(SM_SUCCESS == r);
	check_v2(s);
	SM_TEST_E(SM_SUCCESS == r);

  error:
	SM_RCB_FREE(s);
#endif /* MTA_USE_RCBV_INTN */
}

int
main(int argc, char *argv[])
{
	sm_rpool_P a;

	sm_test_begin(argc, argv, "test rcb 4");

	/* create an rpool for entire test */
	a = sm_rpool_new(NULL);
	SM_TEST(a != NULL);
	test_harness(a);
	sm_rpool_delete(a);

	/* test without rpool */
	test_harness(NULL);
	return sm_test_end();
}


syntax highlighted by Code2HTML, v. 0.9.1