'--------------------------------------------------------------------
' Test case of SPrintf Function and etc...
'--------------------------------------------------------------------

Imports ActiveBasic.Strings
Imports ActiveBasic.Strings.Detail

Namespace SPrintFTest

Sub FormatTest()
	Dim s = Nothing As String, e As Long, sign As Boolean

	s = FloatToChars(1., e, sign)
	UnitTest("FloatToChars(1)", s = "10000000000000000" And e = 0 And sign = False)
	s = FloatToChars(-93.75e-3, e, sign)
	UnitTest("FloatToChars(-93.75e-3)", s = "93750000000000000" And e = 1 - 3 And sign = True)
	s = FloatToChars(.0, e, sign)
	UnitTest("FloatToChars(0)", s = "00000000000000000" And e = 0 And sign = False)

	s = FormatFloatE(9.876543e021, 6, 0, None)
	UnitTest("FormatFloatE(9876543e+21)", s, "9.876543e+21")
	s = FormatFloatE(7.81250000E-03, 8, 0, Cap)
	UnitTest("FormatFloatE(7.81250000E-03)", s, "7.81250000E-03")

	s = FormatFloatE(1.2345e67, 4, 15, 0)
	UnitTest("FormatFloatE(1.2345e+67, field width = 15)", s, "     1.2345e+67")
	s = FormatFloatE(1.2345e67, 4, 15, Zero)
	UnitTest("FormatFloatE(1.2345e+67, field width = 15, Zero)", s, "000001.2345e+67")
	s = FormatFloatE(-1.2345e67, 4, 15, 0)
	UnitTest("FormatFloatE(-1.2345e+67, field width = 15)", s, "    -1.2345e+67")
	s = FormatFloatE(-1.2345e67, 4, 15, Zero)
	UnitTest("FormatFloatE(-1.2345e+67, field width = 15, Zero)", s, "-00001.2345e+67")
	s = FormatFloatE(1.2345e67, 4, 15, Sign)
	UnitTest("FormatFloatE(1.2345e+67, field width = 15, Sign)", s, "    +1.2345e+67")
	s = FormatFloatE(1.2345e67, 4, 15, Zero Or Sign)
	UnitTest("FormatFloatE(1.2345e+67, field width = 15, Zero Or Sign)", s, "+00001.2345e+67")
	s = FormatFloatE(1.2345e67, 4, 15, Zero Or Blank)
	UnitTest("FormatFloatE(1.2345e+67, field width = 15, Zero Or Blank)", s, " 00001.2345e+67")
	s = FormatFloatE(-1.2345e67, 4, 15, Zero Or Sign)
	UnitTest("FormatFloatE(-1.2345e+67, field width = 15, Zero Or Sign)", s, "-00001.2345e+67")
	s = FormatFloatE(-1.2345e67, 4, 15, Zero Or Blank)
	UnitTest("FormatFloatE(-1.2345e+67, field width = 15, Zero Or Blank)", s, "-00001.2345e+67")

	s = FormatFloatE(1.2345e+67, 4, 0, Cap)
	UnitTest("FormatFloatE(1.2345E+67, Cap)", s, "1.2345E+67")
	s = FormatFloatE(1.2345e67, 3, 0, None)
	UnitTest("FormatFloatE(1.2345e+67, precision = 3)", s, "1.234e+67")

	s = FormatIntegerU(777, 0, 0, None)
	UnitTest("FormatIntegerU(777)", s, "777")
	s = FormatIntegerU(513, 0, 5, None)
	UnitTest("FormatIntegerU(513, field width = 5)", s, "  513")

	s = FormatIntegerD(-3, 2, 0, Sign)
	UnitTest("FormatIntegerD(-3, precision = 2)", s, "-03")
	s = FormatIntegerD(3, 0, 5, Sign)
	UnitTest("FormatIntegerD(+3, field width = 5)", s, "   +3")

	s = FormatIntegerO(&o1234567, DWORD_MAX, 0, None)
	UnitTest("FormatIntegerO(&o1234567)", s, "1234567")
	s = FormatIntegerO(&o1234567, DWORD_MAX, 0, Alt)
	UnitTest("FormatIntegerO(&o1234567, Alt)", s, "01234567")
	s = FormatIntegerO(0, DWORD_MAX, 0, Alt)
	UnitTest("FormatIntegerO(0, Alt)", s, "0")

	s = FormatIntegerX(&hffff, DWORD_MAX, 0, None)
	UnitTest("FormatIntegerX(&hffff)", s, "ffff")
	s = FormatIntegerX(&hffff, DWORD_MAX, 0, Cap)
	UnitTest("FormatIntegerX(&hffff, Cap)", s, "FFFF")
	s = FormatIntegerX(&h12345678, DWORD_MAX, 0, Alt)
	UnitTest("FormatIntegerX(&h12345678, Alt)", s, "0x12345678")
	s = FormatIntegerX(1, 2, 0, Alt Or Cap)
	UnitTest("FormatIntegerX(1, precision = 2, Alt, Cap)", s, "0X01")
	s = FormatIntegerX(0, 4, 0, Alt)
	UnitTest("FormatIntegerX(0, precision = 4, Alt)", s, "0000")

	s = FormatFloatF(1, 2, 0, None)
	UnitTest("FormatFloatF(1.00, precision = 2)", s, "1.00")
	s = FormatFloatF(123.456, 3, 0, None)
	UnitTest("FormatFloatF(123.456, precision = 3)", s, "123.456")
	s = FormatFloatF(2., 0, 0, Alt)
	UnitTest("FormatFloatF(2., precision = 0, Alt", s, "2.")
	s = FormatFloatF(-1.234e-1, 3, 0, None)
	UnitTest("FormatFloatF(-1.234e-1, precision = 3) = -0.123", s, "-0.123")
	s = FormatFloatF(-1.234e-2, 3, 0, None)
	UnitTest("FormatFloatF(-1.234e-2, precision = 3) = -0.012", s, "-0.012")
	s = FormatFloatF(-1.234e-3, 3, 0, None)
	UnitTest("FormatFloatF(-1.234e-3, precision = 3) = -0.001", s, "-0.001")
	s = FormatFloatF(-1.234e-4, 3, 0, None)
	UnitTest("FormatFloatF(-1.234e-4, precision = 3) = -0.000", s, "-0.000")
	'現状では末尾の桁の誤差を制御しきれないので、文字列全体の一致比較を避けている。
	s = FormatFloatF(12345678901234567e3, 2, 0, None)
	UnitTest("FormatFloatF(1.00, precision = 2)", Right$(s, 6) = "000.00")

	s = FormatIntegerLO(&hFFFFFFFFFFFFFFFF, 0, 0, Alt)
	UnitTest("FormatIntegerLO(&hFFFFFFFFFFFFFFFF, Alt) = 01777777777777777777777", s, "01777777777777777777777")
	s = FormatIntegerLX(&h123456789abcdef0, 0, 0, None)
	UnitTest("FormatIntegerLX(&h123456789abcdef0)", s, "123456789abcdef0")
	s = FormatIntegerLU(8589934590 As QWord, DWORD_MAX, 0, None)
	UnitTest("FormatIntegerLU(8589934590)", s, "8589934590")
	s = FormatIntegerLD(-8589934590, DWORD_MAX, 0, None)
	UnitTest("FormatIntegerLD(-8589934590)", s, "-8589934590")

	s = FormatFloatG(1.25, DWORD_MAX, 0, None)
	UnitTest("FormatFloatG(1.25)", s, "1.25")
	s = FormatFloatG(0.1, 2, 0, None)
	UnitTest("FormatFloatG(0.1, precision = 2)", s, "0.1")
	s = FormatFloatG(1.2e+60, DWORD_MAX, 0, None)
	UnitTest("FormatFloatG(1.2e+60)", s, "1.2e+60")
	s = FormatFloatG(1.2e-60, DWORD_MAX, 0, None)
	UnitTest("FormatFloatG(1.2e-60)", s, "1.2e-60")
	s = FormatFloatG(1.0, 2, 0, Alt)
	UnitTest("FormatFloatG(1.0, precision = 2, Alt)", s, "1.0")
	s = FormatFloatG(0.01, 2, 0, Alt)
	UnitTest("FormatFloatG(0.01, precision = 2, Alt)", s, "0.010")
	s = FormatFloatG(0.000123456, 4, 0, None)
	UnitTest("FormatFloatG(0.000123456, precision = 4) = 0.0001234", s, "0.0001234")
	s = FormatFloatG(0.0000123456, 4, 0, None)
	UnitTest("FormatFloatG(0.0000123456, precision = 4) = 1.234e-05", s, "1.234e-05")

	s = FormatFloatA(1, DWORD_MAX, 0, None)
	UnitTest("FormatFloatA(1)", s, "0x1p+0")
	s = FormatFloatA(2.2250738585072014e-308, DWORD_MAX, 0, None)
	UnitTest("FormatFloatA(2.2250738585072014e-308) = 0x1p-1022", s, "0x1p-1022")
	s = FormatFloatA(1.11253692925360070e-308, DWORD_MAX, 0, None)
	UnitTest("FormatFloatA(1.11253692925360070e-308) = 0x0.fp-1022", s, "0x0.8p-1022")
	s = FormatFloatA(3.1415926535897932384626433832795, DWORD_MAX, 0, None)
	UnitTest("FormatFloatA(π) = 0x1.921fb54442d18p+1", s, "0x1.921fb54442d18p+1")
	s = FormatFloatA(0, DWORD_MAX, 0, None)
	UnitTest("FormatFloatA(0)", s, "0x0p+0")

	s = FormatString("123456", DWORD_MAX, 0, None)
	UnitTest(Ex"FormatString(\q123456\q)", s, "123456")
	s = FormatString("123456", 3, 0, None)
	UnitTest(Ex"FormatString(\q123456\q, precision = 3) = \q123\q", s, "123")
	s = FormatString("123456", 3, 6, None)
	UnitTest(Ex"FormatString(\q123456\q, precision = 3, field width = 6) = \q   123\q",
		s, "   123")

	s = FormatCharacter(Asc("Y"), DWORD_MAX, 0, None)
	UnitTest(Ex"FormatCharacter(\qY\q)", s, "Y")
	s = FormatCharacter(Asc("I"), DWORD_MAX, 3, LeftSide)
	UnitTest(Ex"FormatCharacter(\qI\q, Left)", s, "I  ")
