Skip to content

Commit

Permalink
Unit Tests added for Chapter12
Browse files Browse the repository at this point in the history
  • Loading branch information
Archiit19 committed Oct 12, 2019
1 parent adade0a commit 417cf36
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 63 deletions.
42 changes: 42 additions & 0 deletions Chapter12/Exercise73/Exercise73_Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//Chapter 12 : Exercise 73

#include "pch.h"
#include <iostream>
#include <memory>
#include <typeinfo>
#include "gtest/gtest.h"
using namespace std;

std::ostringstream out;

std::string TestCase() {
string str;
string str1("Hello, I'm a string!");
string str2(str1);
string str3(str1, 0, 5);
string str4("Hello, I'm a string!", 0, 5);
string str5(10, 'x');
string str6(str4.begin() + 1, str4.end() - 1);

out << str << endl;
out << str1 << endl;
out << str2 << endl;
out << str3 << endl;
out << str4 << endl;
out << str5 << endl;
out << str6 << endl;

return out.str();
}

TEST(Chapter12, Exercise73) {

EXPECT_EQ("\nHello, I'm a string!\nHello, I'm a string!\nHello\nHello\nxxxxxxxxxx\nell\n", TestCase());
}

int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}
46 changes: 46 additions & 0 deletions Chapter12/Exercise74/Exercise74_Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//Chapter 12 : Exercise 74

#include "pch.h"
#include <iostream>
#include <memory>
#include <typeinfo>
#include "gtest/gtest.h"
using namespace std;

std::ostringstream out;

bool isPalindrome(const string& str)
{
// make a copy of the string
string s(str.begin(), str.end());

// remove any spaces or punctuation
s.erase(remove_if(s.begin(), s.end(), [](const char& c) { return ispunct(c) || isspace(c); }), s.end());
// lower case what's left
transform(s.begin(), s.end(), s.begin(), ::tolower);
// create a reversed version of the string
string sr(s.rbegin(), s.rend());

// compare them
return (s == sr);
}

std::string TestCase() {
string str = "Never odd or even";

out << "'" << str << "'" << " is a palindrome? " << (isPalindrome(str) ? "YES" : "NO") << endl;

return out.str();
}

TEST(Chapter12, Exercise74) {

EXPECT_EQ("'Never odd or even' is a palindrome? YES\n", TestCase());
}

int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}
35 changes: 35 additions & 0 deletions Chapter12/Exercise75/Exercise75_Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Chapter 12 : Exercise 75

#include "pch.h"
#include <iostream>
#include <memory>
#include <typeinfo>
#include "gtest/gtest.h"
using namespace std;

std::ostringstream out;

std::string TestCase() {
vector<int> vec = { 1,2,3,4,5,6,7,8,9,10 };

for (auto v : vec)
{
out << v << " ";
}

out << vec[3];

return out.str();
}

TEST(Chapter12, Exercise75) {

EXPECT_EQ("1 2 3 4 5 6 7 8 9 10 4", TestCase());
}

int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}
59 changes: 59 additions & 0 deletions Chapter12/Exercise76/Exercise76_Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//Chapter 12 : Exercise 76

#include "pch.h"
#include <iostream>
#include <memory>
#include <typeinfo>
#include "gtest/gtest.h"
using namespace std;

std::ostringstream out;
class Track
{
public:

Track(float length, string name, int popularity) : m_trackLength(length), m_trackName(name), m_popularityRating(popularity) {}

float getLength() const { return m_trackLength; }
string getName() const { return m_trackName; }
int getPopularity() const { return m_popularityRating; }

private:

float m_trackLength;
string m_trackName;
int m_popularityRating;
};
bool trackLengthCompare(const Track& t1, const Track& t2)
{
return (t1.getLength() < t2.getLength());
}

std::string TestCase() {
vector<Track> tracks;

tracks.push_back(Track(199.0f, "God's Plan", 100));
tracks.push_back(Track(227.0f, "Hold On, We're Going Home", 95));
tracks.push_back(Track(182.0f, "The Motto", 80));

sort(tracks.begin(), tracks.end(), trackLengthCompare);

for (auto t : tracks)
{
out << t.getName() << endl;
}

return out.str();
}

TEST(Chapter12, Exercise76) {

EXPECT_EQ("The Motto\nGod's Plan\nHold On, We're Going Home\n", TestCase());
}

int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}
120 changes: 59 additions & 61 deletions Chapter12/Exercise77/Exercise77.cpp
Original file line number Diff line number Diff line change
@@ -1,78 +1,76 @@
#include <iostream>

#include <map>

#include <string>

#include <vector>

using namespace std;

