-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathEditDistanceCalculatorTest.cpp
More file actions
188 lines (149 loc) · 6.96 KB
/
Copy pathEditDistanceCalculatorTest.cpp
File metadata and controls
188 lines (149 loc) · 6.96 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "../EditDistanceCalculator.h" // @manual
#include <folly/json/json.h>
#include <gtest/gtest.h>
#include <memory>
#include "../EditDistanceInputReader.h" // @manual
#include "../EditDistanceResults.h" // @manual
#include "../MPCTypes.h" // @manual
#include "./Constants.h" // @manual
#include "fbpcf/engine/communication/IPartyCommunicationAgentFactory.h"
#include "fbpcf/engine/communication/test/AgentFactoryCreationHelper.h"
#include "fbpcf/test/TestHelper.h"
#include "tools/cxx/Resources.h"
namespace fbpcf::edit_distance {
template <int schedulerId>
EditDistanceCalculator<schedulerId> createCalculatorWithScheduler(
int myRole,
EditDistanceInputReader&& inputData,
std::shared_ptr<fbpcf::scheduler::ISchedulerFactory<unsafe>>
schedulerFactory) {
auto scheduler = schedulerFactory->create();
fbpcf::scheduler::SchedulerKeeper<schedulerId>::setScheduler(
std::move(scheduler));
InputProcessor<schedulerId> inputProcessor(myRole, std::move(inputData));
return EditDistanceCalculator<schedulerId>(myRole, std::move(inputProcessor));
}
template <int schedulerId>
std::pair<std::vector<int64_t>, std::vector<std::string>> revealOutputs(
int myRole,
EditDistanceCalculator<schedulerId>& calculator) {
if (myRole == 0) {
auto distances =
calculator.getEditDistances().openToParty(PLAYER0).getValue();
auto receiverMessages =
calculator.getReceiverMessages().openToParty(PLAYER0).getValue();
calculator.getEditDistances().openToParty(PLAYER1).getValue();
calculator.getReceiverMessages().openToParty(PLAYER1).getValue();
return std::pair(distances, receiverMessages);
} else {
calculator.getEditDistances().openToParty(PLAYER0).getValue();
calculator.getReceiverMessages().openToParty(PLAYER0).getValue();
auto distances =
calculator.getEditDistances().openToParty(PLAYER1).getValue();
auto receiverMessages =
calculator.getReceiverMessages().openToParty(PLAYER1).getValue();
return std::pair(distances, receiverMessages);
}
}
TEST(EditDistanceCalculatorTest, testEditDistanceCalculator) {
boost::filesystem::path dataFilepath1 =
build::getResourcePath(std::getenv("DATA_FILE_PATH_1"));
boost::filesystem::path dataFilepath2 =
build::getResourcePath(std::getenv("DATA_FILE_PATH_2"));
boost::filesystem::path paramsFilePath =
build::getResourcePath(std::getenv("PARAMS_FILE_PATH"));
auto player0InputData =
EditDistanceInputReader(dataFilepath1.native(), paramsFilePath.native());
auto player1InputData =
EditDistanceInputReader(dataFilepath2.native(), paramsFilePath.native());
auto communicationAgentFactories =
fbpcf::engine::communication::getInMemoryAgentFactory(2);
auto schedulerType = fbpcf::SchedulerType::Lazy;
auto engineType = fbpcf::EngineType::EngineWithTupleFromFERRET;
// Creating shared pointers to the communicationAgentFactories
std::shared_ptr<fbpcf::engine::communication::IPartyCommunicationAgentFactory>
communicationAgentFactory0 = std::move(communicationAgentFactories[0]);
std::shared_ptr<fbpcf::engine::communication::IPartyCommunicationAgentFactory>
communicationAgentFactory1 = std::move(communicationAgentFactories[1]);
auto schedulerFactory0 = fbpcf::getSchedulerFactory<unsafe>(
schedulerType, engineType, 0, *communicationAgentFactory0);
auto schedulerFactory1 = fbpcf::getSchedulerFactory<unsafe>(
schedulerType, engineType, 1, *communicationAgentFactory1);
auto future0 = std::async(
createCalculatorWithScheduler<0>,
PLAYER0,
std::move(player0InputData),
std::move(schedulerFactory0));
auto future1 = std::async(
createCalculatorWithScheduler<1>,
PLAYER1,
std::move(player1InputData),
std::move(schedulerFactory1));
EditDistanceCalculator<0> player0Calculator = future0.get();
EditDistanceCalculator<1> player1Calculator = future1.get();
auto future2 = std::async(
revealOutputs<0>,
PLAYER0,
std::reference_wrapper<EditDistanceCalculator<0>>(player0Calculator));
auto future3 = std::async(
revealOutputs<1>,
PLAYER1,
std::reference_wrapper<EditDistanceCalculator<1>>(player1Calculator));
auto player0Results = future2.get();
auto player1Results = future3.get();
testVectorEq(std::get<0>(player0Results), kExpectedDistances);
testVectorEq(std::get<1>(player0Results), kExpectedReceiverMessages);
testVectorEq(std::get<0>(player1Results), kExpectedDistances);
testVectorEq(std::get<1>(player1Results), kExpectedReceiverMessages);
}
TEST(EditDistanceCalculatorTest, testToJson) {
boost::filesystem::path dataFilepath1 =
build::getResourcePath(std::getenv("DATA_FILE_PATH_1"));
boost::filesystem::path dataFilepath2 =
build::getResourcePath(std::getenv("DATA_FILE_PATH_2"));
boost::filesystem::path paramsFilePath =
build::getResourcePath(std::getenv("PARAMS_FILE_PATH"));
auto player0InputData =
EditDistanceInputReader(dataFilepath1.native(), paramsFilePath.native());
auto player1InputData =
EditDistanceInputReader(dataFilepath2.native(), paramsFilePath.native());
auto communicationAgentFactories =
fbpcf::engine::communication::getInMemoryAgentFactory(2);
auto schedulerType = fbpcf::SchedulerType::Lazy;
auto engineType = fbpcf::EngineType::EngineWithTupleFromFERRET;
// Creating shared pointers to the communicationAgentFactories
std::shared_ptr<fbpcf::engine::communication::IPartyCommunicationAgentFactory>
communicationAgentFactory0 = std::move(communicationAgentFactories[0]);
std::shared_ptr<fbpcf::engine::communication::IPartyCommunicationAgentFactory>
communicationAgentFactory1 = std::move(communicationAgentFactories[1]);
auto schedulerFactory0 = fbpcf::getSchedulerFactory<unsafe>(
schedulerType, engineType, 0, *communicationAgentFactory0);
auto schedulerFactory1 = fbpcf::getSchedulerFactory<unsafe>(
schedulerType, engineType, 1, *communicationAgentFactory1);
auto future0 = std::async(
createCalculatorWithScheduler<0>,
PLAYER0,
std::move(player0InputData),
std::move(schedulerFactory0));
auto future1 = std::async(
createCalculatorWithScheduler<1>,
PLAYER1,
std::move(player1InputData),
std::move(schedulerFactory1));
EditDistanceCalculator<0> player0Calculator = future0.get();
EditDistanceCalculator<1> player1Calculator = future1.get();
auto player0SharesJson = player0Calculator.toJson();
auto player1SharesJson = player1Calculator.toJson();
std::vector<folly::dynamic> shares = {
folly::parseJson(player0SharesJson), folly::parseJson(player1SharesJson)};
EditDistanceResults results(shares);
testVectorEq(results.editDistances, kExpectedDistances);
testVectorEq(results.receiverMessages, kExpectedReceiverMessages);
}
} // namespace fbpcf::edit_distance