1 /*
2 * Copyright (C) 2016 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
19 #include "chre/util/blocking_segmented_queue.h"
20 #include "chre/util/fixed_size_blocking_queue.h"
21 #include "chre/util/unique_ptr.h"
22
23 using chre::BlockingSegmentedQueue;
24 using chre::FixedSizeBlockingQueue;
25 using chre::MakeUnique;
26 using chre::UniquePtr;
27
28 namespace {
29 class ConstructorCount {
30 public:
ConstructorCount(int value_,ssize_t * constructedCount)31 ConstructorCount(int value_, ssize_t *constructedCount)
32 : sConstructedCounter(constructedCount), value(value_) {
33 (*sConstructedCounter)++;
34 }
~ConstructorCount()35 ~ConstructorCount() {
36 (*sConstructedCounter)--;
37 }
getValue()38 int getValue() {
39 return value;
40 }
41
42 ssize_t *sConstructedCounter;
43
44 private:
45 int value;
46 };
47
48 } // namespace
49
TEST(BlockingQueue,IsEmptyByDefault)50 TEST(BlockingQueue, IsEmptyByDefault) {
51 FixedSizeBlockingQueue<int, 16> blockingQueue;
52 ASSERT_TRUE(blockingQueue.empty());
53 }
54
TEST(BlockingQueue,PushPopVerifyOrder)55 TEST(BlockingQueue, PushPopVerifyOrder) {
56 FixedSizeBlockingQueue<int, 16> blockingQueue;
57
58 ASSERT_TRUE(blockingQueue.push(0x1337));
59 ASSERT_TRUE(blockingQueue.push(0xcafe));
60
61 ASSERT_EQ(blockingQueue.pop(), 0x1337);
62 ASSERT_EQ(blockingQueue.pop(), 0xcafe);
63 }
64
TEST(BlockingQueue,PushPopMove)65 TEST(BlockingQueue, PushPopMove) {
66 static constexpr int kVal = 0xbeef;
67 UniquePtr<int> ptr = MakeUnique<int>();
68 *ptr = kVal;
69
70 FixedSizeBlockingQueue<UniquePtr<int>, 16> blockingQueue;
71
72 ASSERT_TRUE(blockingQueue.push(std::move(ptr)));
73 ASSERT_TRUE(ptr.isNull());
74 ASSERT_EQ(*(blockingQueue.pop()), kVal);
75 }
76
TEST(BlockingSegmentedQueue,InitState)77 TEST(BlockingSegmentedQueue, InitState) {
78 constexpr uint8_t blockSize = 16;
79 constexpr uint8_t maxBlockCount = 3;
80 constexpr uint8_t staticBlockCount = 2;
81 BlockingSegmentedQueue<int, blockSize> blockingQueue(maxBlockCount,
82 staticBlockCount);
83 ASSERT_TRUE(blockingQueue.empty());
84 ASSERT_EQ(blockingQueue.block_count(), staticBlockCount);
85 }
86