Blame libfreerdp/primitives/test/TestPrimitivesSet.c

Packit 1fb8d4
/* test_set.c
Packit 1fb8d4
 * vi:ts=4 sw=4
Packit 1fb8d4
 *
Packit 1fb8d4
 * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Packit 1fb8d4
 * not use this file except in compliance with the License. You may obtain
Packit 1fb8d4
 * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
Packit 1fb8d4
 * or implied. See the License for the specific language governing
Packit 1fb8d4
 * permissions and limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/sysinfo.h>
Packit 1fb8d4
#include "prim_test.h"
Packit 1fb8d4
Packit 1fb8d4
/* ------------------------------------------------------------------------- */
Packit 1fb8d4
static BOOL check8(const BYTE* src, UINT32 length, UINT32 offset, BYTE value)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 i;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < length; ++i)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (src[offset + i] != value)
Packit 1fb8d4
		{
Packit 1fb8d4
			printf("SET8U FAILED: off=%"PRIu32" len=%"PRIu32" dest[%"PRIu32"]=0x%02"PRIx8"\n",
Packit 1fb8d4
			       offset, length, i + offset, src[i + offset]);
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL test_set8u_func(void)
Packit 1fb8d4
{
Packit 1fb8d4
	pstatus_t status;
Packit 1fb8d4
	UINT32 off;
Packit 1fb8d4
	BYTE dest[1024];
Packit 1fb8d4
Packit 1fb8d4
	for (off = 0; off < 16; ++off)
Packit 1fb8d4
	{
Packit 1fb8d4
		UINT32 len;
Packit 1fb8d4
		memset(dest, 3, sizeof(dest));
Packit 1fb8d4
Packit 1fb8d4
		for (len = 1; len < 48 - off; ++len)
Packit 1fb8d4
		{
Packit 1fb8d4
			status = generic->set_8u(0xa5, dest + off, len);
Packit 1fb8d4
Packit 1fb8d4
			if (status != PRIMITIVES_SUCCESS)
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			if (!check8(dest, len, off, 0xa5))
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	for (off = 0; off < 16; ++off)
Packit 1fb8d4
	{
Packit 1fb8d4
		UINT32 len;
Packit 1fb8d4
		memset(dest, 3, sizeof(dest));
Packit 1fb8d4
Packit 1fb8d4
		for (len = 1; len < 48 - off; ++len)
Packit 1fb8d4
		{
Packit 1fb8d4
			status = optimized->set_8u(0xa5, dest + off, len);
Packit 1fb8d4
Packit 1fb8d4
			if (status != PRIMITIVES_SUCCESS)
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			if (!check8(dest, len, off, 0xa5))
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* ------------------------------------------------------------------------- */
Packit 1fb8d4
static BOOL test_set8u_speed(void)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE dest[1024];
Packit 1fb8d4
	BYTE value;
Packit 1fb8d4
	UINT32 x;
Packit 1fb8d4
Packit 1fb8d4
	for (x = 0; x < 16; x++)
Packit 1fb8d4
	{
Packit 1fb8d4
		winpr_RAND(&value, sizeof(value));
Packit 1fb8d4
Packit 1fb8d4
		if (!speed_test("set_8u", "", g_Iterations,
Packit 1fb8d4
		                (speed_test_fkt)generic->set_8u,
Packit 1fb8d4
		                (speed_test_fkt)optimized->set_8u,
Packit 1fb8d4
		                value, dest + x, x))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL check32s(const INT32* src, UINT32 length, UINT32 offset, INT32 value)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 i;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < length; ++i)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (src[offset + i] != value)
Packit 1fb8d4
		{
Packit 1fb8d4
			printf("SET8U FAILED: off=%"PRIu32" len=%"PRIu32" dest[%"PRIu32"]=0x%08"PRIx32"\n",
Packit 1fb8d4
			       offset, length, i + offset, src[i + offset]);
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* ------------------------------------------------------------------------- */
Packit 1fb8d4
static BOOL test_set32s_func(void)
Packit 1fb8d4
{
Packit 1fb8d4
	pstatus_t status;
Packit 1fb8d4
	UINT32 off;
Packit 1fb8d4
	INT32 dest[1024];
Packit 1fb8d4
	const INT32 value = -0x12345678;
Packit 1fb8d4
Packit 1fb8d4
	for (off = 0; off < 16; ++off)
Packit 1fb8d4
	{
Packit 1fb8d4
		UINT32 len;
Packit 1fb8d4
		memset(dest, 0, sizeof(dest));
Packit 1fb8d4
Packit 1fb8d4
		for (len = 1; len < 48 - off; ++len)
Packit 1fb8d4
		{
Packit 1fb8d4
			status = generic->set_32s(value, dest + off, len);
Packit 1fb8d4
Packit 1fb8d4
			if (status != PRIMITIVES_SUCCESS)
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			if (!check32s(dest, len, off, value))
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	for (off = 0; off < 16; ++off)
Packit 1fb8d4
	{
Packit 1fb8d4
		UINT32 len;
Packit 1fb8d4
		memset(dest, 0, sizeof(dest));
Packit 1fb8d4
Packit 1fb8d4
		for (len = 1; len < 48 - off; ++len)
Packit 1fb8d4
		{
Packit 1fb8d4
			status = optimized->set_32s(value, dest + off, len);
Packit 1fb8d4
Packit 1fb8d4
			if (status != PRIMITIVES_SUCCESS)
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			if (!check32s(dest, len, off, value))
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL check32u(const UINT32* src, UINT32 length, UINT32 offset, UINT32 value)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 i;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < length; ++i)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (src[offset + i] != value)
Packit 1fb8d4
		{
Packit 1fb8d4
			printf("SET8U FAILED: off=%"PRIu32" len=%"PRIu32" dest[%"PRIu32"]=0x%08"PRIx32"\n",
Packit 1fb8d4
			       offset, length, i + offset, src[i + offset]);
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* ------------------------------------------------------------------------- */
Packit 1fb8d4
static BOOL test_set32u_func(void)
Packit 1fb8d4
{
Packit 1fb8d4
	pstatus_t status;
Packit 1fb8d4
	UINT32 off;
Packit 1fb8d4
	UINT32 dest[1024];
Packit 1fb8d4
	const UINT32 value = 0xABCDEF12;
Packit 1fb8d4
Packit 1fb8d4
	for (off = 0; off < 16; ++off)
Packit 1fb8d4
	{
Packit 1fb8d4
		UINT32 len;
Packit 1fb8d4
		memset(dest, 0, sizeof(dest));
Packit 1fb8d4
Packit 1fb8d4
		for (len = 1; len < 48 - off; ++len)
Packit 1fb8d4
		{
Packit 1fb8d4
			status = generic->set_32u(value, dest + off, len);
Packit 1fb8d4
Packit 1fb8d4
			if (status != PRIMITIVES_SUCCESS)
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			if (!check32u(dest, len, off, value))
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	for (off = 0; off < 16; ++off)
Packit 1fb8d4
	{
Packit 1fb8d4
		UINT32 len;
Packit 1fb8d4
		memset(dest, 0, sizeof(dest));
Packit 1fb8d4
Packit 1fb8d4
		for (len = 1; len < 48 - off; ++len)
Packit 1fb8d4
		{
Packit 1fb8d4
			status = optimized->set_32u(value, dest + off, len);
Packit 1fb8d4
Packit 1fb8d4
			if (status != PRIMITIVES_SUCCESS)
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			if (!check32u(dest, len, off, value))
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* ------------------------------------------------------------------------- */
Packit 1fb8d4
static BOOL test_set32u_speed(void)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 dest[1024];
Packit 1fb8d4
	BYTE value;
Packit 1fb8d4
	UINT32 x;
Packit 1fb8d4
Packit 1fb8d4
	for (x = 0; x < 16; x++)
Packit 1fb8d4
	{
Packit 1fb8d4
		winpr_RAND(&value, sizeof(value));
Packit 1fb8d4
Packit 1fb8d4
		if (!speed_test("set_32u", "", g_Iterations,
Packit 1fb8d4
		                (speed_test_fkt)generic->set_32u,
Packit 1fb8d4
		                (speed_test_fkt)optimized->set_32u,
Packit 1fb8d4
		                value, dest + x, x))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* ------------------------------------------------------------------------- */
Packit 1fb8d4
static BOOL test_set32s_speed(void)
Packit 1fb8d4
{
Packit 1fb8d4
	INT32 dest[1024];
Packit 1fb8d4
	BYTE value;
Packit 1fb8d4
	UINT32 x;
Packit 1fb8d4
Packit 1fb8d4
	for (x = 0; x < 16; x++)
Packit 1fb8d4
	{
Packit 1fb8d4
		winpr_RAND(&value, sizeof(value));
Packit 1fb8d4
Packit 1fb8d4
		if (!speed_test("set_32s", "", g_Iterations,
Packit 1fb8d4
		                (speed_test_fkt)generic->set_32s,
Packit 1fb8d4
		                (speed_test_fkt)optimized->set_32s,
Packit 1fb8d4
		                value, dest + x, x))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int TestPrimitivesSet(int argc, char* argv[])
Packit 1fb8d4
{
Packit 1fb8d4
	prim_test_setup(FALSE);
Packit 1fb8d4
Packit 1fb8d4
	if (!test_set8u_func())
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (!test_set32s_func())
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (!test_set32u_func())
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (g_TestPrimitivesPerformance)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!test_set8u_speed())
Packit 1fb8d4
			return -1;
Packit 1fb8d4
Packit 1fb8d4
		if (!test_set32s_speed())
Packit 1fb8d4
			return -1;
Packit 1fb8d4
Packit 1fb8d4
		if (!test_set32u_speed())
Packit 1fb8d4
			return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}