Added unit tests for FloatNode

This commit is contained in:
Michaël Lemaire 2015-08-14 00:22:20 +02:00
parent d65bc18342
commit d78bd2553c
5 changed files with 59 additions and 1 deletions

View file

@ -23,6 +23,22 @@ PackStream::~PackStream()
}
}
PackStream::PackStream(const PackStream *other)
{
file = NULL;
buffer = new QByteArray();
if (other->file)
{
Logs::error() << "Try to read from a substream bound to a file: " << other->file->fileName().toStdString() << std::endl;
stream = new QDataStream(buffer, QIODevice::ReadOnly);
}
else
{
stream = new QDataStream(other->buffer, QIODevice::ReadOnly);
}
stream->setVersion(QDataStream::Qt_5_2);
}
bool PackStream::bindToFile(const std::string &filepath, bool write)
{
if (not file)

View file

@ -20,6 +20,13 @@ public:
PackStream();
~PackStream();
/**
* Open a reading stream for another stream.
*
* The other stream must not have been bound to a file (still a memory buffer).
*/
PackStream(const PackStream *other);
bool bindToFile(const std::string &filepath, bool write=false);
void write(const int *value);

View file

@ -39,6 +39,7 @@ TEST(DefinitionNode, attachDetach)
TEST(DefinitionNode, saveLoad)
{
PackStream *stream;
int check_in = 42, check_out = 0;
DefinitionNode* before = new DefinitionNode(NULL, "root");
DefinitionNode* before1 = new DefinitionNode(before, "before1");
@ -52,6 +53,7 @@ TEST(DefinitionNode, saveLoad)
stream = new PackStream();
stream->bindToFile("/tmp/test_paysages_pack", true);
before->save(stream);
stream->write(&check_in);
delete stream;
// Same definition tree, but with missing nodes, and added ones
@ -65,12 +67,14 @@ TEST(DefinitionNode, saveLoad)
stream = new PackStream();
stream->bindToFile("/tmp/test_paysages_pack");
after->load(stream);
stream->read(&check_out);
delete stream;
EXPECT_DOUBLE_EQ(-4.3, after12->getValue());
EXPECT_DOUBLE_EQ(0.0, after13->getValue());
EXPECT_DOUBLE_EQ(6.7, after3->getValue());
EXPECT_DOUBLE_EQ(0.0, after4->getValue());
EXPECT_EQ(42, check_out);
delete before;
delete after;

View file

@ -0,0 +1,30 @@
#include "BaseTestCase.h"
#include "FloatNode.h"
#include "PackStream.h"
TEST(FloatNode, toString)
{
FloatNode test(NULL, "test", 2.1);
EXPECT_EQ("test 2.1", test.toString(0));
EXPECT_EQ(" test 2.1", test.toString(2));
}
TEST(FloatNode, saveLoadAndSkip)
{
DefinitionNode root1(NULL, "root");
FloatNode testa1(&root1, "testa", 1.5);
FloatNode testb1(&root1, "testb", 4.3);
PackStream stream1;
root1.save(&stream1);
DefinitionNode root2(NULL, "root");
FloatNode testb2(&root2, "testb");
PackStream stream2(&stream1);
root2.load(&stream2);
EXPECT_DOUBLE_EQ(4.3, testb2.getValue());
}

View file

@ -23,7 +23,8 @@ SOURCES += main.cpp \
CanvasPreview_Test.cpp \
AtmosphereDefinition_Test.cpp \
Scenery_Test.cpp \
DefinitionNode_Test.cpp
DefinitionNode_Test.cpp \
FloatNode_Test.cpp
HEADERS += \
BaseTestCase.h