Jump to content
  • Advertisement
  • entries
    23
  • comments
    12
  • views
    1967

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

8Observer8

1759 views

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

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]);
    }
}

 

 



3 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

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
  • What is your GameDev Story?

    In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

    (You must login to your GameDev.net account.)

  • Blog Entries

  • Similar Content

    • By belfegor
      I had some code that worked before and it doesn't now. I have created minimal c++ project that can reproduce problem.
      #include <iostream> void PrintFooM(); class Foo { int m; explicit Foo(int dummy) {} Foo(); public: static Foo& GetInstance() { static Foo f; return f; } int GetM() const { return m; } }; Foo::Foo() : Foo(-1) { m = 3; PrintFooM(); } void PrintFooM() { std::cout << "M = " << Foo::GetInstance().GetM() << std::endl; } int main() { Foo::GetInstance(); return 0; } Code runs till PrintFooM function is called and its stuck there (like in some endless loop). Never prints anything in console.
      I am using VS2017 community.
    • By ArcEcho
      Recently,I am studying bullet3 and DirectX 11.And I got some wired results like the attachments shows.It should be a sphere not a flat plane.
      I extended the btIDebugDraw to draw lines.The draw line api works correctly independently.And I traced the call stack and found there was some thing wrong with the bullet transform .I have no idea about how it acts like that. So I ask for help here.Any help or tips will  be apreciated.
       


    • By maltman
      I have used EnTT for a few half finished projects. I like it because, as a more intermediate programmer, it forced me into the pattern. Sort of like coding on rails a bit in a good way.
      Part of my feels that it is too much of a bother for most projects. Another part of me feels like that the ECS pattern is only going to get more common and that I should be concerned about data locality and speed no matter what (otherwise why am I even writing in C++?). I've honestly never had a project which needed the extra performance boost that an ECS library would give me. Usually my bottleneck is inefficient collision detection and noob rendering. 
      Two parts of my mind are conflicting: one tells me that using a library is great because it lets me how things are supposed to be done, and that I should use a library of every type before I try to roll my own. Another part of my mind tells me that I will never fully learn this stuff without rolling my own and that I am wasting my time using a library when I can't even roll my own and don't know completely how it all works.
      Anyhow, let me know your thoughts and experiences with ECS libraries.
       
    • By ADDMX
      Hi
      It is possible at all to copy descriptors between _two different_ heaps ? The documentation says so:
       
      First (source) heap is:
          D3D12_DESCRIPTOR_HEAP_DESC HeapDesc;     HeapDesc.NumDescriptors = 256;     HeapDesc.Type           = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;     HeapDesc.Flags          = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;     HeapDesc.NodeMask       = 0; and SrcHeap->GetCPUDescriptorHandleForHeapStart() ==> Handle.ptr == 4 (strange, value indeed, I'd expected ptr as in case of GPU handles)
      Second (destination) heap is:
          HeapDesc.NumDescriptors = 128;     HeapDesc.Type           = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;     HeapDesc.Flags          = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;     HeapDesc.NodeMask       = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; and DstHeap->GetCPUDescriptorHandleForHeapStart() ==> Handle.ptr == 9 (strange, value indeed, I'd expected ptr as in case of GPU handles)
      and I want to copy elements 5, 6, and 7 from first one to the second one
      auto Increment = Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); // Return 32 CD3DX12_CPU_DESCRIPTOR_HANDLE Src = CD3DX12_CPU_DESCRIPTOR_HANDLE(SrcHeap->GetCPUDescriptorHandleForHeapStart(), 5, Increment); CD3DX12_CPU_DESCRIPTOR_HANDLE Dst = CD3DX12_CPU_DESCRIPTOR_HANDLE(DstHeap->GetCPUDescriptorHandleForHeapStart(), 0, Increment); Device->CopyDescriptorsSimple(3, Dst, Src, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); and debug layers says:
      D3D12 ERROR: ID3D12Device::CopyDescriptors: Source ranges and dest ranges overlap, which results in undefined behavior. [ EXECUTION ERROR #653: COPY_DESCRIPTORS_INVALID_RANGES]
      and indeed samplers are not copied to the shader visible descriptors heap ... why ?
      I have win10 1809 (x64), latest nvidia drivers and 2080RTX (I do not have any other cards, and device is initialized on 2080RTX)
      I'v compilled ModelViewer from DXSamples MiniEngine ... and it spills out that same error from within it's DynamicDescriptorHeap implementation :/
       
       
    • By Tarrasque
      Hello, 
      I am new to game development and am 4 courses away from obtaining an undergraduate degree in the field where I am specializing in game play programming. The course I am currently in is designed to help us with the transition period from being a student and entering the job search and making this passion a profession. By the end of the course I should have a pretty good start on what will become a portfolio.
      This course is what actually brought me to this site and suggested it would be a great place to get support and to provide support when our skill level was up enough to be able to help others. So first, I would like to say hello to anyone who has taken the time to read my very first post here and a special thanks to anyone that replies with any feedback at all. I really do appreciate any and all help or criticism. 
      We were asked to create a proof of concept this week and will eventually need to create a prototype that illustrates our skills in programming. I always want to push myself to see what I can accomplish and decided to come up with an inventory system. I am not sure if it is an original thought, but I feel it would be unique enough to help me stand out from the crowd. At least I hope. 
      The concept is that a player can equip and fill a backpack. They can then drop the filled backpack and all the items will remain accounted for within it. The play can then pickup another backpack and repeat this process. It is important to note that if this is used in a multiplayer game, that another player can loot that filled backpack or pick it up and equip it.
      The question and support I am asking for is more in the realm of guidance. I have searched several places and have not had much luck in finding a good reference to build off from when it comes to developing a complex inventory system that is essentially independent from the player character. Would anyone have any tips or resource suggestions that may help me narrow the search down?
      Any suggestions would be greatly appreciated. 
      It might be important to note I plan on making the prototype using C++ within the Unreal Engine. 
      Thanks again for the time it took to read this post and I look forward to any feedback.
      Happy Gaming
      Devan Luciano
×

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!