Include Catch and tests for Core

Catch 1.2.1


Former-commit-id: 4149eaa61b21532d4d204db8a3771c6de8e4672c
This commit is contained in:
Gawaboumga 2015-08-21 11:46:46 +02:00
parent 2d07922478
commit f61aa8f36e
8 changed files with 566 additions and 0 deletions

View File

@ -0,0 +1,216 @@
#include <Nazara/Core/ByteArray.hpp>
#include <catch.hpp>
#include <string>
SCENARIO("ByteArray", "[CORE][BYTEARRAY]")
{
GIVEN("Allocate and raw constructor")
{
NzByteArray byteArray(3);
THEN("Capacity is 3 and size is 0")
{
REQUIRE(byteArray.GetSize() == 0);
REQUIRE(byteArray.GetCapacity() >= 3);
}
WHEN("We add 'data'")
{
byteArray.Append("data", 4);
THEN("We get 'data'")
{
REQUIRE(byteArray.GetSize() == 4);
REQUIRE(byteArray.GetCapacity() >= 4);
REQUIRE(byteArray == NzByteArray("data", 4));
REQUIRE(byteArray.ToString() == "data");
}
}
}
GIVEN("Iterator and default constructor")
{
std::string anotherDataString("anotherData");
NzByteArray defaultByte;
NzByteArray anotherData(anotherDataString.begin(), anotherDataString.end());
WHEN("We assign 'anotherData' with iterator")
{
defaultByte.Assign(anotherData.begin(), anotherData.end());
REQUIRE(anotherData == defaultByte);
REQUIRE(defaultByte.GetSize() == 11);
REQUIRE(defaultByte.GetCapacity() >= 11);
REQUIRE(anotherData.GetSize() == 11);
REQUIRE(anotherData.GetCapacity() >= 11);
}
}
GIVEN("Copy and Move constructor")
{
NzByteArray originalArray(3, 64);
WHEN("We copy")
{
NzByteArray copyByteArray(originalArray);
THEN("We get a copy")
{
REQUIRE(copyByteArray == originalArray);
AND_WHEN("We modify one")
{
for (NzByteArray::size_type i = 0; i < copyByteArray.GetSize(); ++i)
copyByteArray[i] = 46;
THEN("They are no more equal")
{
REQUIRE(copyByteArray != originalArray);
REQUIRE(copyByteArray == NzByteArray(3, 46));
REQUIRE(copyByteArray.GetConstBuffer() != originalArray.GetConstBuffer());
}
}
}
}
WHEN("We move")
{
NzByteArray moveByteArray(std::move(originalArray));
THEN("These results are expected")
{
REQUIRE(moveByteArray == NzByteArray(3, 64));
CHECK(originalArray.IsEmpty());
REQUIRE(originalArray.GetCapacity() == 0);
REQUIRE(moveByteArray.GetConstBuffer() != originalArray.GetConstBuffer());
AND_WHEN("We modify the empty one")
{
originalArray.Prepend(NzByteArray(3, 64));
THEN("They are no more equal")
{
REQUIRE(moveByteArray == originalArray);
REQUIRE(moveByteArray.GetConstBuffer() != originalArray.GetConstBuffer());
}
}
}
}
}
GIVEN("Two byte array (abc) and (cba)")
{
NzByteArray abc("abc", 3);
NzByteArray cba;
cba = std::move(NzByteArray("cba", 3));
WHEN("We do some antagonists operations")
{
THEN("These results are expected")
{
REQUIRE(abc.Back() == cba.Front());
abc.Erase(abc.begin(), abc.begin() + 1);
abc.Erase(abc.begin());
cba.Erase(cba.end() - 1, cba.end());
cba.Erase(cba.end() - 1);
REQUIRE(abc == NzByteArray("c", 1));
REQUIRE(cba == NzByteArray("c", 1));
std::string ab("ab");
abc.Insert(abc.begin(), ab.begin(), ab.end());
cba += NzByteArray("ba", 2);
REQUIRE(abc == NzByteArray("abc", 3));
REQUIRE(cba == NzByteArray("cba", 3));
abc.PopBack();
cba.PopFront();
REQUIRE(abc == NzByteArray("ab", 2));
REQUIRE(cba == NzByteArray("ba", 2));
abc.PushBack('c');
cba.PushFront('c');
REQUIRE(abc == NzByteArray("abc", 3));
REQUIRE(cba == NzByteArray("cba", 3));
}
}
}
GIVEN("One byte array of capacity 10")
{
NzByteArray capacityArray(10);
WHEN("We reserve for 100")
{
capacityArray.Reserve(100);
THEN("Capacity is 100")
{
REQUIRE(capacityArray.GetCapacity() == 100);
}
AND_WHEN("We add information and then shrink to fit")
{
capacityArray.Prepend("information", 11);
capacityArray.ShrinkToFit();
THEN("Capacity is 11")
{
REQUIRE(capacityArray.GetCapacity() == 11);
REQUIRE(capacityArray.GetSize() == 11);
}
}
}
NzByteArray::const_pointer oldBuffer = capacityArray.GetConstBuffer();
WHEN("We reserve for 5, add 'data' for 4 and then shrink to fit")
{
capacityArray.Reserve(5);
THEN("Capacity is still 10")
{
REQUIRE(capacityArray.GetCapacity() == 10);
REQUIRE(capacityArray.GetSize() == 0);
REQUIRE(capacityArray.GetConstBuffer() == oldBuffer);
}
capacityArray.Append("data", 4);
capacityArray.ShrinkToFit();
THEN("Capacity is 4")
{
REQUIRE(capacityArray.GetConstBuffer() != oldBuffer);
REQUIRE(capacityArray.GetCapacity() == 4);
REQUIRE(capacityArray.GetSize() == 4);
}
}
}
GIVEN("Three byte array")
{
NzByteArray first("hello", 5);
NzByteArray second("world", 5);
NzByteArray third;
WHEN("We swap first and third, then second and third and finally third and first")
{
NzByteArray oldFirst(first);
NzByteArray oldSecond(second);
first.Swap(third);
std::swap(second, third);
third.Swap(first);
THEN("First and second have been swapped and third is still empty.")
{
REQUIRE(oldFirst == second);
REQUIRE(oldSecond == first);
REQUIRE(third.IsEmpty());
}
}
}
}

