Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "DocumentParagraphTest.h"
#include "Document.h"
#include "DocumentParagraph.h"
#include <memory>
#include <string>
using namespace std;
namespace DocumentParagraphTesting {
typedef shared_ptr<string> _sps;
class TestInit: public UnitTest
{
public:
TestInit() { testName = "DocumentParagraph Initialization"; }
void PerformTest()
{
DocumentParagraph para(10,10);
AssertEquals(para.GetLineCount(),1);
AssertEquals(para.GetString(),string());
}
};
class TestSetParagraphNumber: public UnitTest
{
public:
TestSetParagraphNumber() { testName = "TestSetParagraphNumber"; }
void PerformTest()
{
DocumentParagraph para(10,10);
para.SetParagraphNumber(1);
AssertEquals(para.GetParagraphNumber(),1);
para.SetParagraphNumber(2);
AssertEquals(para.GetParagraphNumber(),2);
}
};
class TestAppendWord: public UnitTest
{
public:
TestAppendWord() { testName = "TestAppendWord"; }
void PerformTest()
{
DocumentParagraph para(10,10);
AssertEquals(para.GetLineCount(),1);
cout << "We've initialized properly" << endl;
_sps twoLetterWord = _sps(new string("12"));
for (int i = 0; i<3; ++i) {
AssertTrue(para.AppendWord(twoLetterWord));
}
AssertEquals(para.GetLineCount(),1);
AssertEquals(para.GetString().length(),(size_t)8);
cout << "String length should be 9: " << para.GetString().length() << endl;
cout << "Added three instances of '12'. Line count: " << para.GetLineCount() << endl;
cout << "This is valid as 9 < 10" << endl;
// This causes a new line to be formed. It should have worked
AssertTrue(para.AppendWord(twoLetterWord));
AssertEquals(para.GetLineCount(),2);
cout << "We've appended a bunch of short words in order to force a new row to be created in the paragraph" << endl;
cout << "Progress Check for two rows: " << Passed() << endl;
_sps eightLetters = _sps(new string("12345678"));
para.AppendWord(eightLetters);
AssertEquals(para.GetLineCount(),3);
}
};
class TestAppendABunchOfEmptyWords: public UnitTest
{
public:
TestAppendABunchOfEmptyWords() { testName = "GetLine"; }
void PerformTest()
{
_sps word = _sps(new string());
DocumentParagraph para(10,10);
for (int i = 0; i<11; ++i) {
para.AppendWord(word);
}
AssertEquals(para.GetLineCount(),1);
AssertEquals(para.GetString(),string(" "));
para.AppendWord(word);
AssertEquals(para.GetLineCount(),2);
AssertEquals(para.GetString(),string(" "));
}
};
class TestWordLengthEqualsLineLength: public UnitTest
{
public:
TestWordLengthEqualsLineLength() { testName = "Test Word length equals line length"; }
void PerformTest()
{
DocumentParagraph para(10,10);
_sps word = _sps(new string("1234567890"));
AssertTrue(para.AppendWord(word));
cout << "Result of first Addition: " << Passed() << endl;
AssertTrue(para.AppendWord(word));
cout << "Result of second Addition: " << Passed() << endl;
AssertEquals(para.GetLineCount(),2);
}
};
class TestGetLineLength: public UnitTest
{
public:
TestGetLineLength() { testName = "GetLineLength"; }
void PerformTest()
{
DocumentParagraph para(10,10);
string str = "Hello world. Who are you. Tisis iawidj dfjkas whf sis qw d skhfs .";
vector<_sps> words = Document::SplitString(str);
for (_sps word : words) {
AssertTrue(para.AppendWord(word));
}
for (int i = 0; i<para.GetLineCount(); ++i) {
AssertTrue(para.GetLine(i).length() == para.GetLineLength(i));
}
}
};
class TestMaxHeightLimitsParagraph: public UnitTest
{
public:
TestMaxHeightLimitsParagraph() { testName = "Test to see if the max height actually limits the paragraph growth"; }
void PerformTest()
{
DocumentParagraph para(10,10);
_sps word = _sps(new string("1234567890"));
for (int i = 0; i<10; ++i) {
AssertTrue(para.AppendWord(word));
}
cout << "Can't even append an empty word to a full paragraph" << endl;
_sps empty = _sps(new string());
AssertFalse(para.AppendWord(empty));
}
};
class TestTranslateCol: public UnitTest
{
public:
TestTranslateCol() { testName = "Translate Column"; }
void PerformTest()
{
DocumentParagraph para(10,10);
_sps word1 = _sps(new string("a"));
_sps word2 = _sps(new string("1234567890"));
AssertTrue(para.AppendWord(word1));
AssertTrue(para.AppendWord(word2));
int row = 1;
int col = 2;
cout << "The goal here is to land on the '3' in the second line" << endl;
int result = para.TranslateCol(row,col);
cout << "The expected output is col 4: " << result << endl;
AssertEquals(result,4);
AssertTrue(para.AppendWord(word2));
result = para.TranslateCol(2,5);
cout << "Now we're aiming for the 6 in the 3rd line" << endl;
AssertEquals(result,18);
string paraStr = para.GetString();
AssertEquals(paraStr[18],'6');
AssertEquals(paraStr[4],'3');
para = DocumentParagraph(10,10);
cout << "Now we're going to have two words per paragraph" << endl;
word1 = _sps(new string("1234"));
word2 = _sps(new string("5678"));
_sps word3 = _sps(new string());
AssertTrue(para.AppendWord(word1));
AssertTrue(para.AppendWord(word2));
AssertTrue(para.AppendWord(word3));
AssertTrue(para.AppendWord(word1));
AssertTrue(para.AppendWord(word2));
AssertEquals(para.GetString(),string("1234 5678 1234 5678"));
cout << "Character 11 in the paragraph is: " << para.GetString()[11] << endl;
AssertEquals(para.GetString()[11],'1');
AssertEquals(para.TranslateCol(1,0),11);
}
};
}
DocumentParagraphTest :: DocumentParagraphTest()
{
using namespace DocumentParagraphTesting;
testName = "DocumentParagraph";
tests.push_back(new TestInit());
tests.push_back(new TestSetParagraphNumber());
tests.push_back(new TestAppendWord());
tests.push_back(new TestAppendABunchOfEmptyWords());
tests.push_back(new TestWordLengthEqualsLineLength());
tests.push_back(new TestGetLineLength());
tests.push_back(new TestMaxHeightLimitsParagraph());
tests.push_back(new TestTranslateCol());
}
DocumentParagraphTest :: ~DocumentParagraphTest()
{
}