forked from google/fully-homomorphic-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc_transpiler_test.cc
144 lines (121 loc) · 4.83 KB
/
cc_transpiler_test.cc
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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "transpiler/cc_transpiler.h"
#include <string>
#include <type_traits>
#include <vector>
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "xls/common/status/matchers.h"
#include "xls/common/status/status_macros.h"
#include "xls/ir/function.h"
#include "xls/ir/package.h"
#include "xls/ir/type.h"
#include "xls/public/function_builder.h"
namespace fully_homomorphic_encryption::transpiler {
namespace {
// This test verifies that TranslateHeader works when there's no parameters.
TEST(CcTranspilerLibTest, TranslateHeader_NoParam) {
xls::Package package("test_package");
xls::FunctionBuilder builder("test_fn", &package);
std::vector<xls::Value> dummy;
xls::BValue empty_tuple = builder.Literal(xls::Value::Tuple(dummy));
XLS_ASSERT_OK_AND_ASSIGN(xls::Function * function,
builder.BuildWithReturnValue(empty_tuple));
// Mark the top function as returning void with no parameters.
xlscc_metadata::MetadataOutput metadata;
metadata.mutable_top_func_proto()->mutable_return_type()->mutable_as_void();
static constexpr absl::string_view expected_header =
R"(#ifndef TEST_H_
#define TEST_H_
#include "absl/status/status.h"
#include "absl/types/span.h"
absl::Status test_fn();
#endif // TEST_H_
)";
XLS_ASSERT_OK_AND_ASSIGN(
std::string actual,
CcTranspiler::TranslateHeader(function, metadata, "test.h"));
EXPECT_EQ(actual, expected_header);
}
// This test verifies that TranslateHeader works when there's one parameter.
TEST(CcTranspilerLibTest, TranslateHeader_Param) {
xls::Package package("test_package");
xls::FunctionBuilder builder("test_fn", &package);
xls::BitsType* value_type = package.GetBitsType(32);
builder.Param("param", value_type);
XLS_ASSERT_OK_AND_ASSIGN(xls::Function * function, builder.Build());
// Mark the top function as returning void with one parameter.
xlscc_metadata::MetadataOutput metadata;
metadata.mutable_top_func_proto()->mutable_return_type()->mutable_as_void();
xlscc_metadata::FunctionParameter* param =
metadata.mutable_top_func_proto()->add_params();
param->set_name("param");
static constexpr absl::string_view expected_header =
R"(#ifndef TEST_H_
#define TEST_H_
#include "absl/status/status.h"
#include "absl/types/span.h"
absl::Status test_fn(absl::Span<bool> param);
#endif // TEST_H_
)";
XLS_ASSERT_OK_AND_ASSIGN(
std::string actual,
CcTranspiler::TranslateHeader(function, metadata, "test.h"));
EXPECT_EQ(actual, expected_header);
}
// This test verifies that TranslateHeader works when there are multiple
// parameters.
TEST(CcTranspilerLibTest, TranslateHeader_MultipleParams) {
constexpr int kParamNum = 5;
xls::Package package("test_package");
xlscc_metadata::MetadataOutput metadata;
// Mark the top function as returning void.
metadata.mutable_top_func_proto()->mutable_return_type()->mutable_as_void();
xls::FunctionBuilder builder("test_fn", &package);
xls::BitsType* value_type = package.GetBitsType(32);
for (int param_index = 0; param_index < kParamNum; param_index++) {
const std::string param_name = absl::StrCat("param_", param_index);
builder.Param(param_name, value_type);
xlscc_metadata::FunctionParameter* param =
metadata.mutable_top_func_proto()->add_params();
param->set_name(param_name);
}
XLS_ASSERT_OK_AND_ASSIGN(xls::Function * function, builder.Build());
static constexpr absl::string_view expected_header_template =
R"(#ifndef TEST_H_
#define TEST_H_
#include "absl/status/status.h"
#include "absl/types/span.h"
absl::Status test_fn($0);
#endif // TEST_H_
)";
std::vector<std::string> expected_params;
for (int param_index = 0; param_index < kParamNum; param_index++) {
expected_params.push_back(
absl::StrCat("absl::Span<bool> param_", param_index));
}
std::string expected_header = absl::Substitute(
expected_header_template, absl::StrJoin(expected_params, ", "));
XLS_ASSERT_OK_AND_ASSIGN(
std::string actual,
CcTranspiler::TranslateHeader(function, metadata, "test.h"));
EXPECT_EQ(actual, expected_header);
}
} // namespace
} // namespace fully_homomorphic_encryption::transpiler