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

WFVarArgsTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2011 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated 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 Systems Incorporated.

------------------------------------------------------------------------------]]

local WFVarArgsTest = {}

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

local assert = assert

-- local KSTestUtils = require 'KSTestUtils'

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

function WFVarArgsTest.testMap()

	local WFVarArgs = require 'WFVarArgs'
	
	local function addOne( x ) return x + 1 end
	
	local result = { WFVarArgs.map( addOne, 0, 1, 2, 3 ) }
	
	for i = 1, #result do
		assert( i == result[ i ], "Value mismatch at position " .. i )
	end

end

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

function WFVarArgsTest.testMapMultireturn()

	local WFVarArgs = require 'WFVarArgs'
	
	local function succ_seq( x ) return x, x + 1 end
	
	local result = { WFVarArgs.map( succ_seq, 1, 2, 3, 4 ) }
	
	for i = 1, #result do
		assert( i == result[ i ], "Value mismatch at position " .. i )
	end

end

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

function WFVarArgsTest.testMapWithNils()

	local WFVarArgs = require 'WFVarArgs'
	
	local function returnNil( x ) return nil end
	
	local function countAndWrap( ... )
		return select( '#', ... ), { ... }
	end
	
	local count, wrapped = countAndWrap( WFVarArgs.map( returnNil, 1, 2, 3, 4 ) )
	
	assert( count == 4, "Wrong number of values returned" )
	
	assert( next( wrapped ) == nil, "result should have been empty" )

end

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

function WFVarArgsTest.testMapWithEmptyReturns()

	local WFVarArgs = require 'WFVarArgs'
	
	local function returnNothing( x ) end
	
	local function countAndWrap( ... )
		return select( '#', ... ), { ... }
	end
	
	local count, wrapped = countAndWrap( WFVarArgs.map( returnNothing, 1, 2, 3, 4 ) )
	
	assert( count == 3, "Wrong number of values returned" )
		-- Note that we should truncate the last value
	
	assert( next( wrapped ) == nil, "result should have been empty" )

end

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

function WFVarArgsTest.testMapWithSomeNils()

	local WFVarArgs = require 'WFVarArgs'
	
	local function returnNilForOdd( x ) if x % 2 == 0 then return x else return nil end end
	
	local function countAndWrap( ... )
		return select( '#', ... ), { ... }
	end
	
	local count, wrapped = countAndWrap( WFVarArgs.map( returnNilForOdd, 1, 2, 3, 4 ) )
	
	assert( count == 4, "Wrong number of values returned" )
	
	assert( wrapped[ 1 ] == nil, "Bad value for position 1" )
	assert( wrapped[ 2 ] == 2, "Bad value for position 2" )
	assert( wrapped[ 3 ] == nil, "Bad value for position 3" )
	assert( wrapped[ 4 ] == 4, "Bad value for position 4" )

end

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

function WFVarArgsTest.testFoldl()

	local WFVarArgs = require 'WFVarArgs'
	
	local function add( a, b ) return a + b end
	
	assert( 10 == WFVarArgs.foldl( add, 0, 1, 2, 3, 4 ), "Bad sum" )

end

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

function WFVarArgsTest.testFindFirst()

	local WFVarArgs = require 'WFVarArgs'
	
	local function isOdd( n ) return n % 2 == 1 end
	
	assert( 5 == WFVarArgs.findFirst( isOdd, 0, 2, 4, 5, 6, 7, 10 ),
			"Failed to find correct value" )

	assert( 1 == WFVarArgs.findFirst( isOdd, 1, 2, 4, 5, 6, 7, 10 ),
			"Failed to find value in first position." )
		-- First position

	assert( 13 == WFVarArgs.findFirst( isOdd, 0, 2, 4, 6, 13 ),
			"Failed to find value in last position." )
		-- Last position

	assert( select( '#', WFVarArgs.findFirst( isOdd ) ) == 0,
			"Too many results when there were no arguments." )
		-- No arguments
	
	assert( select( '#', WFVarArgs.findFirst( isOdd, 0, 2, 4, 6 ) ) == 0,
			"Should not have returned value when none found." )
		-- Not found

end

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

return WFVarArgsTest