View File

@ -0,0 +1,31 @@
#include <Nazara/Core/Clock.hpp>
#include <catch.hpp>
#include <thread>
SCENARIO("Clock", "[CORE][CLOCK]")
{
GIVEN("A clock paused")
{
nzUInt64 initialTime = 1;
NzClock clock(initialTime, true);
WHEN("We get time")
{
THEN("Time must be the initialTime")
{
REQUIRE(clock.GetMicroseconds() == initialTime);
}
AND_WHEN("We unpause it")
{
clock.Unpause();
THEN("Time must not be the initialTime")
{
std::this_thread::sleep_for(std::chrono::microseconds(10));
REQUIRE(clock.GetMicroseconds() != initialTime);
}
}
}
}
}

View File

@ -0,0 +1,22 @@
#include <Nazara/Core/Color.hpp>
#include <catch.hpp>
SCENARIO("Color", "[CORE][COLOR]")
{
GIVEN("Two colors, one red (255) and one gray (128)")
{
NzColor red(255, 0, 0);
NzColor grey(128);
WHEN("We do operations")
{
THEN("These results are expected")
{
red += NzColor(0, 0, 0);
grey *= NzColor(255);
REQUIRE((red + grey) == NzColor(255, 128, 128));
REQUIRE((red * grey) == NzColor(128, 0, 0));
}
}
}
}

View File

@ -0,0 +1,34 @@
#include <Nazara/Core/Directory.hpp>
#include <catch.hpp>
SCENARIO("Directory", "[CORE][DIRECTORY]")
{
GIVEN("The current directory")
{
NzDirectory currentDirectory(NzDirectory::GetCurrent());
CHECK(currentDirectory.Exists());
currentDirectory.Open();
WHEN("We create a new directory Test Directory")
{
NzDirectory::Create("Test Directory");
THEN("A new directory has been created")
{
CHECK(NzDirectory::Exists(currentDirectory.GetCurrent() + "/Test Directory"));
CHECK(currentDirectory.IsOpen());
}
}
AND_WHEN("We delete it")
{
NzDirectory::Remove(currentDirectory.GetCurrent() + "/Test Directory", true);
THEN("It doesn't exist anymore")
{
CHECK(!NzDirectory::Exists(currentDirectory.GetCurrent() + "/Test Directory"));
}
}
}
}

