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