--[[----------------------------------------------------------------------------

WFStructTest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2025 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe.

------------------------------------------------------------------------------]]
local math = require 'math'

local WFRxTestCase = require 'WFRxTestCase'

local WFStruct = require 'WFStruct'

local WFStructTest = {}

--------------------------------------------------------------------------------

function WFStructTest.rxTestCalcsize( rxTest )

	local size = WFStruct.calcsize('i')
	rxTest:expectEqual(size * 3, WFStruct.calcsize('iii'))
	rxTest:expectEqual(size * 3, WFStruct.calcsize('3i'))

	size = WFStruct.calcsize('cbxxxxxxhhhhiillffd?')
	local sz3 = WFStruct.calcsize('3c3b18x12h6i6l6f3d3?')
	rxTest:expectEqual(size * 3, sz3)

end

--------------------------------------------------------------------------------

function WFStructTest.rxTestIntSimple( rxTest )
	local format = 'i'
	local data = WFStruct.pack( format, 42 )
	local unpacked = WFStruct.unpack( format, data )

	rxTest:expectEqual( 42, unpacked )
end

--------------------------------------------------------------------------------

function WFStructTest.rxTestChar( rxTest )
	local format = 'c'
	local data = WFStruct.pack( format, 'a' )
	local unpacked = WFStruct.unpack( format, data )

	rxTest:expectEqual( 'a', unpacked )

	rxTest:expectError(function()
		WFStruct.pack( format, 'ab' )
	end)
end

--------------------------------------------------------------------------------

function WFStructTest.rxTestString( rxTest )
	local format = '3s'
	local data = WFStruct.pack( format, 'abc' )
	local unpacked = WFStruct.unpack( format, data )

	rxTest:expectEqual( 'abc', unpacked )

	data = WFStruct.pack( format, 'abcde' )
	unpacked = WFStruct.unpack( format, data )

	rxTest:expectEqual( 'abc', unpacked )
end

--------------------------------------------------------------------------------

function WFStructTest.rxTestPString( rxTest )
	local format = '3p'
	local data = WFStruct.pack( format, 'abc' )
	local unpacked = WFStruct.unpack( format, data )

	rxTest:expectEqual( 'abc', unpacked )

	data = WFStruct.pack( format, 'abcde' )
	unpacked = WFStruct.unpack( format, data )

	rxTest:expectEqual( 'abc', unpacked )

	local l, s = WFStruct.unpack( 'b3s', data )
	rxTest:expectEqual( 3, l )
	rxTest:expectEqual( 'abc', s )
end

--------------------------------------------------------------------------------

function WFStructTest.rxTestBool( rxTest )
	local format = '??'

	local data = WFStruct.pack( format, true, false )
	local a, b = WFStruct.unpack( format, data )
	rxTest:expectEqual( true, a )
	rxTest:expectEqual( false, b )
end

--------------------------------------------------------------------------------

function WFStructTest.rxTestByte( rxTest )
	local format = 'b'

	local data = WFStruct.pack( format, 42 )
	local unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 42, unpacked )

	data = WFStruct.pack( format, 127 )
	unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 127, unpacked )

	rxTest:expectError(function()
		WFStruct.pack( format, 128 )
	end)

	format = 'B'

	data = WFStruct.pack( format, 42 )
	unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 42, unpacked )

	data = WFStruct.pack( format, 255 )
	unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 255, unpacked )

	rxTest:expectError(function()
		WFStruct.pack( format, 256 )
	end)

end

--------------------------------------------------------------------------------

function WFStructTest.rxTestShort( rxTest )
	local format = 'h'

	local data = WFStruct.pack( format, 42 )
	local unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 42, unpacked )

	data = WFStruct.pack( format, 32767 )
	unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 32767, unpacked )

	rxTest:expectError(function()
		WFStruct.pack( format, 32768 )
	end)

	format = 'H'

	data = WFStruct.pack( format, 42 )
	unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 42, unpacked )

	data = WFStruct.pack( format, 65535 )
	unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 65535, unpacked )

	rxTest:expectError(function()
		WFStruct.pack( 65536, format )
	end)

end

--------------------------------------------------------------------------------

function WFStructTest.rxTestInt( rxTest )
	local format = 'i'

	local data = WFStruct.pack( format, 42 )
	local unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 42, unpacked )

	data = WFStruct.pack( format, 2147483647 )
	unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 2147483647, unpacked )

	rxTest:expectError(function()
		WFStruct.pack( format, 2147483648 )
	end)

	format = 'I'

	data = WFStruct.pack( format, 42 )
	unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 42, unpacked )

	data = WFStruct.pack( format, 4294967295 )
	unpacked = WFStruct.unpack( format, data )
	rxTest:expectEqual( 4294967295, unpacked )

	rxTest:expectError(function()
		WFStruct.pack( 4294967296, format )
	end)

end

--------------------------------------------------------------------------------

function WFStructTest.rxTestFloat( rxTest )
	local format = 'f'
	local data = WFStruct.pack( format, math.pi )
	local unpacked = WFStruct.unpack( format, data )

	local delta = math.pi - unpacked
	local success = delta < 0.000001

	rxTest:expectEqual( true, success )
end

--------------------------------------------------------------------------------

function WFStructTest.rxTestDouble( rxTest )
	local format = 'd'
	local data = WFStruct.pack( format, math.pi )
	local unpacked = WFStruct.unpack( format, data )

	rxTest:expectEqual( math.pi, unpacked )
end

--------------------------------------------------------------------------------

function WFStructTest.rxTestDoubles( rxTest )
	local format = '3d'
	local data = WFStruct.pack( format, 1.1, 2.2, math.pi )
	local unpacked = { WFStruct.unpack( format, data ) }

	rxTest:expectEqual( 3, #unpacked )

	rxTest:expectEqual( 1.1, unpacked[1] )
	rxTest:expectEqual( 2.2, unpacked[2] )
	rxTest:expectEqual( math.pi, unpacked[3] )
end

--------------------------------------------------------------------------------

function WFStructTest.rxTestMixed( rxTest )
	local format = 'dc3s'
	local data = WFStruct.pack( format, math.pi, 'a', 'foo' )
	local unpacked = { WFStruct.unpack( format, data ) }

	rxTest:expectEqual( 3, #unpacked )

	rxTest:expectEqual( math.pi, unpacked[1] )
	rxTest:expectEqual( 'a', unpacked[2] )
	rxTest:expectEqual( 'foo', unpacked[3] )
end

--------------------------------------------------------------------------------

function WFStructTest.rxTestUnderflow( rxTest )
	local format = 'ii'
	rxTest:expectError(function()
		WFStruct.pack( format, 42 )
	end)

	format = '2i'
	rxTest:expectError(function()
		WFStruct.pack( format, 42 )
	end)

end

--------------------------------------------------------------------------------

return WFRxTestCase.wrapSuite( 'WFStructTest', WFStructTest )
