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.8KB

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