View File

@ -0,0 +1,28 @@
#include <Nazara/Core/Error.hpp>
#include <catch.hpp>
SCENARIO("Error", "[CORE][ERROR]")
{
nzUInt32 oldFlags = NzError::GetFlags();
GIVEN("Multiple errors")
{
WHEN("Calling to error")
{
THEN("These errors should be written in the log file")
{
NzError::Error(nzErrorType_Internal, "nzErrorType_Internal");
NzError::Error(nzErrorType_Internal, "nzErrorType_Internal", 2, "Error.cpp", "2nd place Internal");
REQUIRE("nzErrorType_Internal" == NzError::GetLastError());
NzError::Error(nzErrorType_Normal, "nzErrorType_Normal");
NzError::Error(nzErrorType_Normal, "nzErrorType_Normal", 2, "Error.cpp", "2nd place Normal");
REQUIRE("nzErrorType_Normal" == NzError::GetLastError());
NzError::Error(nzErrorType_Warning, "nzErrorType_Warning");
NzError::Error(nzErrorType_Warning, "nzErrorType_Warning", 2, "Error.cpp", "2nd place Warning");
REQUIRE("nzErrorType_Warning" == NzError::GetLastError());
}
}
}
NzError::SetFlags(oldFlags);
}

View File

@ -0,0 +1,53 @@
#include <Nazara/Core/File.hpp>
#include <catch.hpp>
SCENARIO("File", "[CORE][FILE]")
{
GIVEN("One file")
{
WHEN("We create a new file")
{
NzFile file("Test File.txt", nzOpenMode_ReadWrite);
REQUIRE(file.GetDirectory() == NzDirectory::GetCurrent() + NAZARA_DIRECTORY_SEPARATOR);
CHECK(file.IsOpen());
THEN("We are allowed to write 3 times 'Test String'")
{
const char message[12] = "Test String"; // 11 + '\0'
NzByteArray byteArray(message, 11);
file.Write("Test String");
file.Write(byteArray);
file.Write(message, sizeof(char), 11);
}
AND_THEN("We can retrieve 3 times 'Test String'")
{
char message[12];
REQUIRE(file.Read(message, 11) == 11);
message[11] = '\0';
REQUIRE(NzString(message) == "Test String");
REQUIRE(file.Read(message, sizeof(char), 11) == 11);
message[11] = '\0';
REQUIRE(NzString(message) == "Test String");
}
AND_THEN("We close it")
{
file.Close();
REQUIRE(file.GetSize() == 33U);
CHECK(!file.IsOpen());
}
}
WHEN("We delete this file")
{
NzFile::Delete("Test File.txt");
THEN("It doesn't exist anymore")
{
CHECK(!NzFile::Exists("Test File.txt"));
}
}
}
}

View File

