cfgTest.cpp 4.3 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 35
  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");
  EXPECT_STREQ(cfgStypeStr(CFG_STYPE_APOLLO_URL), "apollo_url");
S
config  
Shengliang Guan 已提交
36
  EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ARG_LIST), "arg_list");
S
Shengliang Guan 已提交
37 38 39 40 41 42 43 44 45
  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 已提交
46 47 48
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DIR), "dir");
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DIR), "dir");
  EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DIR), "dir");
S
Shengliang Guan 已提交
49 50 51 52
  EXPECT_STREQ(cfgDtypeStr(ECfgDataType(1024)), "invalid");
}

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

S
Shengliang Guan 已提交
56 57 58 59 60 61
  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 已提交
62

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

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

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

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

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

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

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

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

  cfgCleanup(pConfig);
}