• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The Android Open Source Project
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 <gtest/gtest.h>
18 #include <cstdint>
19 
20 #include "l2cap/signal_id.h"
21 
22 namespace bluetooth {
23 namespace l2cap {
24 
TEST(L2capSignalIdTest,valid_values)25 TEST(L2capSignalIdTest, valid_values) {
26   int valid = 0;
27   uint8_t i = 0;
28   while (++i != 0) {
29     SignalId signal_id(i);
30     if (signal_id.IsValid()) {
31       valid++;
32     }
33   }
34   ASSERT_TRUE(valid == 255);
35 }
36 
TEST(L2capSignalIdTest,zero_invalid)37 TEST(L2capSignalIdTest, zero_invalid) {
38   SignalId signal_id(0);
39   ASSERT_FALSE(signal_id.IsValid());
40 }
41 
TEST(L2capSignalIdTest,pre_increment)42 TEST(L2capSignalIdTest, pre_increment) {
43   SignalId signal_id(0);
44   ASSERT_FALSE(signal_id.IsValid());
45 
46   for (uint8_t i = 0; i != 0xff; ++i, ++signal_id) {
47     ASSERT_TRUE(i == signal_id.Value());
48   }
49 }
50 
TEST(L2capSignalIdTest,post_increment)51 TEST(L2capSignalIdTest, post_increment) {
52   SignalId signal_id(0);
53   ASSERT_FALSE(signal_id.IsValid());
54 
55   for (uint8_t i = 0; i != 0xff; i++, signal_id++) {
56     ASSERT_TRUE(i == signal_id.Value());
57   }
58 }
59 
TEST(L2capSignalIdTest,almost_wrap_up)60 TEST(L2capSignalIdTest, almost_wrap_up) {
61   SignalId signal_id(0);
62   ASSERT_FALSE(signal_id.IsValid());
63 
64   for (int i = 0; i < 255; i++) {
65     signal_id++;
66   }
67   ASSERT_EQ(0xff, signal_id.Value());
68 }
69 
TEST(L2capSignalIdTest,wrap_up)70 TEST(L2capSignalIdTest, wrap_up) {
71   SignalId signal_id(0);
72   ASSERT_FALSE(signal_id.IsValid());
73 
74   for (int i = 0; i < 256; i++) {
75     signal_id++;
76   }
77   ASSERT_EQ(1, signal_id.Value());
78 }
79 
TEST(L2capSignalIdTest,pre_decrement)80 TEST(L2capSignalIdTest, pre_decrement) {
81   SignalId signal_id(0);
82   ASSERT_FALSE(signal_id.IsValid());
83 
84   for (uint8_t i = 0; i != 0xff; --i, --signal_id) {
85     ASSERT_TRUE(i == signal_id.Value());
86   }
87 }
88 
TEST(L2capSignalIdTest,post_decrement)89 TEST(L2capSignalIdTest, post_decrement) {
90   SignalId signal_id(0);
91   ASSERT_FALSE(signal_id.IsValid());
92 
93   for (uint8_t i = 0; i != 0xff; i--, signal_id--) {
94     ASSERT_TRUE(i == signal_id.Value());
95   }
96 }
97 
TEST(L2capSignalIdTest,almost_wrap_down)98 TEST(L2capSignalIdTest, almost_wrap_down) {
99   SignalId signal_id(0);
100   ASSERT_FALSE(signal_id.IsValid());
101 
102   for (int i = 0; i < 255; i++) {
103     signal_id--;
104   }
105   ASSERT_EQ(1, signal_id.Value());
106 }
107 
TEST(L2capSignalIdTest,wrap_down)108 TEST(L2capSignalIdTest, wrap_down) {
109   SignalId signal_id(0);
110   ASSERT_FALSE(signal_id.IsValid());
111 
112   for (int i = 0; i < 256; i++) {
113     signal_id--;
114   }
115   ASSERT_EQ(0xff, signal_id.Value());
116 }
117 
118 }  // namespace l2cap
119 }  // namespace bluetooth
120