smlTest.cpp 20.1 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);
wmmhello's avatar
wmmhello 已提交
53 54
  ASSERT_EQ(elements.measureLen, strlen(",st"));
  ASSERT_EQ(elements.measureTagsLen, strlen(",st,t1=3,t2=4,t3=t3"));
55 56 57 58 59 60 61 62 63

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

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

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

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

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

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

  // 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(info, sql, sql + strlen(sql), &elements);
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
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));
114 115
  taosArrayDestroy(elements.colArray);
  elements.colArray = NULL;
116 117

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

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

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

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

148 149
}

wmmhello's avatar
wmmhello 已提交
150
TEST(testCase, smlParseCols_Error_Test) {
151 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 208 209 210 211
  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 已提交
212
  for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
wmmhello's avatar
wmmhello 已提交
213 214 215 216
    char       msg[256] = {0};
    SSmlMsgBuf msgBuf;
    msgBuf.buf = msg;
    msgBuf.len = 256;
wmmhello's avatar
wmmhello 已提交
217
    int32_t len = strlen(data[i]);
H
Hongze Cheng 已提交
218
    char   *sql = (char *)taosMemoryCalloc(256, 1);
wmmhello's avatar
wmmhello 已提交
219
    memcpy(sql, data[i], len + 1);
220 221 222
    SSmlLineInfo elements = {0};
    int32_t ret = smlParseInfluxString(info, sql, sql + len, &elements);
//    printf("i:%d\n", i);
wmmhello's avatar
wmmhello 已提交
223
    ASSERT_NE(ret, TSDB_CODE_SUCCESS);
wmmhello's avatar
wmmhello 已提交
224
    taosMemoryFree(sql);
225
    taosArrayDestroy(elements.colArray);
wmmhello's avatar
wmmhello 已提交
226
  }
227
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
228 229
}

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

H
Hongze Cheng 已提交
241
  const char *data =
242
      "st,t=1 cb\\=in=\"pass\\,it "
H
Hongze Cheng 已提交
243 244
      "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,"
245
      "cboolf=f,cnch_=l\"iuwq\" 1626006833639000000";
wmmhello's avatar
wmmhello 已提交
246
  int32_t len = strlen(data);
H
Hongze Cheng 已提交
247
  char   *sql = (char *)taosMemoryCalloc(1024, 1);
wmmhello's avatar
wmmhello 已提交
248
  memcpy(sql, data, len + 1);
249
  int32_t ret = smlParseInfluxString(info, sql, sql + len, &elements);
wmmhello's avatar
wmmhello 已提交
250
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
251 252
  int32_t size = taosArrayGetSize(elements.colArray);
  ASSERT_EQ(size, 20);
wmmhello's avatar
wmmhello 已提交
253 254

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

  // nchar
263
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 2);
wmmhello's avatar
wmmhello 已提交
264 265 266
  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 已提交
267
  ASSERT_EQ(kv->length, 8);
wmmhello's avatar
wmmhello 已提交
268
  ASSERT_EQ(strncasecmp(kv->value, "ii=sd", 5), 0);
wmmhello's avatar
wmmhello 已提交
269 270

  // bool
271
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 3);
wmmhello's avatar
wmmhello 已提交
272 273 274
  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 已提交
275
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
276 277 278
  ASSERT_EQ(kv->i, false);

  // double
279
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 4);
wmmhello's avatar
wmmhello 已提交
280 281 282
  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 已提交
283
  ASSERT_EQ(kv->length, 8);
H
Hongze Cheng 已提交
284
  // ASSERT_EQ(kv->d, 4.31);
285
  printf("4.31 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
286 287

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

  // float
297
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 6);
wmmhello's avatar
wmmhello 已提交
298 299 300 301
  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 已提交
302
  // ASSERT_EQ(kv->f, 8.23);
wmmhello's avatar
wmmhello 已提交
303
  printf("8.23 = kv->f:%f\n", kv->f);
wmmhello's avatar
wmmhello 已提交
304 305

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

  // unsigned tiny int
314
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 8);
wmmhello's avatar
wmmhello 已提交
315 316 317 318 319 320 321
  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
322
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 9);
wmmhello's avatar
wmmhello 已提交
323 324 325 326 327 328 329
  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
330
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 10);
wmmhello's avatar
wmmhello 已提交
331 332 333 334 335 336 337
  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
338
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 11);
wmmhello's avatar
wmmhello 已提交
339 340 341 342 343 344 345
  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
346
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 12);
wmmhello's avatar
wmmhello 已提交
347 348 349 350 351 352 353
  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
354
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 13);
wmmhello's avatar
wmmhello 已提交
355
  ASSERT_EQ(strncasecmp(kv->key, "ci64", 4), 0);
wmmhello's avatar
wmmhello 已提交
356 357
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BIGINT);
wmmhello's avatar
wmmhello 已提交
358 359 360 361
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->i, -89238);

  // bigint
362
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 14);
wmmhello's avatar
wmmhello 已提交
363 364 365 366 367 368 369
  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
370
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 15);
wmmhello's avatar
wmmhello 已提交
371 372 373 374 375 376 377
  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
378
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 16);
wmmhello's avatar
wmmhello 已提交
379 380 381 382 383 384 385
  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
386
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 17);
wmmhello's avatar
wmmhello 已提交
387
  ASSERT_EQ(strncasecmp(kv->key, "cboolt", 6), 0);
wmmhello's avatar
wmmhello 已提交
388 389 390 391
  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 已提交
392 393

  // bool
394
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 18);
wmmhello's avatar
wmmhello 已提交
395 396 397
  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 已提交
398 399
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, false);
wmmhello's avatar
wmmhello 已提交
400

wmmhello's avatar
wmmhello 已提交
401
  // nchar
402
  kv = (SSmlKv *)taosArrayGet(elements.colArray, 19);
wmmhello's avatar
wmmhello 已提交
403 404 405 406 407 408
  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);

409
  taosArrayDestroy(elements.colArray);
wmmhello's avatar
wmmhello 已提交
410
  taosMemoryFree(sql);
411
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
412 413
}

wmmhello's avatar
wmmhello 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
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 已提交
436

wmmhello's avatar
wmmhello 已提交
437
TEST(testCase, smlParseNumber_Test) {
H
Hongze Cheng 已提交
438 439
  SSmlKv     kv = {0};
  char       buf[64] = {0};
wmmhello's avatar
wmmhello 已提交
440 441 442 443 444 445
  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 已提交
446
  printf("res:%d,v:%f, %f\n", res, kv.d, HUGE_VAL);
wmmhello's avatar
wmmhello 已提交
447 448
}

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

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

  smlDestroyInfo(info);
}

wmmhello's avatar
wmmhello 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
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 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
//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);
//}
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

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