8
8
* types and strings.
9
9
*/
10
10
11
-
12
11
class Serializer {
13
12
public:
14
13
/* *
15
- * Serializes fundamental data types (like int, float, double, etc.) to a
16
- * binary file.
14
+ * @brief Serializes fundamental data types (like int, float, double, etc.)
15
+ * to a binary file.
17
16
* @tparam T The type of the data to be serialized.
18
17
* @param out The output stream (std::ofstream).
19
18
* @param data The data to be serialized.
@@ -22,9 +21,7 @@ class Serializer {
22
21
*/
23
22
template <typename T>
24
23
static void serialize (std::ofstream &out, const T &data) {
25
- static_assert (
26
- std::is_fundamental<T>::value,
27
- " Non-fundamental types are not allowed for this function!" );
24
+ static_assert (std::is_fundamental<T>::value, " Non-fundamental types are not allowed for this function!" );
28
25
out.write (reinterpret_cast <const char *>(&data), sizeof (T));
29
26
}
30
27
@@ -119,7 +116,7 @@ int main() {
119
116
Serializer::serialize (outFile, num);
120
117
Serializer::serialize (outFile, pi);
121
118
Serializer::serialize (outFile, message);
122
- outFile.close ();
119
+ outFile.close ();
123
120
124
121
int numRead;
125
122
float piRead;
@@ -142,6 +139,8 @@ int main() {
142
139
* @brief A test suite to perform extensive testing on the Serializer and
143
140
* Deserializer.
144
141
*/
142
+ #include < cassert> // For assert
143
+
145
144
void runTests () {
146
145
std::ofstream outFile (" test_output.bin" , std::ios::binary);
147
146
if (!outFile) {
@@ -181,8 +180,11 @@ void runTests() {
181
180
182
181
inFile.close ();
183
182
184
- std::cout << " Test Int: " << intResult << " \n " ;
185
- std::cout << " Test Double: " << doubleResult << " \n " ;
186
- std::cout << " Test Char: " << charResult << " \n " ;
187
- std::cout << " Test String: " << stringResult << " \n " ;
183
+ // Assert that the original and deserialized values are the same
184
+ assert (testInt == intResult);
185
+ assert (testDouble == doubleResult);
186
+ assert (testChar == charResult);
187
+ assert (testString == stringResult);
188
+
189
+ std::cout << " All tests passed!\n " ;
188
190
}
0 commit comments