1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <folly/lang/Byte.h>
18 
19 #include <folly/portability/GTest.h>
20 
21 class ByteTest : public testing::Test {};
22 
23 using byte = folly::byte;
24 using base = unsigned char;
25 
26 static_assert(sizeof(byte) == 1);
27 static_assert(!std::is_integral_v<byte>);
28 static_assert(std::is_standard_layout_v<byte>);
29 static_assert(std::is_trivial_v<byte>);
30 static_assert(std::is_same_v<std::underlying_type_t<byte>, base>);
31 
TEST_F(ByteTest,operations)32 TEST_F(ByteTest, operations) {
33   for (int b = 0; b <= std::numeric_limits<base>::max(); ++b) {
34     EXPECT_EQ(byte(~b), ~byte(b));
35 
36     for (int c = 0; c <= std::numeric_limits<base>::max(); ++c) {
37       EXPECT_EQ(byte(b & c), byte(b) & byte(c));
38       EXPECT_EQ(byte(b | c), byte(b) | byte(c));
39       EXPECT_EQ(byte(b ^ c), byte(b) ^ byte(c));
40 
41       byte o{};
42       EXPECT_EQ(byte(b & c), (o = byte(b)) &= byte(c));
43       EXPECT_EQ(byte(b | c), (o = byte(b)) |= byte(c));
44       EXPECT_EQ(byte(b ^ c), (o = byte(b)) ^= byte(c));
45     }
46 
47     for (int s = 0; s < int(sizeof(byte)); ++s) {
48       EXPECT_EQ(byte(b << s), byte(b) << s);
49       EXPECT_EQ(byte(b >> s), byte(b) >> s);
50 
51       byte o{};
52       EXPECT_EQ(byte(b << s), (o = byte(b)) <<= s);
53       EXPECT_EQ(byte(b >> s), (o = byte(b)) >>= s);
54     }
55   }
56 }
57 
TEST_F(ByteTest,to_integer)58 TEST_F(ByteTest, to_integer) {
59   EXPECT_EQ(7, folly::to_integer<int>(byte(7)));
60 }
61