cfgTest.cpp 4.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * @file cfgTest.cpp
 * @author slguan (slguan@taosdata.com)
 * @brief config module tests
 * @version 1.0
 * @date 2022-02-20
 *
 * @copyright Copyright (c) 2022
 *
 */

#include <gtest/gtest.h>
S
Shengliang Guan 已提交
13
#include "tconfig.h"
S
Shengliang Guan 已提交
14 15 16 17 18 19 20 21 22

class CfgTest : public ::testing::Test {
 protected:
  static void SetUpTestSuite() {}
  static void TearDownTestSuite() {}

 public:
  void SetUp() override {}
  void TearDown() override {}
S
Shengliang Guan 已提交
23
  void InitCfg(SConfig *pConfig);
S
cfgtest  
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25 26 27
  static const char *pConfig;
};

S
Shengliang Guan 已提交
28
const char *CfgTest::pConfig;
S
Shengliang Guan 已提交
29

S
Shengliang Guan 已提交
30
TEST_F(CfgTest, 01_Str) {
S
config  
Shengliang Guan 已提交
31 32 33 34
  EXPECT_STREQ(cfgStypeStr(CFG_STYPE_DEFAULT), "default");
  EXPECT_STREQ(cfgStypeStr(CFG_STYPE_CFG_FILE), "cfg_file");
  EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ENV_FILE), "env_file");
  EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ENV_VAR), "env_var");
wafwerar's avatar
wafwerar 已提交
35
  EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ENV_CMD), "env_cmd");
S
config  
Shengliang Guan 已提交
36
  EXPECT_STREQ(cfgStypeStr(CFG_STYPE_APOLLO_URL), "apollo_url");
S
config  
Shengliang Guan 已提交
37
  EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ARG_LIST), "arg_list");
S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 46
  EXPECT_STREQ(cfgStypeStr(ECfgSrcType(1024)), "invalid");

  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_NONE), "none");
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_BOOL), "bool");
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT32), "int32");
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT64), "int64");
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_FLOAT), "float");
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_STRING), "string");
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DIR), "dir");
S
Shengliang Guan 已提交
47 48 49
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DIR), "dir");
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DIR), "dir");
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DIR), "dir");
S
Shengliang Guan 已提交
50 51 52 53
  EXPECT_STREQ(cfgDtypeStr(ECfgDataType(1024)), "invalid");
}

TEST_F(CfgTest, 02_Basic) {
S
cfgtest  
Shengliang Guan 已提交
54
  SConfig *pConfig = cfgInit();
S
Shengliang Guan 已提交
55 56
  ASSERT_NE(pConfig, nullptr);

S
Shengliang Guan 已提交
57 58 59 60 61 62
  EXPECT_EQ(cfgAddBool(pConfig, "test_bool", 0, 0), 0);
  EXPECT_EQ(cfgAddInt32(pConfig, "test_int32", 1, 0, 16, 0), 0);
  EXPECT_EQ(cfgAddInt64(pConfig, "test_int64", 2, 0, 16, 0), 0);
  EXPECT_EQ(cfgAddFloat(pConfig, "test_float", 3, 0, 16, 0), 0);
  EXPECT_EQ(cfgAddString(pConfig, "test_string", "4", 0), 0);
  EXPECT_EQ(cfgAddDir(pConfig, "test_dir", "/tmp", 0), 0);
S
cfgtest  
Shengliang Guan 已提交
63

S
Shengliang Guan 已提交
64
  EXPECT_EQ(cfgGetSize(pConfig), 6);
S
cfgtest  
Shengliang Guan 已提交
65

S
Shengliang Guan 已提交
66 67 68
  int32_t size = taosArrayGetSize(pConfig->array);
  for (int32_t i = 0; i < size; ++i) {
    SConfigItem *pItem = (SConfigItem *)taosArrayGet(pConfig->array, i);
S
cfgtest  
Shengliang Guan 已提交
69 70
    switch (pItem->dtype) {
      case CFG_DTYPE_BOOL:
S
Shengliang Guan 已提交
71
        printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->bval);
S
cfgtest  
Shengliang Guan 已提交
72
        break;
S
Shengliang Guan 已提交
73
      case CFG_DTYPE_INT32:
S
Shengliang Guan 已提交
74
        printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->i32);
S
cfgtest  
Shengliang Guan 已提交
75
        break;
S
Shengliang Guan 已提交
76
      case CFG_DTYPE_INT64:
S
Shengliang Guan 已提交
77
        printf("index:%d, cfg:%s value:%" PRId64 "\n", size, pItem->name, pItem->i64);
S
cfgtest  
Shengliang Guan 已提交
78
        break;
S
Shengliang Guan 已提交
79
      case CFG_DTYPE_FLOAT:
S
Shengliang Guan 已提交
80
        printf("index:%d, cfg:%s value:%f\n", size, pItem->name, pItem->fval);
S
cfgtest  
Shengliang Guan 已提交
81
        break;
S
Shengliang Guan 已提交
82
      case CFG_DTYPE_STRING:
S
Shengliang Guan 已提交
83
        printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->str);
