1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <thrift/conformance/data/TestGenerator.h>
18 
19 #include <fmt/core.h>
20 
21 #include <thrift/conformance/cpp2/Object.h>
22 #include <thrift/conformance/data/ValueGenerator.h>
23 #include <thrift/lib/cpp2/type/Name.h>
24 
25 namespace apache::thrift::conformance::data {
26 
27 namespace {
28 
29 template <typename TT>
createRoundTripTest(const AnyRegistry & registry,const Protocol & protocol)30 Test createRoundTripTest(
31     const AnyRegistry& registry, const Protocol& protocol) {
32   Test test;
33   test.name_ref() = protocol.name();
34   for (const auto& value : ValueGenerator<TT>::getInterestingValues()) {
35     RoundTripTestCase roundTrip;
36     roundTrip.request_ref()->value_ref() =
37         registry.store(asValueStruct<TT>(value.value), protocol);
38 
39     auto& testCase = test.testCases_ref()->emplace_back();
40     testCase.name_ref() = fmt::format("{}/{}", type::getName<TT>(), value.name);
41     testCase.test_ref()->set_roundTrip(std::move(roundTrip));
42   }
43 
44   return test;
45 }
46 
47 } // namespace
48 
addRoundTripToSuite(const AnyRegistry & registry,const Protocol & protocol,TestSuite & suite)49 void addRoundTripToSuite(
50     const AnyRegistry& registry, const Protocol& protocol, TestSuite& suite) {
51   suite.tests_ref()->emplace_back(
52       createRoundTripTest<type::bool_t>(registry, protocol));
53   suite.tests_ref()->emplace_back(
54       createRoundTripTest<type::byte_t>(registry, protocol));
55   suite.tests_ref()->emplace_back(
56       createRoundTripTest<type::i16_t>(registry, protocol));
57   suite.tests_ref()->emplace_back(
58       createRoundTripTest<type::i32_t>(registry, protocol));
59   suite.tests_ref()->emplace_back(
60       createRoundTripTest<type::float_t>(registry, protocol));
61   suite.tests_ref()->emplace_back(
62       createRoundTripTest<type::double_t>(registry, protocol));
63   suite.tests_ref()->emplace_back(
64       createRoundTripTest<type::string_t>(registry, protocol));
65   suite.tests_ref()->emplace_back(
66       createRoundTripTest<type::binary_t>(registry, protocol));
67 }
68 
createRoundTripSuite(const std::set<Protocol> & protocols,const AnyRegistry & registry)69 TestSuite createRoundTripSuite(
70     const std::set<Protocol>& protocols, const AnyRegistry& registry) {
71   TestSuite suite;
72   suite.name_ref() = "RoundTripTest";
73   for (const auto& protocol : protocols) {
74     addRoundTripToSuite(registry, protocol, suite);
75   }
76   return suite;
77 }
78 
79 } // namespace apache::thrift::conformance::data
80