/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * 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 #include #include #include namespace apache::thrift::op { namespace { template void testTypeRegistryLoad(const S& seralizer, const T& value) { folly::IOBufQueue queue; seralizer.encode(value, folly::io::QueueAppender{&queue, 2 << 4}); type::SemiAnyStruct sa; sa.protocol() = seralizer.getProtocol(); sa.data() = *queue.front(); sa.type() = type::Type::get(); type::AnyData anyData(sa); auto result = type::TypeRegistry::generated().load(anyData); EXPECT_EQ(result.as(), value); } template void testTypeRegistryStore(const S& seralizer, const T& value) { auto anyData = type::TypeRegistry::generated().store( type::ConstRef(Tag{}, value), seralizer.getProtocol()); folly::io::Cursor cursor(&anyData.data()); auto actual = seralizer.template decode(cursor); EXPECT_EQ(actual, value); } template void testSerialization(const S& seralizer, const T& value) { test::expectRoundTrip(seralizer, value); testTypeRegistryLoad(seralizer, value); testTypeRegistryStore(seralizer, value); } // Checks that the value roundtrips correctly for all standard protocols. template > void testRoundTrip(const T& value) { using type::StandardProtocol; FBTHRIFT_SCOPED_CHECK(testSerialization( StdSerializer(), value)); FBTHRIFT_SCOPED_CHECK(testSerialization( StdSerializer(), value)); FBTHRIFT_SCOPED_CHECK(testSerialization( StdSerializer(), value)); } TEST(StdSerializerTest, Struct) { using protocol::asValueStruct; FBTHRIFT_SCOPED_CHECK(testRoundTrip(asValueStruct(true))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(asValueStruct(1))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(asValueStruct(1))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(asValueStruct(1))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(asValueStruct(1))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(asValueStruct(1))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(asValueStruct(1))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(asValueStruct("hi"))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(asValueStruct("hi"))); } TEST(StdSerializerTest, PrimaryTypes) { FBTHRIFT_SCOPED_CHECK(testRoundTrip(true)); FBTHRIFT_SCOPED_CHECK(testRoundTrip(std::int8_t(16))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(std::int16_t(16))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(std::int32_t(32))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(std::int64_t(64))); FBTHRIFT_SCOPED_CHECK(testRoundTrip(1.1f)); FBTHRIFT_SCOPED_CHECK(testRoundTrip(2.2)); FBTHRIFT_SCOPED_CHECK((testRoundTrip("hi"))); FBTHRIFT_SCOPED_CHECK((testRoundTrip("hi"))); } } // namespace } // namespace apache::thrift::op