S
cfgtest  
Shengliang Guan 已提交
84
        break;
S
Shengliang Guan 已提交
85
      case CFG_DTYPE_DIR:
S
Shengliang Guan 已提交
86
        printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->str);
S
cfgtest  
Shengliang Guan 已提交
87 88
        break;
      default:
S
Shengliang Guan 已提交
89
        printf("index:%d, cfg:%s invalid cfg dtype:%d\n", size, pItem->name, pItem->dtype);
S
cfgtest  
Shengliang Guan 已提交
90 91 92
        break;
    }
  }
S
Shengliang Guan 已提交
93
  EXPECT_EQ(cfgGetSize(pConfig), 6);
S
cfgtest  
Shengliang Guan 已提交
94

S
Shengliang Guan 已提交
95
  SConfigItem *pItem = cfgGetItem(pConfig, "test_bool");
S
config  
Shengliang Guan 已提交
96
  EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
S
cfgtest  
Shengliang Guan 已提交
97 98
  EXPECT_EQ(pItem->dtype, CFG_DTYPE_BOOL);
  EXPECT_STREQ(pItem->name, "test_bool");
S
Shengliang Guan 已提交
99
  EXPECT_EQ(pItem->bval, 0);
S
cfgtest  
Shengliang Guan 已提交
100 101

  pItem = cfgGetItem(pConfig, "test_int32");
S
config  
Shengliang Guan 已提交
102
  EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
S
cfgtest  
Shengliang Guan 已提交
103 104
  EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT32);
  EXPECT_STREQ(pItem->name, "test_int32");
S
Shengliang Guan 已提交
105
  EXPECT_EQ(pItem->i32, 1);
S
cfgtest  
Shengliang Guan 已提交
106 107

  pItem = cfgGetItem(pConfig, "test_int64");
S
config  
Shengliang Guan 已提交
108
  EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
S
Shengliang Guan 已提交
109
  EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT64);
S
cfgtest  
Shengliang Guan 已提交
110
  EXPECT_STREQ(pItem->name, "test_int64");
S
Shengliang Guan 已提交
111
  EXPECT_EQ(pItem->i64, 2);
S
cfgtest  
Shengliang Guan 已提交
112 113

  pItem = cfgGetItem(pConfig, "test_float");
S
config  
Shengliang Guan 已提交
114
  EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
S
cfgtest  
Shengliang Guan 已提交
115 116
  EXPECT_EQ(pItem->dtype, CFG_DTYPE_FLOAT);
  EXPECT_STREQ(pItem->name, "test_float");
S
Shengliang Guan 已提交
117
  EXPECT_EQ(pItem->fval, 3);
S
cfgtest  
Shengliang Guan 已提交
118 119

  pItem = cfgGetItem(pConfig, "test_string");
S
config  
Shengliang Guan 已提交
120
  EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
S
cfgtest  
Shengliang Guan 已提交
121 122
  EXPECT_EQ(pItem->dtype, CFG_DTYPE_STRING);
  EXPECT_STREQ(pItem->name, "test_string");
S
Shengliang Guan 已提交
123
  EXPECT_STREQ(pItem->str, "4");
S
cfgtest  
Shengliang Guan 已提交
124 125

  pItem = cfgGetItem(pConfig, "test_dir");
S
config  
Shengliang Guan 已提交
126
  EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
S
cfgtest  
Shengliang Guan 已提交
127 128
  EXPECT_EQ(pItem->dtype, CFG_DTYPE_DIR);
  EXPECT_STREQ(pItem->name, "test_dir");
S
Shengliang Guan 已提交
129
  EXPECT_STREQ(pItem->str, "/tmp");
S
cfgtest  
Shengliang Guan 已提交
130 131 132

  cfgCleanup(pConfig);
}