Skip to content

Console unit test #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/Platform/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Helium
{
// Scan from STDOUT
// Scan from STDIN
HELIUM_PLATFORM_API int Scan(const char* fmt, ...);
HELIUM_PLATFORM_API int Scan(const wchar_t* fmt, ...);
HELIUM_PLATFORM_API int ScanArgs(const char* fmt, va_list args);
Expand Down
6 changes: 6 additions & 0 deletions Source/Platform/ConsolePosix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ int Helium::ScanArgs(const wchar_t* fmt, va_list args)

int Helium::FileScan(FILE* f, const char* fmt, ...)
{
HELIUM_ASSERT(f);
char buf[1024];
if ( fgets( buf, sizeof( buf ), f ) )
{
Expand All @@ -106,6 +107,7 @@ int Helium::FileScan(FILE* f, const wchar_t* fmt, ...)

int Helium::FileScanArgs(FILE* f, const char* fmt, va_list args)
{
HELIUM_ASSERT(f);
char buf[1024];
if ( fgets( buf, sizeof( buf ), f ) )
{
Expand Down Expand Up @@ -179,6 +181,7 @@ int Helium::PrintArgs(const wchar_t* fmt, va_list args)

int Helium::FilePrint(FILE* f, const char* fmt, ...)
{
HELIUM_ASSERT(f);
va_list args;
va_start(args, fmt);
int result = vfprintf(f, fmt, args);
Expand All @@ -188,6 +191,7 @@ int Helium::FilePrint(FILE* f, const char* fmt, ...)

int Helium::FilePrint(FILE* f, const wchar_t* fmt, ...)
{
HELIUM_ASSERT(f);
va_list args;
va_start(args, fmt);
int result = vfwprintf(f, fmt, args);
Expand All @@ -197,11 +201,13 @@ int Helium::FilePrint(FILE* f, const wchar_t* fmt, ...)

int Helium::FilePrintArgs(FILE* f, const char* fmt, va_list args)
{
HELIUM_ASSERT(f);
return vfprintf(f, fmt, args);
}

int Helium::FilePrintArgs(FILE* f, const wchar_t* fmt, va_list args)
{
HELIUM_ASSERT(f);
return vfwprintf(f, fmt, args);
}

Expand Down
194 changes: 194 additions & 0 deletions Source/Platform/ConsoleTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#include "Precompile.h"

#include "Platform/Console.h"
#include "Platform/Assert.h"

#include "gtest/gtest.h"

#include <fstream>
#include <iostream>
#include <stdio.h>

using namespace Helium;

void setupFileScanTest(const char* const fileName, const char* strings[], size_t stringCount)
{
std::ofstream fileToCheck(fileName);

for (size_t i = 0; i < stringCount; i++)
{
fileToCheck << strings[i];
}

fileToCheck.close();
}

TEST(PlatformConsoleTest, CheckScanFromFile )
{
const char* const fileName = "scan_test1.txt";
const char* strings[] = { "testing123", "\n", "54321" };
setupFileScanTest(fileName, strings, 3);

FILE* fileToScan = fopen(fileName, "r");

char scanedText[64];
int res = FileScan(fileToScan, "%s", scanedText);
ASSERT_TRUE(res == 1);
ASSERT_TRUE(strcmp(strings[0], scanedText) == 0);

int num = 0;
res = FileScan(fileToScan, "%d", &num);
ASSERT_TRUE(res == 1);
ASSERT_TRUE(num == 54321);
fclose(fileToScan);

remove(fileName);
}

TEST(PlatformConsoleTest, CheckScanFromFile2)
{
const char* const fileName = "scan_test2.txt";

// the only diferentce between CheckScanFromFile test
const char* strings[] = { "testing123", " " /*no new line*/, "54321" };
setupFileScanTest(fileName, strings, 3);

FILE* fileToScan = fopen(fileName, "r");

char scanedText[64];
int res = FileScan(fileToScan, "%s", scanedText);
ASSERT_TRUE(res == 1);
ASSERT_TRUE(strcmp(strings[0], scanedText) == 0);

int num = 0;
FileScan(fileToScan, "%d", &num);
ASSERT_TRUE(num == 0);
fclose(fileToScan);

remove(fileName);
}

TEST(PlatformConsoleTest, CheckScanFromFile3)
{
const char* const fileName = "scan_test2.txt";

// the only diferentce between CheckScanFromFile test
const char* strings[] = { "testing123", " " /*no new line*/, "54321" };
setupFileScanTest(fileName, strings, 3);

FILE* fileToScan = fopen(fileName, "r");

char scanedText[64];
int res = FileScan(fileToScan, "", scanedText);
fclose(fileToScan);

remove(fileName);
}

TEST(PlatformConsoleTest, CheckStringScan)
{
int num = 0;
char str[10];
StringScan("9876 foobar", "%d %s", &num, str);

ASSERT_TRUE(num == 9876);
ASSERT_TRUE(strcmp(str, "foobar") == 0);
}

TEST(PlatformConsoleTest, CheckFilePrint)
{
const char* const fileName = "print_test.txt";

FILE* file = fopen(fileName, "w");
ASSERT_TRUE(file != NULL);

int res = FilePrint(file, L"testing %d", 1234);
ASSERT_TRUE(res != 0);
ASSERT_TRUE(fclose(file) == 0);

std::wifstream fileToCheck(fileName);

ASSERT_TRUE(fileToCheck.is_open());
std::wstring text(L"");
int num = 0;
fileToCheck >> text >> num;

fileToCheck.close();

ASSERT_TRUE(num == 1234);
ASSERT_TRUE(wcscmp(text.c_str(), L"testing") == 0);
}

TEST(PlatformConsoleTest, CheckStringPrint)
{
const int size = 10;
char dest[size];

int res = StringPrint(dest, size, "foobar %d", size);
ASSERT_TRUE(res != 0);

int num = 0;
char str[size];
StringScan(dest, "%s %d", str, &num);

ASSERT_TRUE(num == size);
ASSERT_TRUE(strcmp(str, "foobar") == 0);
}

TEST(PlatformConsoleTest, CheckStringPrintWithSizeDeduction)
{
const int size = 10;
char dest[size];

int res = StringPrint(dest, "barbaz %d", size);
ASSERT_TRUE(res != 0);

int num = 0;
char str[size];
StringScan(dest, "%s %d", str, &num);

ASSERT_TRUE(num == size);
ASSERT_TRUE(strcmp(str, "barbaz") == 0);
}

TEST(PlatformConsoleTest, CheckStringPrintOverSize)
{
const int size = 10;
char dest[size];

int res = StringPrint(dest, size, "foobar bar baz %d", 33);

char str1[size];
char str2[size];
StringScan(dest, "%s %s", str1, str2);

ASSERT_TRUE(strcmp(str1, "foobar") == 0);
ASSERT_TRUE(strcmp(str2, "ba") == 0);
}


#if HELIUM_ASSERT_ENABLED
TEST(PlatformConsoleTest, FailOnWCharScan)
{
ASSERT_DEATH(Scan(L""), "");
}

TEST(PlatformConsoleTest, CheckScanFromNullFile)
{
FILE* fileToScan = nullptr;
char scanedText[64];
ASSERT_DEATH(FileScan(fileToScan, "%s", scanedText), "");
}

TEST(PlatformConsoleTest, CheckPrintToNullFile)
{
FILE* file = nullptr;
char scanedText[64];
ASSERT_DEATH(FilePrint(file, "123 %s", scanedText), "");
}

TEST(PlatformConsoleTest, FailOnWCharFileScan)
{
ASSERT_DEATH(FileScan(nullptr, L""), "");
}
#endif
9 changes: 9 additions & 0 deletions Source/Platform/ConsoleWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ int Helium::ScanArgs(const wchar_t* fmt, va_list args)
return 0;
}

// vstepano: consider renaming this function to FileLineScan (or something that will hit that this function works on a line by line basis )
int Helium::FileScan(FILE* f, const char* fmt, ...)
{
HELIUM_ASSERT(f);
// vstepano: Should we also assert on an empty format string? HELIUM_ASSERT(fmt != '\0');
char buf[1024];
fgets( buf, sizeof( buf ), f );

Expand All @@ -93,6 +96,7 @@ int Helium::FileScan(FILE* f, const wchar_t* fmt, ...)

int Helium::FileScanArgs(FILE* f, const char* fmt, va_list args)
{
HELIUM_ASSERT(f);
char buf[1024];
fgets( buf, sizeof( buf ), f );
return vsscanf( buf, fmt, args );
Expand All @@ -116,6 +120,7 @@ int Helium::StringScan(const char* str, const char* fmt, ...)
int Helium::StringScan(const wchar_t* str, const wchar_t* fmt, ...)
{
HELIUM_ASSERT( false );
// vstepano: why can't we use vswscanf?
return 0;
}

Expand Down Expand Up @@ -160,6 +165,7 @@ int Helium::PrintArgs(const wchar_t* fmt, va_list args)

int Helium::FilePrint(FILE* f, const char* fmt, ...)
{
HELIUM_ASSERT(f);
va_list args;
va_start(args, fmt);
int result = vfprintf(f, fmt, args);
Expand All @@ -169,6 +175,7 @@ int Helium::FilePrint(FILE* f, const char* fmt, ...)

int Helium::FilePrint(FILE* f, const wchar_t* fmt, ...)
{
HELIUM_ASSERT(f);
va_list args;
va_start(args, fmt);
int result = vfwprintf(f, fmt, args);
Expand All @@ -178,11 +185,13 @@ int Helium::FilePrint(FILE* f, const wchar_t* fmt, ...)

int Helium::FilePrintArgs(FILE* f, const char* fmt, va_list args)
{
HELIUM_ASSERT(f);
return vfprintf(f, fmt, args);
}

int Helium::FilePrintArgs(FILE* f, const wchar_t* fmt, va_list args)
{
HELIUM_ASSERT(f);
return vfwprintf(f, fmt, args);
}

Expand Down