smlTest.cpp 19.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtest/gtest.h>

#include <taoserror.h>
#include <tglobal.h>
#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"

28 29 30
#include "../src/clientSmlLine.c"
#include "../src/clientSmlJson.c"
#include "../src/clientSmlTelnet.c"
31 32 33
#include "../src/clientSml.c"
#include "taos.h"

wmmhello's avatar
wmmhello 已提交
34
int main(int argc, char **argv) {
35 36 37 38
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

39
TEST(testCase, smlParseInfluxString_Test) {
wmmhello's avatar
wmmhello 已提交
40
  char       msg[256] = {0};
41 42 43 44 45
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;
  SSmlLineInfo elements = {0};

46 47 48
  SSmlHandle *info = smlBuildSmlInfo(NULL);
  info->protocol = TSDB_SML_LINE_PROTOCOL;
  info->dataFormat = false;
wmmhello's avatar
wmmhello 已提交
49
  // case 1
wmmhello's avatar
wmmhello 已提交
50
  char *tmp = "\\,st,t1=3,t2=4,t3=t3 c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64 1626006833639000000    ,32,c=3";
H
Hongze Cheng 已提交
51
  char *sql = (char *)taosMemoryCalloc(256, 1);
wmmhello's avatar
wmmhello 已提交
52
  memcpy(sql, tmp, strlen(tmp) + 1);
53
  int ret = smlParseInfluxString(info, sql, sql + strlen(sql), &elements);
54 55
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.measure, sql);
wmmhello's avatar
wmmhello 已提交
56 57
  ASSERT_EQ(elements.measureLen, strlen(",st"));
  ASSERT_EQ(elements.measureTagsLen, strlen(",st,t1=3,t2=4,t3=t3"));
58 59 60 61 62 63 64 65 66

  ASSERT_EQ(elements.tags, sql + elements.measureLen + 1);
  ASSERT_EQ(elements.tagsLen, strlen("t1=3,t2=4,t3=t3"));

  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 1);
  ASSERT_EQ(elements.colsLen, strlen("c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64"));

  ASSERT_EQ(elements.timestamp, sql + elements.measureTagsLen + 1 + elements.colsLen + 1);
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));
67 68
  taosArrayDestroy(elements.colArray);
  elements.colArray = NULL;
69 70

  // case 2  false
wmmhello's avatar
wmmhello 已提交
71 72
  tmp = "st,t1=3,t2=4,t3=t3 c1=3i64,c3=\"passit hello,c1=2,c2=false,c4=4f64 1626006833639000000";
  memcpy(sql, tmp, strlen(tmp) + 1);
73
  memset(&elements, 0, sizeof(SSmlLineInfo));
74
  ret = smlParseInfluxString(info, sql, sql + strlen(sql), &elements);
75
  ASSERT_NE(ret, 0);
76 77
  taosArrayDestroy(elements.colArray);
  elements.colArray = NULL;
78 79

  // case 4  tag is null
wmmhello's avatar
wmmhello 已提交
80 81
  tmp = "st, c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64 1626006833639000000";
  memcpy(sql, tmp, strlen(tmp) + 1);
82
  memset(&elements, 0, sizeof(SSmlLineInfo));
83
  ret = smlParseInfluxString(info, sql, sql + strlen(sql), &elements);
84 85 86
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.measure, sql);
  ASSERT_EQ(elements.measureLen, strlen("st"));
wmmhello's avatar
wmmhello 已提交
87
  ASSERT_EQ(elements.measureTagsLen, strlen("st,"));
88

wmmhello's avatar
wmmhello 已提交
89
  ASSERT_EQ(elements.tags, sql + elements.measureTagsLen);
90 91
  ASSERT_EQ(elements.tagsLen, 0);

wmmhello's avatar
wmmhello 已提交
92
  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 1);
93 94
  ASSERT_EQ(elements.colsLen, strlen("c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64"));

wmmhello's avatar
wmmhello 已提交
95
  ASSERT_EQ(elements.timestamp, sql + elements.measureTagsLen + 1 + elements.colsLen + 1);
