unitTest.cpp 29.2 KB
Newer Older
H
hjxilinx 已提交
1 2 3 4 5 6
#include <gtest/gtest.h>
#include <iostream>

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

S
Shengliang Guan 已提交
7 8 9 10 11 12
#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 已提交
13 14 15 16 17 18 19
#include "../../client/inc/tscUtil.h"
#include "tutil.h"
#include "tvariant.h"
#include "ttokendef.h"

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

25
  tGetToken(name, &token.type);
W
wpan 已提交
26
  return tscValidateName(&token, false, NULL);
H
hjxilinx 已提交
27 28 29 30
}
}

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

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

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

static void _init_tvariant_bigint(tVariant* t) {
H
Haojun Liao 已提交
46
  t->i64 = -3333333333333;
H
hjxilinx 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  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 已提交
70
  t->nLen = twcslen(t->wpz);
H
hjxilinx 已提交
71 72 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
}

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'";
103
  EXPECT_EQ(testValidateName(t5), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
104 105

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


  // =======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";
171
  EXPECT_EQ(testValidateName(t33), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
172 173

  char t33_0[] = "abc.DEF'";
174
  EXPECT_EQ(testValidateName(t33_0), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  
  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'";
197
  EXPECT_EQ(testValidateName(t39), TSDB_CODE_TSC_INVALID_OPERATION);
H
hjxilinx 已提交
198 199

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

/* 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 已提交
338
  taosParseTime(t1, &time, strlen(t1), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
339 340
  EXPECT_EQ(time, 1514739661952);

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

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

  EXPECT_EQ(time, 1514739661952 + 28800000);

  char t3[] = "2018-1-1 1:01:01.952";
dengyihao's avatar
dengyihao 已提交
350
  taosParseTime(t3, &time, strlen(t3), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
351 352 353 354 355 356 357 358
  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 已提交
359 360
  taosParseTime(t4, &time, strlen(t4), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t5, &time1, strlen(t5), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
361 362
  EXPECT_EQ(time, time1);

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

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

dengyihao's avatar
dengyihao 已提交
371 372
  taosParseTime(t5, &time, strlen(t5), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t8, &time1, strlen(t8), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
373 374 375 376
  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 已提交
377 378
  taosParseTime(t9, &time, strlen(t9), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t10, &time1, strlen(t10), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
379 380 381
  EXPECT_EQ(time, time1);

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

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

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

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

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

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

  char    t42[] = "1997-1-1T0:0:0.999999999Z";
dengyihao's avatar
dengyihao 已提交
409
  taosParseTime(t42, &time, strlen(t42), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
410 411
  EXPECT_EQ(time, 852048000999 - timezone * MILLISECOND_PER_SECOND);

G
Ganlin Zhao 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  // "%Y-%m-%d %H:%M:%S" format with TimeZone appendix is also treated as legal
  // and TimeZone will be processed
  char t60[] = "2017-4-3 1:1:2.980";
  char t61[] = "2017-4-3 2:1:2.98+9:00";
  taosParseTime(t60, &time, strlen(t60), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t61, &time1, strlen(t61), TSDB_TIME_PRECISION_MILLI, 0);
  EXPECT_EQ(time, time1);

  char t62[] = "2017-4-3 2:1:2.98+09:00";
  taosParseTime(t62, &time, strlen(t62), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t61, &time1, strlen(t61), TSDB_TIME_PRECISION_MILLI, 0);
  EXPECT_EQ(time, time1);

  char t63[] = "2017-4-3 2:1:2.98+0900";
  taosParseTime(t63, &time, strlen(t63), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t62, &time1, strlen(t62), TSDB_TIME_PRECISION_MILLI, 0);
  EXPECT_EQ(time, time1);

  char t64[] = "2017-4-2 17:1:2.98Z";
  taosParseTime(t63, &time, strlen(t63), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t64, &time1, strlen(t64), TSDB_TIME_PRECISION_MILLI, 0);
  EXPECT_EQ(time, time1);

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
  // "%Y-%m-%d%H:%M:%S" format with TimeZone appendix is also treated as legal
  // and TimeZone will be processed
  char t80[] = "2017-4-51:1:2.980";
  char t81[] = "2017-4-52:1:2.98+9:00";
  taosParseTime(t80, &time, strlen(t80), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t81, &time1, strlen(t81), TSDB_TIME_PRECISION_MILLI, 0);
  EXPECT_EQ(time, time1);

  char t82[] = "2017-4-52:1:2.98+09:00";
  taosParseTime(t82, &time, strlen(t82), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t81, &time1, strlen(t81), TSDB_TIME_PRECISION_MILLI, 0);
  EXPECT_EQ(time, time1);

  char t83[] = "2017-4-52:1:2.98+0900";
  taosParseTime(t83, &time, strlen(t83), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t82, &time1, strlen(t82), TSDB_TIME_PRECISION_MILLI, 0);
  EXPECT_EQ(time, time1);

  char t84[] = "2017-4-417:1:2.98Z";
  taosParseTime(t83, &time, strlen(t83), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t84, &time1, strlen(t84), TSDB_TIME_PRECISION_MILLI, 0);
  EXPECT_EQ(time, time1);

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

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

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

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

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

  char t20[] = "2017-12-31 9:0:0.1+12:99";
G
Ganlin Zhao 已提交
476
  EXPECT_EQ(taosParseTime(t20, &time, strlen(t20), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
477 478

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

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

  char t23[] = "2017-12-31T9:0:0.1+13:1";
dengyihao's avatar
dengyihao 已提交
485
  EXPECT_EQ(taosParseTime(t23, &time, strlen(t23), TSDB_TIME_PRECISION_MILLI, 0), 0);
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

  char t24[] = "2017-12-31T9:0:0.1+13:001";
  EXPECT_EQ(taosParseTime(t24, &time, strlen(t24), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t25[] = "2017-12-31T9:0:0.1+13:00abc";
  EXPECT_EQ(taosParseTime(t25, &time, strlen(t25), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t26[] = "2017-12-31T9:0:0.1+13001";
  EXPECT_EQ(taosParseTime(t26, &time, strlen(t26), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t27[] = "2017-12-31T9:0:0.1+1300abc";
  EXPECT_EQ(taosParseTime(t27, &time, strlen(t27), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t28[] = "2017-12-31T9:0:0Z+12:00";
  EXPECT_EQ(taosParseTime(t28, &time, strlen(t28), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t29[] = "2017-12-31T9:0:0.123Z+12:00";
  EXPECT_EQ(taosParseTime(t29, &time, strlen(t29), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t65[] = "2017-12-31 9:0:0.1+13:001";
  EXPECT_EQ(taosParseTime(t65, &time, strlen(t65), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t66[] = "2017-12-31 9:0:0.1+13:00abc";
  EXPECT_EQ(taosParseTime(t66, &time, strlen(t66), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t67[] = "2017-12-31 9:0:0.1+13001";
  EXPECT_EQ(taosParseTime(t67, &time, strlen(t67), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t68[] = "2017-12-31 9:0:0.1+1300abc";
  EXPECT_EQ(taosParseTime(t68, &time, strlen(t68), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t69[] = "2017-12-31 9:0:0Z+12:00";
  EXPECT_EQ(taosParseTime(t69, &time, strlen(t69), TSDB_TIME_PRECISION_MILLI, 0), -1);

  char t70[] = "2017-12-31 9:0:0.123Z+12:00";
  EXPECT_EQ(taosParseTime(t70, &time, strlen(t70), TSDB_TIME_PRECISION_MILLI, 0), -1);
522

H
hjxilinx 已提交
523 524
}

525 526 527 528 529 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 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
/* test parse time profiling */
TEST(testCase, parse_time_profile) {
  taos_options(TSDB_OPTION_TIMEZONE, "GMT-8");
  char t1[] = "2018-1-8 1:1:1.952";
  char t2[] = "2018-1-8T1:1:1.952+0800";
  char t3[] = "2018-1-8 1:1:1.952+0800";
  char t4[] = "2018-1-81:1:1.952+0800";

  char t5[] = "2018-1-8 1:1:1.952";
  char t6[] = "2018-1-8T1:1:1.952+08:00";
  char t7[] = "2018-1-8 1:1:1.952+08:00";
  char t8[] = "2018-1-81:1:1.952+08:00";

  char t9[] = "2018-1-8 1:1:1.952";
  char t10[] = "2018-1-8T1:1:1.952Z";
  char t11[] = "2018-1-8 1:1:1.952z";
  char t12[] = "2018-1-81:1:1.952Z";

  struct timeval start, end;
  int64_t time = 0, time1 = 0;

  int32_t total_run = 100000000;
  long total_time_us;

  gettimeofday(&start, NULL);
  for (int i = 0; i < total_run; ++i) {
    taosParseTime(t1, &time, strlen(t1), TSDB_TIME_PRECISION_MILLI, 0);
  }
  gettimeofday(&end, NULL);
  total_time_us = ((end.tv_sec - start.tv_sec)* 1000000) + (end.tv_usec - start.tv_usec);
  printf("[t1] The elapsed time is %f seconds in %d run, average:%fns\n", total_time_us/1000000.0, total_run, 1000*(float)total_time_us/(float)total_run);

  gettimeofday(&start, NULL);
  for (int i = 0; i < total_run; ++i) {
    taosParseTime(t2, &time, strlen(t2), TSDB_TIME_PRECISION_MILLI, 0);
  }
  gettimeofday(&end, NULL);
  total_time_us = ((end.tv_sec - start.tv_sec)* 1000000) + (end.tv_usec - start.tv_usec);
  printf("[t2] The elapsed time is %f seconds in %d run, average:%fns\n", total_time_us/1000000.0, total_run, 1000*(float)total_time_us/(float)total_run);

  gettimeofday(&start, NULL);
  for (int i = 0; i < total_run; ++i) {
    taosParseTime(t3, &time, strlen(t3), TSDB_TIME_PRECISION_MILLI, 0);
  }
  gettimeofday(&end, NULL);
  total_time_us = ((end.tv_sec - start.tv_sec)* 1000000) + (end.tv_usec - start.tv_usec);
  printf("[t3] The elapsed time is %f seconds in %d run, average:%fns\n", total_time_us/1000000.0, total_run, 1000*(float)total_time_us/(float)total_run);

  gettimeofday(&start, NULL);
  for (int i = 0; i < total_run; ++i) {
    taosParseTime(t4, &time, strlen(t4), TSDB_TIME_PRECISION_MILLI, 0);
  }
  gettimeofday(&end, NULL);
  total_time_us = ((end.tv_sec - start.tv_sec)* 1000000) + (end.tv_usec - start.tv_usec);
  printf("[t4] The elapsed time is %f seconds in %d run, average:%fns\n", total_time_us/1000000.0, total_run, 1000*(float)total_time_us/(float)total_run);
H
hjxilinx 已提交
580 581
}

582

H
hjxilinx 已提交
583 584 585 586 587 588
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 已提交
589
  EXPECT_EQ(t.i64, 0);
H
hjxilinx 已提交
590 591 592

  _init_tvariant_bool(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_TINYINT), 0);
H
Haojun Liao 已提交
593
  EXPECT_EQ(t.i64, 0);
H
hjxilinx 已提交
594 595 596

  _init_tvariant_bool(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_SMALLINT), 0);
H
Haojun Liao 已提交
597
  EXPECT_EQ(t.i64, 0);
H
hjxilinx 已提交
598 599 600

  _init_tvariant_bool(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
H
Haojun Liao 已提交
601
  EXPECT_EQ(t.i64, 0);
H
hjxilinx 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

  _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 已提交
624
  EXPECT_EQ(t.i64, 1);
H
hjxilinx 已提交
625 626 627

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_TINYINT), 0);
H
Haojun Liao 已提交
628
  EXPECT_EQ(t.i64, -27);
