smlTest.cpp 21.6 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"

wmmhello's avatar
wmmhello 已提交
28
#include "../inc/clientSml.h"
29 30
#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};

43 44 45
  SSmlHandle *info = smlBuildSmlInfo(NULL);
  info->protocol = TSDB_SML_LINE_PROTOCOL;
  info->dataFormat = false;
wmmhello's avatar
wmmhello 已提交
46
  // case 1
wmmhello's avatar
wmmhello 已提交
47
  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 已提交
48
  char *sql = (char *)taosMemoryCalloc(256, 1);
wmmhello's avatar
wmmhello 已提交
49
  memcpy(sql, tmp, strlen(tmp) + 1);
50
  int ret = smlParseInfluxString(info, sql, sql + strlen(sql), &elements);
51 52
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.measure, sql);
53 54 55
  ASSERT_EQ(elements.measureLen, strlen("\\,st"));
  ASSERT_EQ(elements.measureEscaped, true);
  ASSERT_EQ(elements.measureTagsLen, strlen("\\,st,t1=3,t2=4,t3=t3"));
56 57 58 59 60 61 62 63 64

  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"));
65 66
  taosArrayDestroy(elements.colArray);
  elements.colArray = NULL;
67 68

  // case 2  false
wmmhello's avatar
wmmhello 已提交
69 70
  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);
71
  memset(&elements, 0, sizeof(SSmlLineInfo));
72
  ret = smlParseInfluxString(info, sql, sql + strlen(sql), &elements);
73
  ASSERT_NE(ret, 0);
74 75
  taosArrayDestroy(elements.colArray);
  elements.colArray = NULL;
76 77

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

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

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

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

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

  ASSERT_EQ(elements.tagsLen, 0);

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

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

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

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

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

wmmhello's avatar
wmmhello 已提交
146
  taosMemoryFree(sql);
147 148
  smlDestroyInfo(info);

149 150
}

wmmhello's avatar
wmmhello 已提交
151
TEST(testCase, smlParseCols_Error_Test) {
152 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 196 197 198 199 200 201 202 203 204 205 206 207
  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",
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
                        "st,t=1 c=1=2 1626006833639000000,",
                        // escape error test
                        // measure comma,space
                        "s,t,t=1 c=1 1626006833639000000,",
                        "s t,t=1 c=1 1626006833639000000,",
                        //tag key comma,equal,space
                        "st,t,t=1 c=2 1626006833639000000,",
                        "st,t=t=1 c=2 1626006833639000000,",
                        "st,t t=1 c=2 1626006833639000000,",
                        //tag value comma,equal,space
                        "st,tt=a,a c=2 1626006833639000000,",
                        "st,t=t=a a c=2 1626006833639000000,",
                        "st,t t=a=a c=2 1626006833639000000,",
                        //field key comma,equal,space
                        "st,tt=aa c,1=2 1626006833639000000,",
                        "st,tt=aa c=1=2 1626006833639000000,",
                        "st,tt=aa c 1=2 1626006833639000000,",
                        //field value    double quote,slash
                        "st,tt=aa c=\"a\"a\" 1626006833639000000,",
D
dapan1121 已提交
227 228
                        "escape_test,tag1=\"tag1_value\",tag2=\"tag2_value\" co l0=\"col0_value\",col1=\"col1_value\" 1680918783010000000",
                        "escape_test,tag1=\"tag1_value\",tag2=\"tag2_value\" col0=\"co\"l\"0_value\",col1=\"col1_value\" 1680918783010000000"
229
  };
230 231 232 233

  SSmlHandle *info = smlBuildSmlInfo(NULL);
  info->protocol = TSDB_SML_LINE_PROTOCOL;
  info->dataFormat = false;
H
Hongze Cheng 已提交
234
  for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
wmmhello's avatar
wmmhello 已提交
235 236 237 238
    char       msg[256] = {0};
    SSmlMsgBuf msgBuf;
    msgBuf.buf = msg;
    msgBuf.len = 256;
wmmhello's avatar
wmmhello 已提交
239
    int32_t len = strlen(data[i]);
