OSDN Git Service

Merge commit 'f7f839985b3931682363d2ef3b7c5cae55a842ee' into merge
[android-x86/system-bt.git] / btcore / test / counter_test.cpp
1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 #include <gtest/gtest.h>
20 #include "osi/test/AllocationTestHarness.h"
21
22 extern "C" {
23 #include "btcore/include/counter.h"
24 #include "btcore/include/module.h"
25
26 extern module_t counter_module;
27 }  // "C"
28
29 static const uint64_t COUNTER_TEST_TEN = 10;
30 static const uint64_t COUNTER_TEST_ZERO = 0;
31
32 typedef struct mycounter_t {
33   const char *name;
34   uint64_t val;
35   bool found;
36 } mycounter_t;
37
38 static bool counter_iter(const char *name, counter_data_t val, void *context) {
39   mycounter_t *mycounter = (mycounter_t *)context;
40   if (!strcmp(name, mycounter->name)) {
41     mycounter->val = val;
42     mycounter->found = true;
43     return false;
44   }
45   return true;
46 }
47
48 static bool find_val(const char *name, uint64_t *val) {
49   mycounter_t mycounter;
50
51   mycounter.val = 0;
52   mycounter.name = name;
53   mycounter.found = false;
54   counter_foreach(counter_iter, &mycounter);
55   *val = mycounter.val;
56   if (mycounter.found)
57     return true;
58   return false;
59 }
60
61 class CounterTest : public AllocationTestHarness {
62   protected:
63     virtual void SetUp() {
64       counter_module.init();
65     }
66
67     virtual void TearDown() {
68       counter_module.clean_up();
69     }
70 };
71
72 TEST_F(CounterTest, counter_no_exist) {
73   uint64_t val;
74
75   EXPECT_FALSE(find_val("one.two.three", &val));
76 }
77
78 TEST_F(CounterTest, counter_inc_dec) {
79   uint64_t val;
80
81   counter_add("one.two.three", 1);
82
83   EXPECT_TRUE(find_val("one.two.three", &val));
84   EXPECT_EQ((uint64_t)1, val);
85
86   counter_add("one.two.three", 1);
87   EXPECT_TRUE(find_val("one.two.three", &val));
88   EXPECT_EQ((uint64_t)2, val);
89
90   counter_add("one.two.three", -1);
91   EXPECT_TRUE(find_val("one.two.three", &val));
92   EXPECT_EQ((uint64_t)1, val);
93 }
94
95 TEST_F(CounterTest, counter_get_set) {
96   uint64_t val;
97
98   counter_set("one.two.three", COUNTER_TEST_TEN);
99   EXPECT_TRUE(find_val("one.two.three", &val));
100   EXPECT_EQ(COUNTER_TEST_TEN, val);
101
102   EXPECT_FALSE(find_val("foo.bar", &val));
103 }