Jump to content
  • Advertisement
  • entries
    24
  • comments
    20
  • views
    2647

C++ Set Up GTest for TDD. C++, VS

8Observer8

2492 views

Before I will start I want to advise you to read this book: The Art of Unit Testing: with examples in C#. Yes, as you can see this book contains examples in C# but it is not important. This book contain very useful and important information how to write unit tests.

I made an example of project in VS 2015: SortFunctions.zip  This project will show you how set up Google Test in Visual Studio and run simple unit tests.

Note. If you have another version of VS then before you will run unit tests you need to select VS 2017, like in this screenshot:

Spoiler

PlatformToolset.png.3c9a59798e0e30baeee97b337b6bbe48.png

Google Test library is included in the project as source folder and it is placed in "Libs" folder. You need to:

  • open the solution. The solution is file with name: "SortFunctions.sln"
  • select your version of VS, for example VS 2017 instead of VS 2015 as in screenshot above
  • make the "SortFunction_UnitTests" project as "StartUp Project". For this: make right mouse button click on the "SortFunction_UnitTests" project -> select "Set as StartUp Project"
  • press Ctrl+F5 to run unit tests

You will see this settings in the "SortFunction_UnitTests" project properties:

$(SolutionDir)Libs\gtest-1.8.1\include
$(SolutionDir)Libs\gtest-1.8.1
$(SolutionDir)SortFunction

This solution include two projects:

  • SortFunctions - this project contains modules that we want to test. For example, bubbleSort() method
  • SortFunctions_UnitTests - this project contains unit tests

Add existing files to the "SortFunctions_UnitTests" project:

  • YourSolutionDir/Libs/gtest-1.8.1/src/gtest-all.cc
  • YourSolutionDir/SortFunctions/SortFunctions.cpp

The "SortFunctions" project has two files:

SortFunctions.h

#pragma once
 
extern void bubbleSort(int *array, unsigned int amount);
 
extern void countingSort(int *array, unsigned int amount);

SortFunctions.cpp

#include "SortFunctions.h"
 
void bubbleSort(int *array, unsigned int amount)
{
 
}
 
void countingSort(int *array, unsigned int amount)
{
 
}

The "SortFunctions_UnitTests" project has tests. For example, this is the "bubbleSortTests.cpp" with two tests. The first test is for positive numbers and the second test is for negative numbers:

bubbleSortTests.cpp

#include <gtest/gtest.h>
 
#include "SortFunctions.h"
 
TEST(bubbleSortTest, AllPositiveElements)
{
    // Arrange
    const unsigned int amount = 5;
    int actualArray[amount] = { 5, 3, 10, 2, 7 };
    int expectedArray[amount] = { 2, 3, 5, 7, 10 };
 
    // Act
    bubbleSort(actualArray, amount);
 
    // Assert
    for (size_t i = 0; i < amount; i++)
    {
        ASSERT_EQ(expectedArray[i], actualArray[i]);
    }
}
 
TEST(bubbleSortTest, AllNegativeElements)
{
    // Arrange
    const unsigned int amount = 5;
    int actualArray[amount] = { -5, -3, -10, -2, -7 };
    int expectedArray[amount] = { -10, -7, -5, -3, -2 };
 
    // Act
    bubbleSort(actualArray, amount);
 
    // Assert
    for (size_t i = 0; i < amount; i++)
    {
        ASSERT_EQ(expectedArray[i], actualArray[i]);
    }
}

main.cpp

#include <gtest/gtest.h>

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

 



6 Comments


Recommended Comments

" I opened Setting of the project and add these lines to "C/C++" -> "General" -> "Additional Include Directories": "

It's a lot better creating .prop files for you projects under Property Manager. You can reuse them (put in a common folder) for every other project that uses similar libs/settings. Or you can copy and 'fork' them for a new project. It also relieves the frustration of not picking Release settings when in Debug or vice-versa (What a load of crap that is, right?). If interested, it's has a tab near the Solution Explorer pane tab, close to the bottom.

Oh and... Tests? Tests? We don't need no stinkin' tests. :D j/k

Share this comment


Link to comment
7 hours ago, fleabay said:

It's a lot better creating .prop files for you projects under Property Manager. You can reuse them (put in a common folder)

It useful for projects on my computer where each library has only on copy. But I include libraries in some projects because I need to public VS projects and everyone can open it and run them immediately. I make the "Libs" folder inside each such solution, put all necessary libraries to the "Libs" folder and connect them to the project using "$(SolutionDir)Libs\..." paths.

Share this comment


Link to comment

I added this information:

Quote

Before I will start I want to advise you to read this book: The Art of Unit Testing: with examples in C#. Yes, as you can see this book contains examples in C# but it is not important. This book contain very useful and important information how to write unit tests.

 

Edited by 8Observer8

Share this comment


Link to comment

I added:

Add existing files to the "SortFunctions_UnitTests" project:

  • YourSolutionDir/Libs/gtest-1.8.1/src/gtest-all.cc
  • YourSolutionDir/SortFunctions/SortFunctions.cpp

Share this comment


Link to comment

I add contents of the "main.cpp" for running the unit tests:

main.cpp

#include <gtest/gtest.h>

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

 

