Skip to content

Commit 155df97

Browse files
committed
fix: added Serialization namespace
1 parent 33573a9 commit 155df97

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

others/serialization.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include <string> // for std::string
3232
#include <type_traits> // for std::is_fundamental
3333

34+
/** \namespace ciphers
35+
* \brief Classes for binary Serialization and Deserialization
36+
*/
37+
namespace Serialization {
3438
/**
3539
* @class Serializer
3640
* @brief A utility class for serializing fundamental data types and strings to
@@ -68,8 +72,7 @@ class Serializer {
6872
// Check if the string exceeds the size limit of 1MB.
6973
const std::uint32_t max_size = 1024 * 1024;
7074
if (length > max_size) {
71-
throw std::runtime_error(
72-
"String exceeds the maximum size of 1MB.");
75+
throw std::runtime_error("String exceeds the maximum size of 1MB.");
7376
}
7477

7578
serialize(out, length); // Serialize the length of the string.
@@ -129,6 +132,8 @@ class Deserializer {
129132
}
130133
}
131134
};
135+
} // namespace Serialization
136+
132137
/**
133138
* @brief self test implementation
134139
* @return void
@@ -146,10 +151,10 @@ void tests() {
146151
std::string testString = "Testing String Serialization!";
147152

148153
// Serialize the data
149-
Serializer::serialize(outFile, testInt);
150-
Serializer::serialize(outFile, testDouble);
151-
Serializer::serialize(outFile, testChar);
152-
Serializer::serialize(outFile, testString);
154+
Serialization::Serializer::serialize(outFile, testInt);
155+
Serialization::Serializer::serialize(outFile, testDouble);
156+
Serialization::Serializer::serialize(outFile, testChar);
157+
Serialization::Serializer::serialize(outFile, testString);
153158

154159
outFile.close();
155160

@@ -165,10 +170,10 @@ void tests() {
165170
std::string stringResult;
166171

167172
// Deserialize the data
168-
Deserializer::deserialize(inFile, intResult);
169-
Deserializer::deserialize(inFile, doubleResult);
170-
Deserializer::deserialize(inFile, charResult);
171-
Deserializer::deserialize(inFile, stringResult);
173+
Serialization::Deserializer::deserialize(inFile, intResult);
174+
Serialization::Deserializer::deserialize(inFile, doubleResult);
175+
Serialization::Deserializer::deserialize(inFile, charResult);
176+
Serialization::Deserializer::deserialize(inFile, stringResult);
172177

173178
inFile.close();
174179

@@ -185,7 +190,9 @@ void tests() {
185190
std::ofstream largeOutFile("large_string.bin", std::ios::binary);
186191
// Create a string larger than 1MB
187192
std::string largeString(1024 * 1024 + 1, 'A');
188-
Serializer::serialize(largeOutFile, largeString); // Should throw an error
193+
Serialization::Serializer::serialize(
194+
largeOutFile,
195+
largeString); // Should throw an error
189196
largeOutFile.close();
190197
assert(false); // If we reach here, the test has failed.
191198
} catch (const std::runtime_error &e) {
@@ -195,20 +202,26 @@ void tests() {
195202
}
196203
// Test for missing delimiter in string serialization
197204
try {
198-
std::ofstream missingDelimiterOutFile("missing_delimiter.bin",std::ios::binary);
205+
std::ofstream missingDelimiterOutFile("missing_delimiter.bin",
206+
std::ios::binary);
199207

200208
std::string incompleteString = "Incomplete string test";
201209
std::uint32_t length = incompleteString.size();
202210

203211
// Serialize string length and content without the delimiter
204-
missingDelimiterOutFile.write(reinterpret_cast<const char *>(&length),sizeof(length));
205-
missingDelimiterOutFile.write(incompleteString.c_str(), length); // No delimiter '|'
212+
missingDelimiterOutFile.write(reinterpret_cast<const char *>(&length),
213+
sizeof(length));
214+
missingDelimiterOutFile.write(incompleteString.c_str(),
215+
length); // No delimiter '|'
206216
missingDelimiterOutFile.close();
207217

208-
std::ifstream missingDelimiterInFile("missing_delimiter.bin",std::ios::binary);
218+
std::ifstream missingDelimiterInFile("missing_delimiter.bin",
219+
std::ios::binary);
209220

210221
std::string deserializedString;
211-
Deserializer::deserialize(missingDelimiterInFile,deserializedString); // Should throw an error
222+
Serialization::Deserializer::deserialize(
223+
missingDelimiterInFile,
224+
deserializedString); // Should throw an error
212225
missingDelimiterInFile.close();
213226

214227
assert(false);
@@ -230,6 +243,6 @@ void tests() {
230243
* @returns 0 on successful exit
231244
*/
232245
int main() {
233-
tests(); // run self test implementations
246+
tests(); // run self test implementations
234247
return 0;
235248
}

0 commit comments

Comments
 (0)