// Copyright 2019 Google LLC // // 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 // // https://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 "runtime/cpp/emboss_prelude.h" #include #include "gtest/gtest.h" #include "runtime/cpp/emboss_cpp_util.h" #include "runtime/cpp/emboss_text_util.h" namespace emboss { namespace prelude { namespace test { using ::emboss::support::OffsetBitBlock; using ::emboss::support::ReadWriteContiguousBuffer; template using BitBlockN = ::emboss::support::BitBlock< ::emboss::support::LittleEndianByteOrderer, kBits>; template using ViewParameters = ::emboss::support::FixedSizeViewParameters< kBits, ::emboss::support::AllValuesAreOk>; TEST(FlagView, Methods) { ::std::uint8_t byte = 0; auto flag_view = FlagView, OffsetBitBlock>>{BitBlockN<8>{ ReadWriteContiguousBuffer{&byte, 1}}.GetOffsetStorage<1, 0>(0, 1)}; EXPECT_FALSE(flag_view.Read()); byte = 0xfe; EXPECT_FALSE(flag_view.Read()); byte = 0x01; EXPECT_TRUE(flag_view.Read()); byte = 0xff; EXPECT_TRUE(flag_view.Read()); EXPECT_TRUE(flag_view.CouldWriteValue(false)); EXPECT_TRUE(flag_view.CouldWriteValue(true)); flag_view.Write(false); EXPECT_EQ(0xfe, byte); byte = 0xaa; flag_view.Write(true); EXPECT_EQ(0xab, byte); } TEST(FlagView, TextDecode) { ::std::uint8_t byte = 0; const auto flag_view = FlagView, OffsetBitBlock>>{BitBlockN<8>{ ReadWriteContiguousBuffer{&byte, 1}}.GetOffsetStorage<1, 0>(0, 1)}; EXPECT_FALSE(UpdateFromText(flag_view, "")); EXPECT_FALSE(UpdateFromText(flag_view, "FALSE")); EXPECT_FALSE(UpdateFromText(flag_view, "TRUE")); EXPECT_FALSE(UpdateFromText(flag_view, "+true")); EXPECT_TRUE(UpdateFromText(flag_view, "true")); EXPECT_EQ(0x01, byte); EXPECT_TRUE(UpdateFromText(flag_view, "false")); EXPECT_EQ(0x00, byte); EXPECT_TRUE(UpdateFromText(flag_view, " true")); EXPECT_EQ(0x01, byte); { auto stream = support::TextStream{" false xxx"}; EXPECT_TRUE(flag_view.UpdateFromTextStream(&stream)); EXPECT_EQ(0x00, byte); ::std::string token; EXPECT_TRUE(::emboss::support::ReadToken(&stream, &token)); EXPECT_EQ("xxx", token); } } TEST(FlagView, TextEncode) { ::std::uint8_t byte = 0; const auto flag_view = FlagView, OffsetBitBlock>>{BitBlockN<8>{ ReadWriteContiguousBuffer{&byte, 1}}.GetOffsetStorage<1, 0>(0, 1)}; EXPECT_EQ("false", WriteToString(flag_view)); byte = 1; EXPECT_EQ("true", WriteToString(flag_view)); } template