plugins/testsuite/sorttest.sp
changeset 535 f255342d132f
child 1701 9cf7f48da853
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/testsuite/sorttest.sp	Mon Feb 26 08:21:09 2007 +0000
     1.3 @@ -0,0 +1,164 @@
     1.4 +#include <sourcemod>
     1.5 +
     1.6 +public Plugin:myinfo = 
     1.7 +{
     1.8 +	name = "Sorting Test",
     1.9 +	author = "AlliedModders LLC",
    1.10 +	description = "Tests sorting functions",
    1.11 +	version = "1.0.0.0",
    1.12 +	url = "http://www.sourcemod.net/"
    1.13 +};
    1.14 +
    1.15 +public OnPluginStart(Handle:myself)
    1.16 +{
    1.17 +	RegServerCmd("test_sort_ints", Command_TestSortInts)
    1.18 +	RegServerCmd("test_sort_floats", Command_TestSortFloats)
    1.19 +	RegServerCmd("test_sort_strings", Command_TestSortStrings)
    1.20 +	RegServerCmd("test_sort_1d", Command_TestSort1D)
    1.21 +	RegServerCmd("test_sort_2d", Command_TestSort2D)
    1.22 +}
    1.23 +
    1.24 +/*****************
    1.25 + * INTEGER TESTS *
    1.26 + *****************/
    1.27 +// Note that integer comparison is just int1-int2 (or a variation therein)
    1.28 +
    1.29 +PrintIntegers(const array[], size)
    1.30 +{
    1.31 +	for (new i=0; i<size; i++)
    1.32 +	{
    1.33 +		PrintToServer("array[%d] = %d", i, array[i])
    1.34 +	}
    1.35 +}
    1.36 +
    1.37 +public Action:Command_TestSortInts(args)
    1.38 +{
    1.39 +	new array[10] = {6, 7, 3, 2, 8, 5, 0, 1, 4, 9}
    1.40 +	
    1.41 +	PrintToServer("Testing ascending sort:")
    1.42 +	SortIntegers(array, 10, Sort_Ascending)
    1.43 +	PrintIntegers(array, 10)
    1.44 +	
    1.45 +	PrintToServer("Testing descending sort:")
    1.46 +	SortIntegers(array, 10, Sort_Descending)
    1.47 +	PrintIntegers(array, 10)
    1.48 +	
    1.49 +	return Plugin_Handled
    1.50 +}
    1.51 +
    1.52 +/**************************
    1.53 + * Float comparison tests *
    1.54 + **************************/
    1.55 +
    1.56 +PrintFloats(const Float:array[], size)
    1.57 +{
    1.58 +	for (new i=0; i<size; i++)
    1.59 +	{
    1.60 +		PrintToServer("array[%d] = %f", i, array[i])
    1.61 +	}
    1.62 +}
    1.63 +
    1.64 +public Action:Command_TestSortFloats(args)
    1.65 +{
    1.66 +	new Float:array[10] = {6.3, 7.6, 3.2, 2.1, 8.5, 5.2, 0.4, 1.7, 4.8, 8.2}
    1.67 +	
    1.68 +	PrintToServer("Testing ascending sort:")
    1.69 +	SortFloats(array, 10, Sort_Ascending)
    1.70 +	PrintFloats(array, 10)
    1.71 +	
    1.72 +	PrintToServer("Testing descending sort:")
    1.73 +	SortFloats(array, 10, Sort_Descending)
    1.74 +	PrintFloats(array, 10)
    1.75 +	
    1.76 +	return Plugin_Handled
    1.77 +}
    1.78 +
    1.79 +public Custom1DSort(elem1, elem2, const array[], Handle:hndl)
    1.80 +{
    1.81 +	new Float:f1 = Float:elem1
    1.82 +	new Float:f2 = Float:elem2
    1.83 +	if (f1 > f2)
    1.84 +	{
    1.85 +		return -1;
    1.86 +	} else if (f1 < f2) {
    1.87 +		return 1;
    1.88 +	}
    1.89 +	
    1.90 +	return 0;
    1.91 +}
    1.92 +
    1.93 +public Action:Command_TestSort1D(args)
    1.94 +{
    1.95 +	new Float:array[10] = {6.3, 7.6, 3.2, 2.1, 8.5, 5.2, 0.4, 1.7, 4.8, 8.2}
    1.96 +	
    1.97 +	SortCustom1D(_:array, 10, Custom1DSort)
    1.98 +	PrintFloats(array, 10)
    1.99 +	
   1.100 +	return Plugin_Handled
   1.101 +}
   1.102 +
   1.103 +/***************************
   1.104 + * String comparison tests *
   1.105 + ***************************/
   1.106 +
   1.107 +PrintStrings(const String:array[][], size)
   1.108 +{
   1.109 +	for (new i=0; i<size; i++)
   1.110 +	{
   1.111 +		PrintToServer("array[%d] = %s", i, array[i])
   1.112 +	}
   1.113 +}
   1.114 +
   1.115 +public Action:Command_TestSortStrings(args)
   1.116 +{
   1.117 +	new String:strarray[][] = 
   1.118 +		{
   1.119 +			"faluco",
   1.120 +			"bailopan",
   1.121 +			"pm onoto",
   1.122 +			"damaged soul",
   1.123 +			"sniperbeamer",
   1.124 +			"sidluke",
   1.125 +			"johnny got his gun",
   1.126 +			"gabe newell",
   1.127 +			"hello",
   1.128 +			"WHAT?!"
   1.129 +		}
   1.130 +		
   1.131 +	PrintToServer("Testing ascending sort:")
   1.132 +	SortStrings(strarray, 10, Sort_Ascending)
   1.133 +	PrintStrings(strarray, 10)
   1.134 +	
   1.135 +	PrintToServer("Testing descending sort:")
   1.136 +	SortStrings(strarray, 10, Sort_Descending)
   1.137 +	PrintStrings(strarray, 10)
   1.138 +	
   1.139 +	return Plugin_Handled
   1.140 +}
   1.141 +
   1.142 +public Custom2DSort(String:elem1[], String:elem2[], String:array[][], Handle:hndl)
   1.143 +{
   1.144 +	return StrCompare(elem1, elem2)
   1.145 +}
   1.146 +
   1.147 +public Action:Command_TestSort2D(args)
   1.148 +{
   1.149 +	new String:array[][] = 
   1.150 +		{
   1.151 +			"faluco",
   1.152 +			"bailopan",
   1.153 +			"pm onoto",
   1.154 +			"damaged soul",
   1.155 +			"sniperbeamer",
   1.156 +			"sidluke",
   1.157 +			"johnny got his gun",
   1.158 +			"gabe newell",
   1.159 +			"hello",
   1.160 +			"WHAT?!"
   1.161 +		}
   1.162 +	
   1.163 +	SortCustom2D(_:array, 10, Custom2DSort)
   1.164 +	PrintStrings(array, 10)
   1.165 +	
   1.166 +	return Plugin_Handled
   1.167 +}