Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_file_access.cpp
45997 views
1
/**************************************************************************/
2
/* test_file_access.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_file_access)
34
35
#include "core/io/dir_access.h"
36
#include "core/io/file_access.h"
37
#include "tests/test_utils.h"
38
39
namespace TestFileAccess {
40
41
TEST_CASE("[FileAccess] CSV read") {
42
Ref<FileAccess> f = FileAccess::open(TestUtils::get_data_path("testdata.csv"), FileAccess::READ);
43
REQUIRE(f.is_valid());
44
45
Vector<String> header = f->get_csv_line(); // Default delimiter: ",".
46
REQUIRE(header.size() == 4);
47
48
Vector<String> row1 = f->get_csv_line(","); // Explicit delimiter, should be the same.
49
REQUIRE(row1.size() == 4);
50
CHECK(row1[0] == "GOOD_MORNING");
51
CHECK(row1[1] == "Good Morning");
52
CHECK(row1[2] == "Guten Morgen");
53
CHECK(row1[3] == "Bonjour");
54
55
Vector<String> row2 = f->get_csv_line();
56
REQUIRE(row2.size() == 4);
57
CHECK(row2[0] == "GOOD_EVENING");
58
CHECK(row2[1] == "Good Evening");
59
CHECK(row2[2].is_empty()); // Use case: not yet translated!
60
// https://github.com/godotengine/godot/issues/44269
61
CHECK_MESSAGE(row2[2] != "\"", "Should not parse empty string as a single double quote.");
62
CHECK(row2[3] == "\"\""); // Intentionally testing only escaped double quotes.
63
64
Vector<String> row3 = f->get_csv_line();
65
REQUIRE(row3.size() == 6);
66
CHECK(row3[0] == "Without quotes");
67
CHECK(row3[1] == "With, comma");
68
CHECK(row3[2] == "With \"inner\" quotes");
69
CHECK(row3[3] == "With \"inner\", quotes\",\" and comma");
70
CHECK(row3[4] == "With \"inner\nsplit\" quotes and\nline breaks");
71
CHECK(row3[5] == "With \\nnewline chars"); // Escaped, not an actual newline.
72
73
Vector<String> row4 = f->get_csv_line("~"); // Custom delimiter, makes inline commas easier.
74
REQUIRE(row4.size() == 3);
75
CHECK(row4[0] == "Some other");
76
CHECK(row4[1] == "delimiter");
77
CHECK(row4[2] == "should still work, shouldn't it?");
78
79
Vector<String> row5 = f->get_csv_line("\t"); // Tab separated variables.
80
REQUIRE(row5.size() == 3);
81
CHECK(row5[0] == "What about");
82
CHECK(row5[1] == "tab separated");
83
CHECK(row5[2] == "lines, good?");
84
}
85
86
TEST_CASE("[FileAccess] Get as UTF-8 String") {
87
SUBCASE("Newline == \\n (Unix)") {
88
Ref<FileAccess> f_lf = FileAccess::open(TestUtils::get_data_path("line_endings_lf.test.txt"), FileAccess::READ);
89
REQUIRE(f_lf.is_valid());
90
String s_lf = f_lf->get_as_utf8_string();
91
CHECK(s_lf == "Hello darkness\nMy old friend\nI've come to talk\nWith you again\n");
92
f_lf->seek(0);
93
CHECK(f_lf->get_line() == "Hello darkness");
94
CHECK(f_lf->get_line() == "My old friend");
95
CHECK(f_lf->get_line() == "I've come to talk");
96
CHECK(f_lf->get_line() == "With you again");
97
CHECK(f_lf->get_error() == Error::OK);
98
}
99
100
SUBCASE("Newline == \\r\\n (Windows)") {
101
Ref<FileAccess> f_crlf = FileAccess::open(TestUtils::get_data_path("line_endings_crlf.test.txt"), FileAccess::READ);
102
REQUIRE(f_crlf.is_valid());
103
String s_crlf = f_crlf->get_as_utf8_string();
104
CHECK(s_crlf == "Hello darkness\r\nMy old friend\r\nI've come to talk\r\nWith you again\r\n");
105
f_crlf->seek(0);
106
CHECK(f_crlf->get_line() == "Hello darkness");
107
CHECK(f_crlf->get_line() == "My old friend");
108
CHECK(f_crlf->get_line() == "I've come to talk");
109
CHECK(f_crlf->get_line() == "With you again");
110
CHECK(f_crlf->get_error() == Error::OK);
111
}
112
113
SUBCASE("Newline == \\r (Legacy macOS)") {
114
Ref<FileAccess> f_cr = FileAccess::open(TestUtils::get_data_path("line_endings_cr.test.txt"), FileAccess::READ);
115
REQUIRE(f_cr.is_valid());
116
String s_cr = f_cr->get_as_utf8_string();
117
CHECK(s_cr == "Hello darkness\rMy old friend\rI've come to talk\rWith you again\r");
118
f_cr->seek(0);
119
CHECK(f_cr->get_line() == "Hello darkness");
120
CHECK(f_cr->get_line() == "My old friend");
121
CHECK(f_cr->get_line() == "I've come to talk");
122
CHECK(f_cr->get_line() == "With you again");
123
CHECK(f_cr->get_error() == Error::OK);
124
}
125
126
SUBCASE("Newline == Mixed") {
127
Ref<FileAccess> f_mix = FileAccess::open(TestUtils::get_data_path("line_endings_mixed.test.txt"), FileAccess::READ);
128
REQUIRE(f_mix.is_valid());
129
String s_mix = f_mix->get_as_utf8_string();
130
CHECK(s_mix == "Hello darkness\nMy old friend\r\nI've come to talk\rWith you again");
131
f_mix->seek(0);
132
CHECK(f_mix->get_line() == "Hello darkness");
133
CHECK(f_mix->get_line() == "My old friend");
134
CHECK(f_mix->get_line() == "I've come to talk");
135
CHECK(f_mix->get_line() == "With you again");
136
CHECK(f_mix->get_error() == Error::ERR_FILE_EOF); // Not a bug; the file lacks a final newline.
137
}
138
}
139
140
TEST_CASE("[FileAccess] Get/Store floating point values") {
141
// BigEndian Hex: 0x40490E56
142
// LittleEndian Hex: 0x560E4940
143
float value = 3.1415f;
144
145
SUBCASE("Little Endian") {
146
const String file_path = TestUtils::get_data_path("floating_point_little_endian.bin");
147
const String file_path_new = TestUtils::get_data_path("floating_point_little_endian_new.bin");
148
149
Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);
150
REQUIRE(f.is_valid());
151
CHECK_EQ(f->get_float(), value);
152
153
Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);
154
REQUIRE(fw.is_valid());
155
fw->store_float(value);
156
fw->close();
157
158
CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));
159
160
DirAccess::remove_file_or_error(file_path_new);
161
}
162
163
SUBCASE("Big Endian") {
164
const String file_path = TestUtils::get_data_path("floating_point_big_endian.bin");
165
const String file_path_new = TestUtils::get_data_path("floating_point_big_endian_new.bin");
166
167
Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);
168
REQUIRE(f.is_valid());
169
f->set_big_endian(true);
170
CHECK_EQ(f->get_float(), value);
171
172
Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);
173
REQUIRE(fw.is_valid());
174
fw->set_big_endian(true);
175
fw->store_float(value);
176
fw->close();
177
178
CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));
179
180
DirAccess::remove_file_or_error(file_path_new);
181
}
182
}
183
184
TEST_CASE("[FileAccess] Get/Store floating point half precision values") {
185
// IEEE 754 half-precision binary floating-point format:
186
// sign exponent (5 bits) fraction (10 bits)
187
// 0 01101 0101010101
188
// BigEndian Hex: 0x3555
189
// LittleEndian Hex: 0x5535
190
float value = 0.33325195f;
191
192
SUBCASE("Little Endian") {
193
const String file_path = TestUtils::get_data_path("half_precision_floating_point_little_endian.bin");
194
const String file_path_new = TestUtils::get_data_path("half_precision_floating_point_little_endian_new.bin");
195
196
Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);
197
REQUIRE(f.is_valid());
198
CHECK_EQ(f->get_half(), value);
199
200
Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);
201
REQUIRE(fw.is_valid());
202
fw->store_half(value);
203
fw->close();
204
205
CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));
206
207
DirAccess::remove_file_or_error(file_path_new);
208
}
209
210
SUBCASE("Big Endian") {
211
const String file_path = TestUtils::get_data_path("half_precision_floating_point_big_endian.bin");
212
const String file_path_new = TestUtils::get_data_path("half_precision_floating_point_big_endian_new.bin");
213
214
Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);
215
REQUIRE(f.is_valid());
216
f->set_big_endian(true);
217
CHECK_EQ(f->get_half(), value);
218
219
Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);
220
REQUIRE(fw.is_valid());
221
fw->set_big_endian(true);
222
fw->store_half(value);
223
fw->close();
224
225
CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));
226
227
DirAccess::remove_file_or_error(file_path_new);
228
}
229
230
SUBCASE("4096 bytes fastlz compressed") {
231
const String file_path = TestUtils::get_data_path("exactly_4096_bytes_fastlz.bin");
232
233
Ref<FileAccess> f = FileAccess::open_compressed(file_path, FileAccess::READ, FileAccess::COMPRESSION_FASTLZ);
234
const Vector<uint8_t> full_data = f->get_buffer(4096 * 2);
235
CHECK(full_data.size() == 4096);
236
CHECK(f->eof_reached());
237
238
// Data should be empty.
239
PackedByteArray reference;
240
reference.resize_initialized(4096);
241
CHECK(reference == full_data);
242
243
f->seek(0);
244
const Vector<uint8_t> partial_data = f->get_buffer(4095);
245
CHECK(partial_data.size() == 4095);
246
CHECK(!f->eof_reached());
247
248
reference.resize_initialized(4095);
249
CHECK(reference == partial_data);
250
}
251
}
252
253
TEST_CASE("[FileAccess] Cursor positioning") {
254
Ref<FileAccess> f = FileAccess::open(TestUtils::get_data_path("line_endings_lf.test.txt"), FileAccess::READ);
255
REQUIRE(f.is_valid());
256
257
String full = f->get_as_utf8_string();
258
int64_t len = full.length();
259
260
SUBCASE("Initial position is zero") {
261
f->seek(0);
262
CHECK(f->get_position() == 0);
263
}
264
265
SUBCASE("seek() moves cursor to absolute position") {
266
f->seek(5);
267
CHECK(f->get_position() == 5);
268
}
269
270
SUBCASE("seek() moves cursor beyond file size") {
271
f->seek(len + 10);
272
CHECK(f->get_position() == len + 10);
273
}
274
275
SUBCASE("seek_end() moves cursor to end of file") {
276
f->seek_end(0);
277
CHECK(f->get_position() == len);
278
}
279
280
SUBCASE("seek_end() with positive offset") {
281
f->seek_end(1);
282
CHECK(f->get_position() == len + 1);
283
}
284
285
SUBCASE("seek_end() with negative offset") {
286
f->seek_end(-1);
287
CHECK(f->get_position() == len - 1);
288
289
char last_char = full[full.length() - 1];
290
CHECK(f->get_8() == last_char);
291
}
292
293
SUBCASE("seek_end() beyond file size") {
294
f->seek(5);
295
f->seek_end(-len - 10); // seeking to a position below 0; ignored.
296
CHECK(f->get_position() == 5);
297
}
298
}
299
300
} // namespace TestFileAccess
301
302