Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help writing unit tests using C++, google test suite in Visual Studio. // TODO: Create a test to verify adding a single value

I need help writing unit tests using C++, google test suite in Visual Studio.

// TODO: Create a test to verify adding a single value to an empty collection

TEST_F(CollectionTest, CanAddToEmptyVector)

{

// is the collection empty?

// if empty, the size must be 0

add_entries(1);

// is the collection still empty?

// if not empty, what must the size be?

}

// TODO: Create a test to verify adding five values to collection

TEST_F(CollectionTest, CanAddFiveValuesToVector)

{

add_entries(5);

}

// TODO: Create a test to verify that max size is greater than or equal to size for 0, 1, 5, 10 entries

// TODO: Create a test to verify that capacity is greater than or equal to size for 0, 1, 5, 10 entries

// TODO: Create a test to verify resizing increases the collection

// TODO: Create a test to verify resizing decreases the collection

// TODO: Create a test to verify resizing decreases the collection to zero

// TODO: Create a test to verify clear erases the collection

// TODO: Create a test to verify erase(begin,end) erases the collection

// TODO: Create a test to verify reserve increases the capacity but not the size of the collection

// TODO: Create a test to verify the std::out_of_range exception is thrown when calling at() with an index out of bounds

// NOTE: This is a negative test

// TODO: Create 2 unit tests of your own to test something on the collection - do 1 positive & 1 negative

The provided code is:

// Uncomment the next line to use precompiled headers

#include "pch.h"

// uncomment the next line if you do not use precompiled headers

#include "gtest/gtest.h"

//

// the global test environment setup and tear down

// you should not need to change anything here

class Environment : public ::testing::Environment

{

public:

~Environment() override {}

// Override this to define how to set up the environment.

void SetUp() override

{

//initialize random seed

srand(time(nullptr));

}

// Override this to define how to tear down the environment.

void TearDown() override {}

};

// create our test class to house shared data between tests

// you should not need to change anything here

class CollectionTest : public ::testing::Test

{

protected:

// create a smart point to hold our collection

std::unique_ptr> collection;

void SetUp() override

{ // create a new collection to be used in the test

collection.reset(new std::vector);

}

void TearDown() override

{ //erase all elements in the collection, if any remain

collection->clear();

// free the pointer

collection.reset(nullptr);

}

// helper function to add random values from 0 to 99 count times to the collection

void add_entries(int count)

{

assert(count > 0);

for (auto i = 0; i < count; ++i)

collection->push_back(rand() % 100);

}

};

// When should you use the EXPECT_xxx or ASSERT_xxx macros?

// Use ASSERT when failure should terminate processing, such as the reason for the test case.

// Use EXPECT when failure should notify, but processing should continue

// Test that a collection is empty when created.

// Prior to calling this (and all other TEST_F defined methods),

//CollectionTest::StartUp is called.

// Following this method (and all other TEST_F defined methods),

//CollectionTest::TearDown is called

TEST_F(CollectionTest, CollectionSmartPointerIsNotNull)

{

// is the collection created

ASSERT_TRUE(collection);

// if empty, the size must be 0

ASSERT_NE(collection.get(), nullptr);

}

// Test that a collection is empty when created.

TEST_F(CollectionTest, IsEmptyOnCreate)

{

// is the collection empty?

ASSERT_TRUE(collection->empty());

// if empty, the size must be 0

ASSERT_EQ(collection->size(), 0);

}

/* Comment this test out to prevent the test from running

* Uncomment this test to see a failure in the test explorer */

TEST_F(CollectionTest, AlwaysFail)

{

FAIL();

}

// TODO: Create a test to verify adding a single value to an empty collection

TEST_F(CollectionTest, CanAddToEmptyVector)

{

// is the collection empty?

// if empty, the size must be 0

add_entries(1);

// is the collection still empty?

// if not empty, what must the size be?

}

// TODO: Create a test to verify adding five values to collection

TEST_F(CollectionTest, CanAddFiveValuesToVector)

{

add_entries(5);

}

// TODO: Create a test to verify that max size is greater than or equal to size for 0, 1, 5, 10 entries

// TODO: Create a test to verify that capacity is greater than or equal to size for 0, 1, 5, 10 entries

// TODO: Create a test to verify resizing increases the collection

// TODO: Create a test to verify resizing decreases the collection

// TODO: Create a test to verify resizing decreases the collection to zero

// TODO: Create a test to verify clear erases the collection

// TODO: Create a test to verify erase(begin,end) erases the collection

// TODO: Create a test to verify reserve increases the capacity but not the size of the collection

// TODO: Create a test to verify the std::out_of_range exception is thrown when calling at() with an index out of bounds

// NOTE: This is a negative test

// TODO: Create 2 unit tests of your own to test something on the collection - do 1 positive & 1 negative

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transport Operations

Authors: Allen Stuart

2nd Edition

978-0470115398, 0470115394

Students also viewed these Programming questions

Question

Links to relevant sites on the web

Answered: 1 week ago