Edited by 8Observer8

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • Blog Entries

  • Similar Content

    • By Belfa96
      I've got a single vertex buffer and an index buffer for a cube in Directx9. I now want to generate a LPD3DXMESH  object from them, so that I can manage mouse picking and other stuff with the cube model. How can I achieve this? Here's the code of my vertex and index buffers:
      CUSTOMVERTEX vertices[] = { { -3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 0), }, { 3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 0), }, { -3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 0), }, { 3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 0), }, { -3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 0), }, { 3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 0), }, { -3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 0), }, { 3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 0), }, }; d3ddev->CreateVertexBuffer(8 * sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &vertex_buffer, NULL); VOID* pVoid; vertex_buffer->Lock(0, 0, (void**)&pVoid, 0); memcpy(pVoid, vertices, sizeof(vertices)); vertex_buffer->Unlock(); short indices[] = { 0, 1, 2, 2, 1, 3, 4, 0, 6, 6, 0, 2, 7, 5, 6, 6, 5, 4, 3, 1, 7, 7, 1, 5, 4, 5, 0, 0, 5, 1, 3, 7, 2, 2, 7, 6, }; d3ddev->CreateIndexBuffer(36 * sizeof(short), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &index_buffer, NULL); index_buffer->Lock(0, 0, (void**)&pVoid, 0); memcpy(pVoid, indices, sizeof(indices)); index_buffer->Unlock();  
    • By Tim Leijten
      Hi,
       
      I'm writing a game engine, when my API returns a pointer, most of the time it is meant to be used and then discarded right away. But I have some things where it might be useful to store the pointer.
      E.g. addNewGameObjectToScene. It would create a new object, add it to the scene, and return a pointer. But there is one obvious problem, there is no way to check if the returned pointer is valid. Because the newly created object could be deleted at any point, and then the pointer is invalid and using it would cause crashes. Therefore, to me it would make sense to use smart_pointers to fix that. But have found that not everyone agree's with me on that.(And I personally don't like to use smart pointers in api if I can avoid it)
      So what do you guys think? If I should use raw pointers, how would I fix the issue, and if I should use smart pointers, will it impact performance or drive people away from using my api?
       
      Thanks!
    • By Gnollrunner
      I'm currently using Direct  X 11 for my procedural world/MMO project.  I've tried to find a freeish game engine or simply graphics engine that supports 64 bit coordinates (CPU side), but apparently there is no such animal.  Right now my investment in Direct X is very small as the vast majorly of the code is straight C++ and the graphics is isolated into an interface class. However now I'm going get more into the GPU stuff and I was wondering if it would be better to switch to something else that would give the program better portability.  I was leaning towards Vulkan for no other reason than it seems to be all the rage (ha, ha), but I wanted to get some opinions from folks with more GPU programming experience.
    • By Belfa96
      I'm a total beginner with Directx/3D programming. I need help with implementing hardware instancing on Directx 11. I'm trying to render multiple cubes on the screen, to create some sort of Minecraft-esque voxel engine. The problem is, I don't know where to start to achieve this. This is how my "render frame" function looks:
      void RenderFrame(void) { D3DXMATRIX matView, matProjection; D3DXMATRIX matFinal; // create a view matrix D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 9.0f, 24.0f), // the camera position &D3DXVECTOR3(0.0f, 0.0f, 0.0f), // the look-at position &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); // the up direction // create a projection matrix D3DXMatrixPerspectiveFovLH(&matProjection, (FLOAT)D3DXToRadian(45), // field of view (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio 1.0f, // near view-plane 100.0f); // far view-plane // create the final transform matFinal = matView * matProjection; // clear the back buffer to a deep blue devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f)); // clear the depth buffer devcon->ClearDepthStencilView(zbuffer, D3D11_CLEAR_DEPTH, 1.0f, 0); // select which vertex buffer to display UINT stride = sizeof(VERTEX); UINT offset = 0; devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset); devcon->IASetIndexBuffer(pIBuffer, DXGI_FORMAT_R32_UINT, 0); // select which primtive type we are using devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // draw the Hypercraft devcon->UpdateSubresource(pCBuffer, 0, 0, &matFinal, 0, 0); devcon->DrawIndexed(24, 0, 0); // switch the back buffer and the front buffer swapchain->Present(0, 0); } Notice that there's a single vertex buffer containing the verteces of a cube, and index buffer containing its indeces. I want to render many (5000+) cubes on the screen at once on a single draw call, without performance issues, so I know instancing is the way to go, but I don't know how to implement it in my code. What changes do I need to do to my code in order to display multiple instances of the cube?
      Thanks in advance!
    • By Josheir
      I am trying to visualize if the attached diagram of data of triangles is correct for the attached hill image.  I used a nested for loop to find all triangles with any vertices having a height greater than zero.  The two triangles marked with an X surprisingly have no height values and the shape of the triangles is odd to me.  The triangles marked with A are left triangles and the triangles marked with B are right triangles.  The rendering was done in OpenGL with C++, and the X coordinates and  Z coordinates are by convention.  
      The one point with a height is shown here:
       
      void addHillsManually() { heightMapFromArray[20][20] = .5; } Thank you,
      Josheir


×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!