96
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));
97 98
  taosArrayDestroy(elements.colArray);
  elements.colArray = NULL;
99 100

  // case 5 tag is null
wmmhello's avatar
wmmhello 已提交
101 102
  tmp = " st   c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64  1626006833639000000 ";
  memcpy(sql, tmp, strlen(tmp) + 1);
103
  memset(&elements, 0, sizeof(SSmlLineInfo));
104
  ret = smlParseInfluxString(info, sql, sql + strlen(sql), &elements);
105
  ASSERT_EQ(ret, 0);
wmmhello's avatar
wmmhello 已提交
106
  ASSERT_EQ(elements.measure, sql + 1);
107 108 109 110 111
  ASSERT_EQ(elements.measureLen, strlen("st"));
  ASSERT_EQ(elements.measureTagsLen, strlen("st"));

  ASSERT_EQ(elements.tagsLen, 0);

wmmhello's avatar
wmmhello 已提交
112
  ASSERT_EQ(elements.cols, sql + 1 + elements.measureTagsLen + 3);
113 114
  ASSERT_EQ(elements.colsLen, strlen("c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64"));

wmmhello's avatar
wmmhello 已提交
115
  ASSERT_EQ(elements.timestamp, sql + 1 + elements.measureTagsLen + 3 + elements.colsLen + 2);
116
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));
117 118
  taosArrayDestroy(elements.colArray);
  elements.colArray = NULL;
119 120

  // case 6
wmmhello's avatar
wmmhello 已提交
121 122
  tmp = " st   c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64   ";
  memcpy(sql, tmp, strlen(tmp) + 1);
123
  memset(&elements, 0, sizeof(SSmlLineInfo));
124
  ret = smlParseInfluxString(info, sql, sql + strlen(sql), &elements);
125
  ASSERT_EQ(ret, 0);
126 127 128
  taosArrayDestroy(elements.colArray);
  elements.colArray = NULL;
  smlClearForRerun(info);
129 130

  // case 7
wmmhello's avatar
wmmhello 已提交
131 132
  tmp = " st   ,   ";
  memcpy(sql, tmp, strlen(tmp) + 1);
133
  memset(&elements, 0, sizeof(SSmlLineInfo));
134 135 136 137
  ret = smlParseInfluxString(info, sql, sql + strlen(sql), &elements);
  ASSERT_NE(ret, 0);
  taosArrayDestroy(elements.colArray);
  elements.colArray = NULL;
138 139

  // case 8 false
wmmhello's avatar
wmmhello 已提交
140 141
  tmp = ", st   ,   ";
  memcpy(sql, tmp, strlen(tmp) + 1);
142
  memset(&elements, 0, sizeof(SSmlLineInfo));
143
  ret = smlParseInfluxString(info, sql, sql + strlen(sql), &elements);
144
  ASSERT_NE(ret, 0);
145 146 147
  taosArrayDestroy(elements.colArray);
  elements.colArray = NULL;

wmmhello's avatar
wmmhello 已提交
148
  taosMemoryFree(sql);
149 150
  smlDestroyInfo(info);

151 152
}

