-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 5 tests and fixed a bug in the process!
- Loading branch information
1 parent
c17169f
commit dcf2375
Showing
7 changed files
with
185 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,175 @@ | ||
#include "PrecompiledHeader.h" | ||
|
||
#if _TESTBUILD | ||
|
||
#include "CppUnitTest.h" | ||
#include "Utilities\Utilities.h" | ||
|
||
using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||
using namespace std; | ||
using namespace Utilities; | ||
|
||
namespace RemoteFileBrowser | ||
TEST_CLASS(LoggingTests) | ||
{ | ||
TEST_CLASS(LoggingTests) | ||
private: | ||
wstring m_LogFileName; | ||
|
||
public: | ||
LoggingTests() : | ||
m_LogFileName(Logging::GetLogFileName()) | ||
{ | ||
public: | ||
|
||
TEST_METHOD(TestMethod1) | ||
} | ||
|
||
template <typename LogAction, typename AssertAction> | ||
void DoTest(LogAction logAction, AssertAction assertAction) | ||
{ | ||
Logging::Initialize(true); | ||
logAction(); | ||
Logging::Shutdown(); | ||
|
||
assertAction(); | ||
} | ||
|
||
vector<wstring> GetLogFileContents() | ||
{ | ||
wifstream in(m_LogFileName); | ||
vector<wstring> input; | ||
wstring line; | ||
|
||
while (!in.eof()) | ||
{ | ||
// TODO: Your test code here | ||
getline(in, line); | ||
|
||
if (!in.fail()) | ||
{ | ||
input.push_back(move(line)); | ||
} | ||
} | ||
|
||
}; | ||
} | ||
return input; | ||
} | ||
|
||
void AssertEndsWith(const wstring& expected, const wstring& actual) | ||
{ | ||
Assert::AreEqual(expected.c_str(), actual.c_str() + actual.length() - expected.length()); | ||
} | ||
|
||
void AssertEndsWith(const vector<wstring>& expectedLines, const vector<wstring>& actualLines) | ||
{ | ||
Assert::AreEqual(expectedLines.size(), actualLines.size()); | ||
|
||
for (auto i = 0u; i < actualLines.size(); i++) | ||
{ | ||
AssertEndsWith(expectedLines[i], actualLines[i]); | ||
} | ||
} | ||
|
||
TEST_METHOD(CanOutputMessage) | ||
{ | ||
DoTest( | ||
[]() | ||
{ | ||
Logging::OutputMessage("Hello, world!"); | ||
}, | ||
|
||
[this]() | ||
{ | ||
Assert::AreEqual(L"Hello, world!", GetLogFileContents()[0].c_str()); | ||
} | ||
); | ||
} | ||
|
||
TEST_METHOD(CanLogOnce) | ||
{ | ||
DoTest( | ||
[]() | ||
{ | ||
Logging::Log("Hello, world!"); | ||
}, | ||
|
||
[this]() | ||
{ | ||
AssertEndsWith(L"Hello, world!", GetLogFileContents()[0]); | ||
} | ||
); | ||
} | ||
|
||
TEST_METHOD(CanLogMultipleMessages) | ||
{ | ||
DoTest( | ||
[]() | ||
{ | ||
Logging::Log("Hell", "o, w", "orld!"); | ||
}, | ||
|
||
[this]() | ||
{ | ||
AssertEndsWith(L"Hello, world!", GetLogFileContents()[0].c_str()); | ||
} | ||
); | ||
} | ||
|
||
TEST_METHOD(CanLogMultipleTimes) | ||
{ | ||
DoTest( | ||
[]() | ||
{ | ||
Logging::Log("Hello, world!"); | ||
Logging::Log("Hello, world!"); | ||
Logging::Log("Hello, world!"); | ||
}, | ||
|
||
[this]() | ||
{ | ||
vector<wstring> expected; | ||
expected.push_back(L"Hello, world!"); | ||
expected.push_back(L"Hello, world!"); | ||
expected.push_back(L"Hello, world!"); | ||
AssertEndsWith(expected, GetLogFileContents()); | ||
} | ||
); | ||
} | ||
|
||
static const int kThreadCountForThreadSafeTest = 10; | ||
static const int kMessagesPerThreadForThreadSafeTest = 20; | ||
|
||
TEST_METHOD(LogIsThreadSafe) | ||
{ | ||
DoTest( | ||
[]() | ||
{ | ||
thread threads[kThreadCountForThreadSafeTest]; | ||
|
||
for (int i = 0; i < kThreadCountForThreadSafeTest; i++) | ||
{ | ||
threads[i] = thread([]() | ||
{ | ||
for (int j = 0; j < kMessagesPerThreadForThreadSafeTest; j++) | ||
{ | ||
Logging::Log("Hello, world!"); | ||
} | ||
}); | ||
} | ||
|
||
for (int i = 0; i < kThreadCountForThreadSafeTest; i++) | ||
{ | ||
threads[i].join(); | ||
} | ||
}, | ||
|
||
[this]() | ||
{ | ||
vector<wstring> expected; | ||
|
||
for (int i = 0; i < kThreadCountForThreadSafeTest * kMessagesPerThreadForThreadSafeTest; i++) | ||
{ | ||
expected.push_back(L"Hello, world!"); | ||
} | ||
|
||
AssertEndsWith(expected, GetLogFileContents()); | ||
} | ||
); | ||
} | ||
}; | ||
|
||
#endif // _TESTBUILD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters