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