smlTest.cpp 21.2 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 28 29 30
/*
 * 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"

#include "../src/clientSml.c"
#include "taos.h"

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

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

wmmhello's avatar
wmmhello 已提交
43
  // case 1
wmmhello's avatar
wmmhello 已提交
44 45 46
  char *tmp = "\\,st,t1=3,t2=4,t3=t3 c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64 1626006833639000000    ,32,c=3";
  char *sql = (char*)taosMemoryCalloc(256, 1);
  memcpy(sql, tmp, strlen(tmp) + 1);
47
  int   ret = smlParseInfluxString(sql, &elements, &msgBuf);
48 49
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.measure, sql);
wmmhello's avatar
wmmhello 已提交
50 51
  ASSERT_EQ(elements.measureLen, strlen(",st"));
  ASSERT_EQ(elements.measureTagsLen, strlen(",st,t1=3,t2=4,t3=t3"));
52 53 54 55 56 57 58 59 60 61 62

  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"));

  // case 2  false
wmmhello's avatar
wmmhello 已提交
63 64
  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);
65
  memset(&elements, 0, sizeof(SSmlLineInfo));
66
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
67 68 69
  ASSERT_NE(ret, 0);

  // case 3  false
wmmhello's avatar
wmmhello 已提交
70 71
  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);
72
  memset(&elements, 0, sizeof(SSmlLineInfo));
73
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
74
  ASSERT_EQ(ret, 0);
wmmhello's avatar
wmmhello 已提交
75
  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 1);
76 77 78
  ASSERT_EQ(elements.colsLen, strlen("t1=3,t2=4,t3=t3"));

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

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

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

wmmhello's avatar
wmmhello 已提交
94
  ASSERT_EQ(elements.timestamp, sql + elements.measureTagsLen + 1 + elements.colsLen + 1);
95 96 97
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));

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

  ASSERT_EQ(elements.tagsLen, 0);

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

wmmhello's avatar
wmmhello 已提交
112
  ASSERT_EQ(elements.timestamp, sql + 1 + elements.measureTagsLen + 3 + elements.colsLen + 2);
113 114 115
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));

  // case 6
wmmhello's avatar
wmmhello 已提交
116 117
  tmp = " st   c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64   ";
  memcpy(sql, tmp, strlen(tmp) + 1);
118
  memset(&elements, 0, sizeof(SSmlLineInfo));
119
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
120 121 122
  ASSERT_EQ(ret, 0);

  // case 7
wmmhello's avatar
wmmhello 已提交
123 124
  tmp = " st   ,   ";
  memcpy(sql, tmp, strlen(tmp) + 1);
125
  memset(&elements, 0, sizeof(SSmlLineInfo));
126
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
wmmhello's avatar
wmmhello 已提交
127
  ASSERT_EQ(ret, 0);
128 129

  // case 8 false
wmmhello's avatar
wmmhello 已提交
130 131
  tmp = ", st   ,   ";
  memcpy(sql, tmp, strlen(tmp) + 1);
132
  memset(&elements, 0, sizeof(SSmlLineInfo));
133
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
134
  ASSERT_NE(ret, 0);
wmmhello's avatar
wmmhello 已提交
135
  taosMemoryFree(sql);
136 137
}

wmmhello's avatar
wmmhello 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151
TEST(testCase, smlParseCols_Error_Test) {
  const char *data[] = {
    "c=\"89sd",           // binary, nchar
    "c=j\"89sd\"",
    "c=\"89sd\"k",
    "c=u",                // bool
    "c=truet",
    "c=f64",              // double
    "c=8f64f",
    "c=8ef64",
    "c=f32",              // float
    "c=8f32f",
    "c=8wef32",
    "c=-3.402823466e+39f32",
152
    "c=",                 // double
wmmhello's avatar
wmmhello 已提交
153 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
    "c=8f",
    "c=8we",
    "c=i8",              // tiny int
    "c=-8i8f",
    "c=8wei8",
    "c=-999i8",
    "c=u8",              // u tiny int
    "c=8fu8",
    "c=8weu8",
    "c=999u8",
    "c=-8u8",
    "c=i16",              // small int
    "c=8fi16u",
    "c=8wei16",
    "c=-67787i16",
    "c=u16",              // u small int
    "c=8u16f",
    "c=8weu16",
    "c=-9u16",
    "c=67787u16",
    "c=i32",                // int
    "c=8i32f",
    "c=8wei32",
    "c=2147483649i32",
    "c=u32",                // u int
    "c=8u32f",
    "c=8weu32",
    "c=-4u32",
    "c=42949672958u32",
    "c=i64",                // big int
    "c=8i64i",
    "c=8wei64",
    "c=-9223372036854775809i64",
    "c=i",                  // big int
    "c=8fi",
    "c=8wei",
    "c=9223372036854775808i",
    "c=u64",                // u big int
    "c=8u64f",
    "c=8weu64",
    "c=-3.402823466e+39u64",
    "c=-339u64",
    "c=18446744073709551616u64",
wmmhello's avatar
wmmhello 已提交
196 197
    "c=1,c=2",
    "c=1=2"
wmmhello's avatar
wmmhello 已提交
198 199
  };

200
  SHashObj *dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
201
  for(int i = 0; i < sizeof(data)/sizeof(data[0]); i++){
wmmhello's avatar
wmmhello 已提交
202 203 204 205
    char       msg[256] = {0};
    SSmlMsgBuf msgBuf;
    msgBuf.buf = msg;
    msgBuf.len = 256;
wmmhello's avatar
wmmhello 已提交
206
    int32_t len = strlen(data[i]);
wmmhello's avatar
wmmhello 已提交
207 208
    char *sql = (char*)taosMemoryCalloc(256, 1);
    memcpy(sql, data[i], len + 1);
wmmhello's avatar
wmmhello 已提交
209
    SArray *cols = taosArrayInit(8, POINTER_BYTES);
210
    int32_t ret = smlParseCols(sql, len, cols, NULL, false, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
211
    printf("i:%d\n",i);
wmmhello's avatar
wmmhello 已提交
212
    ASSERT_NE(ret, TSDB_CODE_SUCCESS);
213
    taosHashClear(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
214
    taosMemoryFree(sql);
wmmhello's avatar
wmmhello 已提交
215 216 217 218 219
    for(int j = 0; j < taosArrayGetSize(cols); j++){
      void *kv = taosArrayGetP(cols, j);
      taosMemoryFree(kv);
    }
    taosArrayDestroy(cols);
wmmhello's avatar
wmmhello 已提交
220
  }
221
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
222 223
}

wmmhello's avatar
wmmhello 已提交
224 225 226 227 228 229 230
TEST(testCase, smlParseCols_tag_Test) {
  char       msg[256] = {0};
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;

  SArray *cols = taosArrayInit(16, POINTER_BYTES);
wmmhello's avatar
wmmhello 已提交
231
  ASSERT_NE(cols, nullptr);
232
  SHashObj *dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
233 234

  const char *data =
wmmhello's avatar
wmmhello 已提交
235
      "cbin=\"passit helloc\",cnch=L\"iisdfsf\",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,cboolf=f,cnch_=l\"iuwq\"";
wmmhello's avatar
wmmhello 已提交
236
  int32_t len = strlen(data);
237
  int32_t ret = smlParseCols(data, len, cols, NULL, true, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
238 239 240 241 242
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  int32_t size = taosArrayGetSize(cols);
  ASSERT_EQ(size, 19);

  // nchar
wmmhello's avatar
wmmhello 已提交
243
  SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, 0);
wmmhello's avatar
wmmhello 已提交
244 245 246
  ASSERT_EQ(strncasecmp(kv->key, "cbin", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
wmmhello's avatar
wmmhello 已提交
247
  ASSERT_EQ(kv->length, 15);
wmmhello's avatar
wmmhello 已提交
248 249 250
  ASSERT_EQ(strncasecmp(kv->value, "\"passit", 7), 0);

  // nchar
wmmhello's avatar
wmmhello 已提交
251
  kv = (SSmlKv *)taosArrayGetP(cols, 3);
wmmhello's avatar
wmmhello 已提交
252 253 254
  ASSERT_EQ(strncasecmp(kv->key, "cf64", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
wmmhello's avatar
wmmhello 已提交
255
  ASSERT_EQ(kv->length, 7);
wmmhello's avatar
wmmhello 已提交
256 257
  ASSERT_EQ(strncasecmp(kv->value, "4.31f64", 7), 0);

wmmhello's avatar
wmmhello 已提交
258 259 260 261
  for(int i = 0; i < size; i++){
    void *tmp = taosArrayGetP(cols, i);
    taosMemoryFree(tmp);
  }
wmmhello's avatar
wmmhello 已提交
262
  taosArrayClear(cols);
263 264

  // test tag is null
wmmhello's avatar
wmmhello 已提交
265 266 267
  data = "t=3e";
  len = 0;
  memset(msgBuf.buf, 0, msgBuf.len);
268
  taosHashClear(dumplicateKey);
269
  ret = smlParseCols(data, len, cols, NULL, true, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
270 271
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  size = taosArrayGetSize(cols);
272
  ASSERT_EQ(size, 0);
273 274 275

  taosArrayDestroy(cols);
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
276 277
}

wmmhello's avatar
wmmhello 已提交
278 279 280 281 282 283 284
TEST(testCase, smlParseCols_Test) {
  char       msg[256] = {0};
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;

  SArray *cols = taosArrayInit(16, POINTER_BYTES);
wmmhello's avatar
wmmhello 已提交
285
  ASSERT_NE(cols, nullptr);
wmmhello's avatar
wmmhello 已提交
286

287 288
  SHashObj *dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);

wmmhello's avatar
wmmhello 已提交
289
  const char *data = "cb\\=in=\"pass\\,it 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,cboolf=f,cnch_=l\"iuwq\"";
wmmhello's avatar
wmmhello 已提交
290
  int32_t len = strlen(data);
wmmhello's avatar
wmmhello 已提交
291 292
  char *sql = (char*)taosMemoryCalloc(1024, 1);
  memcpy(sql, data, len + 1);
293
  int32_t ret = smlParseCols(sql, len, cols, NULL, false, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
294 295 296 297 298
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  int32_t size = taosArrayGetSize(cols);
  ASSERT_EQ(size, 19);

  // binary
wmmhello's avatar
wmmhello 已提交
299
  SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, 0);
wmmhello's avatar
wmmhello 已提交
300
  ASSERT_EQ(strncasecmp(kv->key, "cb=in", 5), 0);
wmmhello's avatar
wmmhello 已提交
301
  ASSERT_EQ(kv->keyLen, 5);
wmmhello's avatar
wmmhello 已提交
302
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BINARY);
wmmhello's avatar
wmmhello 已提交
303
  ASSERT_EQ(kv->length, 17);
wmmhello's avatar
wmmhello 已提交
304
  ASSERT_EQ(strncasecmp(kv->value, "pass,it ", 8), 0);
wmmhello's avatar
wmmhello 已提交
305 306 307
  taosMemoryFree(kv);

  // nchar
wmmhello's avatar
wmmhello 已提交
308
  kv = (SSmlKv *)taosArrayGetP(cols, 1);
wmmhello's avatar
wmmhello 已提交
309 310 311
  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 已提交
312
  ASSERT_EQ(kv->length, 8);
wmmhello's avatar
wmmhello 已提交
313
  ASSERT_EQ(strncasecmp(kv->value, "ii=sd", 5), 0);
wmmhello's avatar
wmmhello 已提交
314 315 316
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
317
  kv = (SSmlKv *)taosArrayGetP(cols, 2);
wmmhello's avatar
wmmhello 已提交
318 319 320
  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 已提交
321
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
322 323 324 325
  ASSERT_EQ(kv->i, false);
  taosMemoryFree(kv);

  // double
wmmhello's avatar
wmmhello 已提交
326
  kv = (SSmlKv *)taosArrayGetP(cols, 3);
wmmhello's avatar
wmmhello 已提交
327 328 329
  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 已提交
330 331
  ASSERT_EQ(kv->length, 8);
  //ASSERT_EQ(kv->d, 4.31);
332
  printf("4.31 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
333 334 335
  taosMemoryFree(kv);

  // float
wmmhello's avatar
wmmhello 已提交
336
  kv = (SSmlKv *)taosArrayGetP(cols, 4);
337
  ASSERT_EQ(strncasecmp(kv->key, "cf64_", 5), 0);
wmmhello's avatar
wmmhello 已提交
338
  ASSERT_EQ(kv->keyLen, 5);
339 340
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_EQ(kv->length, 8);
wmmhello's avatar
wmmhello 已提交
341
  //ASSERT_EQ(kv->f, 8.32);
342
  printf("8.32 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
343 344 345
  taosMemoryFree(kv);

  // float
wmmhello's avatar
wmmhello 已提交
346
  kv = (SSmlKv *)taosArrayGetP(cols, 5);
wmmhello's avatar
wmmhello 已提交
347 348 349 350
  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);
wmmhello's avatar
wmmhello 已提交
351 352
  //ASSERT_EQ(kv->f, 8.23);
  printf("8.23 = kv->f:%f\n", kv->f);
wmmhello's avatar
wmmhello 已提交
353 354 355
  taosMemoryFree(kv);

  // tiny int
wmmhello's avatar
wmmhello 已提交
356
  kv = (SSmlKv *)taosArrayGetP(cols, 6);
wmmhello's avatar
wmmhello 已提交
357 358
  ASSERT_EQ(strncasecmp(kv->key, "ci8", 3), 0);
  ASSERT_EQ(kv->keyLen, 3);
wmmhello's avatar
wmmhello 已提交
359
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_TINYINT);
wmmhello's avatar
wmmhello 已提交
360
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
361
  ASSERT_EQ(kv->i, -34);
wmmhello's avatar
wmmhello 已提交
362 363 364
  taosMemoryFree(kv);

  // unsigned tiny int
wmmhello's avatar
wmmhello 已提交
365
  kv = (SSmlKv *)taosArrayGetP(cols, 7);
wmmhello's avatar
wmmhello 已提交
366 367 368 369 370 371 372 373
  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);
  taosMemoryFree(kv);

  // small int
wmmhello's avatar
wmmhello 已提交
374
  kv = (SSmlKv *)taosArrayGetP(cols, 8);
wmmhello's avatar
wmmhello 已提交
375 376 377 378 379 380 381 382
  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);
  taosMemoryFree(kv);

  // unsigned smallint
wmmhello's avatar
wmmhello 已提交
383
  kv = (SSmlKv *)taosArrayGetP(cols, 9);
wmmhello's avatar
wmmhello 已提交
384 385 386 387 388 389 390 391
  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);
  taosMemoryFree(kv);

  // int
wmmhello's avatar
wmmhello 已提交
392
  kv = (SSmlKv *)taosArrayGetP(cols, 10);
wmmhello's avatar
wmmhello 已提交
393 394 395 396 397 398 399 400
  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);
  taosMemoryFree(kv);

  // unsigned int
wmmhello's avatar
wmmhello 已提交
401
  kv = (SSmlKv *)taosArrayGetP(cols, 11);
wmmhello's avatar
wmmhello 已提交
402 403 404 405 406 407 408 409 410
  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);
  taosMemoryFree(kv);


  // bigint
wmmhello's avatar
wmmhello 已提交
411
  kv = (SSmlKv *)taosArrayGetP(cols, 12);
wmmhello's avatar
wmmhello 已提交
412
  ASSERT_EQ(strncasecmp(kv->key, "ci64", 4), 0);
wmmhello's avatar
wmmhello 已提交
413 414
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BIGINT);
wmmhello's avatar
wmmhello 已提交
415 416 417 418 419
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->i, -89238);
  taosMemoryFree(kv);

  // bigint
wmmhello's avatar
wmmhello 已提交
420
  kv = (SSmlKv *)taosArrayGetP(cols, 13);
wmmhello's avatar
wmmhello 已提交
421 422 423 424 425 426 427 428
  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);
  taosMemoryFree(kv);

  // unsigned bigint
wmmhello's avatar
wmmhello 已提交
429
  kv = (SSmlKv *)taosArrayGetP(cols, 14);
wmmhello's avatar
wmmhello 已提交
430 431 432 433 434 435 436 437
  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);
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
438
  kv = (SSmlKv *)taosArrayGetP(cols, 15);
wmmhello's avatar
wmmhello 已提交
439 440 441 442 443 444 445 446 447
  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);
  taosMemoryFree(kv);


  // bool
wmmhello's avatar
wmmhello 已提交
448
  kv = (SSmlKv *)taosArrayGetP(cols, 16);
wmmhello's avatar
wmmhello 已提交
449
  ASSERT_EQ(strncasecmp(kv->key, "cboolt", 6), 0);
wmmhello's avatar
wmmhello 已提交
450 451 452 453
  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 已提交
454 455 456
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
457
  kv = (SSmlKv *)taosArrayGetP(cols, 17);
wmmhello's avatar
wmmhello 已提交
458 459 460
  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 已提交
461 462
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, false);
wmmhello's avatar
wmmhello 已提交
463 464
  taosMemoryFree(kv);

wmmhello's avatar
wmmhello 已提交
465
  // nchar
wmmhello's avatar
wmmhello 已提交
466
  kv = (SSmlKv *)taosArrayGetP(cols, 18);
wmmhello's avatar
wmmhello 已提交
467 468 469 470 471 472 473 474
  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);
  taosMemoryFree(kv);

  taosArrayDestroy(cols);
475
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
476
  taosMemoryFree(sql);
wmmhello's avatar
wmmhello 已提交
477 478
}

wmmhello's avatar
wmmhello 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
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 已提交
502 503 504 505 506 507 508 509 510 511 512 513
TEST(testCase, smlParseNumber_Test) {
  SSmlKv kv = {0};
  char buf[64] = {0};
  SSmlMsgBuf msg = {0};
  msg.buf = buf;
  msg.len = 64;
  kv.value = "3.2e-900";
  kv.length = 8;
  bool res = smlParseNumber(&kv, &msg);
  printf("res:%d,v:%f, %f\n", res,kv.d, HUGE_VAL);
}

514
TEST(testCase, smlParseTelnetLine_error_Test) {
515
  SSmlHandle *info = smlBuildSmlInfo(NULL, NULL, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
516 517
  ASSERT_NE(info, nullptr);

wmmhello's avatar
wmmhello 已提交
518
  const char *sql[] = {
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
      "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=w2",
      "sys.procs.running 1479496100 42 host=web01 host",
wmmhello's avatar
wmmhello 已提交
538 539
      "sys.procs.running 1479496100 42 host=web01=er",
      "sys.procs.running 1479496100 42 host= web01",
540 541
  };
  for(int i = 0; i < sizeof(sql)/sizeof(sql[0]); i++){
542
    int ret = smlParseTelnetLine(info, (void*)sql[i]);
543 544 545 546 547 548 549
    ASSERT_NE(ret, 0);
  }

  smlDestroyInfo(info);
}

TEST(testCase, smlParseTelnetLine_diff_type_Test) {
550 551
  SSmlHandle *info = smlBuildSmlInfo(NULL, NULL, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
  ASSERT_NE(info, nullptr);
552

wmmhello's avatar
wmmhello 已提交
553
  const char *sql[] = {
554
      "sys.procs.running 1479496104000 42 host=web01",
wmmhello's avatar
wmmhello 已提交
555 556
      "sys.procs.running 1479496104000 42u8 host=web01",
      "appywjnuct 1626006833641 True id=\"appywjnuct_40601_49808_1\" t0=t t1=127i8 id=\"appywjnuct_40601_49808_2\" t2=32767i16 t3=2147483647i32 t4=9223372036854775807i64 t5=11.12345f32 t6=22.123456789f64 t7=\"binaryTagValue\" t8=L\"ncharTagValue\""
557 558
  };

559 560 561 562 563 564 565
  int ret = TSDB_CODE_SUCCESS;
  for(int i = 0; i < sizeof(sql)/sizeof(sql[0]); i++){
    ret = smlParseTelnetLine(info, (void*)sql[i]);
    if(ret != TSDB_CODE_SUCCESS) break;
  }
  ASSERT_NE(ret, 0);
  smlDestroyInfo(info);
566 567 568
}

TEST(testCase, smlParseTelnetLine_json_error_Test) {
569
  SSmlHandle *info = smlBuildSmlInfo(NULL, NULL, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
  ASSERT_NE(info, nullptr);

  const char *sql[] = {
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 13468464009999333322222223,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"host\": \"web01\",\n"
      "           \"dc\": \"lga\"\n"
      "        }\n"
      "    },\n"
      "]",
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400i,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"host\": \"web01\",\n"
      "           \"dc\": \"lga\"\n"
      "        }\n"
      "    },\n"
      "]",
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"groupid\": { \n"
      "                 \"value\" : 2,\n"
      "                 \"type\"  : \"nchar\"\n"
      "             },\n"
      "           \"location\": { \n"
      "                 \"value\" : \"北京\",\n"
      "                 \"type\"  : \"binary\"\n"
      "             },\n"
      "           \"id\": \"d1001\"\n"
      "         }\n"
      "    },\n"
      "]",
  };
614 615

  int ret = TSDB_CODE_SUCCESS;
616 617 618 619 620 621 622 623 624 625
  for(int i = 0; i < sizeof(sql)/sizeof(sql[0]); i++){
    ret = smlParseTelnetLine(info, (void*)sql[i]);
    ASSERT_NE(ret, 0);
  }

  smlDestroyInfo(info);
}

TEST(testCase, smlParseTelnetLine_diff_json_type1_Test) {

626 627
  SSmlHandle *info = smlBuildSmlInfo(NULL, NULL, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
  ASSERT_NE(info, nullptr);
628

wmmhello's avatar
wmmhello 已提交
629
  const char *sql[] = {
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"host\": \"lga\"\n"
      "        }\n"
      "    },\n"
      "]",
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"host\": 8\n"
      "        }\n"
      "    },\n"
      "]",
  };
651 652 653 654 655 656 657 658

  int ret = TSDB_CODE_SUCCESS;
  for(int i = 0; i < sizeof(sql)/sizeof(sql[0]); i++){
    ret = smlParseTelnetLine(info, (void*)sql[i]);
    if(ret != TSDB_CODE_SUCCESS) break;
  }
  ASSERT_NE(ret, 0);
  smlDestroyInfo(info);
659 660 661
}

TEST(testCase, smlParseTelnetLine_diff_json_type2_Test) {
662 663
  SSmlHandle *info = smlBuildSmlInfo(NULL, NULL, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
  ASSERT_NE(info, nullptr);
664

wmmhello's avatar
wmmhello 已提交
665
  const char *sql[] = {
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"host\": \"lga\"\n"
      "        }\n"
      "    },\n"
      "]",
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400,\n"
      "        \"value\": \"18\",\n"
      "        \"tags\": {\n"
      "           \"host\": \"fff\"\n"
      "        }\n"
      "    },\n"
      "]",
  };
687 688 689 690 691 692 693
  int ret = TSDB_CODE_SUCCESS;
  for(int i = 0; i < sizeof(sql)/sizeof(sql[0]); i++){
    ret = smlParseTelnetLine(info, (void*)sql[i]);
    if(ret != TSDB_CODE_SUCCESS) break;
  }
  ASSERT_NE(ret, 0);
  smlDestroyInfo(info);
694
}