H
Hongze Cheng 已提交
240
    char   *sql = (char *)taosMemoryCalloc(256, 1);
wmmhello's avatar
wmmhello 已提交
241
    memcpy(sql, data[i], len + 1);
242 243 244
    SSmlLineInfo elements = {0};
    int32_t ret = smlParseInfluxString(info, sql, sql + len, &elements);
//    printf("i:%d\n", i);
wmmhello's avatar
wmmhello 已提交
245
    ASSERT_NE(ret, TSDB_CODE_SUCCESS);
wmmhello's avatar
wmmhello 已提交
246
    taosMemoryFree(sql);
247
    taosArrayDestroy(elements.colArray);
wmmhello's avatar
wmmhello 已提交
248
  }
249
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
250 251
}

wmmhello's avatar
wmmhello 已提交
252 253 254 255 256
TEST(testCase, smlParseCols_Test) {
  char       msg[256] = {0};
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;
257 258 259 260
  SSmlHandle *info = smlBuildSmlInfo(NULL);
  info->protocol = TSDB_SML_LINE_PROTOCOL;
  info->dataFormat = false;
  SSmlLineInfo elements = {0};
wmmhello's avatar
wmmhello 已提交
261
  info->msgBuf = msgBuf;
262

H
Hongze Cheng 已提交
263
  const char *data =
264
      "st,t=1 cb\\=in=\"pass\\,it "
H
Hongze Cheng 已提交
265 266
      "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,"
267
      "cboolf=f,cnch_=l\"iuwq\" 1626006833639000000";
wmmhello's avatar
wmmhello 已提交
268
  int32_t len = strlen(data);
H
Hongze Cheng 已提交
269
  char   *sql = (char *)taosMemoryCalloc(1024, 1);
wmmhello's avatar
wmmhello 已提交
270
  memcpy(sql, data, len + 1);
271
  int32_t ret = smlParseInfluxString(info, sql, sql + len, &elements);
wmmhello's avatar
wmmhello 已提交
272
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
273 274
  int32_t size = taosArrayGetSize(elements.colArray);
  ASSERT_EQ(size, 20);
wmmhello's avatar
wmmhello 已提交
275 276

  // binary
277
  SSmlKv *kv = (SSmlKv *)taosArrayGet(elements.colArray, 1);
wmmhello's avatar
wmmhello 已提交
278
  ASSERT_EQ(strncasecmp(kv->key, "cb=in", 5), 0);
wmmhello's avatar
wmmhello 已提交
279
  ASSERT_EQ(kv->keyLen, 5);
wmmhello's avatar
wmmhello 已提交
280
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BINARY);
281 282 283 284
  ASSERT_EQ(kv->length, 18);
  ASSERT_EQ(kv->keyEscaped, true);
  ASSERT_EQ(kv->valueEscaped, false);
  ASSERT_EQ(strncasecmp(kv->value, "pass\\,it ", 9), 0);
wmmhello's avatar
wmmhello 已提交
285 286

  // nchar
287
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 2);
wmmhello's avatar
wmmhello 已提交
288 289 290
  ASSERT_EQ(strncasecmp(kv->key, "cnch", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
291 292
  ASSERT_EQ(kv->length, 9);
  ASSERT_EQ(strncasecmp(kv->value, "ii\\=sd", 5), 0);
wmmhello's avatar
wmmhello 已提交
293 294

  // bool
295
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 3);
wmmhello's avatar
wmmhello 已提交
296 297 298
  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 已提交
299
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
300 301 302
  ASSERT_EQ(kv->i, false);

  // double
303
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 4);
wmmhello's avatar
wmmhello 已提交
304 305 306
  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 已提交
307
  ASSERT_EQ(kv->length, 8);
H
Hongze Cheng 已提交
308
  // ASSERT_EQ(kv->d, 4.31);
309
  printf("4.31 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
310 311

  // float
312
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 5);
313
  ASSERT_EQ(strncasecmp(kv->key, "cf64_", 5), 0);
