unitTest.cpp 23.5 KB
Newer Older
S
Shengliang Guan 已提交
1
#include "os.h"
H
hjxilinx 已提交
2 3 4 5 6 7 8
#include <gtest/gtest.h>
#include <cassert>
#include <iostream>

#include "taos.h"
#include "tsdb.h"

S
Shengliang Guan 已提交
9 10 11 12 13 14
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"

H
hjxilinx 已提交
15 16 17 18 19 20 21
#include "../../client/inc/tscUtil.h"
#include "tutil.h"
#include "tvariant.h"
#include "ttokendef.h"

namespace {
int32_t testValidateName(char* name) {
H
Haojun Liao 已提交
22
  SStrToken token = {0};
H
hjxilinx 已提交
23 24 25 26
  token.z = name;
  token.n = strlen(name);
  token.type = 0;

27
  tGetToken(name, &token.type);
H
hjxilinx 已提交
28 29 30 31 32
  return tscValidateName(&token);
}
}

static void _init_tvariant_bool(tVariant* t) {
H
Haojun Liao 已提交
33
  t->i64 = TSDB_FALSE;
H
hjxilinx 已提交
34 35 36 37
  t->nType = TSDB_DATA_TYPE_BOOL;
}

static void _init_tvariant_tinyint(tVariant* t) {
H
Haojun Liao 已提交
38
  t->i64 = -27;
H
hjxilinx 已提交
39 40 41 42
  t->nType = TSDB_DATA_TYPE_TINYINT;
}

static void _init_tvariant_int(tVariant* t) {
H
Haojun Liao 已提交
43
  t->i64 = -23997659;
H
hjxilinx 已提交
44 45 46 47
  t->nType = TSDB_DATA_TYPE_INT;
}

static void _init_tvariant_bigint(tVariant* t) {
H
Haojun Liao 已提交
48
  t->i64 = -3333333333333;
H
hjxilinx 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  t->nType = TSDB_DATA_TYPE_BIGINT;
}

static void _init_tvariant_float(tVariant* t) {
  t->dKey = -8991212199.8987878776;
  t->nType = TSDB_DATA_TYPE_FLOAT;
}

static void _init_tvariant_binary(tVariant* t) {
  tVariantDestroy(t);

  t->pz = (char*)calloc(1, 20);  //"2e3");
  t->nType = TSDB_DATA_TYPE_BINARY;
  strcpy(t->pz, "2e5");
  t->nLen = strlen(t->pz);
}

