diff --git a/Makefile b/Makefile index 7360639..fd5b1e3 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ $(APP): setup $(OBJS) $(CXX) -o $(APP) $(OBJS) $(LDFLAGS) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp - $(CXX) -c $< $(CXX_FLAGS) $(TEST_CXX_FLAGS) $(INCLUDES) -o $@ + $(CXX) -c $< $(CXX_FLAGS) $(TEST_CXX_FLAGS) $(INCLUDES) $(TEST_INCLUDES) -o $@ $(TEST_OBJ_DIR)/%.o: $(TEST_DIR)/%.cpp $(CXX) -c $< $(CXX_FLAGS) $(TEST_CXX_FLAGS) $(INCLUDES) $(TEST_INCLUDES) -o $@ diff --git a/src/Main.cpp b/src/Main.cpp index f916175..5b6238e 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -12,6 +12,9 @@ #include "Primos.h" #include "SalvaCalculo.h" #include +#include + +using namespace std; #ifdef WITH_UNIT_TEST #include @@ -27,7 +30,6 @@ int main(int argc, char** argv) { } #else -using namespace std; void imprimeCalculo(Calculo *calculo) { printf("%s\n", calculo->nome().c_str()); @@ -35,6 +37,15 @@ void imprimeCalculo(Calculo *calculo) { for (unsigned int i = 0; i < calculo->numeroResultados(); i++) { printf("%d\t\t%d\n", i, calculo->resultado(i)); } + string with_sep = calculo->toString(';'); + cout << endl; + cout <<"teste do separador com ;" << endl; + cout << with_sep << endl; + + string with_sep2 = calculo->toString(','); + cout << endl; + cout <<"teste do separador com ," << endl; + cout << with_sep2 << endl; } /* diff --git a/src/Primos.cpp b/src/Primos.cpp index d784f73..e9b533c 100644 --- a/src/Primos.cpp +++ b/src/Primos.cpp @@ -1,6 +1,11 @@ #include "Primos.h" #include #include +#include +#include + + +using namespace std; Primos::Primos(int inicio, unsigned int tamanho, Interceptador *interceptador) : Calculo(inicio, tamanho, interceptador) { this->resultados.reserve(tamanho); @@ -38,8 +43,20 @@ int Primos::resultado(unsigned int indice) { } string Primos::toString(char sep){ - // TODO: Implementar - return ""; + string str; + string a; + unsigned int i = 0; + char s[2] = {sep, 0}; + for(vector::iterator it = this->resultados.begin(); it != this->resultados.end(); it++){ + a = to_string(*it); + str.append(a); + if(i < (this->resultados.size() - 1)){ + str.append(s); + } + i++; + } + + return str; } void Primos::limpaCalculo() { diff --git a/test/TestPrimos.cpp b/test/TestPrimos.cpp index 3dcfb8d..15f13c6 100644 --- a/test/TestPrimos.cpp +++ b/test/TestPrimos.cpp @@ -4,6 +4,7 @@ * and open the template in the editor. */ #include +#include #include "Primos.h" @@ -22,10 +23,26 @@ class TestPrimos : public ::testing::Test { }; -TEST_F(TestPrimos, Teste) { - EXPECT_TRUE(primos->resultado(0) == 0); - EXPECT_TRUE(primos->numeroResultados() == 0); -} +class TestPrimosInterceptador : public ::testing::Test { + +protected: + Calculo* primos; + MockRepository *mocks; + Interceptador *interceptador; + + virtual void SetUp( ) { + mocks = new MockRepository(); + interceptador = mocks->Mock(); + primos = new Primos(0, 10, interceptador); + primos->calcula(); + } + + virtual void TearDown( ) { + mocks->OnCallDestructor(interceptador); + delete primos; + } + +}; //////////////////// @@ -48,6 +65,11 @@ class TestPrimosInitialize : public ::testing::Test { }; +TEST_F(TestPrimos, Teste) { + EXPECT_TRUE(primos->resultado(0) == 0); + EXPECT_TRUE(primos->numeroResultados() == 0); +} + TEST_F(TestPrimosInitialize, TesteDeTamanho){ EXPECT_TRUE(primos->numeroResultados() == 10); } @@ -68,4 +90,18 @@ TEST_F(TestPrimosInitialize, TesteDeResultado){ TEST_F(TestPrimosInitialize, TesteDeNome) { EXPECT_STREQ(primos->nome().c_str(), "Primos"); +} + +TEST_F(TestPrimosInitialize, TesteToString) { + EXPECT_STREQ(primos->toString(';').c_str(), "2;3;5;7;11;13;17;19;23;29"); + EXPECT_STREQ(primos->toString(',').c_str(), "2,3,5,7,11,13,17,19,23,29"); + } + +TEST_F(TestPrimosInterceptador, TesteDeResultadoZero) { + mocks->ExpectCall(this->interceptador, Interceptador::intercepta).With(7).Return(15); + mocks->ExpectCall(this->interceptador, Interceptador::intercepta).With(11).Return(66); + mocks->ExpectCall(this->interceptador, Interceptador::intercepta).With(17).Return(44); + EXPECT_TRUE(primos->resultado(3) == 15); + EXPECT_FALSE(primos->resultado(4) == 99); + EXPECT_TRUE(primos->resultado(6) == 44); } \ No newline at end of file