1 /*
2  * Copyright 2023 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 #define LOG_TAG "test"
18 
19 #include "truncating_buffer.h"
20 
21 #include <gtest/gtest.h>
22 
23 #include <format>
24 
25 using namespace bluetooth::log_internal;
26 
27 TEST(TruncatingBufferTest, 1byte) {
28   EXPECT_EQ(sizeof("ab"), 3);
29   truncating_buffer<2> buffer_1;
30   truncating_buffer<3> buffer_2;
31   std::format_to(std::back_insert_iterator(buffer_1), "ab");
32   std::format_to(std::back_insert_iterator(buffer_2), "ab");
33   EXPECT_STREQ(buffer_1.c_str(), "a");
34   EXPECT_STREQ(buffer_2.c_str(), "ab");
35 }
36 
37 TEST(TruncatingBufferTest, 2bytes) {
38   EXPECT_EQ(sizeof("αβ"), 5);
39   truncating_buffer<3> buffer_1;
40   truncating_buffer<4> buffer_2;
41   truncating_buffer<5> buffer_3;
42   std::format_to(std::back_insert_iterator(buffer_1), "αβ");
43   std::format_to(std::back_insert_iterator(buffer_2), "αβ");
44   std::format_to(std::back_insert_iterator(buffer_3), "αβ");
45   EXPECT_STREQ(buffer_1.c_str(), "α");
46   EXPECT_STREQ(buffer_2.c_str(), "α");
47   EXPECT_STREQ(buffer_3.c_str(), "αβ");
48 }
49 
50 TEST(TruncatingBufferTest, 3bytes) {
51   EXPECT_EQ(sizeof("ພຮ"), 7);
52   truncating_buffer<4> buffer_1;
53   truncating_buffer<5> buffer_2;
54   truncating_buffer<6> buffer_3;
55   truncating_buffer<7> buffer_4;
56   std::format_to(std::back_insert_iterator(buffer_1), "ພຮ");
57   std::format_to(std::back_insert_iterator(buffer_2), "ພຮ");
58   std::format_to(std::back_insert_iterator(buffer_3), "ພຮ");
59   std::format_to(std::back_insert_iterator(buffer_4), "ພຮ");
60   EXPECT_STREQ(buffer_1.c_str(), "ພ");
61   EXPECT_STREQ(buffer_2.c_str(), "ພ");
62   EXPECT_STREQ(buffer_3.c_str(), "ພ");
63   EXPECT_STREQ(buffer_4.c_str(), "ພຮ");
64 }
65 
66 TEST(TruncatingBufferTest, 4bytes) {
67   EXPECT_EQ(sizeof("����"), 9);
68   truncating_buffer<5> buffer_1;
69   truncating_buffer<6> buffer_2;
70   truncating_buffer<7> buffer_3;
71   truncating_buffer<8> buffer_4;
72   truncating_buffer<9> buffer_5;
73   std::format_to(std::back_insert_iterator(buffer_1), "����");
74   std::format_to(std::back_insert_iterator(buffer_2), "����");
75   std::format_to(std::back_insert_iterator(buffer_3), "����");
76   std::format_to(std::back_insert_iterator(buffer_4), "����");
77   std::format_to(std::back_insert_iterator(buffer_5), "����");
78   EXPECT_STREQ(buffer_1.c_str(), "��");
79   EXPECT_STREQ(buffer_2.c_str(), "��");
80   EXPECT_STREQ(buffer_3.c_str(), "��");
81   EXPECT_STREQ(buffer_4.c_str(), "��");
82   EXPECT_STREQ(buffer_5.c_str(), "����");
83 }
84