Debian Codegraph @

tests/core/io/test_file_access.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_file_access.h */
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 #pragma once
32
33 #include "core/io/file_access.h"
34 #include "tests/test_macros.h"
35 #include "tests/test_utils.h"
36
37 namespace TestFileAccess {
38
39 TEST_CASE("[FileAccess] CSV read") {
40 Ref<FileAccess> f = FileAccess::open(TestUtils::get_data_path("testdata.csv"), FileAccess::READ);
41 REQUIRE(f.is_valid());
42
43 Vector<String> header = f->get_csv_line(); // Default delimiter: ",".
44 REQUIRE(header.size() == 4);
45
46 Vector<String> row1 = f->get_csv_line(","); // Explicit delimiter, should be the same.
47 REQUIRE(row1.size() == 4);
48 CHECK(row1[0] == "GOOD_MORNING");
49 CHECK(row1[1] == "Good Morning");
50 CHECK(row1[2] == "Guten Morgen");
51 CHECK(row1[3] == "Bonjour");
52
53 Vector<String> row2 = f->get_csv_line();
54 REQUIRE(row2.size() == 4);
55 CHECK(row2[0] == "GOOD_EVENING");
56 CHECK(row2[1] == "Good Evening");
57 CHECK(row2[2].is_empty()); // Use case: not yet translated!
58 // https://github.com/godotengine/godot/issues/44269
59 CHECK_MESSAGE(row2[2] != "\"", "Should not parse empty string as a single double quote.");
60 CHECK(row2[3] == "\"\""); // Intentionally testing only escaped double quotes.
61
62 Vector<String> row3 = f->get_csv_line();
63 REQUIRE(row3.size() == 6);
64 CHECK(row3[0] == "Without quotes");
65 CHECK(row3[1] == "With, comma");
66 CHECK(row3[2] == "With \"inner\" quotes");
67 CHECK(row3[3] == "With \"inner\", quotes\",\" and comma");
68 CHECK(row3[4] == "With \"inner\nsplit\" quotes and\nline breaks");
69 CHECK(row3[5] == "With \\nnewline chars"); // Escaped, not an actual newline.
70
71 Vector<String> row4 = f->get_csv_line("~"); // Custom delimiter, makes inline commas easier.
72 REQUIRE(row4.size() == 3);
73 CHECK(row4[0] == "Some other");
74 CHECK(row4[1] == "delimiter");
75 CHECK(row4[2] == "should still work, shouldn't it?");
76
77 Vector<String> row5 = f->get_csv_line("\t"); // Tab separated variables.
78 REQUIRE(row5.size() == 3);
79 CHECK(row5[0] == "What about");
80 CHECK(row5[1] == "tab separated");
81 CHECK(row5[2] == "lines, good?");
82 }
83
84 TEST_CASE("[FileAccess] Get as UTF-8 String") {
85 SUBCASE("Newline == \\n (Unix)") {
86 Ref<FileAccess> f_lf = FileAccess::open(TestUtils::get_data_path("line_endings_lf.test.txt"), FileAccess::READ);
87 REQUIRE(f_lf.is_valid());
88 String s_lf = f_lf->get_as_utf8_string();
89 CHECK(s_lf == "Hello darkness\nMy old friend\nI've come to talk\nWith you again\n");
90 f_lf->seek(0);
91 CHECK(f_lf->get_line() == "Hello darkness");
92 CHECK(f_lf->get_line() == "My old friend");
93 CHECK(f_lf->get_line() == "I've come to talk");
94 CHECK(f_lf->get_line() == "With you again");
95 CHECK(f_lf->get_error() == Error::OK);
96 }
97
98 SUBCASE("Newline == \\r\\n (Windows)") {
99 Ref<FileAccess> f_crlf = FileAccess::open(TestUtils::get_data_path("line_endings_crlf.test.txt"), FileAccess::READ);
100 REQUIRE(f_crlf.is_valid());
101 String s_crlf = f_crlf->get_as_utf8_string();
102 CHECK(s_crlf == "Hello darkness\r\nMy old friend\r\nI've come to talk\r\nWith you again\r\n");
103 f_crlf->seek(0);
104 CHECK(f_crlf->get_line() == "Hello darkness");
105 CHECK(f_crlf->get_line() == "My old friend");
106 CHECK(f_crlf->get_line() == "I've come to talk");
107 CHECK(f_crlf->get_line() == "With you again");
108 CHECK(f_crlf->get_error() == Error::OK);
109 }
110
111 SUBCASE("Newline == \\r (Legacy macOS)") {
112 Ref<FileAccess> f_cr = FileAccess::open(TestUtils::get_data_path("line_endings_cr.test.txt"), FileAccess::READ);
113 REQUIRE(f_cr.is_valid());
114 String s_cr = f_cr->get_as_utf8_string();
115 CHECK(s_cr == "Hello darkness\rMy old friend\rI've come to talk\rWith you again\r");
116 f_cr->seek(0);
117 CHECK(f_cr->get_line() == "Hello darkness");
118 CHECK(f_cr->get_line() == "My old friend");
119 CHECK(f_cr->get_line() == "I've come to talk");
120 CHECK(f_cr->get_line() == "With you again");
121 CHECK(f_cr->get_error() == Error::OK);
122 }
123
124 SUBCASE("Newline == Mixed") {
125 Ref<FileAccess> f_mix = FileAccess::open(TestUtils::get_data_path("line_endings_mixed.test.txt"), FileAccess::READ);
126 REQUIRE(f_mix.is_valid());
127 String s_mix = f_mix->get_as_utf8_string();
128 CHECK(s_mix == "Hello darkness\nMy old friend\r\nI've come to talk\rWith you again");
129 f_mix->seek(0);
130 CHECK(f_mix->get_line() == "Hello darkness");
131 CHECK(f_mix->get_line() == "My old friend");
132 CHECK(f_mix->get_line() == "I've come to talk");
133 CHECK(f_mix->get_line() == "With you again");
134 CHECK(f_mix->get_error() == Error::ERR_FILE_EOF); // Not a bug; the file lacks a final newline.
135 }
136 }
137
138 TEST_CASE("[FileAccess] Get/Store floating point values") {
139 // BigEndian Hex: 0x40490E56
140 // LittleEndian Hex: 0x560E4940
141 float value = 3.1415f;
142
143 SUBCASE("Little Endian") {
144 const String file_path = TestUtils::get_data_path("floating_point_little_endian.bin");
145 const String file_path_new = TestUtils::get_data_path("floating_point_little_endian_new.bin");
146
147 Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);
148 REQUIRE(f.is_valid());
149 CHECK_EQ(f->get_float(), value);
150
151 Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);
152 REQUIRE(fw.is_valid());
153 fw->store_float(value);
154 fw->close();
155
156 CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));
157
158 DirAccess::remove_file_or_error(file_path_new);
159 }
160
161 SUBCASE("Big Endian") {
162 const String file_path = TestUtils::get_data_path("floating_point_big_endian.bin");
163 const String file_path_new = TestUtils::get_data_path("floating_point_big_endian_new.bin");
164
165 Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);
166 REQUIRE(f.is_valid());
167 f->set_big_endian(true);
168 CHECK_EQ(f->get_float(), value);
169
170 Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);
171 REQUIRE(fw.is_valid());
172 fw->set_big_endian(true);
173 fw->store_float(value);
174 fw->close();
175
176 CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));
177
178 DirAccess::remove_file_or_error(file_path_new);
179 }
180 }
181
182 TEST_CASE("[FileAccess] Get/Store floating point half precision values") {
183 // IEEE 754 half-precision binary floating-point format:
184 // sign exponent (5 bits) fraction (10 bits)
185 // 0 01101 0101010101
186 // BigEndian Hex: 0x3555
187 // LittleEndian Hex: 0x5535
188 float value = 0.33325195f;
189
190 SUBCASE("Little Endian") {
191 const String file_path = TestUtils::get_data_path("half_precision_floating_point_little_endian.bin");
192 const String file_path_new = TestUtils::get_data_path("half_precision_floating_point_little_endian_new.bin");
193
194 Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);
195 REQUIRE(f.is_valid());
196 CHECK_EQ(f->get_half(), value);
197
198 Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);
199 REQUIRE(fw.is_valid());
200 fw->store_half(value);
201 fw->close();
202
203 CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));
204
205 DirAccess::remove_file_or_error(file_path_new);
206 }
207
208 SUBCASE("Big Endian") {
209 const String file_path = TestUtils::get_data_path("half_precision_floating_point_big_endian.bin");
210 const String file_path_new = TestUtils::get_data_path("half_precision_floating_point_big_endian_new.bin");
211
212 Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);
213 REQUIRE(f.is_valid());
214 f->set_big_endian(true);
215 CHECK_EQ(f->get_half(), value);
216
217 Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);
218 REQUIRE(fw.is_valid());
219 fw->set_big_endian(true);
220 fw->store_half(value);
221 fw->close();
222
223 CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));
224
225 DirAccess::remove_file_or_error(file_path_new);
226 }
227
228 SUBCASE("4096 bytes fastlz compressed") {
229 const String file_path = TestUtils::get_data_path("exactly_4096_bytes_fastlz.bin");
230
231 Ref<FileAccess> f = FileAccess::open_compressed(file_path, FileAccess::READ, FileAccess::COMPRESSION_FASTLZ);
232 const Vector<uint8_t> full_data = f->get_buffer(4096 * 2);
233 CHECK(full_data.size() == 4096);
234 CHECK(f->eof_reached());
235
236 // Data should be empty.
237 PackedByteArray reference;
238 reference.resize_initialized(4096);
239 CHECK(reference == full_data);
240
241 f->seek(0);
242 const Vector<uint8_t> partial_data = f->get_buffer(4095);
243 CHECK(partial_data.size() == 4095);
244 CHECK(!f->eof_reached());
245
246 reference.resize_initialized(4095);
247 CHECK(reference == partial_data);
248 }
249 }
250
251 } // namespace TestFileAccess