wmmhello's avatar
wmmhello 已提交
314
  ASSERT_EQ(kv->keyLen, 5);
315 316
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_EQ(kv->length, 8);
H
Hongze Cheng 已提交
317
  // ASSERT_EQ(kv->f, 8.32);
318
  printf("8.32 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
319 320

  // float
321
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 6);
wmmhello's avatar
wmmhello 已提交
322 323 324 325
  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 已提交
326
  // ASSERT_EQ(kv->f, 8.23);
wmmhello's avatar
wmmhello 已提交
327
  printf("8.23 = kv->f:%f\n", kv->f);
wmmhello's avatar
wmmhello 已提交
328 329

  // tiny int
330
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 7);
wmmhello's avatar
wmmhello 已提交
331 332
  ASSERT_EQ(strncasecmp(kv->key, "ci8", 3), 0);
  ASSERT_EQ(kv->keyLen, 3);
wmmhello's avatar
wmmhello 已提交
333
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_TINYINT);
wmmhello's avatar
wmmhello 已提交
334
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
335
  ASSERT_EQ(kv->i, -34);
wmmhello's avatar
wmmhello 已提交
336 337

  // unsigned tiny int
338
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 8);
wmmhello's avatar
wmmhello 已提交
339 340 341 342 343 344 345
  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
346
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 9);
wmmhello's avatar
wmmhello 已提交
347 348 349 350 351 352 353
  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
354
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 10);
wmmhello's avatar
wmmhello 已提交
355 356 357 358 359 360 361
  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
362
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 11);
wmmhello's avatar
wmmhello 已提交
363 364 365 366 367 368 369
  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
370
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 12);
wmmhello's avatar
wmmhello 已提交
371 372 373 374 375 376 377
  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
378
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 13);
wmmhello's avatar
wmmhello 已提交
379
  ASSERT_EQ(strncasecmp(kv->key, "ci64", 4), 0);
wmmhello's avatar
wmmhello 已提交
380 381
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BIGINT);
wmmhello's avatar
wmmhello 已提交
382 383 384 385
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->i, -89238);

  // bigint
386
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 14);
wmmhello's avatar
wmmhello 已提交
387 388 389 390 391 392 393
  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
394
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 15);
wmmhello's avatar
wmmhello 已提交
395 396 397 398 399 400 401
  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
402
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 16);
wmmhello's avatar
wmmhello 已提交
403 404 405 406 407 408 409
  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
410
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 17);
wmmhello's avatar
wmmhello 已提交
411
  ASSERT_EQ(strncasecmp(kv->key, "cboolt", 6), 0);
wmmhello's avatar
wmmhello 已提交
412 413 414 415
  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 已提交
416 417

  // bool
418
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 18);
wmmhello's avatar
wmmhello 已提交
419 420 421
  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 已提交
422 423
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, false);
wmmhello's avatar
wmmhello 已提交
424

wmmhello's avatar
wmmhello 已提交
425
  // nchar
426
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 19);
wmmhello's avatar
wmmhello 已提交
427 428 429 430 431 432
  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);

433
  taosArrayDestroy(elements.colArray);
wmmhello's avatar
wmmhello 已提交
434
  taosMemoryFree(sql);
435
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
436 437
}

wmmhello's avatar
wmmhello 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
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 已提交
460

wmmhello's avatar
wmmhello 已提交
461
TEST(testCase, smlParseNumber_Test) {
H
Hongze Cheng 已提交
462 463
  SSmlKv     kv = {0};
  char       buf[64] = {0};
wmmhello's avatar
wmmhello 已提交
464 465 466 467 468 469
  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 已提交
470
  printf("res:%d,v:%f, %f\n", res, kv.d, HUGE_VAL);
wmmhello's avatar
wmmhello 已提交
471 472
}

