Skip to content

Commit 6e1f86d

Browse files
committed
fix: added assertions in test function.
1 parent 0b19d86 commit 6e1f86d

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

others/serialization.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
* types and strings.
99
*/
1010

11-
1211
class Serializer {
1312
public:
1413
/**
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.
1716
* @tparam T The type of the data to be serialized.
1817
* @param out The output stream (std::ofstream).
1918
* @param data The data to be serialized.
@@ -22,9 +21,7 @@ class Serializer {
2221
*/
2322
template <typename T>
2423
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!");
2825
out.write(reinterpret_cast<const char *>(&data), sizeof(T));
2926
}
3027

@@ -119,7 +116,7 @@ int main() {
119116
Serializer::serialize(outFile, num);
120117
Serializer::serialize(outFile, pi);
121118
Serializer::serialize(outFile, message);
122-
outFile.close();
119+
outFile.close();
123120

124121
int numRead;
125122
float piRead;
@@ -142,6 +139,8 @@ int main() {
142139
* @brief A test suite to perform extensive testing on the Serializer and
143140
* Deserializer.
144141
*/
142+
#include <cassert> // For assert
143+
145144
void runTests() {
146145
std::ofstream outFile("test_output.bin", std::ios::binary);
147146
if (!outFile) {
@@ -181,8 +180,11 @@ void runTests() {
181180

182181
inFile.close();
183182

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";
188190
}

0 commit comments

Comments
 (0)