1#include "test.h"
2#include <gtest/gtest.h>
3#include <memory>
4
5#include <engine/shared/datafile.h>
6#include <engine/storage.h>
7#include <game/mapitems_ex.h>
8
9TEST(Datafile, ExtendedType)
10{
11 auto pStorage = std::unique_ptr<IStorage>(CreateLocalStorage());
12 CTestInfo Info;
13
14 CMapItemTest ItemTest;
15 ItemTest.m_Version = CMapItemTest::CURRENT_VERSION;
16 ItemTest.m_aFields[0] = 1234;
17 ItemTest.m_aFields[1] = 5678;
18 ItemTest.m_Field3 = 9876;
19 ItemTest.m_Field4 = 5432;
20
21 {
22 CDataFileWriter Writer;
23 Writer.Open(pStorage: pStorage.get(), pFilename: Info.m_aFilename);
24
25 Writer.AddItem(Type: MAPITEMTYPE_TEST, Id: 0x8000, Size: sizeof(ItemTest), pData: &ItemTest);
26
27 Writer.Finish();
28 }
29
30 {
31 CDataFileReader Reader;
32 Reader.Open(pStorage: pStorage.get(), pFilename: Info.m_aFilename, StorageType: IStorage::TYPE_ALL);
33
34 int Start, Num;
35 Reader.GetType(Type: MAPITEMTYPE_TEST, pStart: &Start, pNum: &Num);
36 EXPECT_EQ(Num, 1);
37
38 int Index = Reader.FindItemIndex(Type: MAPITEMTYPE_TEST, Id: 0x8000);
39 EXPECT_EQ(Start, Index);
40 ASSERT_GE(Index, 0);
41 ASSERT_EQ(Reader.GetItemSize(Index), (int)sizeof(ItemTest));
42
43 int Type, Id;
44 const CMapItemTest *pTest = (const CMapItemTest *)Reader.GetItem(Index, pType: &Type, pId: &Id);
45 EXPECT_EQ(pTest, Reader.FindItem(MAPITEMTYPE_TEST, 0x8000));
46 EXPECT_EQ(Type, MAPITEMTYPE_TEST);
47 EXPECT_EQ(Id, 0x8000);
48
49 EXPECT_EQ(pTest->m_Version, ItemTest.m_Version);
50 EXPECT_EQ(pTest->m_aFields[0], ItemTest.m_aFields[0]);
51 EXPECT_EQ(pTest->m_aFields[1], ItemTest.m_aFields[1]);
52 EXPECT_EQ(pTest->m_Field3, ItemTest.m_Field3);
53 EXPECT_EQ(pTest->m_Field4, ItemTest.m_Field4);
54
55 Reader.Close();
56 }
57
58 if(!HasFailure())
59 {
60 pStorage->RemoveFile(pFilename: Info.m_aFilename, Type: IStorage::TYPE_SAVE);
61 }
62}
63
64TEST(Datafile, StringData)
65{
66 auto pStorage = std::unique_ptr<IStorage>(CreateLocalStorage());
67 CTestInfo Info;
68
69 {
70 CDataFileWriter Writer;
71 Writer.Open(pStorage: pStorage.get(), pFilename: Info.m_aFilename);
72
73 EXPECT_EQ(Writer.AddDataString(""), -1); // Empty string is not added
74 EXPECT_EQ(Writer.AddDataString("Abc"), 0);
75 EXPECT_EQ(Writer.AddDataString("DDNet最好了"), 1);
76 EXPECT_EQ(Writer.AddDataString("aβい🐘"), 2);
77 EXPECT_EQ(Writer.AddData(3, "Abc"), 3); // Not zero-terminated
78 EXPECT_EQ(Writer.AddData(7, "foo\0bar"), 4); // Early zero-terminator
79 EXPECT_EQ(Writer.AddData(5, "xyz\xff\0"), 5); // Truncated UTF-8
80 EXPECT_EQ(Writer.AddData(4, "XYZ\xff"), 6); // Truncated UTF-8 and not zero-terminated
81
82 Writer.Finish();
83 }
84
85 {
86 CDataFileReader Reader;
87 Reader.Open(pStorage: pStorage.get(), pFilename: Info.m_aFilename, StorageType: IStorage::TYPE_ALL);
88
89 EXPECT_EQ(Reader.GetDataString(-1000), nullptr);
90 EXPECT_STREQ(Reader.GetDataString(-1), "");
91 EXPECT_STREQ(Reader.GetDataString(0), "Abc");
92 EXPECT_STREQ(Reader.GetDataString(1), "DDNet最好了");
93 EXPECT_STREQ(Reader.GetDataString(2), "aβい🐘");
94 EXPECT_EQ(Reader.GetDataString(3), nullptr);
95 EXPECT_EQ(Reader.GetDataString(4), nullptr);
96 EXPECT_EQ(Reader.GetDataString(5), nullptr);
97 EXPECT_EQ(Reader.GetDataString(6), nullptr);
98 EXPECT_EQ(Reader.GetDataString(1000), nullptr);
99
100 Reader.Close();
101 }
102
103 if(!HasFailure())
104 {
105 pStorage->RemoveFile(pFilename: Info.m_aFilename, Type: IStorage::TYPE_SAVE);
106 }
107}
108