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