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
31 typedef struct mycounter_t {
32 const char *name;
33 uint64_t val;
34 bool found;
35 } mycounter_t;
36
counter_iter(const char * name,counter_data_t val,void * context)37 static bool counter_iter(const char *name, counter_data_t val, void *context) {
38 mycounter_t *mycounter = (mycounter_t *)context;
39 if (!strcmp(name, mycounter->name)) {
40 mycounter->val = val;
41 mycounter->found = true;
42 return false;
43 }
44 return true;
45 }
46
find_val(const char * name,uint64_t * val)47 static bool find_val(const char *name, uint64_t *val) {
48 mycounter_t mycounter;
49
50 mycounter.val = 0;
51 mycounter.name = name;
52 mycounter.found = false;
53 counter_foreach(counter_iter, &mycounter);
54 *val = mycounter.val;
55 if (mycounter.found)
56 return true;
57 return false;
58 }
59
60 class CounterTest : public AllocationTestHarness {
61 protected:
SetUp()62 virtual void SetUp() {
63 counter_module.init();
64 }
65
TearDown()66 virtual void TearDown() {
67 counter_module.clean_up();
68 }
69 };
70
TEST_F(CounterTest,counter_no_exist)71 TEST_F(CounterTest, counter_no_exist) {
72 uint64_t val;
73
74 EXPECT_FALSE(find_val("one.two.three", &val));
75 }
76
TEST_F(CounterTest,counter_inc_dec)77 TEST_F(CounterTest, counter_inc_dec) {
78 uint64_t val;
79
80 counter_add("one.two.three", 1);
81
82 EXPECT_TRUE(find_val("one.two.three", &val));
83 EXPECT_EQ((uint64_t)1, val);
84
85 counter_add("one.two.three", 1);
86 EXPECT_TRUE(find_val("one.two.three", &val));
87 EXPECT_EQ((uint64_t)2, val);
88
89 counter_add("one.two.three", -1);
90 EXPECT_TRUE(find_val("one.two.three", &val));
91 EXPECT_EQ((uint64_t)1, val);
92 }
93
TEST_F(CounterTest,counter_get_set)94 TEST_F(CounterTest, counter_get_set) {
95 uint64_t val;
96
97 counter_set("one.two.three", COUNTER_TEST_TEN);
98 EXPECT_TRUE(find_val("one.two.three", &val));
99 EXPECT_EQ(COUNTER_TEST_TEN, val);
100
101 EXPECT_FALSE(find_val("foo.bar", &val));
102 }
103