forked from kimlaine/bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialization.cpp
138 lines (123 loc) · 4.38 KB
/
serialization.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include <sstream>
#include <fstream>
#include <string>
#include <functional>
#include "gtest/gtest.h"
#include "seal/util/defines.h"
#include "seal/serialization.h"
using namespace seal;
using namespace std;
namespace SEALTest
{
struct test_struct
{
int a;
int b;
double c;
void save_members(ostream &stream)
{
stream.write(reinterpret_cast<const char*>(&a), sizeof(int));
stream.write(reinterpret_cast<const char*>(&b), sizeof(int));
stream.write(reinterpret_cast<const char*>(&c), sizeof(double));
}
void load_members(istream &stream)
{
stream.read(reinterpret_cast<char*>(&a), sizeof(int));
stream.read(reinterpret_cast<char*>(&b), sizeof(int));
stream.read(reinterpret_cast<char*>(&c), sizeof(double));
}
streamoff save_size(compr_mode_type compr_mode) const
{
size_t members_size = Serialization::ComprSizeEstimate(
sizeof(test_struct), compr_mode);
return static_cast<streamoff>(sizeof(Serialization::SEALHeader) + members_size);
}
};
TEST(SerializationTest, SaveLoadToStream)
{
test_struct st{ 3, ~0, 3.14159 }, st2;
using namespace std::placeholders;
stringstream stream;
auto out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1),
st.save_size(compr_mode_type::none),
stream, compr_mode_type::none);
auto in_size = Serialization::Load(
bind(&test_struct::load_members, &st2, _1),
stream);
ASSERT_EQ(out_size, in_size);
ASSERT_EQ(st.a, st2.a);
ASSERT_EQ(st.b, st2.b);
ASSERT_EQ(st.c, st2.c);
#ifdef SEAL_USE_ZLIB
test_struct st3;
out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1),
st.save_size(compr_mode_type::none),
stream, compr_mode_type::deflate);
in_size = Serialization::Load(
bind(&test_struct::load_members, &st3, _1),
stream);
ASSERT_EQ(out_size, in_size);
ASSERT_EQ(st.a, st3.a);
ASSERT_EQ(st.b, st3.b);
ASSERT_EQ(st.c, st3.c);
#endif
}
TEST(SerializationTest, SaveLoadToBuffer)
{
test_struct st{ 3, ~0, 3.14159 }, st2;
using namespace std::placeholders;
constexpr size_t arr_size = 1024;
SEAL_BYTE buffer[arr_size]{};
stringstream ss;
auto test_out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1),
st.save_size(compr_mode_type::none),
ss, compr_mode_type::none);
auto out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1),
st.save_size(compr_mode_type::none),
buffer, arr_size, compr_mode_type::none);
ASSERT_EQ(test_out_size, out_size);
for (size_t i = static_cast<size_t>(out_size); i < arr_size; i++)
{
ASSERT_TRUE(SEAL_BYTE(0) == buffer[i]);
}
auto in_size = Serialization::Load(
bind(&test_struct::load_members, &st2, _1),
buffer, arr_size);
ASSERT_EQ(out_size, in_size);
ASSERT_EQ(st.a, st2.a);
ASSERT_EQ(st.b, st2.b);
ASSERT_EQ(st.c, st2.c);
#ifdef SEAL_USE_ZLIB
// Reset buffer back to zero
memset(buffer, 0, arr_size);
test_struct st3;
ss.seekp(0);
test_out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1),
st.save_size(compr_mode_type::none),
ss, compr_mode_type::deflate);
out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1),
st.save_size(compr_mode_type::none),
buffer, arr_size, compr_mode_type::deflate);
ASSERT_EQ(test_out_size, out_size);
for (size_t i = static_cast<size_t>(out_size); i < arr_size; i++)
{
ASSERT_EQ(SEAL_BYTE(0), buffer[i]);
}
in_size = Serialization::Load(
bind(&test_struct::load_members, &st3, _1),
buffer, arr_size);
ASSERT_EQ(out_size, in_size);
ASSERT_EQ(st.a, st3.a);
ASSERT_EQ(st.b, st3.b);
ASSERT_EQ(st.c, st3.c);
#endif
}
}