Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

binary.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*!
  2. * \file test/binary.cpp
  3. * \brief Binary Unit Test
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdio>
  8. #include <iostream>
  9. #include <fstream>
  10. #include "global.h"
  11. #include "utils/binary.h"
  12. const static char testData[] = {
  13. // Unsigned Integers
  14. -1,
  15. -1, -1,
  16. 42, 42, 42, 42,
  17. -1, -1, -1, -1, -1, 0, 0, 0,
  18. // Signed Integers
  19. -1,
  20. 92, -2,
  21. 102, -3, -1, -1,
  22. 66, 66, 66, 66, 66, 66, 66, 66
  23. };
  24. static float f1 = 3.1415926f;
  25. static float f2 = 42.23f;
  26. static void fillFile(const char* name) {
  27. std::ofstream file(name, std::ios_base::out | std::ios_base::binary);
  28. file.write(testData, sizeof(testData) / sizeof(testData[0]));
  29. file.write(reinterpret_cast<char*>(&f1), sizeof(f1));
  30. file.write(reinterpret_cast<char*>(&f2), sizeof(f2));
  31. }
  32. static int test(const char* name) {
  33. BinaryFile file;
  34. if (file.open(name) != 0) {
  35. std::cout << "Error opening file " << name << std::endl;
  36. return 1;
  37. }
  38. if (file.readU8() != 255) {
  39. std::cout << "Error reading U8!" << std::endl;
  40. return 2;
  41. }
  42. if (file.readU16() != 65535) {
  43. std::cout << "Error reading U16!" << std::endl;
  44. return 3;
  45. }
  46. if (file.readU32() != 707406378) {
  47. std::cout << "Error reading U32!" << std::endl;
  48. return 4;
  49. }
  50. if (file.readU64() != 1099511627775) {
  51. std::cout << "Error reading U64!" << std::endl;
  52. return 5;
  53. }
  54. if (file.read8() != -1) {
  55. std::cout << "Error reading 8!" << std::endl;
  56. return 6;
  57. }
  58. if (file.read16() != -420) {
  59. std::cout << "Error reading 16!" << std::endl;
  60. return 7;
  61. }
  62. if (file.read32() != -666) {
  63. std::cout << "Error reading 32!" << std::endl;
  64. return 8;
  65. }
  66. if (file.read64() != 4774451407313060418) {
  67. std::cout << "Error reading 64!" << std::endl;
  68. return 9;
  69. }
  70. std::cout << "This test will fail on big-endian machines!" << std::endl;
  71. if (file.readFloat() != f1) {
  72. std::cout << "Error reading float1!" << std::endl;
  73. return 10;
  74. }
  75. if (file.readFloat() != f2) {
  76. std::cout << "Error reading float2!" << std::endl;
  77. return 11;
  78. }
  79. if (file.tell() != 38) {
  80. std::cout << "Error, file position wrong!" << std::endl;
  81. return 12;
  82. }
  83. return 0;
  84. }
  85. int main() {
  86. char tmpFile[] = "/tmp/openraider_unit_test_0";
  87. FILE* f;
  88. while ((f = fopen(tmpFile, "r")) != NULL) {
  89. fclose(f);
  90. tmpFile[26]++;
  91. }
  92. fillFile(tmpFile);
  93. int error = test(tmpFile);
  94. remove(tmpFile);
  95. return error;
  96. }