File tree 3 files changed +15
-12
lines changed
3 files changed +15
-12
lines changed Original file line number Diff line number Diff line change 3
3
#include < cstring> // Include cstring for memset
4
4
#include < zlib.h>
5
5
6
- std::string tryCompress (const std::string& decompressed)
6
+ std::optional<std:: string> tryCompress (const std::string& decompressed)
7
7
{
8
8
z_stream zs;
9
9
memset (&zs, 0 , sizeof (zs));
10
10
11
11
if (deflateInit (&zs, Z_BEST_COMPRESSION) != Z_OK) {
12
- return " " ;
12
+ return std::nullopt ;
13
13
}
14
14
15
15
zs.next_in = reinterpret_cast <Bytef*>(const_cast <char *>(decompressed.data ()));
@@ -35,19 +35,19 @@ std::string tryCompress(const std::string& decompressed)
35
35
deflateEnd (&zs);
36
36
37
37
if (retCode != Z_STREAM_END) {
38
- return " " ;
38
+ return std::nullopt ;
39
39
}
40
40
41
41
return result;
42
42
}
43
43
44
- std::string tryDecompress (const std::string& compressed)
44
+ std::optional<std:: string> tryDecompress (const std::string& compressed)
45
45
{
46
46
z_stream zs;
47
47
memset (&zs, 0 , sizeof (zs));
48
48
49
49
if (inflateInit (&zs) != Z_OK) {
50
- return " " ;
50
+ return std::nullopt ;
51
51
}
52
52
53
53
zs.next_in = reinterpret_cast <Bytef*>(const_cast <char *>(compressed.data ()));
@@ -74,7 +74,7 @@ std::string tryDecompress(const std::string& compressed)
74
74
inflateEnd (&zs);
75
75
76
76
if (retCode != Z_STREAM_END) {
77
- return " " ;
77
+ return std::nullopt ;
78
78
}
79
79
80
80
return result;
Original file line number Diff line number Diff line change 4
4
#include < string>
5
5
#include < optional>
6
6
7
- std::string tryCompress (const std::string& decompressed);
8
- std::string tryDecompress (const std::string& compressed);
7
+ std::optional<std:: string> tryCompress (const std::string& decompressed);
8
+ std::optional<std:: string> tryDecompress (const std::string& compressed);
9
9
10
10
#endif /* ZLIBUTILS_H */
Original file line number Diff line number Diff line change @@ -7,11 +7,14 @@ TEST_CASE("test_server_zlib_utils", "[vpr]")
7
7
{
8
8
const std::string orig{" This string is going to be compressed now" };
9
9
10
- std::string compressed = tryCompress (orig);
11
- std::string decompressed = tryDecompress (compressed);
10
+ std::optional<std::string> compressedOpt = tryCompress (orig);
11
+ REQUIRE (compressedOpt);
12
+ REQUIRE (orig != compressedOpt.value ());
12
13
13
- REQUIRE (orig != compressed);
14
- REQUIRE (orig == decompressed);
14
+ std::optional<std::string> decompressedOpt = tryDecompress (compressedOpt.value ());
15
+ REQUIRE (decompressedOpt);
16
+
17
+ REQUIRE (orig == decompressedOpt.value ());
15
18
}
16
19
17
20
You can’t perform that action at this time.
0 commit comments