plugins/testsuite/callfunctest.sp
author David Anderson <dvander@alliedmods.net>
Mon Aug 12 00:22:41 2013 +0100 (2013-08-12)
changeset 3989 b73dae36a0da
parent 1952 b7aa2b626807
permissions -rw-r--r--
Eliminate InfoVars::rval (bug 5844 part 6, r=ds).
     1 #include <sourcemod>
     2 
     3 public Plugin:myinfo = 
     4 {
     5 	name = "Function Call Testing Lab",
     6 	author = "AlliedModders LLC",
     7 	description = "Tests basic function calls",
     8 	version = "1.0.0.0",
     9 	url = "http://www.sourcemod.net/"
    10 };
    11 
    12 public OnPluginStart()
    13 {
    14 	RegServerCmd("test_callfunc", Command_CallFunc);
    15 	RegServerCmd("test_callfunc_reentrant", Command_ReentrantCallFunc);
    16 }
    17 
    18 public OnCallFuncReceived(num, Float:fnum, String:str[], String:str2[], &val, &Float:fval, array[], array2[], size, hello2[1])
    19 {
    20 	PrintToServer("Inside OnCallFuncReceived...");
    21 
    22 	PrintToServer("num = %d (expected: %d)", num, 5);
    23 	PrintToServer("fnum = %f (expected: %f)", fnum, 7.17);
    24 	PrintToServer("str[] = \"%s\" (expected: \"%s\")", str, "Gaben");
    25 	PrintToServer("str2[] = \"%s\" (expected: \"%s\")", str2, ".taf si nebaG");
    26 	
    27 	PrintToServer("val = %d (expected %d, setting to %d)", val, 62, 15);
    28 	val = 15;
    29 	
    30 	PrintToServer("fval = %f (expected: %f, setting to %f)", fval, 6.25, 1.5);
    31 	fval = 1.5; 
    32 	
    33 	PrintToServer("Printing %d elements of array[] (expected: %d)", size, 6);
    34 	for (new i = 0; i < size; i++)
    35 	{
    36 		PrintToServer("array[%d] = %d (expected: %d)", i, array[i], i);
    37 	}
    38 	for (new i = 0; i < size; i++)
    39 	{
    40 		PrintToServer("array2[%d] = %d (expected: %d)", i, array[i], i);
    41 	}
    42 	
    43 	/* This shouldn't get copied back */
    44 	strcopy(str, strlen(str) + 1, "Yeti");
    45 	/* This should get copied back */
    46 	strcopy(str2, strlen(str2) + 1, "Gaben is fat.");
    47 	
    48 	/* This should get copied back */
    49 	array[0] = 5;
    50 	array[1] = 6;
    51 	/* This shouldn't get copied back */
    52 	hello2[0] = 25;
    53 	
    54 	return 42;
    55 }
    56 
    57 public OnReentrantCallReceived(num, String:str[])
    58 {
    59 	new err, ret;
    60 	
    61 	PrintToServer("Inside OnReentrantCallReceived...");
    62 	
    63 	PrintToServer("num = %d (expected: %d)", num, 7);
    64 	PrintToServer("str[] = \"%s\" (expected: \"%s\")", str, "nana");
    65 	
    66 	new Function:func = GetFunctionByName(INVALID_HANDLE, "OnReentrantCallReceivedTwo");
    67 	
    68 	if (func == INVALID_FUNCTION)
    69 	{
    70 		PrintToServer("Failed to get the function id of OnReentrantCallReceivedTwo");
    71 		return 0;
    72 	}
    73 	
    74 	PrintToServer("Calling OnReentrantCallReceivedTwo...");
    75 	
    76 	Call_StartFunction(INVALID_HANDLE, func);
    77 	Call_PushFloat(8.0);
    78 	err = Call_Finish(ret);
    79 	
    80 	PrintToServer("Call to OnReentrantCallReceivedTwo has finished!");
    81 	PrintToServer("Error code = %d (expected: %d)", err, 0);
    82 	PrintToServer("Return value = %d (expected: %d)", ret, 707);
    83 	
    84 	return 11;
    85 }
    86 
    87 public OnReentrantCallReceivedTwo(Float:fnum)
    88 {
    89 	PrintToServer("Inside OnReentrantCallReceivedTwo...");
    90 	
    91 	PrintToServer("fnum = %f (expected: %f)", fnum, 8.0);
    92 
    93 	return 707;
    94 }
    95 
    96 public Action:Command_CallFunc(args)
    97 {
    98 	new a = 62;
    99 	new Float:b = 6.25;
   100 	new const String:what[] = "Gaben";
   101 	new String:truth[] = ".taf si nebaG";
   102 	new hello[] = {0, 1, 2, 3, 4, 5};
   103 	new hello2[] = {9};
   104 	new pm = 6;
   105 	new err;
   106 	new ret;
   107 	
   108 	new Function:func = GetFunctionByName(INVALID_HANDLE, "OnCallFuncReceived");
   109 	
   110 	if (func == INVALID_FUNCTION)
   111 	{
   112 		PrintToServer("Failed to get the function id of OnCallFuncReceived");
   113 		return Plugin_Handled;
   114 	}
   115 	
   116 	PrintToServer("Calling OnCallFuncReceived...");
   117 	
   118 	Call_StartFunction(INVALID_HANDLE, func);
   119 	Call_PushCell(5);
   120 	Call_PushFloat(7.17);
   121 	Call_PushString(what);
   122 	Call_PushStringEx(truth, sizeof(truth), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
   123 	Call_PushCellRef(a);
   124 	Call_PushFloatRef(b);
   125 	Call_PushArrayEx(hello, pm, SM_PARAM_COPYBACK);
   126 	Call_PushArray(hello, pm);
   127 	Call_PushCell(pm);
   128 	Call_PushArray(hello2, 1);
   129 	err = Call_Finish(ret);
   130 	
   131 	PrintToServer("Call to OnCallFuncReceived has finished!");
   132 	PrintToServer("Error code = %d (expected: %d)", err, 0);
   133 	PrintToServer("Return value = %d (expected: %d)", ret, 42);
   134 	
   135 	PrintToServer("a = %d (expected: %d)", a, 15);
   136 	PrintToServer("b = %f (expected: %f)", b, 1.5);
   137 	PrintToServer("what = \"%s\" (expected: \"%s\")", what, "Gaben");
   138 	PrintToServer("truth = \"%s\" (expected: \"%s\")", truth, "Gaben is fat.");
   139 	PrintToServer("hello[0] = %d (expected: %d)", hello[0], 5);
   140 	PrintToServer("hello[1] = %d (expected: %d)", hello[1], 6);
   141 	PrintToServer("hello2[0] = %d (expected: %d)", hello2[0], 9);
   142 	
   143 	return Plugin_Handled;
   144 }
   145 
   146 public Action:Command_ReentrantCallFunc(args)
   147 {	
   148 	new err, ret;
   149 	new Function:func = GetFunctionByName(INVALID_HANDLE, "OnReentrantCallReceived");
   150 	
   151 	if (func == INVALID_FUNCTION)
   152 	{
   153 		PrintToServer("Failed to get the function id of OnReentrantCallReceived");
   154 		return Plugin_Handled;
   155 	}
   156 	
   157 	PrintToServer("Calling OnReentrantCallReceived...");
   158 	
   159 	Call_StartFunction(INVALID_HANDLE, func);
   160 	Call_PushCell(7);
   161 	Call_PushString("nana");
   162 	err = Call_Finish(ret);
   163 	
   164 	PrintToServer("Call to OnReentrantCallReceived has finished!");
   165 	PrintToServer("Error code = %d (expected: %d)", err, 0);
   166 	PrintToServer("Return value = %d (expected: %d)", ret, 11);
   167 	
   168 	return Plugin_Handled;
   169 }