Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / test / core / gprpp / global_config_test.cc
1 /*
2  *
3  * Copyright 2019 gRPC authors.
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 "src/core/lib/gprpp/global_config.h"
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <gtest/gtest.h>
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/lib/gpr/env.h"
30 #include "src/core/lib/gprpp/memory.h"
31
32 GPR_GLOBAL_CONFIG_DECLARE_BOOL(bool_var);
33
34 GPR_GLOBAL_CONFIG_DEFINE_BOOL(bool_var, false, "");
35 GPR_GLOBAL_CONFIG_DEFINE_INT32(int32_var, 0, "");
36 GPR_GLOBAL_CONFIG_DEFINE_STRING(string_var, "", "");
37
38 TEST(GlobalConfigTest, BoolTest) {
39   EXPECT_FALSE(GPR_GLOBAL_CONFIG_GET(bool_var));
40   GPR_GLOBAL_CONFIG_SET(bool_var, true);
41   EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var));
42 }
43
44 TEST(GlobalConfigTest, Int32Test) {
45   EXPECT_EQ(0, GPR_GLOBAL_CONFIG_GET(int32_var));
46   GPR_GLOBAL_CONFIG_SET(int32_var, 1024);
47   EXPECT_EQ(1024, GPR_GLOBAL_CONFIG_GET(int32_var));
48 }
49
50 TEST(GlobalConfigTest, StringTest) {
51   grpc_core::UniquePtr<char> value;
52
53   value = GPR_GLOBAL_CONFIG_GET(string_var);
54   EXPECT_EQ(0, strcmp(value.get(), ""));
55
56   GPR_GLOBAL_CONFIG_SET(string_var, "Test");
57
58   value = GPR_GLOBAL_CONFIG_GET(string_var);
59   EXPECT_EQ(0, strcmp(value.get(), "Test"));
60 }
61
62 int main(int argc, char** argv) {
63   ::testing::InitGoogleTest(&argc, argv);
64   int ret = RUN_ALL_TESTS();
65   return ret;
66 }