wmmhello's avatar
wmmhello 已提交
153
TEST(testCase, smlParseCols_Error_Test) {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  const char *data[] = {"st,t=1 c=\"89sd 1626006833639000000",  // binary, nchar
                        "st,t=1 c=j\"89sd\" 1626006833639000000",
                        "st,t=1 c=\"89sd\"k 1626006833639000000",
                        "st,t=1 c=u 1626006833639000000",  // bool
                        "st,t=1 c=truet 1626006833639000000",
                        "st,t=1 c=f64 1626006833639000000",  // double
                        "st,t=1 c=8f64f 1626006833639000000",
                        "st,t=1 c=8ef64 1626006833639000000",
                        "st,t=1 c=f32 1626006833639000000",  // float
                        "st,t=1 c=8f32f 1626006833639000000",
                        "st,t=1 c=8wef32 1626006833639000000",
                        "st,t=1 c=-3.402823466e+39f32 1626006833639000000",
                        "st,t=1 c= 1626006833639000000",  // double
                        "st,t=1 c=8f 1626006833639000000",
                        "st,t=1 c=8we 1626006833639000000",
                        "st,t=1 c=i8 1626006833639000000",  // tiny int
                        "st,t=1 c=-8i8f 1626006833639000000",
                        "st,t=1 c=8wei8 1626006833639000000",
                        "st,t=1 c=-999i8 1626006833639000000",
                        "st,t=1 c=u8 1626006833639000000",  // u tiny int
                        "st,t=1 c=8fu8 1626006833639000000",
                        "st,t=1 c=8weu8 1626006833639000000",
                        "st,t=1 c=999u8 1626006833639000000",
                        "st,t=1 c=-8u8 1626006833639000000",
                        "st,t=1 c=i16 1626006833639000000",  // small int
                        "st,t=1 c=8fi16u 1626006833639000000",
                        "st,t=1 c=8wei16 1626006833639000000",
                        "st,t=1 c=-67787i16 1626006833639000000",
                        "st,t=1 c=u16 1626006833639000000",  // u small int
                        "st,t=1 c=8u16f 1626006833639000000",
                        "st,t=1 c=8weu16 1626006833639000000",
                        "st,t=1 c=-9u16 1626006833639000000",
                        "st,t=1 c=67787u16 1626006833639000000",
                        "st,t=1 c=i32 1626006833639000000",  // int
                        "st,t=1 c=8i32f 1626006833639000000",
                        "st,t=1 c=8wei32 1626006833639000000",
                        "st,t=1 c=2147483649i32 1626006833639000000",
                        "st,t=1 c=u32 1626006833639000000",  // u int
                        "st,t=1 c=8u32f 1626006833639000000",
                        "st,t=1 c=8weu32 1626006833639000000",
                        "st,t=1 c=-4u32 1626006833639000000",
                        "st,t=1 c=42949672958u32 1626006833639000000",
                        "st,t=1 c=i64 1626006833639000000",  // big int
                        "st,t=1 c=8i64i 1626006833639000000",
                        "st,t=1 c=8wei64 1626006833639000000",
                        "st,t=1 c=-9223372036854775809i64 1626006833639000000",
                        "st,t=1 c=i 1626006833639000000",  // big int
                        "st,t=1 c=8fi 1626006833639000000",
                        "st,t=1 c=8wei 1626006833639000000",
                        "st,t=1 c=9223372036854775808i 1626006833639000000",
                        "st,t=1 c=u64 1626006833639000000",  // u big int
                        "st,t=1 c=8u64f 1626006833639000000",
                        "st,t=1 c=8weu64 1626006833639000000",
                        "st,t=1 c=-3.402823466e+39u64 1626006833639000000",
                        "st,t=1 c=-339u64 1626006833639000000",
                        "st,t=1 c=18446744073709551616u64 1626006833639000000",
                        "st,t=1 c=1=2 1626006833639000000"};

  SSmlHandle *info = smlBuildSmlInfo(NULL);
  info->protocol = TSDB_SML_LINE_PROTOCOL;
  info->dataFormat = false;
H
Hongze Cheng 已提交
215
  for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
wmmhello's avatar
wmmhello 已提交
216 217 218 219
    char       msg[256] = {0};
    SSmlMsgBuf msgBuf;
    msgBuf.buf = msg;
    msgBuf.len = 256;
wmmhello's avatar
wmmhello 已提交
220
    int32_t len = strlen(data[i]);
H
Hongze Cheng 已提交
221
    char   *sql = (char *)taosMemoryCalloc(256, 1);
wmmhello's avatar
wmmhello 已提交
222
    memcpy(sql, data[i], len + 1);
223 224 225
    SSmlLineInfo elements = {0};
    int32_t ret = smlParseInfluxString(info, sql, sql + len, &elements);
//    printf("i:%d\n", i);
wmmhello's avatar
wmmhello 已提交
226
    ASSERT_NE(ret, TSDB_CODE_SUCCESS);
wmmhello's avatar
wmmhello 已提交
227
    taosMemoryFree(sql);
228
    taosArrayDestroy(elements.colArray);
wmmhello's avatar
wmmhello 已提交
229
  }
