Symphony Of Empires
archive.cpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // triangle.hpp
20 //
21 // Abstract:
22 // Small program to test that rendering works.
23 // ----------------------------------------------------------------------------
24 
25 #include <iostream>
26 #include <vector>
27 #include <array>
28 #include <algorithm>
29 
30 #include "eng3d/state.hpp"
31 #include "eng3d/string.hpp"
32 #include "eng3d/event.hpp"
33 #include "eng3d/serializer.hpp"
34 
35 struct GameState : Eng3D::State {
36  GameState(const std::vector<std::string>& pkg_paths)
37  : Eng3D::State::State(pkg_paths)
38  {
39 
40  }
41  ~GameState() = default;
42 };
43 
44 template<typename T>
45 static int test_numerical_vector(size_t max_samples = 65536) {
46  std::vector<T> fuzz(max_samples);
47  std::iota(fuzz.begin(), fuzz.end(), 1);
48  const auto expected = fuzz;
49  {
51  Eng3D::Deser::serialize(ar, fuzz);
52  ar.to_file("fuzz.bin");
53  }
54 
55  {
57  ar.from_file("fuzz.bin");
58  Eng3D::Deser::deserialize(ar, fuzz);
59 
60  std::cout << "Elements stored " << fuzz.size() << " expected " << expected.size() << std::endl;
61  for(size_t i = 0; i < fuzz.size(); i++) {
62  if(fuzz[i] != expected[i]) {
63  std::cout << "Test failed, element " << i << " is different (" << fuzz[i] << " != " << expected[i] << ")" << std::endl;
64  return -1;
65  }
66  }
67  }
68  std::cout << "Test passed" << std::endl;
69  return 0;
70 }
71 
72 template<typename T, int max_samples = 32>
73 static int test_numerical_array() {
74  std::array<T, max_samples> fuzz;
75  std::iota(fuzz.begin(), fuzz.end(), 1);
76  const auto expected = fuzz;
77  {
79  Eng3D::Deser::serialize(ar, fuzz);
80  ar.to_file("fuzz.bin");
81  }
82 
83  {
85  ar.from_file("fuzz.bin");
86  Eng3D::Deser::deserialize(ar, fuzz);
87 
88  std::cout << "Elements stored " << fuzz.size() << " expected " << expected.size() << std::endl;
89  for(size_t i = 0; i < fuzz.size(); i++) {
90  if(fuzz[i] != expected[i]) {
91  std::cout << "Test failed, element " << i << " is different (" << fuzz[i] << " != " << expected[i] << ")" << std::endl;
92  return -1;
93  }
94  }
95  }
96  std::cout << "Test passed" << std::endl;
97  return 0;
98 }
99 
100 static int test_string(size_t max_samples = 32) {
101  std::string fuzz;
102  fuzz.resize(max_samples);
103 
104  fuzz[max_samples] = '\0';
105  while(--max_samples)
106  fuzz[max_samples] = 'A' + (rand() % 26);
107 
108  const auto expected = fuzz;
109  {
111  Eng3D::Deser::serialize(ar, fuzz);
112  ar.to_file("fuzz.bin");
113  }
114 
115  {
117  ar.from_file("fuzz.bin");
118  Eng3D::Deser::deserialize(ar, fuzz);
119 
120  std::cout << "Elements stored " << fuzz.size() << " expected " << expected.size() << std::endl;
121  for(size_t i = 0; i < fuzz.size(); i++) {
122  if(fuzz[i] != expected[i]) {
123  std::cout << "Test failed, element " << i << " is different (" << fuzz[i] << " != " << expected[i] << ")" << std::endl;
124  return -1;
125  }
126  }
127  }
128  std::cout << "Test passed" << std::endl;
129  return 0;
130 }
131 
132 int main(int, char**) {
133  std::cout << "std::vector" << std::endl;
134  test_numerical_vector<int>();
135  test_numerical_vector<unsigned int>();
136  test_numerical_vector<long long>();
137  test_numerical_vector<unsigned long long>();
138 
139  std::cout << "std::array" << std::endl;
140  test_numerical_array<int>();
141  test_numerical_array<unsigned int>();
142  test_numerical_array<long long>();
143  test_numerical_array<unsigned long long>();
144 
145  std::cout << "std::string" << std::endl;
146  test_string();
147 }
int main(int, char **)
Definition: archive.cpp:132
State(const std::vector< std::string > &pkg_paths)
Definition: state.cpp:318
~GameState()=default
GameState(const std::vector< std::string > &pkg_paths)
Definition: archive.cpp:36
void deserialize(Eng3D::Deser::Archive &ar, T &obj)
Definition: serializer.hpp:154
void serialize(Eng3D::Deser::Archive &ar, const T &obj)
Definition: serializer.hpp:144
Base class that serves as archiver, stores (in memory) the data required for serialization/deserializ...
Definition: serializer.hpp:64
void to_file(const ::std::string &path)
Definition: serializer.cpp:40
void from_file(const ::std::string &path)
Definition: serializer.cpp:61