Asseracje w pawn

Do czego służą asseracje można znaleźć na wielu stronach np. W zasadzie w pawnie służą one do tego samego co wszędzie czyli do debugowania. Mamy nawet dwa dostępne typy asseracji
  • Pierwszy z nich to dyrektywa preprocesora w postaci
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    #assert constant expression
    #assert constant expression
    #assert constant expression
    Warunek jest sprawdzany podczas kompilacji Przykład
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    #include <amxmodx></amxmodx>
    public plugin_init()
    {
    #assert 1 == 2
    }
    #include <amxmodx></amxmodx> public plugin_init() { #assert 1 == 2 }
    #include 
    
    public plugin_init()
    {
    #assert 1 == 2
    }
    i komunikat
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    fatal error 110: assertion failed: 1 == 2
    fatal error 110: assertion failed: 1 == 2
     fatal error 110: assertion failed: 1 == 2
    lub
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    #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 
    
    const constVariable = 2;
    
    public plugin_init()
    {
    #assert constVariable == 1
    }
    komunikat
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    fatal error 110: assertion failed: constVariable == 1
    fatal error 110: assertion failed: constVariable == 1
    fatal error 110: assertion failed: constVariable == 1
  • Drugi typ asseracji to aseracje sprawdzane podczas działania pluginu. Używamy tutaj instrukcji
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    assert wyrażenie
    assert 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 crasha 😉 Przykład użycia
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    #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 
    #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
    }
    i komunikat w logach
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    [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/
Nie ma co ukrywać że modyfikacja parametrów kompilacji nie jest najprzyjemniejszym sposobem szczególnie jeśli udostępniamy nasz kod publicznie 😉 Dlatego napisałem mały plik inc który pozwala na wyłączanie mechanizmu asseracji za pomocą definiowania makra preprocesora Udostępnia on dwa makra
  • 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
Same asseracje wyłączamy dodając taką o to definicję przed include pliku assert.inc
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#define NDEBUG
#define NDEBUG
#define NDEBUG
Przykład użycia
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#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 ) }
#include 
#include 

#define PLUGIN	"New Plugin"
#define AUTHOR	"DarkGL"
#define VERSION	"1.0"

#include 

public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)

new n = 1;

assert( n < 0 )
}
i przykład gdzie asseracje są wyłączone
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#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 ) }
#include 
#include 

#define PLUGIN	"New Plugin"
#define AUTHOR	"DarkGL"
#define VERSION	"1.0"

#define NDEBUG
#include 

public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)

new n = 1;

assert( n < 0 )
}
oraz shouldntReachHere
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#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() }
#include 
#include 

#define PLUGIN	"New Plugin"
#define AUTHOR	"DarkGL"
#define VERSION	"1.0"

#include 

public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)

shouldntReachHere()
}
assert.inc Download

Jeden komentarz o “Asseracje w pawn

Dodaj komentarz

This site uses Akismet to reduce spam. Learn how your comment data is processed.