End Sub

Sub TestMain()
	FormatTest()

	Imports System

	UnitTest(Ex"SPrintf(\q123\q)", ActiveBasic.Strings.Detail.SPrintf("'123'", 0, 0), "'123'")
	UnitTest(Ex"%s, \qhello\q", SPrintf("'%s'", "hello"), "'hello'")
	UnitTest(Ex"%7s, \qhello\q", SPrintf("'%7s'", "hello"), "'  hello'")
	UnitTest(Ex"%*s, -7, \qhello\q", SPrintf("'%*s'", New Int32(-7), "hello"), "'hello  '")
	UnitTest(Ex"%.3s, \qhello\q", SPrintf("'%.3s'", "hello"), "'hel'")
	UnitTest(Ex"%.*s, 4, \qhello\q", SPrintf("'%.*s'", New Int32(4), "hello"), "'hell'")
	UnitTest(Ex"%-5.3s, \qhello\q", SPrintf("'%-5.3s'", "hello"), "'hel  '")
	UnitTest(Ex"%*.*s, 2, 1, \qhello\q", SPrintf("'%*.*s'", New Int32(2), New Int32(1), "hello"), "' h'")
#ifdef UNICODE
	UnitTest(Ex"%c, '0'", SPrintf("'%c'", New System.UInt16(&h30)), "'0'")
