Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

binary.cpp 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. assertEqual(file.open(name), 0);
  36. assertEqual(file.readU8(), 255);
  37. assertEqual(file.readU16(), 65535);
  38. assertEqual(file.readU32(), 707406378);
  39. assertEqual(file.readU64(), 1099511627775);
  40. assertEqual(file.read8(), -1);
  41. assertEqual(file.read16(), -420);
  42. assertEqual(file.read32(), -666);
  43. assertEqual(file.read64(), 4774451407313060418);
  44. std::cout << "This test will fail on big-endian machines!" << std::endl;
  45. assertEqual(file.readFloat(), f1);
  46. assertEqual(file.readFloat(), f2);
  47. assertEqual(file.tell(), 38);
  48. return 0;
  49. }
  50. }
  51. int main() {
  52. char tmpFile[] = "/tmp/openraider_unit_test_0";
  53. FILE *f;
  54. while ((f = fopen(tmpFile, "r")) != NULL) {
  55. fclose(f);
  56. tmpFile[25]++;
  57. }
  58. fillFile(tmpFile);
  59. int error = test(tmpFile);
  60. remove(tmpFile);
  61. return error;
  62. }