using google test on VS2010

Illustrated Guide to Google Test  + VS2010 Configuration.

1. Download latest source of Google Test (a.k.a gtest)

Image

2. Build gtest library yourself (they don’t give you one that you have to)

2-1) Extract zip file

2-2) Move to msvc folder

2-3) Open solution file (gtest.sln) using VS2010

2-4) Build solution

2-5) Confirm gtestd.lib has been generated

Image

3. Setup project configuration

Image

Image

Image

Image

Image

4. Write Code to test gtest


// BinarySearch.cpp : Simple Binary Search Function for Gtest testing
//

#include "stdio.h"
#include "gtest\gtest.h"

int BIN_search(int a[], int n, int key)
{
    int found = 0;
    int low = 0;
    int mid;
    int high = n-1;

    while (low <= high )
    {
        mid = (low+high)/2;

        if (a[mid] < key)
        {
            low = mid + 1;
        }
        else if (a[mid] > key)
        {
            high = mid - 1;
        }
        else
        {
            found = 1;
        }

        if(found)
            break;
    }

    return found;
}

int intArr[] = {1,2,3,4,5,6,7,8,9};

TEST(IsInsideArr, Positive)
{
    EXPECT_EQ(1, BIN_search(intArr, 9, 1)); //refer to gtest document for details
    EXPECT_EQ(1, BIN_search(intArr, 9, 2));
    EXPECT_EQ(1, BIN_search(intArr, 9, 3));
    EXPECT_EQ(1, BIN_search(intArr, 9, 4));
    EXPECT_EQ(1, BIN_search(intArr, 9, 5));
    EXPECT_EQ(1, BIN_search(intArr, 9, 6));
    EXPECT_EQ(1, BIN_search(intArr, 9, 7));
    EXPECT_EQ(1, BIN_search(intArr, 9, 8));
    EXPECT_EQ(1, BIN_search(intArr, 9, 9));
}

TEST(IsInsideArr, Negative)
{
    EXPECT_EQ(0, BIN_search(intArr, 9, -1));
    EXPECT_EQ(0, BIN_search(intArr, 9, 0));
    EXPECT_EQ(0, BIN_search(intArr, 9, 12));
    EXPECT_EQ(0, BIN_search(intArr, 9, 13));
    EXPECT_EQ(0, BIN_search(intArr, 9, 14));
    EXPECT_EQ(0, BIN_search(intArr, 9, 45));
}

int main(int argc, char** argv)
{

    if(BIN_search(intArr, 9, 3))
        printf("found\n");
    else
        printf("not found\n");

    printf("running all tests ....\n");

    ::testing::InitGoogleTest(&argc, argv); //you have to initialize before its use
    RUN_ALL_TESTS(); //run all tests

    return 0;
}

5. Run and Enjoy

Image

Advertisements

About sydlife

Lazy programmer, Dad and Husband.
%d bloggers like this: