- http://pl.wikibooks.org/wiki/C/assert
- http://pornel.net/assert
- http://www.cplusplus.com/reference/cassert/assert/
- Pierwszy z nich to dyrektywa preprocesora w postaci
#assert constant expression#assert constant expression
#assert constant expression
Warunek jest sprawdzany podczas kompilacji Przykład#include <amxmodx></amxmodx>public plugin_init(){#assert 1 == 2}#include <amxmodx></amxmodx> public plugin_init() { #assert 1 == 2 }#include
i komunikatpublic plugin_init() { #assert 1 == 2 } fatal error 110: assertion failed: 1 == 2fatal error 110: assertion failed: 1 == 2fatal error 110: assertion failed: 1 == 2
lub#include <amxmodx></amxmodx>const constVariable = 2;public plugin_init(){#assert constVariable == 1}#include <amxmodx></amxmodx> const constVariable = 2; public plugin_init() { #assert constVariable == 1 }#include
komunikatconst constVariable = 2; public plugin_init() { #assert constVariable == 1 } fatal error 110: assertion failed: constVariable == 1fatal error 110: assertion failed: constVariable == 1fatal error 110: assertion failed: constVariable == 1
- Drugi typ asseracji to aseracje sprawdzane podczas działania pluginu. Używamy tutaj instrukcji assert wyrażenieassert wyrażenie
assert wyrażenie
Należy pamiętać że podczas testowania kodu z asseracjami należy załadować plugin w trybie debug inaczej przy warunku w asseracji który zwróci false nasz serwer zaliczy crashaPrzykład użycia
#include <amxmodx>#include <amxmisc>#define PLUGIN "New Plugin"#define AUTHOR "DarkGL"#define VERSION "1.0"public plugin_init(){register_plugin(PLUGIN, VERSION, AUTHOR)new testVariable = 1;assert testVariable < 0}</amxmisc></amxmodx>#include <amxmodx> #include <amxmisc> #define PLUGIN "New Plugin" #define AUTHOR "DarkGL" #define VERSION "1.0" public plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR) new testVariable = 1; assert testVariable < 0 }</amxmisc></amxmodx>#include
i komunikat w logach#include #define PLUGIN "New Plugin" #define AUTHOR "DarkGL" #define VERSION "1.0" public plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR) new testVariable = 1; assert testVariable < 0 } [AMXX] Displaying debug trace (plugin "assertTest3.amxx")[AMXX] Run time error 2: assertion failed[AMXX] [0] assertTest3.sma::plugin_init (line 14)[AMXX] Displaying debug trace (plugin "assertTest3.amxx") [AMXX] Run time error 2: assertion failed [AMXX] [0] assertTest3.sma::plugin_init (line 14)[AMXX] Displaying debug trace (plugin "assertTest3.amxx") [AMXX] Run time error 2: assertion failed [AMXX] [0] assertTest3.sma::plugin_init (line 14)
Możemy tu podawać dowolne warunki ale zgodnie z zasadami opisanymi w linkach na początku wpisu. Asseracji w kodzie który chcemy opublikować ( a raczej w jego skompilowanej wersji ) pozbywamy się poprzez dołaczenie do parametrów kompilacji opcji -d1 lub -d0 ( http://darkgl.pl/index.php/2013/08/19/optymalizacja-dzialania-pluginow-poprzez-parametry-kompilacji/
- assert – ten sam efekt co drugi sposób asseracji opisany powyżej
- shouldntReachHere – plugin nie powinien dotrzeć do tego miejsca w kodzie następuje natychmiastowy „wyrzut” błędnej asseracji do logów
#define NDEBUG
#define NDEBUG
#define NDEBUGPrzykład użycia
#include <amxmodx>
#include <amxmisc></amxmisc></amxmodx>
#define PLUGIN "New Plugin"
#define AUTHOR "DarkGL"
#define VERSION "1.0"
#include <assert></assert>
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
new n = 1;
assert( n < 0 )
}
#include <amxmodx>
#include <amxmisc></amxmisc></amxmodx>
#define PLUGIN "New Plugin"
#define AUTHOR "DarkGL"
#define VERSION "1.0"
#include <assert></assert>
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
new n = 1;
assert( n < 0 )
}
#includei przykład gdzie asseracje są wyłączone#include #define PLUGIN "New Plugin" #define AUTHOR "DarkGL" #define VERSION "1.0" #includepublic plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR) new n = 1; assert( n < 0 ) }
#include <amxmodx>
#include <amxmisc></amxmisc></amxmodx>
#define PLUGIN "New Plugin"
#define AUTHOR "DarkGL"
#define VERSION "1.0"
#define NDEBUG
#include <assert></assert>
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
new n = 1;
assert( n < 0 )
}
#include <amxmodx>
#include <amxmisc></amxmisc></amxmodx>
#define PLUGIN "New Plugin"
#define AUTHOR "DarkGL"
#define VERSION "1.0"
#define NDEBUG
#include <assert></assert>
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
new n = 1;
assert( n < 0 )
}
#includeoraz shouldntReachHere#include #define PLUGIN "New Plugin" #define AUTHOR "DarkGL" #define VERSION "1.0" #define NDEBUG #includepublic plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR) new n = 1; assert( n < 0 ) }
#include <amxmodx>
#include <amxmisc></amxmisc></amxmodx>
#define PLUGIN "New Plugin"
#define AUTHOR "DarkGL"
#define VERSION "1.0"
#include <assert></assert>
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
shouldntReachHere()
}
#include <amxmodx>
#include <amxmisc></amxmisc></amxmodx>
#define PLUGIN "New Plugin"
#define AUTHOR "DarkGL"
#define VERSION "1.0"
#include <assert></assert>
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
shouldntReachHere()
}
#includeassert.inc Download#include #define PLUGIN "New Plugin" #define AUTHOR "DarkGL" #define VERSION "1.0" #includepublic plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR) shouldntReachHere() }
Hmmmm… Plugin odpalający się w określony dzień lub okres (święta). Genialne.