1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <gtest/gtest.h>
16 
17 #include "circle_stream_buffer.h"
18 #include "stream_buffer.h"
19 
20 namespace OHOS {
21 namespace MMI {
22 namespace {
23 using namespace testing::ext;
24 } // namespace
25 class CircleStreamBufferTest : public testing::Test {
26 public:
SetUpTestCase(void)27     static void SetUpTestCase(void) {}
TearDownTestCase(void)28     static void TearDownTestCase(void) {}
29 };
30 
31 /**
32  * @tc.name:CircleStreamBufferTest_CopyDataToBegin_001
33  * @tc.desc:Test the funcation CopyDataToBegin
34  * @tc.type: FUNC
35  * @tc.require:
36  */
37 HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_CopyDataToBegin_001, TestSize.Level1)
38 {
39     CircleStreamBuffer circleStreamBuffer;
40     StreamBuffer streamBuffer;
41     streamBuffer.wPos_ = 10;
42     streamBuffer.rPos_ = 5;
43     ASSERT_NO_FATAL_FAILURE(circleStreamBuffer.CopyDataToBegin());
44     streamBuffer.rPos_ = -5;
45     ASSERT_NO_FATAL_FAILURE(circleStreamBuffer.CopyDataToBegin());
46     streamBuffer.wPos_ = 5;
47     streamBuffer.rPos_ = 10;
48     ASSERT_NO_FATAL_FAILURE(circleStreamBuffer.CopyDataToBegin());
49 }
50 
51 /**
52  * @tc.name:CircleStreamBufferTest_CheckWrite_001
53  * @tc.desc:Test the funcation CheckWrite
54  * @tc.type: FUNC
55  * @tc.require:
56  */
57 HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_CheckWrite_001, TestSize.Level1)
58 {
59     CircleStreamBuffer circleStreamBuffer;
60     StreamBuffer streamBuffer;
61     size_t size = 4;
62     streamBuffer.wPos_ = 20000;
63     streamBuffer.rPos_ = 1;
64     bool ret = circleStreamBuffer.CheckWrite(size);
65     ASSERT_TRUE(ret);
66     streamBuffer.rPos_ = -10;
67     ret = circleStreamBuffer.CheckWrite(size);
68     ASSERT_TRUE(ret);
69     streamBuffer.wPos_ = 100;
70     ret = circleStreamBuffer.CheckWrite(size);
71     ASSERT_TRUE(ret);
72 }
73 
74 /**
75  * @tc.name:CircleStreamBufferTest_Write_001
76  * @tc.desc:Test the funcation Write
77  * @tc.type: FUNC
78  * @tc.require:
79  */
80 HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_Write_001, TestSize.Level1)
81 {
82     CircleStreamBuffer circleStreamBuffer;
83     StreamBuffer streamBuffer;
84     const char *buf = "1234#";
85     size_t size = 6;
86     streamBuffer.wPos_ = 30000;
87     streamBuffer.rPos_ = 8;
88     bool ret = circleStreamBuffer.Write(buf, size);
89     ASSERT_TRUE(ret);
90     streamBuffer.rPos_ = -11;
91     ret = circleStreamBuffer.Write(buf, size);
92     ASSERT_TRUE(ret);
93     streamBuffer.wPos_ = 200;
94     ret = circleStreamBuffer.Write(buf, size);
95     ASSERT_TRUE(ret);
96 }
97 } // namespace MMI
98 } // namespace OHOS
99