/*
    Copyright (C) 2008-2009, Millistream Market Data <support@millistream.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/* This is a test that out endianess swap functions works as advertised */

#include "../src/common.h"

int main (int argc, char **argv)
{
	/* how the values should be on a little-endian machine: */
	uint8_t ui16_value[] = { 0x34, 0x12 };
	uint8_t ui32_value[] = { 0x78, 0x56, 0x34, 0x12 };
	uint8_t ui64_value[] = { 0xEF, 0xCD, 0xAB, 0x90, 0x78, 0x56, 0x34, 0x12 };

	uint16_t ui16;
	uint32_t ui32;
	uint64_t ui64;

	int fail = 0;

	/* let's test cpu_to_lexx() */
	ui16 = cpu_to_le16 (0x1234);
	ui32 = cpu_to_le32 (0x12345678);
	ui64 = cpu_to_le64 (0x1234567890ABCDEF);

	if (memcmp (ui16_value, &ui16, sizeof ui16) != 0) {
		printf ("cpu_to_le16 failed\n");
		fail = 1;
	}

	if (memcmp (ui32_value, &ui32, sizeof ui32) != 0) {
		printf ("cpu_to_le32 failed\n");
		fail = 1;
	}

	if (memcmp (ui64_value, &ui64, sizeof ui64) != 0) {
		printf ("cpu_to_le64 failed\n");
		fail = 1;
	}

	/* and now we test lexx_to_cpu() */
	ui16 = le16_to_cpu (*((uint16_t *)ui16_value));
	ui32 = le32_to_cpu (*((uint32_t *)ui32_value));
	ui64 = le64_to_cpu (*((uint64_t *)ui64_value));

	if (ui16 != 0x1234) {
		printf ("le16_to_cpu failed\n");
		fail = 1;
	}

	if (ui32 != 0x12345678) {
		printf ("le32_to_cpu failed\n");
		fail = 1;
	}

	if (ui64 != 0x1234567890ABCDEF) {
		printf ("le64_to_cpu failed\n");
		fail = 1;
	}

	return fail;
}

