Wednesday, June 12, 2013

Article 2: Macro VS Inline Function.

Article 2: Macro VS Inline Function. There can be ‘n’ number of cases where macro will fail. I will try to demonstrate few of them.

#define MIN_VERSION_1(a,b) a < b ? a : b
#define MIN_VERSION_2(a,b) (a) < (b) ? (a) : (b)
#define MIN_VERSION_3(a,b) ( (a) < (b) ? (a) : (b) )

inline int min_function(int a ,int b)
{
    return ( a < b ? a : b );
}

All the use cases will be checked on the above code snippet.
Consideration:
1. Let’s consider we have two variables with name a,b and we have to find min from them.
int a = 10;
int b = 20;
int min = 0;
2. I have created one method to print the result in little good manner, you can write your own. The function is written in just for formatted output.
#define MAX_STRING_LENGTH 45
#define WIDTH 4
void printResult(const char* useCase, int a, int b,int min, bool pass)
{
    std::cout<<"\n";
    std::cout<<" | "<<useCase;
    // Extra padding for good output.
    int lk = std::strlen(useCase);
    for(int i = 0 ; i < abs(MAX_STRING_LENGTH - std::strlen(useCase)); i++)
    {
        std::cout<<" ";
    }
    std::cout<<std::setw(WIDTH)<<" | " <<a;
    std::cout<<std::setw(WIDTH)<<" | " <<b;
    std::cout<<std::setw(WIDTH)<<" | " <<min;
    std::cout<<std::setw(WIDTH)<<" | " <<( pass ? " PASS  | " :" FAIL  | ");
}

void printResult(const char* useCase, const char* a, const char* b,const char* min, const char* result)
{
    std::cout<<"\n";
    std::cout<<" | "<<useCase;
    // Extra padding for good output.
    int lk = std::strlen(useCase);
    for(int i = 0 ; i < abs(MAX_STRING_LENGTH - std::strlen(useCase)); i++)
    {
        std::cout<<" ";
    }
    std::cout<<std::setw(WIDTH)<<" | " <<a;
    std::cout<<std::setw(WIDTH)<<" | " <<b;
    std::cout<<std::setw(WIDTH)<<" | " <<min;
    std::cout<<std::setw(WIDTH - 1)<<" | " <<result;
}

3. A function is created for resetting the values to default values:
void resetValues(int&a, int&b, int &min)
{
    a = 10;
    b = 20;
    min = 0;
}

Case 1:
    std::cout<<"\n\n------------------------------------------------";
    std::cout<<"\n\t\tFinding output for min( a, b)";
    std::cout<<"\n--------------------------------------------------";
    printResult("Use Case","a ","b ","min","Result |");
    std::cout<<"\n--------------------------------------------------";
    resetValues(a,b,min);
    min = min_function(a ,b);
    printResult("Expected",a,b,min,1);

    resetValues(a,b,min);
    min = MIN_VERSION_1(a ,b);
    printResult("MIN_VERSION_1(a,b) a < b ? a : b",a,b,min,1);

    resetValues(a,b,min);
    min = MIN_VERSION_2(a ,b);
    printResult("MIN_VERSION_2(a,b) (a) < (b) ? (a) : (b)",a,b,min,1);

    resetValues(a,b,min);
    min = MIN_VERSION_3(a ,b);
    printResult("MIN_VERSION_3(a,b) ( (a) < (b) ? (a) : (b) )",a,b,min,1);

Output:





Case 2:
    std::cout<<"\n\n------------------------------------------------";
    std::cout<<"\n\t\tFinding output for min( a += 3, b)";
    std::cout<<"\n--------------------------------------------------";
    printResult("Use Case","a ","b ","min","Result |");
    std::cout<<"\n--------------------------------------------------";
    resetValues(a,b,min);
    min = min_function(a +=3 ,b);
    printResult("Expected",a,b,min,1);

    resetValues(a,b,min);
    min = MIN_VERSION_1(a +=3 ,b);
    printResult("MIN_VERSION_1(a,b) a < b ? a : b",a,b,min,0);

    resetValues(a,b,min);
    min = MIN_VERSION_2(a +=3 ,b);
    printResult("MIN_VERSION_2(a,b) (a) < (b) ? (a) : (b)",a,b,min,0);

    resetValues(a,b,min);
    min = MIN_VERSION_3(a +=3 ,b);
    printResult("MIN_VERSION_3(a,b) ( (a) < (b) ? (a) : (b) )",a,b,min,0);

Output:













Case 3:
    std::cout<<"\n\n------------------------------------------------";
    std::cout<<"\n\t\tFinding output for min( a , b) + 25";
    std::cout<<"\n--------------------------------------------------";
    printResult("Use Case","a ","b ","min","Result |");
    std::cout<<"\n--------------------------------------------------";
    resetValues(a,b,min);
    min = min_function(a ,b) + 25;
    printResult("Expected",a,b,min,1);

    resetValues(a,b,min);
    min = MIN_VERSION_1(a ,b) + 25;
    printResult("MIN_VERSION_1(a,b) a < b ? a : b",a,b,min,0);

    resetValues(a,b,min);
    min = MIN_VERSION_2(a ,b) + 25;
    printResult("MIN_VERSION_2(a,b) (a) < (b) ? (a) : (b)",a,b,min,0);

    resetValues(a,b,min);
    min = MIN_VERSION_3(a ,b) + 25;
    printResult("MIN_VERSION_3(a,b) ( (a) < (b) ? (a) : (b) )",a,b,min,1);

Output:





Case 4:
    std::cout<<"\n\n------------------------------------------------";
    std::cout<<"\n\t\tFinding output for min( ++a , b)";
    std::cout<<"\n--------------------------------------------------";
    printResult("Use Case","a ","b ","min","Result |");
    std::cout<<"\n--------------------------------------------------";
    resetValues(a,b,min);
    min = min_function( ++a , b);
    printResult("Expected",a,b,min,1);

    resetValues(a,b,min);
    min = MIN_VERSION_1( ++a , b);
    printResult("MIN_VERSION_1(a,b) a < b ? a : b",a,b,min,0);

    resetValues(a,b,min);
    min = MIN_VERSION_2( ++a , b);
    printResult("MIN_VERSION_2(a,b) (a) < (b) ? (a) : (b)",a,b,min,0);

    resetValues(a,b,min);
    min = MIN_VERSION_3( ++a , b);
    printResult("MIN_VERSION_3(a,b) ( (a) < (b) ? (a) : (b) )",a,b,min,0);

Output:


No comments:

Post a Comment