少年修仙传客户端基础资源
hch
2024-04-11 4c71d74b77c9eb62a0323698c9a0db3b641a917e
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#if ENABLE_UNIT_TESTS
 
#include "UnitTest++.h"
 
#include "../MemoryMappedFile-c-api.h"
#include "../../../utils/MemoryMappedFile.h"
#include "../../File.h"
#include "../File-c-api.h"
#include "PathHelper.h"
 
#if IL2CPP_TARGET_POSIX
#include <fcntl.h>
#include <unistd.h>
#endif
 
static const char* TEST_FILE_NAME = CURRENT_DIRECTORY("MEM_MAP_TEST_FILE");
static const char* TEST_STRING = "THIS IS A TEST\r\nSTRING TO \r\nBE USED IN A \r\nMEMORY MAPPED FILE\r\n";
 
class MapTestsWithParamsFixture
{
public:
    MapTestsWithParamsFixture() : offset(0), length(7)
    {
        Initialize();
    }
 
    MapTestsWithParamsFixture(size_t offset_in, size_t length_in) : offset(offset_in), length(length_in)
    {
        Initialize();
    }
 
    ~MapTestsWithParamsFixture()
    {
        il2cpp::utils::MemoryMappedFile::Unmap(apiAddress);
        il2cpp::utils::MemoryMappedFile::Unmap(classAddress);
        CloseTestFile(handle);
        DeleteTestFile(handle);
        handle = NULL;
    }
 
    il2cpp::os::FileHandle* handle;
    void* apiAddress;
    void* classAddress;
    int64_t length;
    int64_t offset;
 
private:
    void Initialize()
    {
        handle = NULL;
        handle = CreateTestFile();
        WriteCharactersToTestFile(handle);
        apiAddress = NULL;
        classAddress = il2cpp::utils::MemoryMappedFile::Map(handle, length, offset);
    }
 
    il2cpp::os::FileHandle* CreateTestFile()
    {
        int error;
        il2cpp::os::FileHandle* handle = il2cpp::os::File::Open(TEST_FILE_NAME, kFileModeOpenOrCreate, kFileAccessReadWrite, kFileShareReadWrite, 0, &error);
 
        return handle;
    }
 
    void WriteCharactersToTestFile(il2cpp::os::FileHandle* handle)
    {
        static const char* buffer = TEST_STRING;
        int error;
 
        il2cpp::os::File::Write(handle, buffer, (int)strlen(buffer), &error);
    }
 
    int CloseTestFile(il2cpp::os::FileHandle* handle)
    {
        int error;
        il2cpp::os::File::Close(handle, &error);
        return error;
    }
 
    int DeleteTestFile(il2cpp::os::FileHandle* handle)
    {
        int error;
        il2cpp::os::File::DeleteFile(TEST_FILE_NAME, &error);
        return error;
    }
};
 
class MapTestsFixture : public MapTestsWithParamsFixture
{
public:
    MapTestsFixture() : MapTestsWithParamsFixture(0, 0)
    {
    }
};
 
SUITE(MemoryMappedFile)
{
    TEST_FIXTURE(MapTestsFixture, MapReturnsAValidPointer)
    {
        apiAddress = UnityPalMemoryMappedFileMap(handle);
        CHECK_NOT_NULL(apiAddress);
    }
 
    TEST_FIXTURE(MapTestsFixture, MappedPointerHasMatchingCharactersAsFile)
    {
        apiAddress = UnityPalMemoryMappedFileMap(handle);
        CHECK_EQUAL(0, strncmp("THIS", (const char*)apiAddress, 4));
    }
 
    TEST_FIXTURE(MapTestsFixture, MappedPointerHasMatchingSizeAsFile)
    {
        apiAddress = UnityPalMemoryMappedFileMap(handle);
        CHECK_EQUAL(strlen(TEST_STRING), strlen((const char*)apiAddress));
    }
 
    TEST_FIXTURE(MapTestsFixture, ApiMapReturnsPointerThatDoesNotMatchClass)
    {
        apiAddress = UnityPalMemoryMappedFileMap(handle);
        CHECK_NOT_EQUAL(apiAddress, classAddress);
    }
 
    TEST_FIXTURE(MapTestsFixture, ApiMappedPointerCharactersMatchClassMappedPointer)
    {
        apiAddress = UnityPalMemoryMappedFileMap(handle);
        CHECK_EQUAL(strncmp("THIS", (const char*)classAddress, 4), strncmp("THIS", (const char*)apiAddress, 4));
    }
 
    TEST_FIXTURE(MapTestsFixture, ApiMappedLengthMatchesClassMatchedLength)
    {
        apiAddress = UnityPalMemoryMappedFileMap(handle);
        CHECK_EQUAL(strlen((const char*)classAddress), strlen((const char*)apiAddress));
    }
 
    TEST_FIXTURE(MapTestsWithParamsFixture, MapWithParamsReturnsAValidPointer)
    {
        apiAddress = UnityPalMemoryMappedFileMapWithParams(handle, length, offset);
        CHECK_NOT_NULL(apiAddress);
    }
 
    TEST_FIXTURE(MapTestsWithParamsFixture, MappedWithParamsPointerHasMatchingCharactersAsFile)
    {
        apiAddress = UnityPalMemoryMappedFileMapWithParams(handle, length, offset);
        CHECK_EQUAL(0, strncmp("THIS IS", (const char*)apiAddress, (size_t)length));
    }
 
    TEST_FIXTURE(MapTestsWithParamsFixture, MappedWithParamsPointerHasMatchingSizeAsFile)
    {
        apiAddress = UnityPalMemoryMappedFileMapWithParams(handle, length, offset);
        CHECK_EQUAL(strlen(TEST_STRING), strlen((const char*)apiAddress));
    }
 
    TEST_FIXTURE(MapTestsWithParamsFixture, ApiMapWithParamsReturnsPointerThatDoesNotMatchClass)
    {
        apiAddress = UnityPalMemoryMappedFileMapWithParams(handle, length, offset);
        CHECK_NOT_EQUAL(apiAddress, classAddress);
    }
 
    TEST_FIXTURE(MapTestsWithParamsFixture, ApiMappedWithParamsPointerCharactersMatchClassMappedPointer)
    {
        apiAddress = UnityPalMemoryMappedFileMapWithParams(handle, length, offset);
        CHECK_EQUAL(strncmp("THIS IS", (const char*)classAddress, (size_t)length), strncmp("THIS IS", (const char*)apiAddress, (size_t)length));
    }
 
    TEST_FIXTURE(MapTestsWithParamsFixture, ApiMappedWithParamsLengthMatchesClassMatchedLength)
    {
        apiAddress = UnityPalMemoryMappedFileMapWithParams(handle, length, offset);
        CHECK_EQUAL(strlen((const char*)classAddress), strlen((const char*)apiAddress));
    }
}
 
#endif // ENABLE_UNIT_TESTS