473
TEST(testCase, smlParseTelnetLine_error_Test) {
474 475 476
  SSmlHandle *info = smlBuildSmlInfo(NULL);
  info->dataFormat = false;
  info->protocol = TSDB_SML_TELNET_PROTOCOL;
477 478
  ASSERT_NE(info, nullptr);

wmmhello's avatar
wmmhello 已提交
479
  const char *sql[] = {
480 481 482 483 484 485 486 487 488 489 490 491 492
      "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",
wmmhello's avatar
wmmhello 已提交
493 494 495
//      "sys.procs.running 1479496100 true host=web01",
//      "sys.procs.running 1479496100 \"binary\" host=web01",
//      "sys.procs.running 1479496100 L\"rfr\" host=web01",
496 497
      "sys.procs.running 1479496100 42 host=web01 cpu= ",
      "sys.procs.running 1479496100 42 host=web01 host",
wmmhello's avatar
wmmhello 已提交
498 499
      "sys.procs.running 1479496100 42 host=web01=er",
      "sys.procs.running 1479496100 42 host= web01",
500
  };
H
Hongze Cheng 已提交
501
  for (int i = 0; i < sizeof(sql) / sizeof(sql[0]); i++) {
502 503 504
    SSmlLineInfo elements = {0};
    int ret = smlParseTelnetString(info, (char*)sql[i], (char*)(sql[i] + strlen(sql[i])), &elements);
//    printf("i:%d\n", i);
505 506 507 508 509 510
    ASSERT_NE(ret, 0);
  }

  smlDestroyInfo(info);
}

wmmhello's avatar
wmmhello 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
TEST(testCase, smlParseTelnetLine_Test) {
  SSmlHandle *info = smlBuildSmlInfo(NULL);
  info->dataFormat = false;
  info->protocol = TSDB_SML_TELNET_PROTOCOL;
  ASSERT_NE(info, nullptr);

  const char *sql[] = {
      "twudyr 1626006833641 \"abcd`~!@#$%^&*()_-{[}]|:;<.>?lfjal\" id=twudyr_17102_17825 t0=t t1=127i8 t2=32767i16 t3=2147483647i32 t4=9223372036854775807i64 t5=11.12345f32 t6=22.123456789f64 t7=\"abcd`~!@#$%^&*()_-{[}]|:;<.>?lfjal\" t8=L\"abcd`~!@#$%^&*()_-{[}]|:;<.>?lfjal\"",
  };
  for (int i = 0; i < sizeof(sql) / sizeof(sql[0]); i++) {
    SSmlLineInfo elements = {0};
    int ret = smlParseTelnetString(info, (char*)sql[i], (char*)(sql[i] + strlen(sql[i])), &elements);
//    printf("i:%d\n", i);
    ASSERT_EQ(ret, 0);
  }

  smlDestroyInfo(info);
}

wmmhello's avatar
wmmhello 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
//TEST(testCase, smlParseTelnetLine_diff_json_type2_Test) {
//  SSmlHandle *info = smlBuildSmlInfo(NULL);
//  info->protocol = TSDB_SML_JSON_PROTOCOL;
//  ASSERT_NE(info, nullptr);
//
//  const char *sql[] = {
//      "[{\"metric\":\"sys.cpu.nice\",\"timestamp\": 1346846400,\"value\": 18,\"tags\": {\"host\": \"lga\"}},{\"metric\": \"sys.sdfa\",\"timestamp\": 1346846400,\"value\": \"18\",\"tags\": {\"host\": 8932}},]",
//  };
//  for (int i = 0; i < sizeof(sql) / sizeof(sql[0]); i++) {
//    char *dataPointStart = (char *)sql[i];
//    int8_t offset[4] = {0};
//    while (1) {
//      SSmlLineInfo elements = {0};
//      if(offset[0] == 0){
//        smlJsonParseObjFirst(&dataPointStart, &elements, offset);
//      }else{
//        smlJsonParseObj(&dataPointStart, &elements, offset);
//      }
//      if(*dataPointStart == '\0') break;
//
//      SArray *tags = smlJsonParseTags(elements.tags, elements.tags + elements.tagsLen);
//      size_t num = taosArrayGetSize(tags);
//      ASSERT_EQ(num, 1);
//
//      taosArrayDestroy(tags);
//    }
//  }
//  smlDestroyInfo(info);
//}
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

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