Skip to content

Commit

Permalink
Added 5 tests and fixed a bug in the process!
Browse files Browse the repository at this point in the history
  • Loading branch information
TautvydasZilys committed Oct 20, 2014
1 parent c17169f commit dcf2375
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ x64/
build/
[Bb]in/
[Oo]bj/
Intermediate/

# Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
!packages/*/build/
Expand Down
173 changes: 165 additions & 8 deletions RemoteFileBrowser/RemoteFileBrowser/LoggingTests.cpp
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_DEBUG;_TESTBUILD;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<PrecompiledHeaderFile>PrecompiledHeader.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(ProjectDir)</AdditionalIncludeDirectories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CriticalSection
CriticalSection& m_Section;

public:
inline Lock(CriticalSection criticalSection) :
inline Lock(CriticalSection& criticalSection) :
m_Section(criticalSection)
{
m_Section.Enter();
Expand Down
17 changes: 13 additions & 4 deletions RemoteFileBrowser/RemoteFileBrowser/Utilities/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ CriticalSection Logging::s_LogCriticalSection;
static HANDLE s_OutputFile;
static const wchar_t kLogFileName[] = L"LogFile.log";

void Logging::Initialize()
void Logging::Initialize(bool forceOverwrite)
{
s_OutputFile = CreateFile(kLogFileName, FILE_GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
auto openMode = forceOverwrite ? CREATE_ALWAYS : CREATE_NEW;
s_OutputFile = CreateFile(kLogFileName, FILE_GENERIC_WRITE, FILE_SHARE_READ, nullptr, openMode, FILE_ATTRIBUTE_NORMAL, nullptr);
Assert(s_OutputFile != INVALID_HANDLE_VALUE || !forceOverwrite);

if (s_OutputFile == INVALID_HANDLE_VALUE)
{
Expand All @@ -28,13 +30,20 @@ void Logging::Initialize()
Assert(result != FALSE);
Assert(bytesWritten = sizeof(threeNewLines));
}

SetLastError(ERROR_SUCCESS);
}

void Logging::Shutdown()
{
CloseHandle(s_OutputFile);
}

std::wstring Logging::GetLogFileName()
{
return kLogFileName;
}

void Logging::OutputMessage(const char* message, size_t length)
{
if (IsDebuggerPresent())
Expand Down Expand Up @@ -71,8 +80,8 @@ void Logging::OutputCurrentTimestamp()

char buffer[kBufferSize];
wchar_t wbuffer[kBufferSize];
SystemTimeToStringInline(wbuffer);
Encoding::Utf16ToUtf8Inline(wbuffer, buffer);
auto dateTimeLength = SystemTimeToStringInline(wbuffer);
Encoding::Utf16ToUtf8Inline(wbuffer, dateTimeLength, buffer, kBufferSize);

OutputMessage(buffer);
OutputMessage("] ");
Expand Down
11 changes: 2 additions & 9 deletions RemoteFileBrowser/RemoteFileBrowser/Utilities/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ namespace Utilities
template <typename ...Message>
static inline void LogFatalErrorIfFailed(bool failed, Message&& ...message);

static void Initialize();
static void Initialize(bool forceOverwrite = false);
static void Shutdown();
static std::wstring GetLogFileName();

Logging() = delete;
Logging(const Logging&) = delete;
Expand All @@ -58,18 +59,10 @@ namespace Utilities
namespace Encoding
{
size_t Utf8ToUtf16Inline(const char* str, size_t strLength, wchar_t* destination, size_t destinationLength);

template <size_t SourceLength, size_t DestinationLength>
inline size_t Utf8ToUtf16Inline(const char (&str)[SourceLength], wchar_t (&destination)[DestinationLength]);

std::wstring Utf8ToUtf16(const char* str, size_t strLength);
inline std::wstring Utf8ToUtf16(const std::string& str);

size_t Utf16ToUtf8Inline(const wchar_t* wstr, size_t wstrLength, char* destination, size_t destinationLength);

template <size_t SourceLength, size_t DestinationLength>
inline size_t Utf16ToUtf8Inline(const wchar_t (&wstr)[SourceLength], char (&destination)[DestinationLength]);

std::string Utf16ToUtf8(const wchar_t* wstr, size_t wstrLength);
inline std::string Utf16ToUtf8(const std::wstring& wstr);

Expand Down
16 changes: 2 additions & 14 deletions RemoteFileBrowser/RemoteFileBrowser/Utilities/Utilities.inl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ inline void Utilities::Logging::Win32ErrorToMessageInline(int win32ErrorCode, ch
{
wchar_t wBuffer[kBufferSize];

FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, win32ErrorCode, 0, wBuffer, kBufferSize, nullptr);
Utilities::Encoding::Utf16ToUtf8Inline(wBuffer, buffer);
auto messageLength = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, win32ErrorCode, 0, wBuffer, kBufferSize, nullptr);
Utilities::Encoding::Utf16ToUtf8Inline(wBuffer, messageLength, buffer, kBufferSize);
}

template <typename Message>
Expand Down Expand Up @@ -118,23 +118,11 @@ inline void Utilities::Logging::LogFatalErrorIfFailed(bool failed, Message&& ...

// Encoding

template <size_t SourceLength, size_t DestinationLength>
inline size_t Utilities::Encoding::Utf8ToUtf16Inline(const char (&str)[SourceLength], wchar_t (&destination)[DestinationLength])
{
return Utf8ToUtf16Inline(str, SourceLength, destination, DestinationLength);
}

inline std::wstring Utilities::Encoding::Utf8ToUtf16(const std::string& str)
{
return Utf8ToUtf16(str.c_str(), str.length());
}

template <size_t SourceLength, size_t DestinationLength>
inline size_t Utilities::Encoding::Utf16ToUtf8Inline(const wchar_t(&wstr)[SourceLength], char(&destination)[DestinationLength])
{
return Utf16ToUtf8Inline(wstr, SourceLength, destination, DestinationLength);
}

inline std::string Utilities::Encoding::Utf16ToUtf8(const std::wstring& wstr)
{
return Utf16ToUtf8(wstr.c_str(), wstr.length());
Expand Down

0 comments on commit dcf2375

Please sign in to comment.