Here's what you are trying to do:
Code:
# Makefile
CXXFLAGS=-g
LDFLAGS=-g
LDLIBS=-lm -lgtest
TARGET=test
OBJECTS=test.o whattotest.o
$(TARGET): CC=$(CXX)
$(TARGET): $(OBJECTS)
test.o: test.cc whattotest.hpp
whattotest.o: whattotest.cc whattotest.hpp
.PHONY: clean distclean
clean:
-rm $(OBJECTS)
distclean: clean
rm $(TARGET)
Code:
// test.cc
#include <gtest/gtest.h>
#include "whattotest.hpp"
TEST ( SquareRootTest, PositiveNos )
{
ASSERT_EQ ( 6, squareRoot ( 36.0 ) );
ASSERT_EQ ( 18.0, squareRoot ( 324.0 ) );
ASSERT_EQ ( 25.4, squareRoot ( 645.16 ) );
ASSERT_EQ ( 0, squareRoot ( 0.0 ) );
}
TEST ( SquareRootTest, NegativeNos )
{
ASSERT_EQ ( -1.0, squareRoot ( -15.0 ) );
ASSERT_EQ ( -1.0, squareRoot ( -0.2 ) );
}
int main ( int argc, char **argv )
{
testing::InitGoogleTest ( &argc, argv );
return RUN_ALL_TESTS();
}
Code:
// whattotest.cc
#include <math.h>
#include "whattotest.hpp"
double squareRoot ( double a )
{
double b = sqrt ( a );
if ( isnan(b) ) // nan check
return -1.0;
return b;
}
Code:
// whattotest.hpp
#ifndef WHATTOTEST_HPP_
#define WHATTOTEST_HPP_
double squareRoot(double);
#endif
Code:
$ make
g++ -g -c -o test.o test.cc
g++ -g -c -o whattotest.o whattotest.cc
g++ -g test.o whattotest.o -lm -lgtest -o test
$ ./test
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from SquareRootTest
[ RUN ] SquareRootTest.PositiveNos
[ OK ] SquareRootTest.PositiveNos (0 ms)
[ RUN ] SquareRootTest.NegativeNos
[ OK ] SquareRootTest.NegativeNos (0 ms)
[----------] 2 tests from SquareRootTest (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (0 ms total)
[ PASSED ] 2 tests.