230
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
231 232
}

wmmhello's avatar
wmmhello 已提交
233 234 235 236 237
TEST(testCase, smlParseCols_Test) {
  char       msg[256] = {0};
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;
238 239 240 241
  SSmlHandle *info = smlBuildSmlInfo(NULL);
  info->protocol = TSDB_SML_LINE_PROTOCOL;
  info->dataFormat = false;
  SSmlLineInfo elements = {0};
wmmhello's avatar
wmmhello 已提交
242
  info->msgBuf = msgBuf;
243

H
Hongze Cheng 已提交
244
  const char *data =
245
      "st,t=1 cb\\=in=\"pass\\,it "
H
Hongze Cheng 已提交
246 247
      "hello,c=2\",cnch=L\"ii\\=sdfsf\",cbool=false,cf64=4.31f64,cf64_=8.32,cf32=8.23f32,ci8=-34i8,cu8=89u8,ci16="
      "233i16,cu16=898u16,ci32=98289i32,cu32=12323u32,ci64=-89238i64,ci=989i,cu64=8989323u64,cbooltrue=true,cboolt=t,"
248
      "cboolf=f,cnch_=l\"iuwq\" 1626006833639000000";
wmmhello's avatar
wmmhello 已提交
249
  int32_t len = strlen(data);
H
Hongze Cheng 已提交
250
  char   *sql = (char *)taosMemoryCalloc(1024, 1);
wmmhello's avatar
wmmhello 已提交
251
  memcpy(sql, data, len + 1);
252
  int32_t ret = smlParseInfluxString(info, sql, sql + len, &elements);
wmmhello's avatar
wmmhello 已提交
253
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
254 255
  int32_t size = taosArrayGetSize(elements.colArray);
  ASSERT_EQ(size, 20);
wmmhello's avatar
wmmhello 已提交
256 257

  // binary
258
  SSmlKv *kv = (SSmlKv *)taosArrayGet(elements.colArray, 1);
wmmhello's avatar
wmmhello 已提交
259
  ASSERT_EQ(strncasecmp(kv->key, "cb=in", 5), 0);
wmmhello's avatar
wmmhello 已提交
260
  ASSERT_EQ(kv->keyLen, 5);
wmmhello's avatar
wmmhello 已提交
261
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BINARY);
wmmhello's avatar
wmmhello 已提交
262
  ASSERT_EQ(kv->length, 17);
wmmhello's avatar
wmmhello 已提交
263
  ASSERT_EQ(strncasecmp(kv->value, "pass,it ", 8), 0);
wmmhello's avatar
wmmhello 已提交
264 265

  // nchar
