/engines/ags/tests/test_file.cpp

https://github.com/somaen/scummvm · C++ · 195 lines · 150 code · 19 blank · 26 comment · 9 complexity · 02d51eafac27031e28952ac00eac16ec MD5 · raw file

  1. /* ScummVM - Graphic Adventure Engine
  2. *
  3. * ScummVM is the legal property of its developers, whose names
  4. * are too numerous to list here. Please refer to the COPYRIGHT
  5. * file distributed with this source distribution.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "ags/shared/core/platform.h"
  22. //include <string.h>
  23. #include "ags/shared/debugging/assert.h"
  24. #include "ags/shared/util/alignedstream.h"
  25. #include "ags/shared/util/file.h"
  26. namespace AGS3 {
  27. using namespace AGS::Shared;
  28. struct TTrickyAlignedData {
  29. char a;
  30. int b;
  31. int c;
  32. short d[3];
  33. int e;
  34. char f[17];
  35. int g[4];
  36. short h[13];
  37. char i[3];
  38. short j;
  39. int k;
  40. short l;
  41. short m;
  42. int n;
  43. int64_t i64a;
  44. char o;
  45. int64_t i64b;
  46. short p;
  47. int64_t i64c;
  48. short q;
  49. short r;
  50. int64_t i64d;
  51. char final;
  52. };
  53. void Test_File() {
  54. //-----------------------------------------------------
  55. // Operations
  56. Stream *out = File::OpenFile("test.tmp", AGS::Shared::kFile_CreateAlways, AGS::Shared::kFile_Write);
  57. out->WriteInt16(10);
  58. out->WriteInt64(-20202);
  59. String::WriteString("test.tmp", out);
  60. String very_long_string;
  61. very_long_string.FillString('a', 10000);
  62. very_long_string.Write(out);
  63. TTrickyAlignedData tricky_data_out;
  64. memset(&tricky_data_out, 0xAA, sizeof(tricky_data_out));
  65. {
  66. tricky_data_out.a = 11;
  67. tricky_data_out.b = 12;
  68. tricky_data_out.c = 13;
  69. tricky_data_out.d[0] = 14;
  70. tricky_data_out.d[1] = 15;
  71. tricky_data_out.d[2] = 16;
  72. tricky_data_out.e = 17;
  73. memset(tricky_data_out.f, 0, 17);
  74. tricky_data_out.g[0] = 18;
  75. tricky_data_out.g[1] = 19;
  76. tricky_data_out.g[2] = 20;
  77. tricky_data_out.g[3] = 21;
  78. memset(tricky_data_out.h, 0, 13 * sizeof(short));
  79. tricky_data_out.i[0] = 22;
  80. tricky_data_out.i[1] = 23;
  81. tricky_data_out.i[2] = 24;
  82. tricky_data_out.j = 25;
  83. tricky_data_out.k = 26;
  84. tricky_data_out.l = 27;
  85. tricky_data_out.m = 28;
  86. tricky_data_out.n = 29;
  87. tricky_data_out.i64a = 30;
  88. tricky_data_out.o = 31;
  89. tricky_data_out.i64b = 32;
  90. tricky_data_out.p = 33;
  91. tricky_data_out.i64c = 34;
  92. tricky_data_out.q = 35;
  93. tricky_data_out.r = 36;
  94. tricky_data_out.i64d = 37;
  95. tricky_data_out.final = 38;
  96. #if defined (TEST_BIGENDIAN)
  97. TTrickyAlignedData bigend_data = tricky_data_out;
  98. bigend_data.b = BBOp::SwapBytesInt32(bigend_data.b);
  99. bigend_data.c = BBOp::SwapBytesInt32(bigend_data.c);
  100. for (int i = 0; i < 3; ++i) {
  101. bigend_data.d[i] = BBOp::SwapBytesInt16(bigend_data.d[i]);
  102. }
  103. bigend_data.e = BBOp::SwapBytesInt32(bigend_data.e);
  104. for (int i = 0; i < 4; ++i) {
  105. bigend_data.g[i] = BBOp::SwapBytesInt32(bigend_data.g[i]);
  106. }
  107. for (int i = 0; i < 13; ++i) {
  108. bigend_data.h[i] = BBOp::SwapBytesInt16(bigend_data.h[i]);
  109. }
  110. bigend_data.j = BBOp::SwapBytesInt16(bigend_data.j);
  111. bigend_data.k = BBOp::SwapBytesInt32(bigend_data.k);
  112. bigend_data.l = BBOp::SwapBytesInt16(bigend_data.l);
  113. bigend_data.m = BBOp::SwapBytesInt16(bigend_data.m);
  114. bigend_data.n = BBOp::SwapBytesInt32(bigend_data.n);
  115. bigend_data.i64a = BBOp::SwapBytesInt64(bigend_data.i64a);
  116. bigend_data.i64b = BBOp::SwapBytesInt64(bigend_data.i64b);
  117. bigend_data.p = BBOp::SwapBytesInt16(bigend_data.p);
  118. bigend_data.i64c = BBOp::SwapBytesInt64(bigend_data.i64c);
  119. bigend_data.q = BBOp::SwapBytesInt16(bigend_data.q);
  120. bigend_data.r = BBOp::SwapBytesInt16(bigend_data.r);
  121. bigend_data.i64d = BBOp::SwapBytesInt64(bigend_data.i64d);
  122. out->Write(&bigend_data, sizeof(TTrickyAlignedData));
  123. #else
  124. out->Write(&tricky_data_out, sizeof(TTrickyAlignedData));
  125. #endif
  126. }
  127. out->WriteInt32(20);
  128. delete out;
  129. //-------------------------------------------------------------------------
  130. Stream *in = File::OpenFile("test.tmp", AGS::Shared::kFile_Open, AGS::Shared::kFile_Read);
  131. int16_t int16val = in->ReadInt16();
  132. int64_t int64val = in->ReadInt64();
  133. String str1 = String::FromStream(in);
  134. String str2 = String::FromStream(in);
  135. TTrickyAlignedData tricky_data_in;
  136. memset(&tricky_data_in, 0xAA, sizeof(tricky_data_in));
  137. {
  138. AlignedStream as(in, AGS::Shared::kAligned_Read);
  139. tricky_data_in.a = as.ReadInt8();
  140. tricky_data_in.b = as.ReadInt32();
  141. tricky_data_in.c = as.ReadInt32();
  142. as.ReadArrayOfInt16(tricky_data_in.d, 3);
  143. tricky_data_in.e = as.ReadInt32();
  144. as.Read(tricky_data_in.f, 17);
  145. as.ReadArrayOfInt32(tricky_data_in.g, 4);
  146. as.ReadArrayOfInt16(tricky_data_in.h, 13);
  147. as.Read(tricky_data_in.i, 3);
  148. tricky_data_in.j = as.ReadInt16();
  149. tricky_data_in.k = as.ReadInt32();
  150. tricky_data_in.l = as.ReadInt16();
  151. tricky_data_in.m = as.ReadInt16();
  152. tricky_data_in.n = as.ReadInt32();
  153. tricky_data_in.i64a = as.ReadInt64();
  154. tricky_data_in.o = as.ReadInt8();
  155. tricky_data_in.i64b = as.ReadInt64();
  156. tricky_data_in.p = as.ReadInt16();
  157. tricky_data_in.i64c = as.ReadInt64();
  158. tricky_data_in.q = as.ReadInt16();
  159. tricky_data_in.r = as.ReadInt16();
  160. tricky_data_in.i64d = as.ReadInt64();
  161. tricky_data_in.final = as.ReadInt8();
  162. }
  163. int32_t int32val = in->ReadInt32();
  164. delete in;
  165. File::DeleteFile("test.tmp");
  166. //-----------------------------------------------------
  167. // Assertions
  168. assert(int16val == 10);
  169. assert(int64val == -20202);
  170. assert(strcmp(str1, "test.tmp") == 0);
  171. assert(strcmp(str2, very_long_string) == 0);
  172. assert(memcmp(&tricky_data_in, &tricky_data_out, sizeof(TTrickyAlignedData)) == 0);
  173. assert(int32val == 20);
  174. assert(!File::TestReadFile("test.tmp"));
  175. }
  176. } // namespace AGS3