**Step 1**

The function should have this prototype:

extern bool isPrime(int value);Let's write a first implementation, any value we pass in, the function says it is not a prime:

bool isPrime(int value)

{

return false;

}

We know it is not working properly, but at least it works.

**Step 2**

We setup the Google Test environment, so we are going to call RUN_ALL_TEST on a few tests we are about to write.

Negative numbers are not prime, so here is a first lot of tests:

TEST(IsPrimeTest, Negative) {

EXPECT_FALSE(isPrime(-1));

EXPECT_FALSE(isPrime(-2));

EXPECT_FALSE(isPrime(INT_MIN));

}

Let's consider the first positive numbers:

TEST(IsPrimeTest, Trivial) {

EXPECT_FALSE(isPrime(0));

EXPECT_FALSE(isPrime(1));

EXPECT_TRUE(isPrime(2));

EXPECT_TRUE(isPrime(3));

}

And then do some testing on other positive numbers:

TEST(IsPrimeTest, Positive) {

EXPECT_FALSE(isPrime(4));

EXPECT_TRUE(isPrime(5));

EXPECT_FALSE(isPrime(6));

EXPECT_TRUE(isPrime(23));

}

**Step 3**

We compile and run our application. If we correctly setup Google Test, we are going to find out that the first lot of testing succeed (hurray!) but we have a few errors in the other two ones:

IsPrimeTest.Trivial:

(...)\test.cpp(94): error: Value of: isPrime(2)

Actual: false

Expected: true

(...)\test.cpp(95): error: Value of: isPrime(3)

Actual: false

Expected: true

IsPrimeTest.Trivial:

(...)\test.cpp(101): error: Value of: isPrime(5)

Actual: false

Expected: true

(...)\test.cpp(103): error: Value of: isPrime(23)

Actual: false

Expected: true

Looks like the algorithm we applied in our function is not perfect.

**Step 4**

We change of function in this way:

bool isPrime(int value)

{

if(value < 2)

return false;

// 2 is prime, all other even numbers not

if((value != 2) && (value % 2 == 0))

return false;

return true;

}

Wow - no errors detected. But have we done enough testing?

**Step 4**

Let's add some more tests:

TEST(IsPrimeTest, LessThan100) {

EXPECT_FALSE(isPrime(87));

EXPECT_TRUE(isPrime(89));

EXPECT_FALSE(isPrime(95));

EXPECT_TRUE(isPrime(97));

}

Looks like we still have problems:

(...)\test.cpp(107): error: Value of: isPrime(87)

Actual: true

Expected: false

(...)\test.cpp(109): error: Value of: isPrime(95)

Actual: true

Expected: false

And so on ...

## No comments:

## Post a Comment