/* * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved * * 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 using namespace CKM; namespace { RawBuffer buf({0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}); } // namespace anonymous BOOST_AUTO_TEST_SUITE(BINARY_QUEUE_TEST) BOOST_AUTO_TEST_CASE(copy_assignment) { BinaryQueue bq1; bq1.AppendCopy(buf.data(), buf.size()); BinaryQueue bq2; bq2 = bq1; BOOST_REQUIRE(bq1.Size() == bq2.Size() && bq1.Size() == buf.size()); RawBuffer buf1(bq1.Size(), 0x00); RawBuffer buf2(bq2.Size(), 0x00); bq1.Flatten(buf1.data(), buf1.size()); bq2.Flatten(buf2.data(), buf2.size()); BOOST_REQUIRE(buf1 == buf2); } BOOST_AUTO_TEST_CASE(append_copy_to) { BinaryQueue bq1; bq1.AppendCopy(buf.data(), buf.size()); BinaryQueue bq2; bq1.AppendCopyTo(bq2); BOOST_REQUIRE(bq1.Size() == bq2.Size() && bq1.Size() == buf.size()); RawBuffer buf1(bq1.Size(), 0x00); RawBuffer buf2(bq2.Size(), 0x00); bq1.Flatten(buf1.data(), buf1.size()); bq2.Flatten(buf2.data(), buf2.size()); BOOST_REQUIRE(buf1 == buf2); } BOOST_AUTO_TEST_CASE(append_move_to) { BinaryQueue bq1; bq1.AppendCopy(buf.data(), buf.size()); BinaryQueue bq2; bq1.AppendMoveTo(bq2); BOOST_REQUIRE(bq2.Size() == buf.size() && bq1.Empty()); RawBuffer buf2(bq2.Size(), 0x00); bq2.Flatten(buf2.data(), buf2.size()); BOOST_REQUIRE(buf == buf2); } BOOST_AUTO_TEST_CASE(read) { BinaryQueue bq1; bq1.AppendCopy(buf.data(), buf.size()); auto bq2 = bq1.Read(buf.size()); BOOST_REQUIRE(bq1.Empty()); RawBuffer buf2(bq2->Size(), 0x00); bq2->Flatten(buf2.data(), buf2.size()); BOOST_REQUIRE(buf == buf2); } BOOST_AUTO_TEST_CASE(write) { BinaryQueue bq1; bq1.AppendCopy(buf.data(), buf.size()); BinaryQueue bq2; bq2.Write(bq1, bq1.Size()); RawBuffer buf1(bq1.Size(), 0x00); RawBuffer buf2(bq2.Size(), 0x00); bq1.Flatten(buf1.data(), buf1.size()); bq2.Flatten(buf2.data(), buf2.size()); BOOST_REQUIRE(buf1 == buf2); } BOOST_AUTO_TEST_CASE(bucket_visitor) { static std::vector globalBuf; class BucketVisitorTest : public BinaryQueue::BucketVisitor { public: virtual void OnVisitBucket(const void *buffer, size_t bufferSize) override { for (size_t i = 0; i < bufferSize; ++i) globalBuf.push_back(static_cast(buffer)[i]); } }; BucketVisitorTest visitor; constexpr size_t BucketNum = 3; BinaryQueue bq; for (size_t i = 0; i < BucketNum; ++i) bq.AppendCopy(buf.data(), buf.size()); bq.VisitBuckets(&visitor); BOOST_REQUIRE(globalBuf.size() == buf.size() * BucketNum); for (size_t i = 0; i < BucketNum; ++i) for (size_t j = 0; j < buf.size(); ++j) BOOST_REQUIRE(globalBuf[i * buf.size() + j] == buf[j]); } BOOST_AUTO_TEST_SUITE_END()