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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "test.h"
  12. #include "utils/binary.h"
  13. namespace {
  14. const char testData[] = {
  15. // Unsigned Integers
  16. -1,
  17. -1, -1,
  18. 42, 42, 42, 42,
  19. -1, -1, -1, -1, -1, 0, 0, 0,
  20. // Signed Integers
  21. -1,
  22. 92, -2,
  23. 102, -3, -1, -1,
  24. 66, 66, 66, 66, 66, 66, 66, 66
  25. };
  26. float f1 = 3.1415926f;
  27. float f2 = 42.23f;
  28. void fillFile(const char *name) {
  29. std::ofstream file(name, std::ios_base::out | std::ios_base::binary);
  30. file.write(testData, sizeof(testData) / sizeof(testData[0]));
  31. file.write(reinterpret_cast<char *>(&f1), sizeof(f1));
  32. file.write(reinterpret_cast<char *>(&f2), sizeof(f2));
  33. }
  34. int test(const char *name) {
  35. BinaryFile file;
  36. assertEqual(file.open(name), 0);
  37. assertEqual(file.readU8(), 255);
  38. assertEqual(file.readU16(), 65535);
  39. assertEqual(file.readU32(), 707406378);
  40. assertEqual(file.readU64(), 1099511627775);
  41. assertEqual(file.read8(), -1);
  42. assertEqual(file.read16(), -420);
  43. assertEqual(file.read32(), -666);
  44. assertEqual(file.read64(), 4774451407313060418);
  45. std::cout << "This test will fail on big-endian machines!" << std::endl;
  46. assertEqual(file.readFloat(), f1);
  47. assertEqual(file.readFloat(), f2);
  48. assert(file.tell() == 38);
  49. return 0;
  50. }
  51. }
  52. int main() {
  53. char tmpFile[] = "/tmp/openraider_unit_test_0";
  54. FILE *f;
  55. while ((f = fopen(tmpFile, "r")) != NULL) {
  56. fclose(f);
  57. tmpFile[25]++;
  58. }
  59. fillFile(tmpFile);
  60. int error = test(tmpFile);
  61. remove(tmpFile);
  62. return error;
  63. }