@ -0,0 +1,111 @@
#include <Nazara/Core/String.hpp>
#include <catch.hpp>
SCENARIO("String", "[CORE][STRING]")
{
GIVEN("One string 'a'")
{
NzString aDefaultString(1, 'a');
WHEN("We add information")
{
aDefaultString.Append("Default");
aDefaultString.Insert(aDefaultString.GetSize(), "String");
THEN("The result should be 'aDefaultString'")
{
REQUIRE(aDefaultString == "aDefaultString");
REQUIRE(aDefaultString.GetSize() == 14);
REQUIRE(aDefaultString.GetCapacity() >= 14);
}
AND_WHEN("We test Contains and Find")
{
THEN("These results are expected")
{
CHECK(aDefaultString.Contains('D'));
CHECK(aDefaultString.Contains("String", 3));
CHECK(aDefaultString.Contains(NzString("sTRING"), 3, NzString::CaseInsensitive));
REQUIRE(aDefaultString.FindLast('g') == aDefaultString.GetSize() - 1);
CHECK(aDefaultString.EndsWith('G', NzString::CaseInsensitive));
aDefaultString.Append(" ng bla");
REQUIRE(aDefaultString.FindWord("ng") == aDefaultString.GetSize() - 6);
//TODO REQUIRE(aDefaultString.FindWord(NzString("ng")) == aDefaultString.GetSize() - 6);
CHECK(aDefaultString.StartsWith("aD"));
}
}
}
WHEN("We do operators")
{
aDefaultString[0] = 'a';
aDefaultString += "Default";
aDefaultString = aDefaultString + "String";
THEN("The result should be 'aDefaultString'")
{
REQUIRE(aDefaultString == "aDefaultString");
REQUIRE(aDefaultString.GetSize() == 14);
REQUIRE(aDefaultString.GetCapacity() >= 14);
}
AND_WHEN("We test Count")
{
THEN("These results are expected")
{
REQUIRE(aDefaultString.Count('D') == 1);
REQUIRE(aDefaultString.Count("t", 2) == 2);
}
}
}
}
GIVEN("The string of number 16 in base 16")
{
NzString number16;
CHECK(number16.IsEmpty());
CHECK(number16.IsNull());
WHEN("We assign to number 16")
{
number16 = NzString::Number(16, 16);
THEN("These results are expected")
{
CHECK(number16.IsNumber(16));
number16.Prepend("0x");
REQUIRE(number16.GetSize() == 4);
REQUIRE(number16.GetCapacity() >= 4);
REQUIRE(number16.SubStringFrom('x', 1) == "10");
}
}
}
/* TODO
GIVEN("One unicode string")
{
NzString unicodeString = NzString::Unicode(U"àéçœÂ官話");
WHEN("We convert to other UTF")
{
REQUIRE(unicodeString.GetSize() == 7);
REQUIRE(unicodeString.GetCapacity() >= 7);
CHECK(unicodeString.Contains(""));
THEN("The result should be the identity")
{
char* utf8 = unicodeString.GetUtf8Buffer();
NzString utf8String = NzString::Unicode(utf8);
char16_t* utf16 = unicodeString.GetUtf16Buffer();
NzString utf16String = NzString::Unicode(utf16);
char32_t* utf32 = unicodeString.GetUtf32Buffer();
NzString utf32String = NzString::Unicode(utf32);
REQUIRE(utf8String == utf16String);
REQUIRE(utf16String == utf32String);
}
}
}*/
}

View File

@ -0,0 +1,71 @@
#include <Nazara/Core/StringStream.hpp>
#include <catch.hpp>
SCENARIO("StringStream", "[CORE][STRINGSTREAM]")
{
GIVEN("A string stream")
{
NzStringStream stringstream("default");
WHEN("We add bool and char")
{
stringstream << true;
char valueCharSigned = 64;
stringstream << valueCharSigned;
unsigned char valueCharUnsigned = 64;
stringstream << valueCharUnsigned;
REQUIRE(stringstream.ToString() == "defaulttrue@@");
}
AND_WHEN("We add short and int")
{
short valueShortSigned = -3;
stringstream << valueShortSigned;
unsigned short valueShortUnsigned = 3;
stringstream << valueShortUnsigned;
int valueIntSigned = -3;
stringstream << valueIntSigned;
unsigned int valueIntUnsigned = 3;
stringstream << valueIntUnsigned;
REQUIRE(stringstream.ToString() == "default-33-33");
}
AND_WHEN("We add long and long long")
{
long valueLongSigned = -3;
stringstream << valueLongSigned;
unsigned long valueLongUnsigned = 3;
stringstream << valueLongUnsigned;
long long valueLongLongSigned = -3;
stringstream << valueLongLongSigned;
unsigned long long valueLongLongUnsigned = 3;
stringstream << valueLongLongUnsigned;
REQUIRE(stringstream.ToString() == "default-33-33");
}
AND_WHEN("We add floating points")
{
stringstream << 3.f;
stringstream << 3.0;
stringstream << 3.0L;
REQUIRE(stringstream.ToString() == "default333");
}
AND_WHEN("We add string and pointer")
{
stringstream << "3";
stringstream << std::string("3");
stringstream << NzString("3");
stringstream << static_cast<void*>(nullptr);
REQUIRE(stringstream.ToString() == (NzString("default3330x") + NzString(sizeof(void*) * 2, "0")));
}
}
}