266
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 2);
wmmhello's avatar
wmmhello 已提交
267 268 269
  ASSERT_EQ(strncasecmp(kv->key, "cnch", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
wmmhello's avatar
wmmhello 已提交
270
  ASSERT_EQ(kv->length, 8);
wmmhello's avatar
wmmhello 已提交
271
  ASSERT_EQ(strncasecmp(kv->value, "ii=sd", 5), 0);
wmmhello's avatar
wmmhello 已提交
272 273

  // bool
274
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 3);
wmmhello's avatar
wmmhello 已提交
275 276 277
  ASSERT_EQ(strncasecmp(kv->key, "cbool", 5), 0);
  ASSERT_EQ(kv->keyLen, 5);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
wmmhello's avatar
wmmhello 已提交
278
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
279 280 281
  ASSERT_EQ(kv->i, false);

  // double
282
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 4);
wmmhello's avatar
wmmhello 已提交
283 284 285
  ASSERT_EQ(strncasecmp(kv->key, "cf64", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_DOUBLE);
wmmhello's avatar
wmmhello 已提交
286
  ASSERT_EQ(kv->length, 8);
H
Hongze Cheng 已提交
287
  // ASSERT_EQ(kv->d, 4.31);
288
  printf("4.31 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
289 290

  // float
291
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 5);
292
  ASSERT_EQ(strncasecmp(kv->key, "cf64_", 5), 0);
wmmhello's avatar
wmmhello 已提交
293
  ASSERT_EQ(kv->keyLen, 5);
294 295
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_EQ(kv->length, 8);
H
Hongze Cheng 已提交
296
  // ASSERT_EQ(kv->f, 8.32);
297
  printf("8.32 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
298 299

  // float
300
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 6);
wmmhello's avatar
wmmhello 已提交
301 302 303 304
  ASSERT_EQ(strncasecmp(kv->key, "cf32", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_FLOAT);
  ASSERT_EQ(kv->length, 4);
H
Hongze Cheng 已提交
305
  // ASSERT_EQ(kv->f, 8.23);
wmmhello's avatar
wmmhello 已提交
306
  printf("8.23 = kv->f:%f\n", kv->f);
wmmhello's avatar
wmmhello 已提交
307 308

  // tiny int
309
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 7);
wmmhello's avatar
wmmhello 已提交
310 311
  ASSERT_EQ(strncasecmp(kv->key, "ci8", 3), 0);
  ASSERT_EQ(kv->keyLen, 3);
wmmhello's avatar
wmmhello 已提交
312
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_TINYINT);
wmmhello's avatar
wmmhello 已提交
313
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
314
  ASSERT_EQ(kv->i, -34);
wmmhello's avatar
wmmhello 已提交
315 316

  // unsigned tiny int
317
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 8);
wmmhello's avatar
wmmhello 已提交
318 319 320 321 322 323 324
  ASSERT_EQ(strncasecmp(kv->key, "cu8", 3), 0);
  ASSERT_EQ(kv->keyLen, 3);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_UTINYINT);
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->u, 89);

  // small int
325
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 9);
wmmhello's avatar
wmmhello 已提交
326 327 328 329 330 331 332
  ASSERT_EQ(strncasecmp(kv->key, "ci16", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_SMALLINT);
  ASSERT_EQ(kv->length, 2);
  ASSERT_EQ(kv->u, 233);

  // unsigned smallint
333
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 10);
wmmhello's avatar
wmmhello 已提交
334 335 336 337 338 339 340
  ASSERT_EQ(strncasecmp(kv->key, "cu16", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_USMALLINT);
  ASSERT_EQ(kv->length, 2);
  ASSERT_EQ(kv->u, 898);

  // int
341
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 11);
wmmhello's avatar
wmmhello 已提交
342 343 344 345 346 347 348
  ASSERT_EQ(strncasecmp(kv->key, "ci32", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_INT);
  ASSERT_EQ(kv->length, 4);
  ASSERT_EQ(kv->u, 98289);

  // unsigned int
349
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 12);
wmmhello's avatar
wmmhello 已提交
350 351 352 353 354 355 356
  ASSERT_EQ(strncasecmp(kv->key, "cu32", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_UINT);
  ASSERT_EQ(kv->length, 4);
  ASSERT_EQ(kv->u, 12323);

  // bigint
357
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 13);
wmmhello's avatar
wmmhello 已提交
358
  ASSERT_EQ(strncasecmp(kv->key, "ci64", 4), 0);
wmmhello's avatar
wmmhello 已提交
359 360
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BIGINT);
wmmhello's avatar
wmmhello 已提交
361 362 363 364
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->i, -89238);

  // bigint
365
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 14);
wmmhello's avatar
wmmhello 已提交
366 367 368 369 370 371 372
  ASSERT_EQ(strncasecmp(kv->key, "ci", 2), 0);
  ASSERT_EQ(kv->keyLen, 2);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BIGINT);
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->i, 989);

  // unsigned bigint
373
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 15);
wmmhello's avatar
wmmhello 已提交
374 375 376 377 378 379 380
  ASSERT_EQ(strncasecmp(kv->key, "cu64", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_UBIGINT);
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->u, 8989323);

  // bool