H
hjxilinx 已提交
629 630 631

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_SMALLINT), 0);
H
Haojun Liao 已提交
632
  EXPECT_EQ(t.i64, -27);
H
hjxilinx 已提交
633 634 635

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_INT), 0);
H
Haojun Liao 已提交
636
  EXPECT_EQ(t.i64, -27);
H
hjxilinx 已提交
637 638 639

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

  _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 已提交
664
  EXPECT_EQ(t.i64, 1);
H
hjxilinx 已提交
665 666 667 668 669 670 671 672 673

  _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 已提交
674
  EXPECT_EQ(t.i64, -23997659);
H
hjxilinx 已提交
675 676 677

  _init_tvariant_int(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
H
Haojun Liao 已提交
678
  EXPECT_EQ(t.i64, -23997659);
H
hjxilinx 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701

  _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 已提交
702
  EXPECT_EQ(t.i64, 1);
H
hjxilinx 已提交
703 704 705 706 707 708 709 710 711 712 713 714

  _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 已提交
715
  EXPECT_EQ(t.i64, -3333333333333);
H
hjxilinx 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738

  _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 已提交
739
  EXPECT_EQ(t.i64, 1);
H
hjxilinx 已提交
740 741 742

  _init_tvariant_float(&t);
  EXPECT_EQ(tVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
H
Haojun Liao 已提交
743
  EXPECT_EQ(t.i64, -8991212199);
H
hjxilinx 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768

  _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 已提交
769
  EXPECT_EQ(t.i64, 1);
H
hjxilinx 已提交
770 771 772 773 774 775

  _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 已提交
776
  EXPECT_EQ(t.i64, 200000);
H
hjxilinx 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

  _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 已提交
802
  EXPECT_EQ(t.i64, 0);
H
hjxilinx 已提交
803 804 805 806 807 808

  _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 已提交
809
  EXPECT_EQ(t.i64, -2000000);
H
hjxilinx 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

  _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;

834
  int32_t len = tGetToken(s, &type);
H
hjxilinx 已提交
835 836 837 838
  EXPECT_EQ(type, TK_FLOAT);
  EXPECT_EQ(len, strlen(s) - 1);

  char s1[] = "1.123e10 ";
839
  len = tGetToken(s1, &type);
H
hjxilinx 已提交
840 841 842 843
  EXPECT_EQ(type, TK_FLOAT);
  EXPECT_EQ(len, strlen(s1) - 1);

  char s4[] = "0xff ";
844
  len = tGetToken(s4, &type);
H
hjxilinx 已提交
845 846 847 848 849
  EXPECT_EQ(type, TK_HEX);
  EXPECT_EQ(len, strlen(s4) - 1);

  // invalid data type
  char s2[] = "e10 ";
850
  len = tGetToken(s2, &type);
H
hjxilinx 已提交
851 852 853
  EXPECT_FALSE(type == TK_FLOAT);

  char s3[] = "1.1.1.1";
854
  len = tGetToken(s3, &type);
H
hjxilinx 已提交
855 856 857 858
  EXPECT_EQ(type, TK_IPTOKEN);
  EXPECT_EQ(len, strlen(s3));

  char s5[] = "0x ";
859
  len = tGetToken(s5, &type);
H
hjxilinx 已提交
860 861 862
  EXPECT_FALSE(type == TK_HEX);
}

H
Haojun Liao 已提交
863 864
static SStrToken createStrToken(char* s) {
  SStrToken t = {0};//.type = TK_STRING, .z = s, .n = strlen(s)};
H
hjxilinx 已提交
865 866 867 868 869 870 871 872
  t.type = TK_STRING;
  t.z = s;
  t.n = strlen(s);

  return t;
}

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

H
Haojun Liao 已提交
875
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);
H
hjxilinx 已提交
876 877

  t1 = createStrToken("0xabc");
