1 /*
2 * Copyright (c) 2023 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
16 #include "audio_errors.h"
17 #include "audio_service_log.h"
18 #include "audio_info.h"
19 #include "audio_ring_cache.h"
20 #include "audio_process_config.h"
21 #include "linear_pos_time_model.h"
22 #include "oh_audio_buffer.h"
23 #include <gtest/gtest.h>
24
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace AudioStandard {
28 std::unique_ptr<LinearPosTimeModel> linearPosTimeModel;
29 std::unique_ptr<AudioSharedMemory> audioSharedMemory;
30 std::shared_ptr<OHAudioBuffer> oHAudioBuffer;
31 const int32_t TEST_NUM = 1000;
32 const int32_t TEST_RET_NUM = 0;
33 const int64_t NANO_COUNT_PER_SECOND = 1000000000;
34 class AudioServiceCommonUnitTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase(void)42 void AudioServiceCommonUnitTest::SetUpTestCase(void)
43 {
44 // input testsuit setup step,setup invoked before all testcases
45 }
46
TearDownTestCase(void)47 void AudioServiceCommonUnitTest::TearDownTestCase(void)
48 {
49 // input testsuit teardown step,teardown invoked after all testcases
50 }
51
SetUp(void)52 void AudioServiceCommonUnitTest::SetUp(void)
53 {
54 // input testcase setup step,setup invoked before each testcases
55 }
56
TearDown(void)57 void AudioServiceCommonUnitTest::TearDown(void)
58 {
59 // input testcase teardown step,teardown invoked after each testcases
60 }
61
62 /**
63 * @tc.name : Test ProcessConfig API
64 * @tc.type : FUNC
65 * @tc.number: ProcessConfigTest_001
66 * @tc.desc : Test ProcessConfig test.
67 */
68 HWTEST(AudioServiceCommonUnitTest, ProcessConfigTest_001, TestSize.Level1)
69 {
70 AudioPlaybackCaptureConfig config = {{{STREAM_USAGE_MUSIC}, FilterMode::INCLUDE, {0}, FilterMode::INCLUDE}, false};
71 std::string dumpStr = ProcessConfig::DumpInnerCapConfig(config);
72 EXPECT_NE(dumpStr, "");
73 }
74
75 /**
76 * @tc.name : Test LinearPosTimeModel API
77 * @tc.type : FUNC
78 * @tc.number: LinearPosTimeModel_001
79 * @tc.desc : Test LinearPosTimeModel interface.
80 */
81 HWTEST(AudioServiceCommonUnitTest, LinearPosTimeModel_001, TestSize.Level1)
82 {
83 linearPosTimeModel = std::make_unique<LinearPosTimeModel>();
84
85 uint64_t posInFrame = 20;
86 int64_t invalidTime = -1;
87 int64_t retPos = linearPosTimeModel->GetTimeOfPos(posInFrame);
88 EXPECT_EQ(invalidTime, retPos);
89
90 int32_t sampleRate = -1;
91 bool isConfig = linearPosTimeModel->ConfigSampleRate(sampleRate);
92 EXPECT_EQ(false, isConfig);
93
94 sampleRate = (int32_t)AudioSamplingRate::SAMPLE_RATE_44100;
95 isConfig = linearPosTimeModel->ConfigSampleRate(sampleRate);
96 EXPECT_EQ(true, isConfig);
97
98 isConfig = linearPosTimeModel->ConfigSampleRate(sampleRate);
99 EXPECT_EQ(false, isConfig);
100 }
101
102 /**
103 * @tc.name : Test LinearPosTimeModel API
104 * @tc.type : FUNC
105 * @tc.number: LinearPosTimeModel_002
106 * @tc.desc : Test LinearPosTimeModel interface.
107 */
108 HWTEST(AudioServiceCommonUnitTest, LinearPosTimeModel_002, TestSize.Level1)
109 {
110 int64_t deltaFrame = 0;
111 uint64_t frame = 0;
112 int64_t nanoTime = 0;
113 linearPosTimeModel->ResetFrameStamp(frame, nanoTime);
114
115 uint64_t spanCountInFrame = 2;
116 linearPosTimeModel->SetSpanCount(spanCountInFrame);
117
118 uint64_t posInFrame = 20;
119 int64_t retPos = linearPosTimeModel->GetTimeOfPos(posInFrame);
120
121 deltaFrame = posInFrame - frame;
122 int64_t retPosCal1 = nanoTime + deltaFrame * NANO_COUNT_PER_SECOND / (int64_t)AudioSamplingRate::SAMPLE_RATE_44100;
123 EXPECT_EQ(retPos, retPosCal1);
124
125 frame = 40;
126 nanoTime = 50;
127 linearPosTimeModel->UpdataFrameStamp(frame, nanoTime);
128
129 retPos = linearPosTimeModel->GetTimeOfPos(posInFrame);
130 deltaFrame = frame - posInFrame;
131 int64_t retPosCal2 = nanoTime + deltaFrame * NANO_COUNT_PER_SECOND / (int64_t)AudioSamplingRate::SAMPLE_RATE_44100;
132 EXPECT_NE(retPos, retPosCal2);
133 }
134
135 /**
136 * @tc.name : Test OHAudioBuffer API
137 * @tc.type : FUNC
138 * @tc.number: OHAudioBuffer_001
139 * @tc.desc : Test OHAudioBuffer interface.
140 */
141 HWTEST(AudioServiceCommonUnitTest, OHAudioBuffer_001, TestSize.Level1)
142 {
143 uint32_t spanSizeInFrame = 1000;
144 uint32_t totalSizeInFrame = spanSizeInFrame - 1;
145 uint32_t byteSizePerFrame = 1000;
146 oHAudioBuffer = OHAudioBuffer::CreateFromLocal(totalSizeInFrame, spanSizeInFrame, byteSizePerFrame);
147 EXPECT_EQ(nullptr, oHAudioBuffer);
148 }
149
150 /**
151 * @tc.name : Test OHAudioBuffer API
152 * @tc.type : FUNC
153 * @tc.number: OHAudioBuffer_002
154 * @tc.desc : Test OHAudioBuffer interface.
155 */
156 HWTEST(AudioServiceCommonUnitTest, OHAudioBuffer_002, TestSize.Level1)
157 {
158 uint32_t spanSizeInFrame = 1000;
159 uint32_t totalSizeInFrame = spanSizeInFrame;
160 uint32_t byteSizePerFrame = 1000;
161 oHAudioBuffer = OHAudioBuffer::CreateFromLocal(totalSizeInFrame, spanSizeInFrame, byteSizePerFrame);
162 EXPECT_NE(nullptr, oHAudioBuffer);
163
164 uint32_t totalSizeInFrameRet;
165 uint32_t spanSizeInFrameRet;
166 uint32_t byteSizePerFrameRet;
167
168 int32_t ret = oHAudioBuffer->GetSizeParameter(totalSizeInFrameRet, spanSizeInFrameRet, byteSizePerFrameRet);
169 EXPECT_EQ(spanSizeInFrame, spanSizeInFrameRet);
170 EXPECT_EQ(totalSizeInFrame, totalSizeInFrameRet);
171 EXPECT_EQ(byteSizePerFrame, byteSizePerFrameRet);
172 EXPECT_EQ(SUCCESS, ret);
173 }
174
175 /**
176 * @tc.name : Test OHAudioBuffer API
177 * @tc.type : FUNC
178 * @tc.number: OHAudioBuffer_003
179 * @tc.desc : Test OHAudioBuffer interface.
180 */
181 HWTEST(AudioServiceCommonUnitTest, OHAudioBuffer_003, TestSize.Level1)
182 {
183 uint64_t frames = 1000;
184 int64_t nanoTime = NANO_COUNT_PER_SECOND;
185 oHAudioBuffer->SetHandleInfo(frames, nanoTime);
186 bool ret = oHAudioBuffer->GetHandleInfo(frames, nanoTime);
187 EXPECT_EQ(true, ret);
188 }
189
190 /**
191 * @tc.name : Test OHAudioBuffer API
192 * @tc.type : FUNC
193 * @tc.number: OHAudioBuffer_004
194 * @tc.desc : Test OHAudioBuffer interface.
195 */
196 HWTEST(AudioServiceCommonUnitTest, OHAudioBuffer_004, TestSize.Level1)
197 {
198 int32_t ret = -1;
199 uint64_t writeFrame = 3000;
200 uint64_t readFrame = writeFrame - 1001;
201
202 ret = oHAudioBuffer->ResetCurReadWritePos(readFrame, writeFrame);
203 EXPECT_EQ(ERR_INVALID_PARAM, ret);
204
205 ret = oHAudioBuffer->GetAvailableDataFrames();
206 EXPECT_EQ(TEST_NUM, ret);
207
208 writeFrame = 1001;
209 readFrame = 1000;
210
211 ret = oHAudioBuffer->ResetCurReadWritePos(readFrame, writeFrame);
212 EXPECT_EQ(SUCCESS, ret);
213
214 ret = oHAudioBuffer->GetAvailableDataFrames();
215 EXPECT_EQ(TEST_NUM - 1, ret);
216
217 uint64_t writeFrameRet = oHAudioBuffer->GetCurWriteFrame();
218 uint64_t readFrameRet = oHAudioBuffer->GetCurReadFrame();
219 EXPECT_EQ(writeFrame, writeFrameRet);
220 EXPECT_EQ(readFrame, readFrameRet);
221
222 writeFrame = 5000;
223 ret = oHAudioBuffer->SetCurWriteFrame(writeFrame);
224 EXPECT_EQ(ERR_INVALID_PARAM, ret);
225
226 writeFrame = readFrame - 1;
227 ret = oHAudioBuffer->SetCurWriteFrame(writeFrame);
228 EXPECT_EQ(ERR_INVALID_PARAM, ret);
229
230 writeFrame = 1000;
231 ret = oHAudioBuffer->SetCurWriteFrame(writeFrame);
232 EXPECT_LT(ret, TEST_RET_NUM);
233
234 writeFrame = 3000 + 2;
235 ret = oHAudioBuffer->SetCurWriteFrame(writeFrame);
236 EXPECT_LT(ret, TEST_RET_NUM);
237 }
238
239 /**
240 * @tc.name : Test OHAudioBuffer API
241 * @tc.type : FUNC
242 * @tc.number: OHAudioBuffer_005
243 * @tc.desc : Test OHAudioBuffer interface.
244 */
245 HWTEST(AudioServiceCommonUnitTest, OHAudioBuffer_005, TestSize.Level1)
246 {
247 int32_t ret = -1;
248 uint64_t writeFrame = 5000;
249 ret = oHAudioBuffer->SetCurReadFrame(writeFrame);
250 EXPECT_EQ(ERR_INVALID_PARAM, ret);
251
252 uint64_t readFrame = 1000;
253 ret = oHAudioBuffer->SetCurReadFrame(readFrame);
254 EXPECT_EQ(SUCCESS, ret);
255
256 readFrame = 1000;
257 ret = oHAudioBuffer->SetCurReadFrame(readFrame);
258 EXPECT_EQ(SUCCESS, ret);
259
260 readFrame = 2000;
261 ret = oHAudioBuffer->SetCurReadFrame(readFrame);
262 EXPECT_LT(ret, TEST_RET_NUM);
263
264 readFrame = 3000 + 2;
265 ret = oHAudioBuffer->SetCurReadFrame(readFrame);
266 EXPECT_LT(ret, TEST_RET_NUM);
267 }
268
269 /**
270 * @tc.name : Test OHAudioBuffer API
271 * @tc.type : FUNC
272 * @tc.number: OHAudioBuffer_006
273 * @tc.desc : Test OHAudioBuffer interface.
274 */
275 HWTEST(AudioServiceCommonUnitTest, OHAudioBuffer_006, TestSize.Level1)
276 {
277 int32_t ret = -1;
278 BufferDesc bufferDesc;
279 uint64_t posInFrame = 1000;
280
281 ret = oHAudioBuffer->GetBufferByFrame(posInFrame, bufferDesc);
282 EXPECT_EQ(SUCCESS, ret);
283
284 posInFrame = 3000 + 1;
285 ret = oHAudioBuffer->GetBufferByFrame(posInFrame, bufferDesc);
286 EXPECT_LT(ret, TEST_RET_NUM);
287
288 uint64_t writePosInFrame = 1000;
289 ret = oHAudioBuffer->GetWriteBuffer(writePosInFrame, bufferDesc);
290 EXPECT_EQ(SUCCESS, ret);
291
292 writePosInFrame = 3000 +1;
293 ret = oHAudioBuffer->GetWriteBuffer(writePosInFrame, bufferDesc);
294 EXPECT_LT(ret, TEST_RET_NUM);
295
296 uint64_t readPosInFrame = 1000;
297 ret = oHAudioBuffer->GetReadbuffer(readPosInFrame, bufferDesc);
298 EXPECT_EQ(SUCCESS, ret);
299
300 readPosInFrame = 3000;
301 ret = oHAudioBuffer->GetReadbuffer(readPosInFrame, bufferDesc);
302 EXPECT_LT(ret, TEST_RET_NUM);
303 }
304
305 /**
306 * @tc.name : Test OHAudioBuffer API
307 * @tc.type : FUNC
308 * @tc.number: OHAudioBuffer_007
309 * @tc.desc : Test OHAudioBuffer interface.
310 */
311 HWTEST(AudioServiceCommonUnitTest, OHAudioBuffer_007, TestSize.Level1)
312 {
313 uint64_t posInFrame = 4000;
314 SpanInfo *spanInfo = oHAudioBuffer->GetSpanInfo(posInFrame);
315 EXPECT_EQ(NULL, spanInfo);
316
317 uint32_t spanIndex = 2;
318 SpanInfo *spanInfoFromIndex = oHAudioBuffer->GetSpanInfoByIndex(spanIndex);
319 EXPECT_EQ(NULL, spanInfoFromIndex);
320
321 uint32_t spanCount = oHAudioBuffer->GetSpanCount();
322 uint32_t spanCountExpect = 1;
323 EXPECT_EQ(spanCountExpect, spanCount);
324
325
326 size_t totalSize = oHAudioBuffer->GetDataSize();
327 EXPECT_EQ(totalSize > TEST_RET_NUM, true);
328
329 uint8_t * dataBase = oHAudioBuffer->GetDataBase();
330 EXPECT_NE(nullptr, dataBase);
331 }
332
333 /**
334 * @tc.name : Test OHAudioBuffer API
335 * @tc.type : FUNC
336 * @tc.number: OHAudioBuffer_008
337 * @tc.desc : Test OHAudioBuffer interface.
338 */
339 HWTEST(AudioServiceCommonUnitTest, OHAudioBuffer_008, TestSize.Level1)
340 {
341 MessageParcel parcel;
342
343 int32_t ret = oHAudioBuffer->WriteToParcel(oHAudioBuffer, parcel);
344 EXPECT_EQ(SUCCESS, ret);
345
346 oHAudioBuffer = oHAudioBuffer->ReadFromParcel(parcel);
347 EXPECT_NE(nullptr, oHAudioBuffer);
348 }
349
350 /**
351 * @tc.name : Test AudioRingCache API
352 * @tc.type : FUNC
353 * @tc.number: AudioRingCache_001
354 * @tc.desc : Test AudioRingCache interface.
355 */
356 HWTEST(AudioServiceCommonUnitTest, AudioRingCache_001, TestSize.Level1)
357 {
358 size_t testSize = 3840;
359 std::unique_ptr<AudioRingCache> ringCache = AudioRingCache::Create(testSize);
360 EXPECT_NE(nullptr, ringCache);
361
362 std::unique_ptr<uint8_t[]> writeBuffer = std::make_unique<uint8_t[]>(testSize);
363 std::unique_ptr<uint8_t[]> readBuffer = std::make_unique<uint8_t[]>(testSize);
364
365 BufferWrap writeWrap = {writeBuffer.get(), testSize};
366 BufferWrap readWrap = {readBuffer.get(), testSize};
367
368 int32_t tryCount = 200;
369 while (tryCount-- > 0) {
370 OptResult result1 = ringCache->Enqueue(writeWrap);
371 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
372
373 OptResult result2 = ringCache->Dequeue(readWrap);
374 EXPECT_EQ(result2.ret, OPERATION_SUCCESS);
375 }
376 }
377
378 /**
379 * @tc.name : Test AudioRingCache API
380 * @tc.type : FUNC
381 * @tc.number: AudioRingCache_002
382 * @tc.desc : Test AudioRingCache interface.
383 */
384 HWTEST(AudioServiceCommonUnitTest, AudioRingCache_002, TestSize.Level1)
385 {
386 size_t testSize = 3840;
387 std::unique_ptr<AudioRingCache> ringCache = AudioRingCache::Create(testSize);
388 EXPECT_NE(nullptr, ringCache);
389
390 size_t tempSize = 1920;
391 std::unique_ptr<uint8_t[]> writeBuffer = std::make_unique<uint8_t[]>(tempSize);
392 std::unique_ptr<uint8_t[]> readBuffer = std::make_unique<uint8_t[]>(tempSize);
393
394 BufferWrap writeWrap = {writeBuffer.get(), tempSize};
395 BufferWrap readWrap = {readBuffer.get(), tempSize};
396
397 int32_t tryCount = 200;
398 while (tryCount-- > 0) {
399 OptResult result1 = ringCache->GetWritableSize();
400 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
401 EXPECT_EQ(result1.size, testSize);
402
403 result1 = ringCache->Enqueue(writeWrap); // write 1920
404 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
405
406 result1 = ringCache->GetWritableSize();
407 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
408 EXPECT_EQ(result1.size, testSize - tempSize); // left 1920
409
410
411 OptResult result2 = ringCache->GetReadableSize();
412 EXPECT_EQ(result2.ret, OPERATION_SUCCESS);
413 EXPECT_EQ(result2.size, tempSize); // can read 1920
414
415 result2 = ringCache->Dequeue(readWrap);
416 EXPECT_EQ(result2.ret, OPERATION_SUCCESS);
417
418 result2 = ringCache->GetReadableSize();
419 EXPECT_EQ(result2.ret, OPERATION_SUCCESS);
420 EXPECT_EQ(result2.size, 0); // can read 0
421 }
422 }
423
424 /**
425 * @tc.name : Test AudioRingCache API
426 * @tc.type : FUNC
427 * @tc.number: AudioRingCache_003
428 * @tc.desc : Test AudioRingCache interface.
429 */
430 HWTEST(AudioServiceCommonUnitTest, AudioRingCache_003, TestSize.Level1)
431 {
432 size_t cacheSize = 960;
433 std::unique_ptr<AudioRingCache> ringCache = AudioRingCache::Create(cacheSize);
434
435 size_t tempSize = 19200;
436 std::unique_ptr<uint8_t[]> writeBuffer = std::make_unique<uint8_t[]>(tempSize);
437 std::unique_ptr<uint8_t[]> readBuffer = std::make_unique<uint8_t[]>(tempSize);
438
439 for (size_t index = 0; index < tempSize;index++) {
440 writeBuffer[index] = index;
441 }
442
443 int32_t totalCount = tempSize / cacheSize;
444 size_t offset = 0;
445 while (totalCount-- > 0) {
446 uint8_t *writePtr = writeBuffer.get() + offset;
447 BufferWrap spanWrap = {writePtr, cacheSize};
448 OptResult result1 = ringCache->Enqueue(spanWrap);
449 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
450
451 uint8_t *readPtr = readBuffer.get() + offset;
452 BufferWrap readWrap = {readPtr, cacheSize};
453 OptResult result2 = ringCache->Dequeue(readWrap);
454 EXPECT_EQ(result2.ret, OPERATION_SUCCESS);
455 offset += cacheSize;
456 }
457
458 for (size_t index = 0; index < tempSize;index++) {
459 EXPECT_EQ(writeBuffer[index], readBuffer[index]);
460 }
461 }
462
463 /**
464 * @tc.name : Test AudioRingCache API
465 * @tc.type : FUNC
466 * @tc.number: AudioRingCache_004
467 * @tc.desc : Test AudioRingCache interface.
468 */
469 HWTEST(AudioServiceCommonUnitTest, AudioRingCache_004, TestSize.Level1)
470 {
471 size_t cacheSize = 960;
472 std::unique_ptr<AudioRingCache> ringCache = AudioRingCache::Create(cacheSize);
473
474 size_t tempSize = 480;
475 std::unique_ptr<uint8_t[]> writeBuffer = std::make_unique<uint8_t[]>(tempSize);
476 std::unique_ptr<uint8_t[]> readBuffer = std::make_unique<uint8_t[]>(tempSize);
477
478 for (size_t index = 0; index < tempSize;index++) {
479 writeBuffer[index] = index;
480 }
481
482 BufferWrap writeWrap = {writeBuffer.get(), tempSize};
483 BufferWrap readWrap = {readBuffer.get(), tempSize};
484
485 OptResult result1 = ringCache->Enqueue(writeWrap);
486 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
487
488 result1 = ringCache->ReConfig(tempSize, true); // test copyRemained is true
489 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
490 EXPECT_EQ(result1.size, tempSize);
491
492 result1 = ringCache->Dequeue(readWrap);
493 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
494 EXPECT_EQ(result1.size, tempSize);
495
496 for (size_t index = 0; index < tempSize;index++) {
497 EXPECT_EQ(writeBuffer[index], readBuffer[index]);
498 }
499 }
500
501 /**
502 * @tc.name : Test AudioRingCache API
503 * @tc.type : FUNC
504 * @tc.number: AudioRingCache_005
505 * @tc.desc : Test AudioRingCache interface.
506 */
507 HWTEST(AudioServiceCommonUnitTest, AudioRingCache_005, TestSize.Level1)
508 {
509 size_t cacheSize = 960;
510 std::unique_ptr<AudioRingCache> ringCache = AudioRingCache::Create(cacheSize);
511
512 size_t tempSize = 480;
513 std::unique_ptr<uint8_t[]> writeBuffer = std::make_unique<uint8_t[]>(tempSize);
514 std::unique_ptr<uint8_t[]> readBuffer = std::make_unique<uint8_t[]>(tempSize);
515
516 for (size_t index = 0; index < tempSize;index++) {
517 writeBuffer[index] = index;
518 }
519
520 BufferWrap writeWrap = {writeBuffer.get(), tempSize};
521 BufferWrap readWrap = {readBuffer.get(), tempSize};
522
523 OptResult result1 = ringCache->Enqueue(writeWrap);
524 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
525
526 result1 = ringCache->Dequeue(readWrap);
527 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
528 EXPECT_EQ(result1.size, tempSize);
529
530 result1 = ringCache->Enqueue(writeWrap);
531 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
532
533 result1 = ringCache->ReConfig(tempSize, true); // test copyRemained is true
534 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
535 EXPECT_EQ(result1.size, tempSize);
536
537 result1 = ringCache->Dequeue(readWrap);
538 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
539 EXPECT_EQ(result1.size, tempSize);
540
541 for (size_t index = 0; index < tempSize;index++) {
542 EXPECT_EQ(writeBuffer[index], readBuffer[index]);
543 }
544 }
545
546 /**
547 * @tc.name : Test AudioRingCache API
548 * @tc.type : FUNC
549 * @tc.number: AudioRingCache_006
550 * @tc.desc : Test AudioRingCache interface.
551 */
552 HWTEST(AudioServiceCommonUnitTest, AudioRingCache_006, TestSize.Level1)
553 {
554 size_t cacheSize = 960;
555 std::unique_ptr<AudioRingCache> ringCache = AudioRingCache::Create(cacheSize);
556
557 size_t tempSize = 480;
558 std::unique_ptr<uint8_t[]> writeBuffer = std::make_unique<uint8_t[]>(tempSize);
559
560 for (size_t index = 0; index < tempSize;index++) {
561 writeBuffer[index] = index;
562 }
563
564 BufferWrap writeWrap = {writeBuffer.get(), tempSize};
565
566 OptResult result1 = ringCache->Enqueue(writeWrap);
567 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
568
569 result1 = ringCache->Dequeue(writeWrap);
570 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
571 EXPECT_EQ(result1.size, tempSize);
572
573 result1 = ringCache->Enqueue(writeWrap);
574 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
575
576 result1 = ringCache->ReConfig(tempSize, false); // test copyRemained is false
577 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
578 EXPECT_EQ(result1.size, tempSize);
579
580 result1 = ringCache->GetReadableSize();
581 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
582 EXPECT_EQ(result1.size, 0);
583 }
584
585 /**
586 * @tc.name : Test AudioRingCache API
587 * @tc.type : FUNC
588 * @tc.number: AudioRingCache_007
589 * @tc.desc : Test AudioRingCache interface.
590 */
591 HWTEST(AudioServiceCommonUnitTest, AudioRingCache_007, TestSize.Level1)
592 {
593 size_t cacheSize = 480;
594 std::unique_ptr<AudioRingCache> ringCache = AudioRingCache::Create(cacheSize);
595
596 size_t tempSize = 480;
597 std::unique_ptr<uint8_t[]> writeBuffer = std::make_unique<uint8_t[]>(tempSize);
598
599 for (size_t index = 0; index < tempSize;index++) {
600 writeBuffer[index] = index;
601 }
602
603 BufferWrap writeWrap = {writeBuffer.get(), tempSize};
604
605 OptResult result1 = ringCache->Enqueue(writeWrap);
606 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
607
608 size_t reSize = tempSize + tempSize;
609 result1 = ringCache->ReConfig(reSize, false); // test copyRemained is false
610 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
611 EXPECT_EQ(result1.size, reSize);
612
613 result1 = ringCache->GetReadableSize();
614 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
615 EXPECT_EQ(result1.size, 0);
616
617 result1 = ringCache->GetWritableSize();
618 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
619 EXPECT_EQ(result1.size, reSize);
620
621 result1 = ringCache->Enqueue(writeWrap);
622 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
623 EXPECT_EQ(result1.size, tempSize);
624
625 result1 = ringCache->GetWritableSize();
626 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
627 EXPECT_EQ(result1.size, tempSize);
628
629 result1 = ringCache->Enqueue(writeWrap);
630 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
631 EXPECT_EQ(result1.size, tempSize);
632
633 result1 = ringCache->GetWritableSize();
634 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
635 EXPECT_EQ(result1.size, 0);
636 }
637
638 /**
639 * @tc.name : Test AudioRingCache API
640 * @tc.type : FUNC
641 * @tc.number: AudioRingCache_008
642 * @tc.desc : Test cross ring cache.
643 */
644 HWTEST(AudioServiceCommonUnitTest, AudioRingCache_008, TestSize.Level1)
645 {
646 size_t cacheSize = 480;
647 std::unique_ptr<AudioRingCache> ringCache = AudioRingCache::Create(cacheSize);
648
649 size_t tempSize = 1920;
650 std::unique_ptr<uint8_t[]> writeBuffer = std::make_unique<uint8_t[]>(tempSize);
651 std::unique_ptr<uint8_t[]> readBuffer = std::make_unique<uint8_t[]>(tempSize);
652
653 for (size_t index = 0; index < tempSize;index++) {
654 writeBuffer[index] = index % UINT8_MAX;
655 }
656
657 size_t offset = 0;
658 size_t spanSize = 320; // 480 * 2 /3
659 int32_t totalCount = tempSize / spanSize;
660 while (totalCount-- > 0) {
661 uint8_t *writePtr = writeBuffer.get() + offset;
662 BufferWrap spanWrap = {writePtr, spanSize};
663 OptResult result1 = ringCache->Enqueue(spanWrap);
664 EXPECT_EQ(result1.ret, OPERATION_SUCCESS);
665
666 uint8_t *readPtr = readBuffer.get() + offset;
667 BufferWrap readWrap = {readPtr, spanSize};
668 OptResult result2 = ringCache->Dequeue(readWrap);
669 EXPECT_EQ(result2.ret, OPERATION_SUCCESS);
670 offset += spanSize;
671 }
672
673 for (size_t index = 0; index < tempSize;index++) {
674 EXPECT_EQ(writeBuffer[index], readBuffer[index]);
675 }
676 }
677 } // namespace AudioStandard
678 } // namespace OHOS