First, why still can't I run my mainFib.cpp!? Whatever I try to run that exact file, the compiler just runs the .lib file and gives the error! |
You do
not run "mainFib.cpp". You run the
executable file that is created when building your "main" project. Your executable file is built from the "mainFib.cpp" source file and any additional libraries that you link in.
Also: When you click the "run" icon in Visual Studio (or press F5), then it will run the
default project in the current solution. Be sure that the "main" project is selected as
default project! (
not the static library)
How to test that please?
Should I create a vector in test.cpp and run the function using it and then check the result!? |
First of all, you add that new function to your
static library, just like you did with the Fibonacci function. Again, with the
implementation in the
.cpp file and the corresponding
declaration in the header (
.h) file.
Then you can call this new function from your "main" project as well as form the GoogleTest project, just like you did with the Fibonacci function. And, yes, if that function expects an
std::vector
as parameter, then you'll need to pass one! Set up vector, pass it to the function, then check the results. Something like:
1 2 3 4 5 6 7 8 9
|
/* add2.h */
#ifndef _INC_ADD2_H
#define _INC_ADD2_H
#include <vector> // <-- note!
void add2(std::vector<int> &vec);
#endif
|
1 2 3 4 5 6 7 8
|
/* add2.cpp */
#include "add2.h"
void add2(std::vector<int> &vec)
{
for (auto& v : vec)
v += 2;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include "gtest/gtest.h"
#include "add2.h"
TEST(Add2Test, SampleTest)
{
std::vector<int> vec;
vec.push_back( 0);
vec.push_back( 1);
vec.push_back(40);
EXPECT_EQ( 0, vec[0]);
EXPECT_EQ( 1, vec[1]);
EXPECT_EQ(40, vec[2]);
add2(vec);
EXPECT_EQ( 2, vec[0]);
EXPECT_EQ( 3, vec[1]);
EXPECT_EQ(42, vec[2]);
}
|
(BTW: I don't think you can use
auto
in the function declaration)