@@ -214,20 +214,33 @@ TEST(EncodeTest, EncodeAdapted) {
214214 testEncodeAdapted<conformance::StandardProtocol::Compact>();
215215}
216216
217- template <conformance::StandardProtocol Protocol, typename Tag, typename T>
218- void testDecode (T value) {
219- SCOPED_TRACE (folly::pretty_name<Tag>());
217+ // Encodes the given value with EncodeTag, and decodes the result with DecodeTag
218+ // to DecodeT type using the Protocol.
219+ template <
220+ conformance::StandardProtocol Protocol,
221+ typename EncodeTag,
222+ typename DecodeTag,
223+ typename DecodeT,
224+ typename EncodeT>
225+ DecodeT encodeAndDecode (EncodeT value) {
220226 protocol_writer_t <Protocol> writer;
221227 folly::IOBufQueue queue;
222228 writer.setOutput (&queue);
223- encode<Tag >(writer, value);
229+ encode<EncodeTag >(writer, value);
224230
225231 protocol_reader_t <Protocol> reader;
226232 auto serialized = queue.move ();
227233 reader.setInput (serialized.get ());
228- T result;
229- decode<Tag>(reader, result);
230- EXPECT_EQ (result, value);
234+ DecodeT result;
235+ decode<DecodeTag>(reader, result);
236+ return result;
237+ }
238+
239+ template <conformance::StandardProtocol Protocol, typename Tag, typename T>
240+ void testDecode (T value) {
241+ SCOPED_TRACE (folly::pretty_name<Tag>());
242+ EXPECT_EQ (
243+ (encodeAndDecode<Protocol, Tag, Tag, decltype (value)>(value)), value);
231244}
232245
233246template <conformance::StandardProtocol Protocol>
@@ -246,10 +259,128 @@ void testDecodeBasicTypes() {
246259 testDecode<Protocol, type::enum_t <MyEnum>>(MyEnum::value);
247260}
248261
262+ template <conformance::StandardProtocol Protocol>
263+ void testDecodeContainers () {
264+ SCOPED_TRACE (apache::thrift::util::enumNameSafe (Protocol));
265+ testDecode<Protocol, type::list<type::enum_t <int >>>(
266+ std::vector<int32_t >{1 , 2 , 3 });
267+ testDecode<Protocol, type::list<type::bool_t >>(
268+ std::vector<bool >{true , false , true });
269+ testDecode<Protocol, type::set<type::bool_t >>(std::set<bool >{true , false });
270+ testDecode<Protocol, type::map<type::string_t , type::byte_t >>(
271+ std::map<std::string, int8_t >{
272+ {std::string (" foo" ), 1 }, {std::string (" foo" ), 2 }});
273+
274+ // Test if it skips when value type doesn't match.
275+ {
276+ auto result = encodeAndDecode<
277+ Protocol,
278+ type::list<type::i32_t >,
279+ type::list<type::string_t >,
280+ std::vector<std::string>>(std::vector<int32_t >{1 , 2 , 3 });
281+ EXPECT_TRUE (result.empty ());
282+ }
283+ {
284+ auto result = encodeAndDecode<
285+ Protocol,
286+ type::set<type::i32_t >,
287+ type::set<type::i64_t >,
288+ std::set<int64_t >>(std::set<int32_t >{1 , 2 , 3 });
289+ EXPECT_TRUE (result.empty ());
290+ }
291+ {
292+ auto result = encodeAndDecode<
293+ Protocol,
294+ type::map<type::i32_t , type::bool_t >,
295+ type::map<type::string_t , type::bool_t >,
296+ std::map<std::string, bool >>(
297+ std::map<int32_t , bool >{{1 , true }, {2 , false }});
298+ EXPECT_TRUE (result.empty ());
299+ }
300+ {
301+ auto result = encodeAndDecode<
302+ Protocol,
303+ type::map<type::string_t , type::bool_t >,
304+ type::map<type::string_t , type::i32_t >,
305+ std::map<std::string, int32_t >>(
306+ std::map<std::string, bool >{{" 1" , true }, {" 2" , false }});
307+ EXPECT_TRUE (result.empty ());
308+ }
309+ }
310+
311+ template <
312+ conformance::StandardProtocol Protocol,
313+ typename Struct,
314+ typename Tag,
315+ bool IsAdapted = false ,
316+ typename T>
317+ void testDecodeObject (T value) {
318+ Struct s;
319+ s.field_1_ref () = value;
320+ if constexpr (IsAdapted) {
321+ using AdaptedTag = type::adapted<test::TemplatedTestAdapter, Tag>;
322+ testDecode<Protocol, AdaptedTag>(test::TemplatedTestAdapter::fromThrift (s));
323+ } else {
324+ testDecode<Protocol, Tag>(s);
325+ }
326+ }
327+
328+ template <conformance::StandardProtocol Protocol>
329+ void testDecodeStruct () {
330+ SCOPED_TRACE (apache::thrift::util::enumNameSafe (Protocol));
331+ using Struct =
332+ test::testset::struct_with<type::map<type::string_t , type::i32_t >>;
333+ std::map<std::string, int > mapValues = {{" one" , 1 }, {" four" , 4 }, {" two" , 2 }};
334+ testDecodeObject<Protocol, Struct, type::struct_t <Struct>>(mapValues);
335+ using Union = test::testset::union_with<type::set<type::string_t >>;
336+ std::set<std::string> setValues = {" foo" , " bar" , " baz" };
337+ testDecodeObject<Protocol, Union, type::union_t <Union>>(setValues);
338+ using Exception = test::testset::exception_with<type::i64_t >;
339+ testDecodeObject<Protocol, Exception, type::exception_t <Exception>>(1 );
340+ }
341+
342+ template <conformance::StandardProtocol Protocol>
343+ void testDecodeCppType () {
344+ SCOPED_TRACE (apache::thrift::util::enumNameSafe (Protocol));
345+ testDecode<Protocol, type::cpp_type<int , type::i16_t >>(1 );
346+ }
347+
348+ template <conformance::StandardProtocol Protocol>
349+ void testDecodeAdapted () {
350+ SCOPED_TRACE (apache::thrift::util::enumNameSafe (Protocol));
351+ using AdaptedTag = type::adapted<test::TemplatedTestAdapter, type::string_t >;
352+ testDecode<Protocol, AdaptedTag>(
353+ test::TemplatedTestAdapter::fromThrift (std::string ()));
354+ using Struct = test::testset::struct_with<type::i32_t >;
355+ testDecodeObject<Protocol, Struct, type::struct_t <Struct>, true >(1 );
356+ using Union = test::testset::union_with<type::i32_t >;
357+ testDecodeObject<Protocol, Union, type::union_t <Union>, true >(1 );
358+ }
359+
249360TEST (DecodeTest, DecodeBasicTypes) {
250361 testDecodeBasicTypes<conformance::StandardProtocol::Binary>();
251362 testDecodeBasicTypes<conformance::StandardProtocol::Compact>();
252363}
253364
365+ TEST (DecodeTest, DecodeContainers) {
366+ testDecodeContainers<conformance::StandardProtocol::Binary>();
367+ testDecodeContainers<conformance::StandardProtocol::Compact>();
368+ }
369+
370+ TEST (DecodeTest, DecodeStruct) {
371+ testDecodeStruct<conformance::StandardProtocol::Binary>();
372+ testDecodeStruct<conformance::StandardProtocol::Compact>();
373+ }
374+
375+ TEST (DecodeTest, DecodeCppType) {
376+ testDecodeCppType<conformance::StandardProtocol::Binary>();
377+ testDecodeCppType<conformance::StandardProtocol::Compact>();
378+ }
379+
380+ TEST (DecodeTest, DecodeAdapted) {
381+ testDecodeAdapted<conformance::StandardProtocol::Binary>();
382+ testDecodeAdapted<conformance::StandardProtocol::Compact>();
383+ }
384+
254385} // namespace
255386} // namespace apache::thrift::op
0 commit comments