1 /******************************************************************************
3 * Copyright (C) 2014 Google, Inc.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 #include <gtest/gtest.h>
20 #include "osi/test/AllocationTestHarness.h"
23 #include "btcore/include/counter.h"
24 #include "btcore/include/module.h"
26 extern module_t counter_module;
29 static const uint64_t COUNTER_TEST_TEN = 10;
30 static const uint64_t COUNTER_TEST_ZERO = 0;
32 typedef struct mycounter_t {
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)) {
42 mycounter->found = true;
48 static bool find_val(const char *name, uint64_t *val) {
49 mycounter_t mycounter;
52 mycounter.name = name;
53 mycounter.found = false;
54 counter_foreach(counter_iter, &mycounter);
61 class CounterTest : public AllocationTestHarness {
63 virtual void SetUp() {
64 counter_module.init();
67 virtual void TearDown() {
68 counter_module.clean_up();
72 TEST_F(CounterTest, counter_no_exist) {
75 EXPECT_FALSE(find_val("one.two.three", &val));
78 TEST_F(CounterTest, counter_inc_dec) {
81 counter_add("one.two.three", 1);
83 EXPECT_TRUE(find_val("one.two.three", &val));
84 EXPECT_EQ((uint64_t)1, val);
86 counter_add("one.two.three", 1);
87 EXPECT_TRUE(find_val("one.two.three", &val));
88 EXPECT_EQ((uint64_t)2, val);
90 counter_add("one.two.three", -1);
91 EXPECT_TRUE(find_val("one.two.three", &val));
92 EXPECT_EQ((uint64_t)1, val);
95 TEST_F(CounterTest, counter_get_set) {
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);
102 EXPECT_FALSE(find_val("foo.bar", &val));