Path: blob/master/tests/core/io/test_file_access.cpp
45997 views
/**************************************************************************/1/* test_file_access.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_file_access)3334#include "core/io/dir_access.h"35#include "core/io/file_access.h"36#include "tests/test_utils.h"3738namespace TestFileAccess {3940TEST_CASE("[FileAccess] CSV read") {41Ref<FileAccess> f = FileAccess::open(TestUtils::get_data_path("testdata.csv"), FileAccess::READ);42REQUIRE(f.is_valid());4344Vector<String> header = f->get_csv_line(); // Default delimiter: ",".45REQUIRE(header.size() == 4);4647Vector<String> row1 = f->get_csv_line(","); // Explicit delimiter, should be the same.48REQUIRE(row1.size() == 4);49CHECK(row1[0] == "GOOD_MORNING");50CHECK(row1[1] == "Good Morning");51CHECK(row1[2] == "Guten Morgen");52CHECK(row1[3] == "Bonjour");5354Vector<String> row2 = f->get_csv_line();55REQUIRE(row2.size() == 4);56CHECK(row2[0] == "GOOD_EVENING");57CHECK(row2[1] == "Good Evening");58CHECK(row2[2].is_empty()); // Use case: not yet translated!59// https://github.com/godotengine/godot/issues/4426960CHECK_MESSAGE(row2[2] != "\"", "Should not parse empty string as a single double quote.");61CHECK(row2[3] == "\"\""); // Intentionally testing only escaped double quotes.6263Vector<String> row3 = f->get_csv_line();64REQUIRE(row3.size() == 6);65CHECK(row3[0] == "Without quotes");66CHECK(row3[1] == "With, comma");67CHECK(row3[2] == "With \"inner\" quotes");68CHECK(row3[3] == "With \"inner\", quotes\",\" and comma");69CHECK(row3[4] == "With \"inner\nsplit\" quotes and\nline breaks");70CHECK(row3[5] == "With \\nnewline chars"); // Escaped, not an actual newline.7172Vector<String> row4 = f->get_csv_line("~"); // Custom delimiter, makes inline commas easier.73REQUIRE(row4.size() == 3);74CHECK(row4[0] == "Some other");75CHECK(row4[1] == "delimiter");76CHECK(row4[2] == "should still work, shouldn't it?");7778Vector<String> row5 = f->get_csv_line("\t"); // Tab separated variables.79REQUIRE(row5.size() == 3);80CHECK(row5[0] == "What about");81CHECK(row5[1] == "tab separated");82CHECK(row5[2] == "lines, good?");83}8485TEST_CASE("[FileAccess] Get as UTF-8 String") {86SUBCASE("Newline == \\n (Unix)") {87Ref<FileAccess> f_lf = FileAccess::open(TestUtils::get_data_path("line_endings_lf.test.txt"), FileAccess::READ);88REQUIRE(f_lf.is_valid());89String s_lf = f_lf->get_as_utf8_string();90CHECK(s_lf == "Hello darkness\nMy old friend\nI've come to talk\nWith you again\n");91f_lf->seek(0);92CHECK(f_lf->get_line() == "Hello darkness");93CHECK(f_lf->get_line() == "My old friend");94CHECK(f_lf->get_line() == "I've come to talk");95CHECK(f_lf->get_line() == "With you again");96CHECK(f_lf->get_error() == Error::OK);97}9899SUBCASE("Newline == \\r\\n (Windows)") {100Ref<FileAccess> f_crlf = FileAccess::open(TestUtils::get_data_path("line_endings_crlf.test.txt"), FileAccess::READ);101REQUIRE(f_crlf.is_valid());102String s_crlf = f_crlf->get_as_utf8_string();103CHECK(s_crlf == "Hello darkness\r\nMy old friend\r\nI've come to talk\r\nWith you again\r\n");104f_crlf->seek(0);105CHECK(f_crlf->get_line() == "Hello darkness");106CHECK(f_crlf->get_line() == "My old friend");107CHECK(f_crlf->get_line() == "I've come to talk");108CHECK(f_crlf->get_line() == "With you again");109CHECK(f_crlf->get_error() == Error::OK);110}111112SUBCASE("Newline == \\r (Legacy macOS)") {113Ref<FileAccess> f_cr = FileAccess::open(TestUtils::get_data_path("line_endings_cr.test.txt"), FileAccess::READ);114REQUIRE(f_cr.is_valid());115String s_cr = f_cr->get_as_utf8_string();116CHECK(s_cr == "Hello darkness\rMy old friend\rI've come to talk\rWith you again\r");117f_cr->seek(0);118CHECK(f_cr->get_line() == "Hello darkness");119CHECK(f_cr->get_line() == "My old friend");120CHECK(f_cr->get_line() == "I've come to talk");121CHECK(f_cr->get_line() == "With you again");122CHECK(f_cr->get_error() == Error::OK);123}124125SUBCASE("Newline == Mixed") {126Ref<FileAccess> f_mix = FileAccess::open(TestUtils::get_data_path("line_endings_mixed.test.txt"), FileAccess::READ);127REQUIRE(f_mix.is_valid());128String s_mix = f_mix->get_as_utf8_string();129CHECK(s_mix == "Hello darkness\nMy old friend\r\nI've come to talk\rWith you again");130f_mix->seek(0);131CHECK(f_mix->get_line() == "Hello darkness");132CHECK(f_mix->get_line() == "My old friend");133CHECK(f_mix->get_line() == "I've come to talk");134CHECK(f_mix->get_line() == "With you again");135CHECK(f_mix->get_error() == Error::ERR_FILE_EOF); // Not a bug; the file lacks a final newline.136}137}138139TEST_CASE("[FileAccess] Get/Store floating point values") {140// BigEndian Hex: 0x40490E56141// LittleEndian Hex: 0x560E4940142float value = 3.1415f;143144SUBCASE("Little Endian") {145const String file_path = TestUtils::get_data_path("floating_point_little_endian.bin");146const String file_path_new = TestUtils::get_data_path("floating_point_little_endian_new.bin");147148Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);149REQUIRE(f.is_valid());150CHECK_EQ(f->get_float(), value);151152Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);153REQUIRE(fw.is_valid());154fw->store_float(value);155fw->close();156157CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));158159DirAccess::remove_file_or_error(file_path_new);160}161162SUBCASE("Big Endian") {163const String file_path = TestUtils::get_data_path("floating_point_big_endian.bin");164const String file_path_new = TestUtils::get_data_path("floating_point_big_endian_new.bin");165166Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);167REQUIRE(f.is_valid());168f->set_big_endian(true);169CHECK_EQ(f->get_float(), value);170171Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);172REQUIRE(fw.is_valid());173fw->set_big_endian(true);174fw->store_float(value);175fw->close();176177CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));178179DirAccess::remove_file_or_error(file_path_new);180}181}182183TEST_CASE("[FileAccess] Get/Store floating point half precision values") {184// IEEE 754 half-precision binary floating-point format:185// sign exponent (5 bits) fraction (10 bits)186// 0 01101 0101010101187// BigEndian Hex: 0x3555188// LittleEndian Hex: 0x5535189float value = 0.33325195f;190191SUBCASE("Little Endian") {192const String file_path = TestUtils::get_data_path("half_precision_floating_point_little_endian.bin");193const String file_path_new = TestUtils::get_data_path("half_precision_floating_point_little_endian_new.bin");194195Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);196REQUIRE(f.is_valid());197CHECK_EQ(f->get_half(), value);198199Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);200REQUIRE(fw.is_valid());201fw->store_half(value);202fw->close();203204CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));205206DirAccess::remove_file_or_error(file_path_new);207}208209SUBCASE("Big Endian") {210const String file_path = TestUtils::get_data_path("half_precision_floating_point_big_endian.bin");211const String file_path_new = TestUtils::get_data_path("half_precision_floating_point_big_endian_new.bin");212213Ref<FileAccess> f = FileAccess::open(file_path, FileAccess::READ);214REQUIRE(f.is_valid());215f->set_big_endian(true);216CHECK_EQ(f->get_half(), value);217218Ref<FileAccess> fw = FileAccess::open(file_path_new, FileAccess::WRITE);219REQUIRE(fw.is_valid());220fw->set_big_endian(true);221fw->store_half(value);222fw->close();223224CHECK_EQ(FileAccess::get_sha256(file_path_new), FileAccess::get_sha256(file_path));225226DirAccess::remove_file_or_error(file_path_new);227}228229SUBCASE("4096 bytes fastlz compressed") {230const String file_path = TestUtils::get_data_path("exactly_4096_bytes_fastlz.bin");231232Ref<FileAccess> f = FileAccess::open_compressed(file_path, FileAccess::READ, FileAccess::COMPRESSION_FASTLZ);233const Vector<uint8_t> full_data = f->get_buffer(4096 * 2);234CHECK(full_data.size() == 4096);235CHECK(f->eof_reached());236237// Data should be empty.238PackedByteArray reference;239reference.resize_initialized(4096);240CHECK(reference == full_data);241242f->seek(0);243const Vector<uint8_t> partial_data = f->get_buffer(4095);244CHECK(partial_data.size() == 4095);245CHECK(!f->eof_reached());246247reference.resize_initialized(4095);248CHECK(reference == partial_data);249}250}251252TEST_CASE("[FileAccess] Cursor positioning") {253Ref<FileAccess> f = FileAccess::open(TestUtils::get_data_path("line_endings_lf.test.txt"), FileAccess::READ);254REQUIRE(f.is_valid());255256String full = f->get_as_utf8_string();257int64_t len = full.length();258259SUBCASE("Initial position is zero") {260f->seek(0);261CHECK(f->get_position() == 0);262}263264SUBCASE("seek() moves cursor to absolute position") {265f->seek(5);266CHECK(f->get_position() == 5);267}268269SUBCASE("seek() moves cursor beyond file size") {270f->seek(len + 10);271CHECK(f->get_position() == len + 10);272}273274SUBCASE("seek_end() moves cursor to end of file") {275f->seek_end(0);276CHECK(f->get_position() == len);277}278279SUBCASE("seek_end() with positive offset") {280f->seek_end(1);281CHECK(f->get_position() == len + 1);282}283284SUBCASE("seek_end() with negative offset") {285f->seek_end(-1);286CHECK(f->get_position() == len - 1);287288char last_char = full[full.length() - 1];289CHECK(f->get_8() == last_char);290}291292SUBCASE("seek_end() beyond file size") {293f->seek(5);294f->seek_end(-len - 10); // seeking to a position below 0; ignored.295CHECK(f->get_position() == 5);296}297}298299} // namespace TestFileAccess300301302