OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
binary.cpp
Go to the documentation of this file.
1 
8 #include <cstdio>
9 #include <iostream>
10 #include <fstream>
11 
12 #include "global.h"
13 #include "utils/binary.h"
14 
15 const static char testData[] = {
16  // Unsigned Integers
17  -1,
18  -1, -1,
19  42, 42, 42, 42,
20  -1, -1, -1, -1, -1, 0, 0, 0,
21 
22  // Signed Integers
23  -1,
24  92, -2,
25  102, -3, -1, -1,
26  66, 66, 66, 66, 66, 66, 66, 66
27 };
28 
29 static float f1 = 3.1415926f;
30 static float f2 = 42.23f;
31 
32 static void fillFile(const char* name) {
33  std::ofstream file(name, std::ios_base::out | std::ios_base::binary);
34  file.write(testData, sizeof(testData) / sizeof(testData[0]));
35  file.write(reinterpret_cast<char*>(&f1), sizeof(f1));
36  file.write(reinterpret_cast<char*>(&f2), sizeof(f2));
37 }
38 
39 static int test(const char* name) {
40  BinaryFile file;
41  if (file.open(name) != 0) {
42  std::cout << "Error opening file " << name << std::endl;
43  return 1;
44  }
45 
46  if (file.readU8() != 255) {
47  std::cout << "Error reading U8!" << std::endl;
48  return 2;
49  }
50 
51  if (file.readU16() != 65535) {
52  std::cout << "Error reading U16!" << std::endl;
53  return 3;
54  }
55 
56  if (file.readU32() != 707406378) {
57  std::cout << "Error reading U32!" << std::endl;
58  return 4;
59  }
60 
61  if (file.readU64() != 1099511627775) {
62  std::cout << "Error reading U64!" << std::endl;
63  return 5;
64  }
65 
66  if (file.read8() != -1) {
67  std::cout << "Error reading 8!" << std::endl;
68  return 6;
69  }
70 
71  if (file.read16() != -420) {
72  std::cout << "Error reading 16!" << std::endl;
73  return 7;
74  }
75 
76  if (file.read32() != -666) {
77  std::cout << "Error reading 32!" << std::endl;
78  return 8;
79  }
80 
81  if (file.read64() != 4774451407313060418) {
82  std::cout << "Error reading 64!" << std::endl;
83  return 9;
84  }
85 
86  std::cout << "This test will fail on big-endian machines!" << std::endl;
87 
88  if (file.readFloat() != f1) {
89  std::cout << "Error reading float1!" << std::endl;
90  return 10;
91  }
92 
93  if (file.readFloat() != f2) {
94  std::cout << "Error reading float2!" << std::endl;
95  return 11;
96  }
97 
98  if (file.tell() != 38) {
99  std::cout << "Error, file position wrong!" << std::endl;
100  return 12;
101  }
102 
103  return 0;
104 }
105 
106 int main() {
107  char tmpFile[] = "/tmp/openraider_unit_test_0";
108  FILE* f;
109  while ((f = fopen(tmpFile, "r")) != NULL) {
110  fclose(f);
111  tmpFile[26]++;
112  }
113 
114  fillFile(tmpFile);
115  int error = test(tmpFile);
116  remove(tmpFile);
117 
118  return error;
119 }
120 
virtual uint8_t readU8()
Definition: binary.cpp:14
virtual int8_t read8()
Definition: binary.cpp:70
Binary file reading utilities.
virtual long long tell()
Definition: binary.cpp:123
Included everywhere.
virtual int16_t read16()
Definition: binary.cpp:77
static void error(char *msg)
Definition: commander.c:19
static int test(const char *name)
Definition: binary.cpp:39
virtual float readFloat()
Definition: binary.cpp:39
int main()
Definition: binary.cpp:106
virtual uint16_t readU16()
Definition: binary.cpp:21
virtual int64_t read64()
Definition: binary.cpp:93
static float f1
Definition: binary.cpp:29
static float f2
Definition: binary.cpp:30
virtual uint32_t readU32()
Definition: binary.cpp:27
virtual int32_t read32()
Definition: binary.cpp:85
static void fillFile(const char *name)
Definition: binary.cpp:32
static const char testData[]
Definition: binary.cpp:15
virtual uint64_t readU64()
Definition: binary.cpp:33
int open(std::string f="")
Definition: binary.cpp:112