// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you 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 "util/coding.h" #include #include #include #include "gtest/gtest_pred_impl.h" namespace doris { class CodingTest : public testing::Test { public: CodingTest() {} virtual ~CodingTest() {} }; TEST_F(CodingTest, fixed_le) { uint8_t buf[64]; encode_fixed8(buf, 124); uint8_t val8 = decode_fixed8(buf); EXPECT_EQ(124, val8); encode_fixed16_le(buf, 12345); uint16_t val16 = decode_fixed16_le(buf); EXPECT_EQ(12345, val16); encode_fixed32_le(buf, 1234554321); uint32_t val32 = decode_fixed32_le(buf); EXPECT_EQ(1234554321, val32); encode_fixed64_le(buf, 12345543211234554321UL); uint64_t val64 = decode_fixed64_le(buf); EXPECT_EQ(12345543211234554321UL, val64); std::string str; put_fixed32_le(&str, val32); put_fixed64_le(&str, val64); EXPECT_EQ(4 + 8, str.size()); val32 = decode_fixed32_le((const uint8_t*)str.data()); EXPECT_EQ(1234554321, val32); encode_fixed64_le(buf, 12345543211234554321UL); val64 = decode_fixed64_le((const uint8_t*)str.data() + 4); EXPECT_EQ(12345543211234554321UL, val64); } TEST_F(CodingTest, variant) { uint8_t buf[64]; { uint8_t* ptr = buf; ptr = encode_varint32(ptr, 1); ptr = encode_varint64(ptr, 2); EXPECT_EQ(2, ptr - buf); } const uint8_t* ptr = buf; const uint8_t* limit = ptr + 64; uint32_t val32; ptr = decode_varint32_ptr(ptr, limit, &val32); EXPECT_NE(nullptr, ptr); EXPECT_EQ(1, val32); uint64_t val64; ptr = decode_varint64_ptr(ptr, limit, &val64); EXPECT_NE(nullptr, ptr); EXPECT_EQ(2, val64); } TEST_F(CodingTest, variant_bigvalue) { uint8_t buf[64]; { uint8_t* ptr = buf; ptr = encode_varint32(ptr, 1234554321UL); EXPECT_EQ(5, ptr - buf); ptr = encode_varint64(ptr, 12345543211234554321UL); EXPECT_EQ(5 + 10, ptr - buf); } const uint8_t* ptr = buf; const uint8_t* limit = ptr + 64; uint32_t val32; ptr = decode_varint32_ptr(ptr, limit, &val32); EXPECT_NE(nullptr, ptr); EXPECT_EQ(1234554321UL, val32); uint64_t val64; ptr = decode_varint64_ptr(ptr, limit, &val64); EXPECT_NE(nullptr, ptr); EXPECT_EQ(12345543211234554321UL, val64); } TEST_F(CodingTest, variant_fail) { uint8_t buf[64]; { uint8_t* ptr = buf; ptr = encode_varint32(ptr, 1234554321UL); } { const uint8_t* ptr = buf; const uint8_t* limit = ptr + 4; uint32_t val32; ptr = decode_varint32_ptr(ptr, limit, &val32); EXPECT_EQ(nullptr, ptr); } { uint8_t* ptr = buf; ptr = encode_varint64(ptr, 12345543211234554321UL); } { const uint8_t* ptr = buf; const uint8_t* limit = ptr + 4; uint64_t val64; ptr = decode_varint64_ptr(ptr, limit, &val64); EXPECT_EQ(nullptr, ptr); } } TEST_F(CodingTest, put_varint) { std::string val; put_varint32(&val, 1); put_varint64(&val, 2); put_varint64_varint32(&val, 3, 4); EXPECT_EQ(4, val.size()); const uint8_t* ptr = (const uint8_t*)val.data(); const uint8_t* limit = ptr + 4; uint32_t val32; uint64_t val64; ptr = decode_varint32_ptr(ptr, limit, &val32); EXPECT_EQ(1, val32); ptr = decode_varint64_ptr(ptr, limit, &val64); EXPECT_EQ(2, val64); ptr = decode_varint64_ptr(ptr, limit, &val64); EXPECT_EQ(3, val64); ptr = decode_varint32_ptr(ptr, limit, &val32); EXPECT_EQ(4, val32); } } // namespace doris