#else
	UnitTest(Ex"%c, '0'", SPrintf("'%c'", New System.SByte(&h30)), "'0'")
#endif
	Dim i = Nothing As Object, mi = Nothing As Object
	i = New Int32(10)
	mi = New Int32(-10)
	UnitTest(Ex"%d, 10", SPrintf("'%d'", i), "'10'")
	UnitTest(Ex"%+d, 10", SPrintf("'%+d'", i), "'+10'")
	UnitTest(Ex"% d, 10", SPrintf("'% d'", i), "' 10'")
	UnitTest(Ex"%4d, 10", SPrintf("'%4d'", i), "'  10'")
	UnitTest(Ex"%4d, -10", SPrintf("'%4d'", mi), "' -10'")
	UnitTest(Ex"%04d, 10", SPrintf("'%04d'", i), "'0010'")
	UnitTest(Ex"%04d, -10", SPrintf("'%04d'", mi), "'-010'")
	UnitTest(Ex"%5.3d, 10", SPrintf("'%5.3d'", i), "'  010'")

	UnitTest(Ex"%i, 10", SPrintf("'%i'", i), "'10'")
	UnitTest(Ex"%ld, -4294967296", SPrintf("'%ld'", New System.Int64(-4294967296)), "'-4294967296'")
	UnitTest(Ex"%li, -4294967296", SPrintf("'%li'", New System.Int64(-4294967296)), "'-4294967296'")
	UnitTest(Ex"%u, 4294967295", SPrintf("'%u'", New UInt32(4294967295)), "'4294967295'")
	UnitTest(Ex"%lu, 4294967296", SPrintf("'%lu'", New UInt64(4294967296)), "'4294967296'")
	UnitTest(Ex"%x, ffffffff", SPrintf("'%x'", New UInt32(4294967295)), "'ffffffff'")
	UnitTest(Ex"%#x, 0xffffffff", SPrintf("'%#x'", New UInt32(4294967295)), "'0xffffffff'")
	UnitTest(Ex"%&x, &hffffffff", SPrintf("'%&x'", New UInt32(4294967295)), "'&hffffffff'")
	UnitTest(Ex"%lx, 12345789abcdef", SPrintf("'%lx'", New UInt64(&h12345789abcdef)), "'12345789abcdef'")
	UnitTest(Ex"%lX, 12345789ABCDEF", SPrintf("'%lX'", New UInt64(&h12345789abcdef)), "'12345789ABCDEF'")
	Dim o777 = Nothing As Object
	o777 = New UInt32(&o777)
	UnitTest(Ex"%o, 777", SPrintf("'%o'", o777), "'777'")
	UnitTest(Ex"%#o, 0777", SPrintf("'%#o'", o777), "'0777'")
	UnitTest(Ex"%&o, &o777", SPrintf("'%&o'", o777), "'&o777'")
	UnitTest(Ex"%&O, &O777", SPrintf("'%&O'", o777), "'&O777'")
	UnitTest(Ex"%lo, 100000000000", SPrintf("'%lo'", New UInt64(&o100000000000)), "'100000000000'")

	Dim d = Nothing As Object
	d = New System.Double(123.25)
	UnitTest(Ex"%g, 123.25", SPrintf("'%g'", d), "'123.25'")
	UnitTest(Ex"%f, 123.25", SPrintf("'%f'", d), "'123.250000'")
	UnitTest(Ex"%e, 123.25", SPrintf("'%e'", d), "'1.232500e+02'")
	UnitTest(Ex"%a, 123.25", SPrintf("'%a'", d), "'0x1.edp+6'")

	UnitTest(Ex"%%", SPrintf("'%%'", d), "'%'")
End Sub

End Namespace 'SPrintFTest

SPrintFTest.TestMain()
