-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZmeyaTest09.cpp
139 lines (116 loc) · 3.92 KB
/
ZmeyaTest09.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "TestHelper.h"
#include "Zmeya.h"
#include "gtest/gtest.h"
struct Vec2
{
float x;
float y;
Vec2() = default;
Vec2(float _x, float _y)
: x(_x)
, y(_y)
{
}
};
struct Node
{
zm::String name;
};
// as long as inheritance = aggregation it is supported (but beware of different compilers paddings!)
struct Object : public Node
{
zm::Pointer<Object> parent;
Vec2 position;
};
struct SimpleFileTestRoot
{
uint32_t magic;
zm::Array<Object> objects;
zm::HashSet<zm::String> hashSet;
zm::HashMap<zm::String, float> hashMap;
};
static void validate(const SimpleFileTestRoot* root)
{
EXPECT_EQ(root->magic, 0x59454D5Au);
EXPECT_EQ(root->objects.size(), std::size_t(6));
EXPECT_EQ(root->objects[0].name, "root");
EXPECT_EQ(root->objects[1].name, "test1");
EXPECT_EQ(root->objects[2].name, "floor");
EXPECT_EQ(root->objects[3].name, "window");
EXPECT_EQ(root->objects[4].name, "arrow");
EXPECT_EQ(root->objects[5].name, "door");
for (size_t i = 0; i < root->objects.size(); i++)
{
const Object& object = root->objects[i];
EXPECT_FLOAT_EQ(object.position.x, float(i));
EXPECT_FLOAT_EQ(object.position.y, float(i + 4));
if (i == 0)
{
EXPECT_TRUE(object.parent == nullptr);
}
else
{
EXPECT_TRUE(object.parent.get() == &root->objects[i - 1]);
}
}
EXPECT_EQ(root->hashSet.size(), std::size_t(3));
EXPECT_TRUE(root->hashSet.contains("one"));
EXPECT_TRUE(root->hashSet.contains("two"));
EXPECT_TRUE(root->hashSet.contains("three"));
EXPECT_EQ(root->hashMap.size(), std::size_t(3));
EXPECT_FLOAT_EQ(root->hashMap.find("1", 0.0f), 1.0f);
EXPECT_FLOAT_EQ(root->hashMap.find("2", 0.0f), 2.0f);
EXPECT_FLOAT_EQ(root->hashMap.find("3", 0.0f), 3.0f);
}
static void generateTestFile(const char* fileName)
{
std::vector<char> bytesCopy;
{
std::vector<std::string> objectNames = {"root", "test1", "floor", "window", "arrow", "door"};
std::shared_ptr<zm::BlobBuilder> blobBuilder = zm::BlobBuilder::create(1);
zm::BlobPtr<SimpleFileTestRoot> root = blobBuilder->allocate<SimpleFileTestRoot>();
root->magic = 0x59454D5A;
blobBuilder->resizeArray(root->objects, 6);
for (size_t i = 0; i < root->objects.size(); i++)
{
zm::BlobPtr<Object> object = blobBuilder->getArrayElement(root->objects, i);
blobBuilder->copyTo(object->name, objectNames[i]);
object->position = Vec2(float(i), float(i + 4));
if (i > 0)
{
const Object& parentObject = root->objects[i - 1];
blobBuilder->assignTo(object->parent, parentObject);
}
}
blobBuilder->copyTo(root->hashSet, {"one", "two", "three"});
blobBuilder->copyTo(root->hashMap, {{"1", 1.0f}, {"2", 2.0f}, {"3", 3.0f}});
validate(root.get());
zm::Span<char> bytes = blobBuilder->finalize(32);
EXPECT_TRUE((bytes.size % 32) == 0);
bytesCopy = utils::copyBytes(bytes);
std::memset(bytes.data, 0xFF, bytes.size);
}
const SimpleFileTestRoot* rootCopy = (const SimpleFileTestRoot*)(bytesCopy.data());
validate(rootCopy);
FILE* file = fopen(fileName, "wb");
ASSERT_TRUE(file != nullptr);
fwrite(bytesCopy.data(), bytesCopy.size(), 1, file);
fclose(file);
}
TEST(ZmeyaTestSuite, SimpleFileTest)
{
const char* fileName = "test.zm";
generateTestFile(fileName);
std::vector<char> content;
// read file
FILE* file = fopen(fileName, "rb");
ASSERT_TRUE(file != nullptr);
fseek(file, 0L, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0L, SEEK_SET);
content.resize(fileSize);
fread(content.data(), fileSize, 1, file);
fclose(file);
const SimpleFileTestRoot* fileRoot = (const SimpleFileTestRoot*)(content.data());
validate(fileRoot);
}