class Question {
public:
public:

Question(int questionNumber, string question, int answerIndex): m_questionNumber(questionNumber), m_question(question), m_answerIndex(answerIndex) {}
Question(int questionNumber, string question, int answerIndex) : m_questionNumber(questionNumber), m_question(question), m_answerIndex(answerIndex) {}

int getQuestionNumber() const {
return m_questionNumber;
}
string getQuestion() const {
return m_question;
}
int getAnswerIndex() const {
return m_answerIndex;
}
int getQuestionNumber() const {
return m_questionNumber;
}
string getQuestion() const {
return m_question;
}
int getAnswerIndex() const {
return m_answerIndex;
}

private:
private:

int m_questionNumber;
string m_question;
int m_answerIndex;
int m_questionNumber;
string m_question;
int m_answerIndex;
};

struct QuestionCompare {
bool operator()(const Question & lhs,
const Question & rhs) const {
return lhs.getQuestionNumber() < rhs.getQuestionNumber();
}
bool operator()(const Question& lhs,
const Question& rhs) const {
return lhs.getQuestionNumber() < rhs.getQuestionNumber();
}
};
int main() {
map < Question, vector < string > , QuestionCompare > quiz;
Question question1(1, "Which two actors directed themselves in movies and won Oscars for Best Actor?", 2);
vector < string > question1Answers = {
"Al Pacino and Timothy Hutton",
"Jack Nicholson and Kevin Spacey",
"Laurence Olivier and Roberto Benigni",
"Tom Hanks and Paul Newman"
};
Question question2(2, "\"After all, tomorrow is another day!\" was the last line in which Oscar-winning Best Picture?", 0);
vector < string > question2Answers = {
"Gone With the Wind",
"Great Expectations",
"Harold and Maude",
"The Matrix"
};
quiz.insert(make_pair(question1, question1Answers));
quiz.insert(make_pair(question2, question2Answers));
cout << "Welcome to the movie quiz" << endl;
cout << "Type your answer between 1-4 and press enter:" << endl;
map < Question, vector < string >> ::iterator quizIterator = quiz.begin();
vector < bool > correctAnswers;
while (quizIterator != quiz.end()) {
cout << quizIterator - > first.getQuestion() << endl;
int answerIndex = 1;
for (auto answer: quizIterator - > second) {
cout << answerIndex << " : " << answer << endl;
answerIndex++;
}
int answer;
cin >> answer;
int correctAnswer = quizIterator - > first.getAnswerIndex();
bool wasCorrect = answer - 1 == correctAnswer;
cout << (wasCorrect ? "CORRECT!" : "INCORRECT!") << " Correct answer is: " << quizIterator - > second[correctAnswer] << endl;
if (wasCorrect)
correctAnswers.push_back(answer);
quizIterator++;
}
cout << "Your score was " << correctAnswers.size() << " out of " << quiz.size() << endl;
cout << "done";
map < Question, vector < string >, QuestionCompare > quiz;
Question question1(1, "Which two actors directed themselves in movies and won Oscars for Best Actor?", 2);
vector < string > question1Answers = {
"Al Pacino and Timothy Hutton",
"Jack Nicholson and Kevin Spacey",
"Laurence Olivier and Roberto Benigni",
"Tom Hanks and Paul Newman"
};
Question question2(2, "\"After all, tomorrow is another day!\" was the last line in which Oscar-winning Best Picture?", 0);
vector < string > question2Answers = {
"Gone With the Wind",
"Great Expectations",
"Harold and Maude",
"The Matrix"
};
quiz.insert(make_pair(question1, question1Answers));
quiz.insert(make_pair(question2, question2Answers));
cout << "Welcome to the movie quiz" << endl;
cout << "Type your answer between 1-4 and press enter:" << endl;
map < Question, vector < string >> ::iterator quizIterator = quiz.begin();
vector < bool > correctAnswers;
while (quizIterator != quiz.end()) {
cout << quizIterator->first.getQuestion() << endl;
int answerIndex = 1;
for (auto answer : quizIterator->second) {
cout << answerIndex << " : " << answer << endl;
answerIndex++;
}
int answer;
cin >> answer;
int correctAnswer = quizIterator->first.getAnswerIndex();
bool wasCorrect = answer - 1 == correctAnswer;
cout << (wasCorrect ? "CORRECT!" : "INCORRECT!") << " Correct answer is: " << quizIterator->second[correctAnswer] << endl;
if (wasCorrect)
correctAnswers.push_back(answer);
quizIterator++;
}
cout << "Your score was " << correctAnswers.size() << " out of " << quiz.size() << endl;
cout << "done";
}
Loading

0 comments on commit 417cf36

Please sign in to comment.