plugins/testsuite/sorttest.sp
author Borja Ferrer <faluco@alliedmods.net>
Fri Sep 28 20:13:42 2007 +0000 (2007-09-28)
changeset 1499 e55ac4555a2b
parent 535 f255342d132f
child 1701 9cf7f48da853
permissions -rw-r--r--
synced linux build
     1 #include <sourcemod>
     2 
     3 public Plugin:myinfo = 
     4 {
     5 	name = "Sorting Test",
     6 	author = "AlliedModders LLC",
     7 	description = "Tests sorting functions",
     8 	version = "1.0.0.0",
     9 	url = "http://www.sourcemod.net/"
    10 };
    11 
    12 public OnPluginStart(Handle:myself)
    13 {
    14 	RegServerCmd("test_sort_ints", Command_TestSortInts)
    15 	RegServerCmd("test_sort_floats", Command_TestSortFloats)
    16 	RegServerCmd("test_sort_strings", Command_TestSortStrings)
    17 	RegServerCmd("test_sort_1d", Command_TestSort1D)
    18 	RegServerCmd("test_sort_2d", Command_TestSort2D)
    19 }
    20 
    21 /*****************
    22  * INTEGER TESTS *
    23  *****************/
    24 // Note that integer comparison is just int1-int2 (or a variation therein)
    25 
    26 PrintIntegers(const array[], size)
    27 {
    28 	for (new i=0; i<size; i++)
    29 	{
    30 		PrintToServer("array[%d] = %d", i, array[i])
    31 	}
    32 }
    33 
    34 public Action:Command_TestSortInts(args)
    35 {
    36 	new array[10] = {6, 7, 3, 2, 8, 5, 0, 1, 4, 9}
    37 	
    38 	PrintToServer("Testing ascending sort:")
    39 	SortIntegers(array, 10, Sort_Ascending)
    40 	PrintIntegers(array, 10)
    41 	
    42 	PrintToServer("Testing descending sort:")
    43 	SortIntegers(array, 10, Sort_Descending)
    44 	PrintIntegers(array, 10)
    45 	
    46 	return Plugin_Handled
    47 }
    48 
    49 /**************************
    50  * Float comparison tests *
    51  **************************/
    52 
    53 PrintFloats(const Float:array[], size)
    54 {
    55 	for (new i=0; i<size; i++)
    56 	{
    57 		PrintToServer("array[%d] = %f", i, array[i])
    58 	}
    59 }
    60 
    61 public Action:Command_TestSortFloats(args)
    62 {
    63 	new Float:array[10] = {6.3, 7.6, 3.2, 2.1, 8.5, 5.2, 0.4, 1.7, 4.8, 8.2}
    64 	
    65 	PrintToServer("Testing ascending sort:")
    66 	SortFloats(array, 10, Sort_Ascending)
    67 	PrintFloats(array, 10)
    68 	
    69 	PrintToServer("Testing descending sort:")
    70 	SortFloats(array, 10, Sort_Descending)
    71 	PrintFloats(array, 10)
    72 	
    73 	return Plugin_Handled
    74 }
    75 
    76 public Custom1DSort(elem1, elem2, const array[], Handle:hndl)
    77 {
    78 	new Float:f1 = Float:elem1
    79 	new Float:f2 = Float:elem2
    80 	if (f1 > f2)
    81 	{
    82 		return -1;
    83 	} else if (f1 < f2) {
    84 		return 1;
    85 	}
    86 	
    87 	return 0;
    88 }
    89 
    90 public Action:Command_TestSort1D(args)
    91 {
    92 	new Float:array[10] = {6.3, 7.6, 3.2, 2.1, 8.5, 5.2, 0.4, 1.7, 4.8, 8.2}
    93 	
    94 	SortCustom1D(_:array, 10, Custom1DSort)
    95 	PrintFloats(array, 10)
    96 	
    97 	return Plugin_Handled
    98 }
    99 
   100 /***************************
   101  * String comparison tests *
   102  ***************************/
   103 
   104 PrintStrings(const String:array[][], size)
   105 {
   106 	for (new i=0; i<size; i++)
   107 	{
   108 		PrintToServer("array[%d] = %s", i, array[i])
   109 	}
   110 }
   111 
   112 public Action:Command_TestSortStrings(args)
   113 {
   114 	new String:strarray[][] = 
   115 		{
   116 			"faluco",
   117 			"bailopan",
   118 			"pm onoto",
   119 			"damaged soul",
   120 			"sniperbeamer",
   121 			"sidluke",
   122 			"johnny got his gun",
   123 			"gabe newell",
   124 			"hello",
   125 			"WHAT?!"
   126 		}
   127 		
   128 	PrintToServer("Testing ascending sort:")
   129 	SortStrings(strarray, 10, Sort_Ascending)
   130 	PrintStrings(strarray, 10)
   131 	
   132 	PrintToServer("Testing descending sort:")
   133 	SortStrings(strarray, 10, Sort_Descending)
   134 	PrintStrings(strarray, 10)
   135 	
   136 	return Plugin_Handled
   137 }
   138 
   139 public Custom2DSort(String:elem1[], String:elem2[], String:array[][], Handle:hndl)
   140 {
   141 	return StrCompare(elem1, elem2)
   142 }
   143 
   144 public Action:Command_TestSort2D(args)
   145 {
   146 	new String:array[][] = 
   147 		{
   148 			"faluco",
   149 			"bailopan",
   150 			"pm onoto",
   151 			"damaged soul",
   152 			"sniperbeamer",
   153 			"sidluke",
   154 			"johnny got his gun",
   155 			"gabe newell",
   156 			"hello",
   157 			"WHAT?!"
   158 		}
   159 	
   160 	SortCustom2D(_:array, 10, Custom2DSort)
   161 	PrintStrings(array, 10)
   162 	
   163 	return Plugin_Handled
   164 }