test_file_access.h 9.2 KB

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