H
Haojun Liao 已提交
878
  EXPECT_EQ(tGetNumericStringType(&t1), TK_HEX);
H
hjxilinx 已提交
879 880

  t1 = createStrToken("0b11101");
H
Haojun Liao 已提交
881
  EXPECT_EQ(tGetNumericStringType(&t1), TK_BIN);
H
hjxilinx 已提交
882 883

  t1 = createStrToken(".134abc");
H
Haojun Liao 已提交
884
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);
H
hjxilinx 已提交
885 886

  t1 = createStrToken("1e1 ");
H
Haojun Liao 已提交
887
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);
H
hjxilinx 已提交
888 889

  t1 = createStrToken("1+2");
H
Haojun Liao 已提交
890
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);
H
hjxilinx 已提交
891 892

  t1 = createStrToken("-0x123");
H
Haojun Liao 已提交
893
  EXPECT_EQ(tGetNumericStringType(&t1), TK_HEX);
H
hjxilinx 已提交
894 895

  t1 = createStrToken("-1");
H
Haojun Liao 已提交
896
  EXPECT_EQ(tGetNumericStringType(&t1), TK_INTEGER);
H
hjxilinx 已提交
897 898

  t1 = createStrToken("-0b1110");
H
Haojun Liao 已提交
899
  EXPECT_EQ(tGetNumericStringType(&t1), TK_BIN);
H
hjxilinx 已提交
900 901

  t1 = createStrToken("-.234");
H
Haojun Liao 已提交
902
  EXPECT_EQ(tGetNumericStringType(&t1), TK_FLOAT);
H
hjxilinx 已提交
903 904 905 906 907 908
}

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

S
slguan 已提交
909
  taosGetTmpfilePath("new_tmp", path);
H
hjxilinx 已提交
910 911 912
  printf("%s\n", path);
}