Macros, using a parameter as a functioncall and as a string
#1

Hey,

So, i have made myself a little assert function:

PHP код:
assertThat(title[], conditionexpectedResultbool:sendDefaultMessage true)
{
    if(
condition == expectedResult)
    {
        return 
true;
    }
    if(
sendDefaultMessage)
    {
        
printf("%s: Expected: %d Result: %d"titleexpectedResultcondition);    
    }
    
    return 
false;

It takes a string, a condition (function call) and the expected result.
I wanted to simplify that thing like this:

PHP код:
#define ASSERT(%0,%1); assertThat(%0,%0,%1); 
So, i have tried to use %0 as the condition parameter and the string parameter, which didn't work.

Here is what i already tried:

PHP код:
#define ASSERT(%0,%1); assertThat("%0",%0,%1); //prints %0 
PHP код:
#define ASSERT(%0,%1); assertThat(#%0,%0,%1); //Gives mutliple errors 
PHP код:
#define ASSERT(%0,%1); assertThat("" #%0,%0,%1); //Gives mutliple errors 
Does anyone have an idea on how to do this? if it even is possible
Reply
#2

Try that:
PHP код:
#define ASSERT(%0,%1) assertThat(#%0,%0(),%1)
main()
{
    
ASSERT(test23);
}
assertThat(title[], conditionexpectedResultbool:sendDefaultMessage true)
{
    if(
condition == expectedResult)
    {
        return 
true;
    }
    if(
sendDefaultMessage)
    {
        
printf("%s: Expected: %d Result: %d"titleexpectedResultcondition);
    }
    return 
false;
}
test2(){
    print(
"test2 called");
    return 
1;

Worked for me and this test will print (test2: Expected: 3 Result: 1);
Reply
#3

@renatog

Well, doing it like that, it only works because the function has no parameters, if i were to wrap all functions so they have no prameters when testing them, i would have failed at simplifying it So that is not an option
Reply
#4

PHP код:
#define ASSERT(%0(%1),%2) assertThat(#%0,%0(%1),%2) 
Should technically work if # works as you expect it to be.
and you'd be doing ASSERT(function(args), result)
Reply
#5

Edit: I am dumb nvm, thanks progamer, so far it works

Another Edit:

My initial goal was to keep the parameters, so i know exactly which functioncall failed, i have tried this, but it doesnt seem to work, i have tried this:

PHP код:
#define ASSERT(%0(%1),%2); assertThat(#%0 "(" #%1 ")",%0(%1),%2); 
but when i use it, it leads to this:

PHP код:
D:\Downloads\GTA Server\filterscripts\testassert.pwn(6) : error 001expected token","but found "-string-"
D:\Downloads\GTA Server\filterscripts\testassert.pwn(6) : warning 215expression has no effect
D
:\Downloads\GTA Server\filterscripts\testassert.pwn(6) : warning 215expression has no effect
D
:\Downloads\GTA Server\filterscripts\testassert.pwn(6) : error 001expected token";"but found ")"
D:\Downloads\GTA Server\filterscripts\testassert.pwn(6) : error 029invalid expressionassumed zero
D
:\Downloads\GTA Server\filterscripts\testassert.pwn(6) : fatal error 107too many error messages on one line 
Is there a way to only run the pre compiling, so i can actually see the output?

If it is not possible, i'll stay with what ProGamer provided.
Reply
#6

Run pawnc.exe with -l (lowercase "L") argument.
Reply
#7

"If the compiler comes across ";", ")" or "," after a hash it will stop the string:". - ******

So I think it's not possible. I recommend you read this:
https://sampforum.blast.hk/showthread.php?tid=570933
Reply
#8

Quote:
Originally Posted by renatog
Посмотреть сообщение
"If the compiler comes across ";", ")" or "," after a hash it will stop the string:". - ******

So I think it's not possible. I recommend you read this:
https://sampforum.blast.hk/showthread.php?tid=570933
Thank you
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)