381
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 16);
wmmhello's avatar
wmmhello 已提交
382 383 384 385 386 387 388
  ASSERT_EQ(strncasecmp(kv->key, "cbooltrue", 9), 0);
  ASSERT_EQ(kv->keyLen, 9);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, true);

  // bool
389
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 17);
wmmhello's avatar
wmmhello 已提交
390
  ASSERT_EQ(strncasecmp(kv->key, "cboolt", 6), 0);
wmmhello's avatar
wmmhello 已提交
391 392 393 394
  ASSERT_EQ(kv->keyLen, 6);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, true);
wmmhello's avatar
wmmhello 已提交
395 396

  // bool
397
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 18);
wmmhello's avatar
wmmhello 已提交
398 399 400
  ASSERT_EQ(strncasecmp(kv->key, "cboolf", 6), 0);
  ASSERT_EQ(kv->keyLen, 6);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
wmmhello's avatar
wmmhello 已提交
401 402
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, false);
wmmhello's avatar
wmmhello 已提交
403

wmmhello's avatar
wmmhello 已提交
404
  // nchar
405
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 19);
wmmhello's avatar
wmmhello 已提交
406 407 408 409 410 411
  ASSERT_EQ(strncasecmp(kv->key, "cnch_", 5), 0);
  ASSERT_EQ(kv->keyLen, 5);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
  ASSERT_EQ(kv->length, 4);
  ASSERT_EQ(strncasecmp(kv->value, "iuwq", 4), 0);

412
  taosArrayDestroy(elements.colArray);
wmmhello's avatar
wmmhello 已提交
413
  taosMemoryFree(sql);
414
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
415 416
}

wmmhello's avatar
wmmhello 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
//TEST(testCase, smlGetTimestampLen_Test) {
//  uint8_t len = smlGetTimestampLen(0);
//  ASSERT_EQ(len, 1);
//
//  len = smlGetTimestampLen(1);
//  ASSERT_EQ(len, 1);
//
//  len = smlGetTimestampLen(10);
//  ASSERT_EQ(len, 2);
//
//  len = smlGetTimestampLen(390);
//  ASSERT_EQ(len, 3);
//
//  len = smlGetTimestampLen(-1);
//  ASSERT_EQ(len, 1);
//
//  len = smlGetTimestampLen(-10);
//  ASSERT_EQ(len, 2);
//
//  len = smlGetTimestampLen(-390);
//  ASSERT_EQ(len, 3);
//}
wmmhello's avatar
wmmhello 已提交
439

wmmhello's avatar
wmmhello 已提交
440
TEST(testCase, smlParseNumber_Test) {
H
Hongze Cheng 已提交
441 442
  SSmlKv     kv = {0};
  char       buf[64] = {0};
wmmhello's avatar
wmmhello 已提交
443 444 445 446 447 448
  SSmlMsgBuf msg = {0};
  msg.buf = buf;
  msg.len = 64;
  kv.value = "3.2e-900";
  kv.length = 8;
  bool res = smlParseNumber(&kv, &msg);
H
Hongze Cheng 已提交
449
  printf("res:%d,v:%f, %f\n", res, kv.d, HUGE_VAL);
wmmhello's avatar
wmmhello 已提交
450 451
}

