/*
This program is distributed under the terms of the 'MIT license'. The text
of this licence follows...

Copyright (c) 2007-2009 J.D.Medhurst (a.k.a. Tixy)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
@file
@brief	Test code for string formatting utilities. Main test function is #TestStringF()
@test
*/

#include "common.h"
#include "test/test.h"
#include "../stringf.h"



//
// Testing helpers
//

static bool Match(const char* start,int size,const char* expected)
	{
	while(size>0)
		{
		if(!*expected || *start!=*expected)
			{
			BREAKPOINT;
			return false;
			}
		++start;
		++expected;
		--size;
		}
	if(*expected!=0)
		{
		BREAKPOINT;
		return false;
		}
	if(size!=0)
		{
		BREAKPOINT;
		return false;
		}
	return true;
	}

#define CHECK(expected) (void)(Match(buffer,size,expected)||TEST_FAILED)
#define CHECK_EQ(a,b)	(void)((a)==(b)||TEST_FAILED)


static int SPrintF(char* str, const char* format, ...)
	{
	va_list args;
	va_start(args,format);
	int size = vsprintf(str,format,args);
	va_end(args);
	return size;
	}


static int HexDumpLine(char* dst, const void* data, size_t size, ptrdiff_t addressOffset)
	{
	StringBufferFormatter formatter(dst,StringFormatter::HexDumpLineSize);
	formatter.HexDumpLine(data,size,addressOffset);
	return formatter.End()-dst;
	}



//
// Top level test function
//