static void _init_tvariant_nchar(tVariant* t) {
  tVariantDestroy(t);

  t->wpz = (wchar_t*)calloc(1, 20 * TSDB_NCHAR_SIZE);
  t->nType = TSDB_DATA_TYPE_NCHAR;
  wcscpy(t->wpz, L"-2000000.8765");
H
[TD-92]  
Hui Li 已提交
72
  t->nLen = twcslen(t->wpz);
H
hjxilinx 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

/* test validate the names for table/database */
TEST(testCase, db_table_name) {

  char t01[] = "abc";
  EXPECT_EQ(testValidateName(t01), TSDB_CODE_SUCCESS);

  char t02[] = "'abc'";
  EXPECT_EQ(testValidateName(t02), TSDB_CODE_SUCCESS);

  char t1[] = "abc.def";
  EXPECT_EQ(testValidateName(t1), TSDB_CODE_SUCCESS);
  printf("%s\n", t1);

  char t2[] = "'abc.def'";
  EXPECT_EQ(testValidateName(t2), TSDB_CODE_SUCCESS);
  printf("%s\n", t2);

  char t3[] = "'abc'.def";
  EXPECT_EQ(testValidateName(t3), TSDB_CODE_SUCCESS);
  printf("%s\n", t3);

  char t4[] = "'abc'.'def'";
  EXPECT_EQ(testValidateName(t4), TSDB_CODE_SUCCESS);

  char t5[] = "table.'def'";
105
  EXPECT_EQ(testValidateName(t5), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
106 107

  char t6[] = "'table'.'def'";
108
  EXPECT_EQ(testValidateName(t6), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
109 110 111 112 113 114

  char t7[] = "'_ab1234'.'def'";
  EXPECT_EQ(testValidateName(t7), TSDB_CODE_SUCCESS);
  printf("%s\n", t7);

  char t8[] = "'_ab&^%1234'.'def'";
115
  EXPECT_EQ(testValidateName(t8), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
116 117

  char t9[] = "'_123'.'gtest中文'";
118
  EXPECT_EQ(testValidateName(t9), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
119 120

  char t10[] = "abc.'gtest中文'";
121
  EXPECT_EQ(testValidateName(t10), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
122 123

  char t10_1[] = "abc.'中文gtest'";
124
  EXPECT_EQ(testValidateName(t10_1), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
125 126
  
  char t11[] = "'192.168.0.1'.abc";
127
  EXPECT_EQ(testValidateName(t11), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
128 129

  char t12[] = "192.168.0.1.abc";
130
  EXPECT_EQ(testValidateName(t12), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
131 132

  char t13[] = "abc.";
133
  EXPECT_EQ(testValidateName(t13), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
134 135

  char t14[] = ".abc";
136
  EXPECT_EQ(testValidateName(t14), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
137 138

  char t15[] = ".'abc'";
139
  EXPECT_EQ(testValidateName(t15), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
140 141

  char t16[] = ".abc'";
142
  EXPECT_EQ(testValidateName(t16), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
143 144

  char t17[] = "123a.\"abc\"";
145
  EXPECT_EQ(testValidateName(t17), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
146 147 148 149 150 151 152
  printf("%s\n", t17);

  char t18[] = "a.\"abc\"";
  EXPECT_EQ(testValidateName(t18), TSDB_CODE_SUCCESS);
  printf("%s\n", t18);

  char t19[] = "'_ab1234'.'def'.'ab123'";
153
  EXPECT_EQ(testValidateName(t19), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
154 155

  char t20[] = "'_ab1234*&^'";
156
  EXPECT_EQ(testValidateName(t20), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
157 158

  char t21[] = "'1234_abc'";
159
  EXPECT_EQ(testValidateName(t21), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172


  // =======Containing capital letters=================
  char t30[] = "ABC";
  EXPECT_EQ(testValidateName(t30), TSDB_CODE_SUCCESS);

  char t31[] = "'ABC'";
  EXPECT_EQ(testValidateName(t31), TSDB_CODE_SUCCESS);

  char t32[] = "ABC.def";
  EXPECT_EQ(testValidateName(t32), TSDB_CODE_SUCCESS);

  char t33[] = "'ABC.def";
173
  EXPECT_EQ(testValidateName(t33), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
174 175

  char t33_0[] = "abc.DEF'";
176
  EXPECT_EQ(testValidateName(t33_0), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  
  char t34[] = "'ABC.def'";
  //int32_t tmp0 = testValidateName(t34);
  EXPECT_EQ(testValidateName(t34), TSDB_CODE_SUCCESS);

  char t35[] = "'ABC'.def";
  EXPECT_EQ(testValidateName(t35), TSDB_CODE_SUCCESS);

  char t36[] = "'ABC'.'DEF'";
  EXPECT_EQ(testValidateName(t36), TSDB_CODE_SUCCESS);

  char t37[] = "abc.'DEF'";
  EXPECT_EQ(testValidateName(t37), TSDB_CODE_SUCCESS);

  char t37_1[] = "abc.'_123DEF'";
  EXPECT_EQ(testValidateName(t37_1), TSDB_CODE_SUCCESS);

  char t38[] = "'abc'.'DEF'";
  EXPECT_EQ(testValidateName(t38), TSDB_CODE_SUCCESS);

  // do not use key words 
  char t39[] = "table.'DEF'";
199
  EXPECT_EQ(testValidateName(t39), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
200 201

  char t40[] = "'table'.'DEF'";
202
  EXPECT_EQ(testValidateName(t40), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
203 204 205 206 207

  char t41[] = "'_abXYZ1234'.'deFF'";
  EXPECT_EQ(testValidateName(t41), TSDB_CODE_SUCCESS);

  char t42[] = "'_abDEF&^%1234'.'DIef'";
208
  EXPECT_EQ(testValidateName(t42), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
209 210

  char t43[] = "'_123'.'Gtest中文'";
211
  EXPECT_EQ(testValidateName(t43), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
212 213

  char t44[] = "'aABC'.'Gtest中文'";
214
  EXPECT_EQ(testValidateName(t44), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
215 216
  
  char t45[] = "'ABC'.";
217
  EXPECT_EQ(testValidateName(t45), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
218 219

  char t46[] = ".'ABC'";
220
  EXPECT_EQ(testValidateName(t46), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
221 222 223 224 225 226

  char t47[] = "a.\"aTWc\"";
  EXPECT_EQ(testValidateName(t47), TSDB_CODE_SUCCESS);

 // ================has space =================
  char t60[] = " ABC ";
227
  EXPECT_EQ(testValidateName(t60), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
228 229

  char t60_1[] = "   ABC ";
230
  EXPECT_EQ(testValidateName(t60_1), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
231 232

  char t61[] = "' ABC '";
233
  EXPECT_EQ(testValidateName(t61), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
234 235

  char t61_1[] = "'  ABC '";
236
  EXPECT_EQ(testValidateName(t61_1), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
237 238

  char t62[] = " ABC . def ";
239
  EXPECT_EQ(testValidateName(t62), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
240 241

  char t63[] = "' ABC . def ";
242
  EXPECT_EQ(testValidateName(t63), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
243 244

  char t63_0[] = "  abc . DEF ' ";
245
  EXPECT_EQ(testValidateName(t63_0), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
246 247 248
  
  char t64[] = " '  ABC .  def ' ";
  //int32_t tmp1 = testValidateName(t64);
249
  EXPECT_EQ(testValidateName(t64), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
250 251

  char t65[] = " ' ABC  '. def ";
252
  EXPECT_EQ(testValidateName(t65), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
253 254

  char t66[] = "' ABC '.'  DEF '";
255
  EXPECT_EQ(testValidateName(t66), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
256 257

  char t67[] = "abc . '  DEF  '";
258
  EXPECT_EQ(testValidateName(t67), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
259 260

  char t68[] = "'  abc '.'   DEF '";
261
  EXPECT_EQ(testValidateName(t68), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
262 263 264

  // do not use key words 
  char t69[] = "table.'DEF'";
265
  EXPECT_EQ(testValidateName(t69), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
266 267

  char t70[] = "'table'.'DEF'";
268
  EXPECT_EQ(testValidateName(t70), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
269 270

  char t71[] = "'_abXYZ1234  '.' deFF  '";
271
  EXPECT_EQ(testValidateName(t71), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
272 273

  char t72[] = "'_abDEF&^%1234'.'  DIef'";
274
  EXPECT_EQ(testValidateName(t72), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
275 276

  char t73[] = "'_123'.'  Gtest中文'";
277
  EXPECT_EQ(testValidateName(t73), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
278 279

  char t74[] = "' aABC'.'Gtest中文'";
280
  EXPECT_EQ(testValidateName(t74), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
281 282
  
  char t75[] = "' ABC '.";
283
  EXPECT_EQ(testValidateName(t75), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
284 285

  char t76[] = ".' ABC'";
286
  EXPECT_EQ(testValidateName(t76), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
287 288

  char t77[] = " a . \"aTWc\" ";
289
  EXPECT_EQ(testValidateName(t77), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
290 291

  char t78[] = "  a.\"aTWc  \"";
292
  EXPECT_EQ(testValidateName(t78), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
293 294 295 296 297


  // ===============muti string by space ===================
  // There's no such case.
  //char t160[] = "A BC";
298
  //EXPECT_EQ(testValidateName(t160), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
299 300 301 302
  //printf("end:%s\n", t160);

  // There's no such case.
  //char t161[] = "' A BC '";
303
  //EXPECT_EQ(testValidateName(t161), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
304 305

  char t162[] = " AB C . de f ";
306
  EXPECT_EQ(testValidateName(t162), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
307 308

  char t163[] = "' AB C . de f ";
309
  EXPECT_EQ(testValidateName(t163), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
310 311

  char t163_0[] = "  ab c . DE F ' ";
312
  EXPECT_EQ(testValidateName(t163_0), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
313 314 315
  
  char t164[] = " '  AB C .  de f ' ";
  //int32_t tmp2 = testValidateName(t164);
316
  EXPECT_EQ(testValidateName(t164), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
317 318

  char t165[] = " ' A BC  '. de f ";
319
  EXPECT_EQ(testValidateName(t165), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
320 321

  char t166[] = "' AB C '.'  DE  F '";
322
  EXPECT_EQ(testValidateName(t166), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
323 324

  char t167[] = "ab  c . '  D  EF  '";
325
  EXPECT_EQ(testValidateName(t167), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
326 327

  char t168[] = "'  a bc '.'   DE  F '";
328
  EXPECT_EQ(testValidateName(t168), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
329 330 331 332 333 334 335 336 337 338 339
  
}

/* test parse time function */
TEST(testCase, parse_time) {
  taos_options(TSDB_OPTION_TIMEZONE, "GMT-8");
  char t1[] = "2018-1-1 1:1:1.952798";
  char t13[] = "1970-1-1 0:0:0";

  int64_t time = 0, time1 = 0;

dengyihao's avatar
dengyihao 已提交
340
  taosParseTime(t1, &time, strlen(t1), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
341 342
  EXPECT_EQ(time, 1514739661952);

dengyihao's avatar
dengyihao 已提交
343
  taosParseTime(t13, &time, strlen(t13), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
344 345 346
  EXPECT_EQ(time, timezone * MILLISECOND_PER_SECOND);

  char t2[] = "2018-1-1T1:1:1.952Z";
dengyihao's avatar
dengyihao 已提交
347
  taosParseTime(t2, &time, strlen(t2), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
348 349 350 351

  EXPECT_EQ(time, 1514739661952 + 28800000);

  char t3[] = "2018-1-1 1:01:01.952";
dengyihao's avatar
dengyihao 已提交
352
  taosParseTime(t3, &time, strlen(t3), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
353 354 355 356 357 358 359 360
  EXPECT_EQ(time, 1514739661952);

  char t4[] = "2018-1-1 1:01:01.9";
  char t5[] = "2018-1-1 1:01:1.900";
  char t6[] = "2018-01-01 1:1:1.90";
  char t7[] = "2018-01-01 01:01:01.9";
  char t8[] = "2018-01-01 01:01:01.9007865";

dengyihao's avatar
dengyihao 已提交
361 362
  taosParseTime(t4, &time, strlen(t4), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t5, &time1, strlen(t5), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
363 364
  EXPECT_EQ(time, time1);

dengyihao's avatar
dengyihao 已提交
365 366
  taosParseTime(t4, &time, strlen(t4), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t6, &time1, strlen(t6), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
367 368
  EXPECT_EQ(time, time1);

dengyihao's avatar
dengyihao 已提交
369 370
  taosParseTime(t4, &time, strlen(t4), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t7, &time1, strlen(t7), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
371 372
  EXPECT_EQ(time, time1);

dengyihao's avatar
dengyihao 已提交
373 374
  taosParseTime(t5, &time, strlen(t5), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t8, &time1, strlen(t8), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
375 376 377 378
  EXPECT_EQ(time, time1);

  char t9[] = "2017-4-3 1:1:2.980";
  char t10[] = "2017-4-3T2:1:2.98+9:00";
dengyihao's avatar
dengyihao 已提交
379 380
  taosParseTime(t9, &time, strlen(t9), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t10, &time1, strlen(t10), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
381 382 383
  EXPECT_EQ(time, time1);

  char t11[] = "2017-4-3T2:1:2.98+09:00";
dengyihao's avatar
dengyihao 已提交
384 385
  taosParseTime(t11, &time, strlen(t11), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t10, &time1, strlen(t10), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
386 387 388
  EXPECT_EQ(time, time1);

  char t12[] = "2017-4-3T2:1:2.98+0900";
dengyihao's avatar
dengyihao 已提交
389 390
  taosParseTime(t11, &time, strlen(t11), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t12, &time1, strlen(t12), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
391 392 393
  EXPECT_EQ(time, time1);

  taos_options(TSDB_OPTION_TIMEZONE, "UTC");
dengyihao's avatar
dengyihao 已提交
394
  taosParseTime(t13, &time, strlen(t13), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
395 396 397 398
  EXPECT_EQ(time, 0);

  taos_options(TSDB_OPTION_TIMEZONE, "Asia/Shanghai");
  char t14[] = "1970-1-1T0:0:0Z";
dengyihao's avatar
dengyihao 已提交
399
  taosParseTime(t14, &time, strlen(t14), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
400 401 402
  EXPECT_EQ(time, 0);

  char t40[] = "1970-1-1 0:0:0.999999999";
dengyihao's avatar
dengyihao 已提交
403
  taosParseTime(t40, &time, strlen(t40), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
404 405 406
  EXPECT_EQ(time, 999 + timezone * MILLISECOND_PER_SECOND);

  char t41[] = "1997-1-1 0:0:0.999999999";
dengyihao's avatar
dengyihao 已提交
407
  taosParseTime(t41, &time, strlen(t41), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
408 409 410 411
  EXPECT_EQ(time, 852048000999);

  int64_t k = timezone;
  char    t42[] = "1997-1-1T0:0:0.999999999Z";
dengyihao's avatar
dengyihao 已提交
412
  taosParseTime(t42, &time, strlen(t42), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
413 414 415 416 417
  EXPECT_EQ(time, 852048000999 - timezone * MILLISECOND_PER_SECOND);

  ////////////////////////////////////////////////////////////////////
  // illegal timestamp format
  char t15[] = "2017-12-33 0:0:0";
dengyihao's avatar
dengyihao 已提交
418
  EXPECT_EQ(taosParseTime(t15, &time, strlen(t15), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
419 420

  char t16[] = "2017-12-31 99:0:0";
dengyihao's avatar
dengyihao 已提交
421
  EXPECT_EQ(taosParseTime(t16, &time, strlen(t16), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
422 423

  char t17[] = "2017-12-31T9:0:0";
dengyihao's avatar
dengyihao 已提交
424
  EXPECT_EQ(taosParseTime(t17, &time, strlen(t17), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
425 426

  char t18[] = "2017-12-31T9:0:0.Z";
dengyihao's avatar
dengyihao 已提交
427
  EXPECT_EQ(taosParseTime(t18, &time, strlen(t18), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
428 429

  char t19[] = "2017-12-31 9:0:0.-1";
dengyihao's avatar
dengyihao 已提交
430
  EXPECT_EQ(taosParseTime(t19, &time, strlen(t19), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
431 432

  char t20[] = "2017-12-31 9:0:0.1+12:99";
dengyihao's avatar
dengyihao 已提交
433
  EXPECT_EQ(taosParseTime(t20, &time, strlen(t20), TSDB_TIME_PRECISION_MILLI, 0), 0);
H
hjxilinx 已提交
434 435 436
  EXPECT_EQ(time, 1514682000100);

  char t21[] = "2017-12-31T9:0:0.1+12:99";
dengyihao's avatar
dengyihao 已提交
437
  EXPECT_EQ(taosParseTime(t21, &time, strlen(t21), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
438 439

  char t22[] = "2017-12-31 9:0:0.1+13:1";
dengyihao's avatar
dengyihao 已提交
440
  EXPECT_EQ(taosParseTime(t22, &time, strlen(t22), TSDB_TIME_PRECISION_MILLI, 0), 0);
H
hjxilinx 已提交
441 442

  char t23[] = "2017-12-31T9:0:0.1+13:1";
dengyihao's avatar
dengyihao 已提交
443
  EXPECT_EQ(taosParseTime(t23, &time, strlen(t23), TSDB_TIME_PRECISION_MILLI, 0), 0);
H
hjxilinx 已提交
444 445 446 447 448 449 450 451
}

TEST(testCase, tvariant_convert) {
  // 1. bool data to all other data types
  tVariant t = {0};
  _init_tvariant_bool(&t);

  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
H
Haojun Liao 已提交
452
  EXPECT_EQ(t.i64, 0);
H
hjxilinx 已提交
453 454 455

  _init_tvariant_bool(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_TINYINT), 0);
H
Haojun Liao 已提交
456
  EXPECT_EQ(t.i64, 0);
H
hjxilinx 已提交
457 458 459

  _init_tvariant_bool(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_SMALLINT), 0);
H
Haojun Liao 已提交
460
  EXPECT_EQ(t.i64, 0);
H
hjxilinx 已提交
461 462 463

  _init_tvariant_bool(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
H
Haojun Liao 已提交
464
  EXPECT_EQ(t.i64, 0);
H
hjxilinx 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

  _init_tvariant_bool(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_EQ(t.dKey, 0);

  _init_tvariant_bool(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_EQ(t.dKey, 0);

  _init_tvariant_bool(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "FALSE");
  tVariantDestroy(&t);

  _init_tvariant_bool(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"FALSE");
  tVariantDestroy(&t);

  // 2. tinyint to other data types
  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
H
Haojun Liao 已提交
487
  EXPECT_EQ(t.i64, 1);
H
hjxilinx 已提交
488 489 490

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_TINYINT), 0);
H
Haojun Liao 已提交
491
  EXPECT_EQ(t.i64, -27);
H
hjxilinx 已提交
492 493 494

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_SMALLINT), 0);
H
Haojun Liao 已提交
495
  EXPECT_EQ(t.i64, -27);
H
hjxilinx 已提交
496 497 498

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_INT), 0);
H
Haojun Liao 已提交
499
  EXPECT_EQ(t.i64, -27);
H
hjxilinx 已提交
500 501 502

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
H
Haojun Liao 已提交
503
  EXPECT_EQ(t.i64, -27);
H
hjxilinx 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_EQ(t.dKey, -27);

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_EQ(t.dKey, -27);

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "-27");
  tVariantDestroy(&t);

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"-27");
  tVariantDestroy(&t);

  // 3. int to other data
  // types//////////////////////////////////////////////////////////////////
  _init_tvariant_int(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
H
Haojun Liao 已提交
527
  EXPECT_EQ(t.i64, 1);
H
hjxilinx 已提交
528 529 530 531 532 533 534 535 536

  _init_tvariant_int(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_TINYINT), 0);

  _init_tvariant_int(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_SMALLINT), 0);

  _init_tvariant_int(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_INT), 0);
H
Haojun Liao 已提交
537
  EXPECT_EQ(t.i64, -23997659);
H
hjxilinx 已提交
538 539 540

  _init_tvariant_int(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
H
Haojun Liao 已提交
541
  EXPECT_EQ(t.i64, -23997659);
H
hjxilinx 已提交
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564

  _init_tvariant_int(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_EQ(t.dKey, -23997659);

  _init_tvariant_int(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_EQ(t.dKey, -23997659);

  _init_tvariant_int(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "-23997659");
  tVariantDestroy(&t);

  _init_tvariant_int(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"-23997659");
  tVariantDestroy(&t);

  // 4. bigint to other data
  // type//////////////////////////////////////////////////////////////////////////////
  _init_tvariant_bigint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
H
Haojun Liao 已提交
565
  EXPECT_EQ(t.i64, 1);
H
hjxilinx 已提交
566 567 568 569 570 571 572 573 574 575 576 577

  _init_tvariant_bigint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_TINYINT), 0);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_SMALLINT), 0);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_INT), 0);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
H
Haojun Liao 已提交
578
  EXPECT_EQ(t.i64, -3333333333333);
H
hjxilinx 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

  _init_tvariant_bigint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_EQ(t.dKey, -3333333333333);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_EQ(t.dKey, -3333333333333);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "-3333333333333");
  tVariantDestroy(&t);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"-3333333333333");
  tVariantDestroy(&t);

  // 5. float to other data
  // types////////////////////////////////////////////////////////////////////////
  _init_tvariant_float(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
H
Haojun Liao 已提交
602
  EXPECT_EQ(t.i64, 1);
H
hjxilinx 已提交
603 604 605

  _init_tvariant_float(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
H
Haojun Liao 已提交
606
  EXPECT_EQ(t.i64, -8991212199);
H
hjxilinx 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

  _init_tvariant_float(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_DOUBLE_EQ(t.dKey, -8991212199.8987885);

  _init_tvariant_float(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_DOUBLE_EQ(t.dKey, -8991212199.8987885);

  _init_tvariant_float(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "-8991212199.898788");
  tVariantDestroy(&t);

  _init_tvariant_float(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"-8991212199.898788");
  tVariantDestroy(&t);

  // 6. binary to other data types
  // //////////////////////////////////////////////////////////////////
  t.pz = "true";
  t.nLen = strlen(t.pz);
  t.nType = TSDB_DATA_TYPE_BINARY;
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
H
Haojun Liao 已提交
632
  EXPECT_EQ(t.i64, 1);
H
hjxilinx 已提交
633 634 635 636 637 638

  _init_tvariant_binary(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), -1);

  _init_tvariant_binary(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
H
Haojun Liao 已提交
639
  EXPECT_EQ(t.i64, 200000);
H
hjxilinx 已提交
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

  _init_tvariant_binary(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_DOUBLE_EQ(t.dKey, 200000);

  _init_tvariant_binary(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_DOUBLE_EQ(t.dKey, 200000);

  _init_tvariant_binary(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "2e5");
  tVariantDestroy(&t);

  _init_tvariant_binary(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"2e5");
  tVariantDestroy(&t);

  // 7. nchar to other data types
  // //////////////////////////////////////////////////////////////////
  t.wpz = L"FALSE";
  t.nLen = wcslen(t.wpz);
  t.nType = TSDB_DATA_TYPE_NCHAR;
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
H
Haojun Liao 已提交
665
  EXPECT_EQ(t.i64, 0);
H
hjxilinx 已提交
666 667 668 669 670 671

  _init_tvariant_nchar(&t);
  EXPECT_LE(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);

  _init_tvariant_nchar(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
H
Haojun Liao 已提交
672
  EXPECT_EQ(t.i64, -2000000);
H
hjxilinx 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

  _init_tvariant_nchar(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_DOUBLE_EQ(t.dKey, -2000000.8765);

  _init_tvariant_nchar(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_DOUBLE_EQ(t.dKey, -2000000.8765);

  _init_tvariant_nchar(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "-2000000.8765");
  tVariantDestroy(&t);

  _init_tvariant_nchar(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"-2000000.8765");
  tVariantDestroy(&t);
}

TEST(testCase, tGetToken_Test) {
  char* s = ".123 ";
  uint32_t type = 0;

697
  int32_t len = tGetToken(s, &type);
H
hjxilinx 已提交
698 699 700 701
  EXPECT_EQ(type, TK_FLOAT);
  EXPECT_EQ(len, strlen(s) - 1);

  char s1[] = "1.123e10 ";
702
  len = tGetToken(s1, &type);
H
hjxilinx 已提交
703 704 705 706
  EXPECT_EQ(type, TK_FLOAT);
  EXPECT_EQ(len, strlen(s1) - 1);

  char s4[] = "0xff ";
707
  len = tGetToken(s4, &type);
H
hjxilinx 已提交
708 709 710 711 712
  EXPECT_EQ(type, TK_HEX);
  EXPECT_EQ(len, strlen(s4) - 1);

  // invalid data type
  char s2[] = "e10 ";
713
  len = tGetToken(s2, &type);
H
hjxilinx 已提交
714 715 716
  EXPECT_FALSE(type == TK_FLOAT);

  char s3[] = "1.1.1.1";
717
  len = tGetToken(s3, &type);
H
hjxilinx 已提交
718 719 720 721
  EXPECT_EQ(type, TK_IPTOKEN);
  EXPECT_EQ(len, strlen(s3));

  char s5[] = "0x ";
722
  len = tGetToken(s5, &type);
H
hjxilinx 已提交
723 724 725
  EXPECT_FALSE(type == TK_HEX);
}

H
Haojun Liao 已提交
726 727
static SStrToken createStrToken(char* s) {
  SStrToken t = {0};//.type = TK_STRING, .z = s, .n = strlen(s)};
H
hjxilinx 已提交
728 729 730 731 732 733 734 735
  t.type = TK_STRING;
  t.z = s;
  t.n = strlen(s);

  return t;
}

TEST(testCase, isValidNumber_test) {
H
Haojun Liao 已提交
736
  SStrToken t1 = createStrToken("123abc");
H
hjxilinx 已提交
737

H
Haojun Liao 已提交
738
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);
H
hjxilinx 已提交
739 740

  t1 = createStrToken("0xabc");
H
Haojun Liao 已提交
741
  EXPECT_EQ(tGetNumericStringType(&t1), TK_HEX);
H
hjxilinx 已提交
742 743

  t1 = createStrToken("0b11101");
H
Haojun Liao 已提交
744
  EXPECT_EQ(tGetNumericStringType(&t1), TK_BIN);
H
hjxilinx 已提交
745 746

  t1 = createStrToken(".134abc");
H
Haojun Liao 已提交
747
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);
H
hjxilinx 已提交
748 749

  t1 = createStrToken("1e1 ");
H
Haojun Liao 已提交
750
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);
H
hjxilinx 已提交
751 752

  t1 = createStrToken("1+2");
H
Haojun Liao 已提交
753
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);
H
hjxilinx 已提交
754 755

  t1 = createStrToken("-0x123");
H
Haojun Liao 已提交
756
  EXPECT_EQ(tGetNumericStringType(&t1), TK_HEX);
H
hjxilinx 已提交
757 758

  t1 = createStrToken("-1");
H
Haojun Liao 已提交
759
  EXPECT_EQ(tGetNumericStringType(&t1), TK_INTEGER);
H
hjxilinx 已提交
760 761

  t1 = createStrToken("-0b1110");
H
Haojun Liao 已提交
762
  EXPECT_EQ(tGetNumericStringType(&t1), TK_BIN);
H
hjxilinx 已提交
763 764

  t1 = createStrToken("-.234");
H
Haojun Liao 已提交
765
  EXPECT_EQ(tGetNumericStringType(&t1), TK_FLOAT);
H
hjxilinx 已提交
766 767 768 769 770 771
}

TEST(testCase, getTempFilePath_test) {
  char path[4096] = {0};
  memset(path, 1, 4096);

S
slguan 已提交
772
  taosGetTmpfilePath("new_tmp", path);
H
hjxilinx 已提交
773 774 775
  printf("%s\n", path);
}