452
TEST(testCase, smlParseTelnetLine_error_Test) {
453 454 455
  SSmlHandle *info = smlBuildSmlInfo(NULL);
  info->dataFormat = false;
  info->protocol = TSDB_SML_TELNET_PROTOCOL;
456 457
  ASSERT_NE(info, nullptr);

wmmhello's avatar
wmmhello 已提交
458
  const char *sql[] = {
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
      "sys.procs.running 14794961040 42 host=web01",
      "sys.procs.running 14791040 42 host=web01",
      "sys.procs.running erere 42 host=web01",
      "sys.procs.running 1.6e10 42 host=web01",
      "sys.procs.running 1.47949610 42 host=web01",
      "sys.procs.running 147949610i 42 host=web01",
      "sys.procs.running -147949610 42 host=web01",
      "",
      "   ",
      "sys  ",
      "sys.procs.running 1479496100 42 ",
      "sys.procs.running 1479496100 42 host= ",
      "sys.procs.running 1479496100 42or host=web01",
      "sys.procs.running 1479496100 true host=web01",
      "sys.procs.running 1479496100 \"binary\" host=web01",
      "sys.procs.running 1479496100 L\"rfr\" host=web01",
      "sys.procs.running 1479496100 42 host=web01 cpu= ",
      "sys.procs.running 1479496100 42 host=web01 host",
wmmhello's avatar
wmmhello 已提交
477 478
      "sys.procs.running 1479496100 42 host=web01=er",
      "sys.procs.running 1479496100 42 host= web01",
479
  };
H
Hongze Cheng 已提交
480
  for (int i = 0; i < sizeof(sql) / sizeof(sql[0]); i++) {
481 482 483
    SSmlLineInfo elements = {0};
    int ret = smlParseTelnetString(info, (char*)sql[i], (char*)(sql[i] + strlen(sql[i])), &elements);
//    printf("i:%d\n", i);
484 485 486 487 488 489
    ASSERT_NE(ret, 0);
  }

  smlDestroyInfo(info);
}

wmmhello's avatar
wmmhello 已提交
490
TEST(testCase, smlParseTelnetLine_diff_json_type2_Test) {
491 492
  SSmlHandle *info = smlBuildSmlInfo(NULL);
  info->protocol = TSDB_SML_JSON_PROTOCOL;
493 494 495
  ASSERT_NE(info, nullptr);

  const char *sql[] = {
wmmhello's avatar
wmmhello 已提交
496
      "[{\"metric\":\"sys.cpu.nice\",\"timestamp\": 1346846400,\"value\": 18,\"tags\": {\"host\": \"lga\"}},{\"metric\": \"sys.sdfa\",\"timestamp\": 1346846400,\"value\": \"18\",\"tags\": {\"host\": 8932}},]",
497
  };
H
Hongze Cheng 已提交
498
  for (int i = 0; i < sizeof(sql) / sizeof(sql[0]); i++) {
wmmhello's avatar
wmmhello 已提交
499 500 501
    char *dataPointStart = (char *)sql[i];
    while (1) {
      SSmlLineInfo elements = {0};
wmmhello's avatar
wmmhello 已提交
502 503
      smlJsonParseObj(&dataPointStart, &elements);
      if(*dataPointStart == '\0') break;
504

wmmhello's avatar
wmmhello 已提交
505 506 507
      SArray *tags = smlJsonParseTags(elements.tags, elements.tags + elements.tagsLen);
      size_t num = taosArrayGetSize(tags);
      ASSERT_EQ(num, 1);
508

wmmhello's avatar
wmmhello 已提交
509 510
      taosArrayDestroy(tags);
    }
511 512 513
  }
  smlDestroyInfo(info);
}
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

TEST(testCase, smlParseNumber_performance_Test) {
  char       msg[256] = {0};
  SSmlMsgBuf msgBuf;
  SSmlKv kv;

  char* str[3] = {"2893f64", "2323u32", "93u8"};
  for (int i = 0; i < 3; ++i) {
    int64_t t1 = taosGetTimestampUs();
    for (int j = 0; j < 10000000; ++j) {
      kv.value = str[i];
      kv.length = strlen(str[i]);
      smlParseNumber(&kv, &msgBuf);
    }
    printf("smlParseNumber:%s cost:%" PRId64, str[i], taosGetTimestampUs() - t1);
    printf("\n");
    int64_t t2 = taosGetTimestampUs();
    for (int j = 0; j < 10000000; ++j) {
      kv.value = str[i];
      kv.length = strlen(str[i]);
      smlParseNumberOld(&kv, &msgBuf);
    }
    printf("smlParseNumberOld:%s cost:%" PRId64, str[i], taosGetTimestampUs() - t2);
    printf("\n\n");
  }
}