void TestStringF()
	{
	char buffer[256];
	int size;

	// test plain text...

	size = SPrintF(buffer,"");
	CHECK("");
	size = SPrintF(buffer,"a%%d");
	CHECK("a%d");

	// test decimal integers...

	size = SPrintF(buffer,"%u,%u,%u,%u",0,1,1234567890,4294967295u);
	CHECK("0,1,1234567890,4294967295");
	size = SPrintF(buffer,"%d,%d,%d,%d",0,1,1234567890,2147483647);
	CHECK("0,1,1234567890,2147483647");
	size = SPrintF(buffer,"%d,%d,%d,%d",0,-1,-1234567890,-2147483647-1);
	CHECK("0,-1,-1234567890,-2147483648");
	size = SPrintF(buffer,"%i,%i,%i,%i",0,1,1234567890,2147483647);
	CHECK("0,1,1234567890,2147483647");
	size = SPrintF(buffer,"%i,%i,%i,%i",0,-1,-1234567890,-2147483647-1);
	CHECK("0,-1,-1234567890,-2147483648");

	// test hex integers...

	size = SPrintF(buffer,"%x,%x,%x,%x",0,1,0x89abcdef,0xffffffff);
	CHECK("0,1,89abcdef,ffffffff");
	size = SPrintF(buffer,"%X,%X,%X,%X",0,1,0x89abcdef,0xffffffff);
	CHECK("0,1,89ABCDEF,FFFFFFFF");
	size = SPrintF(buffer,"%#x,%#x,%#x,%#x",0,1,0x89abcdef,0xffffffff);
	CHECK("0,0x1,0x89abcdef,0xffffffff");

	// test octal integers...

	size = SPrintF(buffer,"%o,%o,%o,%o",0,1,01234567,037777777777);
	CHECK("0,1,1234567,37777777777");
	size = SPrintF(buffer,"%#o,%#o,%#o,%#o",0,1,01234567,037777777777);
	CHECK("0,01,01234567,037777777777");

	// test sign character specifiers (what's the correct sign for zero?)...

	size = SPrintF(buffer,"%+d,%+d,% d,% d",0,2147483647,0,2147483647);
	CHECK("+0,+2147483647, 0, 2147483647");
	size = SPrintF(buffer,"% +d,% +d,%+ d,%+ d",0,2147483647,0,2147483647);
	CHECK("+0,+2147483647,+0,+2147483647");

	// test different sized integers...

	ASSERT_COMPILE(sizeof(short)==sizeof(int16_t));
	size = SPrintF(buffer,"%hd,%hd,%hd,%hd,%hd",0,1,-1,32767,-32768);
	CHECK("0,1,-1,32767,-32768");
	size = SPrintF(buffer,"%hd,%hd,%hd,%hd,%hd",0^0x10000,1^0x10000,-1^0x10000,32767^0x10000,-32768^0x10000);
	CHECK("0,1,-1,32767,-32768");

	ASSERT_COMPILE(sizeof(char)==sizeof(int8_t));
	size = SPrintF(buffer,"%hhd,%hhd,%hhd,%hhd,%hhd",0,1,-1,127,-128);
	CHECK("0,1,-1,127,-128");
	size = SPrintF(buffer,"%hhd,%hhd,%hhd,%hhd,%hhd",0^0x100,1^0x100,-1^0x100,127^0x100,-128^0x100);
	CHECK("0,1,-1,127,-128");

	size = SPrintF(buffer,"%ld,%ld,%ld,%ld,%ld",(long)0,(long)1,-(long)1,(long)2147483647,-(long)2147483647-1);
	CHECK("0,1,-1,2147483647,-2147483648");
	size = SPrintF(buffer,"%lld,%lld,%lld,%lld,%lld",(longlong)0,(longlong)1,-(longlong)1,(longlong)2147483647,-(longlong)2147483647-1);
	CHECK("0,1,-1,2147483647,-2147483648");

	ASSERT_COMPILE(sizeof(longlong)==sizeof(int64_t));
	size = SPrintF(buffer,"%lld,%lld,%llu",(~(ulonglong)0)>>1,~((~(ulonglong)0)>>1),~(ulonglong)0);
	CHECK("9223372036854775807,-9223372036854775808,18446744073709551615");
	size = SPrintF(buffer,"%llx,%llx,%llx",(~(ulonglong)0)>>1,~((~(ulonglong)0)>>1),~(ulonglong)0);
	CHECK("7fffffffffffffff,8000000000000000,ffffffffffffffff");
	size = SPrintF(buffer,"%llo,%llo,%llo",(~(ulonglong)0)>>1,~((~(ulonglong)0)>>1),~(ulonglong)0);
	CHECK("777777777777777777777,1000000000000000000000,1777777777777777777777");

	ASSERT_COMPILE(sizeof(intmax_t)==sizeof(int64_t));
	size = SPrintF(buffer,"%lld,%lld,%llu",(~(ulonglong)0)>>1,~((~(ulonglong)0)>>1),~(ulonglong)0);
	CHECK("9223372036854775807,-9223372036854775808,18446744073709551615");

	ASSERT_COMPILE(sizeof(size_t)==sizeof(int32_t));
	size = SPrintF(buffer,"%zd,%zd,%zd,%zd,%zd",(size_t)0,(size_t)1,-(size_t)1,(size_t)2147483647,-(size_t)2147483647-1);
	CHECK("0,1,-1,2147483647,-2147483648");

	ASSERT_COMPILE(sizeof(ptrdiff_t)==sizeof(int32_t));
	size = SPrintF(buffer,"%td,%td,%td,%td,%td",(ptrdiff_t)0,(ptrdiff_t)1,-(ptrdiff_t)1,(ptrdiff_t)2147483647,-(ptrdiff_t)2147483647-1);
	CHECK("0,1,-1,2147483647,-2147483648");

	// test characters and strings (assumes 'l' type modifier is ignored)...

	ASSERT_COMPILE(sizeof(char)==sizeof(int8_t));
	size = SPrintF(buffer,"%c%c%lc",'Q','*'+0x100,'A'+0x100);
	CHECK("Q*A");
	size = SPrintF(buffer,"%s%s%ls","","abc","xyz");
	CHECK("abcxyz");

	// test %p...

	ASSERT_COMPILE(sizeof(void*)==sizeof(uint32_t));
	size = SPrintF(buffer,"%p",(void*)0);
	CHECK("00000000");
	size = SPrintF(buffer,"%p",(void*)0x78abcdef);
	CHECK("78abcdef");

	// test %n...

	{
	int l[4] = { ~0,~0,~0,~0 };
	size = SPrintF(buffer,"%n%d%n",l,123,l+2);
	CHECK("123");
	CHECK_EQ(l[0],0);
	CHECK_EQ(l[1],~0);
	CHECK_EQ(l[2],3);
	CHECK_EQ(l[3],~0);
	}

	{
	short l[4] = { ~0,~0,~0,~0 };
	size = SPrintF(buffer,"%hn%d%hn",l,123,l+2);
	CHECK("123");
	CHECK_EQ(l[0],0);
	CHECK_EQ(l[1],~0);
	CHECK_EQ(l[2],3);
	CHECK_EQ(l[3],~0);
	}

	{
	char l[4] = { ~0,~0,~0,~0 };
	size = SPrintF(buffer,"%hhn%d%hhn",l,123,l+2);
	CHECK("123");
	CHECK_EQ(l[0],0);
	CHECK_EQ(l[1],~0);
	CHECK_EQ(l[2],3);
	CHECK_EQ(l[3],~0);
	}

	{
	long l[4] = { ~0l,~0l,~0l,~0l };
	size = SPrintF(buffer,"%ln%d%ln",l,123,l+2);
	CHECK("123");
	CHECK_EQ(l[0],0l);
	CHECK_EQ(l[1],~0l);
	CHECK_EQ(l[2],3l);
	CHECK_EQ(l[3],~0l);
	}

	{
	longlong l[4] = { ~(longlong)0,~(longlong)0,~(longlong)0,~(longlong)0 };
	size = SPrintF(buffer,"%lln%d%lln",l,123,l+2);
	CHECK("123");
	CHECK_EQ(l[0],(longlong)0);
	CHECK_EQ(l[1],~(longlong)0);
	CHECK_EQ(l[2],(longlong)3);
	CHECK_EQ(l[3],~(longlong)0);
	}

	// test padding...

	size = SPrintF(buffer,"%04u,%04u,%04u,%04u",0,123,1234,12345);
	CHECK("0000,0123,1234,12345");
	size = SPrintF(buffer,"%04d,%04d,%04d,%04d",0,123,1234,12345);
	CHECK("0000,0123,1234,12345");
	size = SPrintF(buffer,"%04d,%04d,%04d,%04d",-12,-123,-1234,-12345);
	CHECK("-012,-123,-1234,-12345");
	size = SPrintF(buffer,"%04x,%04x,%04x,%04x",0,0x123,0x1234,0x12345);
	CHECK("0000,0123,1234,12345");
	size = SPrintF(buffer,"%#06x,%#06x,%#06x,%#06x",0,0x123,0x1234,0x12345);
	CHECK("000000,0x0123,0x1234,0x12345");
	size = SPrintF(buffer,"%04o,%04o,%04o,%04o",0,0123,01234,012345);
	CHECK("0000,0123,1234,12345");
	size = SPrintF(buffer,"%#05o,%#05o,%#05o,%#05o",0,0123,01234,012345);
	CHECK("00000,00123,01234,012345");

	size = SPrintF(buffer,"%-4d,%-4d,%-4d,%-4d",0,123,1234,12345);
	CHECK("0   ,123 ,1234,12345");
	size = SPrintF(buffer,"%-4d,%-4d,%-4d,%-4d",-12,-123,-1234,-12345);
	CHECK("-12 ,-123,-1234,-12345");
	size = SPrintF(buffer,"%#-6x,%#-6x,%#-6x,%#-6x",0,0x123,0x1234,0x12345);
	CHECK("0     ,0x123 ,0x1234,0x12345");
	size = SPrintF(buffer,"%#-5o,%#-5o,%#-5o,%#-5o",0,0123,01234,012345);
	CHECK("0    ,0123 ,01234,012345");

	size = SPrintF(buffer,"%4d,%4d,%4d,%4d",0,123,1234,12345);
	CHECK("   0, 123,1234,12345");
	size = SPrintF(buffer,"%4d,%4d,%4d,%4d",-12,-123,-1234,-12345);
	CHECK(" -12,-123,-1234,-12345");
	size = SPrintF(buffer,"%#6x,%#6x,%#6x,%#6x",0,0x123,0x1234,0x12345);
	CHECK("     0, 0x123,0x1234,0x12345");
	size = SPrintF(buffer,"%#5o,%#5o,%#5o,%#5o",0,0123,01234,012345);
	CHECK("    0, 0123,01234,012345");

	size = SPrintF(buffer,"%2s,%2s,%2s,%2s","","a","bc","xyz");
	CHECK("  , a,bc,xyz");
	size = SPrintF(buffer,"%-2s,%-2s,%-2s,%-2s","","a","bc","xyz");
	CHECK("  ,a ,bc,xyz");
	size = SPrintF(buffer,"%1c,%2c,%-1c,%-2c",'a','b','c','d');
	CHECK("a, b,c,d ");

	// test precission...

	size = SPrintF(buffer,"%.4u,%.4u,%.4u,%.4u",0,123,1234,12345);
	CHECK("0000,0123,1234,12345");
	size = SPrintF(buffer,"%.4d,%.4d,%.4d,%.4d",0,123,1234,12345);
	CHECK("0000,0123,1234,12345");
	size = SPrintF(buffer,"%.4d,%.4d,%.4d,%.4d",-12,-123,-1234,-12345);
	CHECK("-0012,-0123,-1234,-12345");
	size = SPrintF(buffer,"%.4x,%.4x,%.4x,%.4x",0,0x123,0x1234,0x12345);
	CHECK("0000,0123,1234,12345");
	size = SPrintF(buffer,"%#.4x,%#.4x,%#.4x,%#.4x",0,0x123,0x1234,0x12345);
	CHECK("0000,0x0123,0x1234,0x12345");
	size = SPrintF(buffer,"%04o,%04o,%04o,%04o",0,0123,01234,012345);
	CHECK("0000,0123,1234,12345");
	size = SPrintF(buffer,"%#.5o,%#.5o,%#.5o,%#.5o",0,0123,01234,012345);
	CHECK("00000,00123,01234,012345");

	size = SPrintF(buffer,"%06.4d,%06.4d,%06.4d,%06.4d",0,123,1234,12345);
	CHECK("  0000,  0123,  1234, 12345");
	size = SPrintF(buffer,"%06.4d,%06.4d,%06.4d,%06.4d",-12,-123,-1234,-12345);
	CHECK(" -0012, -0123, -1234,-12345");

	size = SPrintF(buffer,"%.0u,%.0u,%.0u,%.0u",0,1,12,123);
	CHECK(",1,12,123");
	size = SPrintF(buffer,"%.u,%.u,%.u,%.u",0,1,12,123);
	CHECK(",1,12,123");

	size = SPrintF(buffer,"%#.0x,%#.0x,%#.4x,%#.4x",0,0x123,0x1234,0x12345);
	CHECK(",0x123,0x1234,0x12345");
	size = SPrintF(buffer,"%.2s%.0s%.2s%.4s","","q","abc","xyz");
	CHECK("abxyz");

	// test '*' ...

	size = SPrintF(buffer,"%.*u,%.*u,%.*u,%.*u",4,0,4,123,4,1234,4,12345);
	CHECK("0000,0123,1234,12345");
	size = SPrintF(buffer,"%.*u,%.*u,%.*u,%.*u",-4,0,-4,123,-4,1234,-4,12345);
	CHECK("0,123,1234,12345");
	size = SPrintF(buffer,"%*u,%*u,%*u,%*u",4,0,4,123,4,1234,4,12345);
	CHECK("   0, 123,1234,12345");
	size = SPrintF(buffer,"%*u,%*u,%*u,%*u",-4,0,-4,123,-4,1234,-4,12345);
	CHECK("0   ,123 ,1234,12345");
	size = SPrintF(buffer,"%*.*u,%*.*u",6,4,123,-6,4,123);
	CHECK("  0123,0123  ");

	// test sign character specifiers with width and precission...

	size = SPrintF(buffer,"%+3d,%+3d,%+3d",-1,0,1);
	CHECK(" -1, +0, +1");
	size = SPrintF(buffer,"%+-3d,%+-3d,%+-3d",-1,0,1);
	CHECK("-1 ,+0 ,+1 ");
	size = SPrintF(buffer,"%+03d,%+03d,%+03d",-1,0,1);
	CHECK("-01,+00,+01");
	size = SPrintF(buffer,"%+.0d,%+.0d,%+.0d",-1,0,1);
	CHECK("-1,+,+1");
	size = SPrintF(buffer,"% +.0d,% +.0d,% +.0d",-1,0,1);
	CHECK("-1,+,+1");

	// test bad format...

	size = SPrintF(buffer,"%");
	CHECK("");
	size = SPrintF(buffer,"%\0x");
	CHECK("");
	size = SPrintF(buffer,",%0.1");
	CHECK(",");
	size = SPrintF(buffer,",%0.1-");
	CHECK(",");

	// test hex dump...

	ASSERT_COMPILE(sizeof(void*)==sizeof(uint32_t));
	const char* dump = "\nbcdefg ijklmno\377";
	size = HexDumpLine(buffer, dump, 17, 0x12345678-(uintptr_t)dump);
	CHECK("12345678  0a 62 63 64  65 66 67 20  69 6a 6b 6c  6d 6e 6f ff  .bcdefg ijklmno.\n");
	size = HexDumpLine(buffer, dump, 15, 0x123456-(uintptr_t)dump);
	CHECK("00123456  0a 62 63 64  65 66 67 20  69 6a 6b 6c  6d 6e 6f     .bcdefg ijklmno \n");
	}

