stringTest.cpp 5.0 KB
Newer Older
H
hjxilinx 已提交
1 2 3 4 5 6 7 8
#include <gtest/gtest.h>
#include <limits.h>
#include <taosdef.h>
#include <iostream>

#include "taos.h"
#include "tutil.h"

wmmhello's avatar
wmmhello 已提交
9 10
TEST(testCase, str_rmquote_test) {
  char    t1[] = "\"\".dd";
11
  int32_t len = strRmquote(t1, strlen(t1));
wmmhello's avatar
wmmhello 已提交
12
  printf("t1:%s, len:%d\n", t1, len);
13 14
  EXPECT_EQ(3, len);
  EXPECT_STRCASEEQ(t1, ".dd");
wmmhello's avatar
wmmhello 已提交
15 16

  char    t2[] = "\"fsd\\\"fs\".dd";
17
  len = strRmquote(t2, strlen(t2));
wmmhello's avatar
wmmhello 已提交
18
  printf("t2:%s, len:%d\n", t2, len);
19 20
  EXPECT_EQ(9, len);
  EXPECT_STRCASEEQ(t2, "fsd\"fs.dd");
wmmhello's avatar
wmmhello 已提交
21 22

  char    t3[] = "fs\\_d\\%.d\\d";
23
  len = strRmquote(t3, strlen(t3));
wmmhello's avatar
wmmhello 已提交
24
  printf("t3:%s, len:%d\n", t3, len);
25 26
  EXPECT_EQ(10, len);
  EXPECT_STRCASEEQ(t3, "fs\\_d\\%.dd");
wmmhello's avatar
wmmhello 已提交
27 28

  char    t4[] = "\"fs\\_d\\%\".dd";
29
  len = strRmquote(t4, strlen(t4));
wmmhello's avatar
wmmhello 已提交
30
  printf("t4:%s, len:%d\n", t4, len);
31 32 33 34 35 36 37 38 39 40 41 42 43 44
  EXPECT_EQ(10, len);
  EXPECT_STRCASEEQ(t4, "fs\\_d\\%.dd");

  char    t5[] = "\"fs\\_d\\%\"";
  len = strRmquote(t5, strlen(t5));
  printf("t5:%s, len:%d\n", t5, len);
  EXPECT_EQ(7, len);
  EXPECT_STRCASEEQ(t5, "fs\\_d\\%");

  char    t6[] = "'fs\\_d\\%'";
  len = strRmquote(t6, strlen(t6));
  printf("t6:%s, len:%d\n", t6, len);
  EXPECT_EQ(7, len);
  EXPECT_STRCASEEQ(t6, "fs\\_d\\%");
wmmhello's avatar
wmmhello 已提交
45 46
}

H
hjxilinx 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
TEST(testCase, string_dequote_test) {
  char    t1[] = "'abc'";
  int32_t len = strdequote(t1);

  EXPECT_EQ(3, len);
  EXPECT_STRCASEEQ(t1, "abc");

  char t2[] = "\"abc\"";
  len = strdequote(t2);

  EXPECT_EQ(3, len);
  EXPECT_STRCASEEQ(t1, "abc");

  char t21[] = " abc ";
H
Haojun Liao 已提交
61
  int32_t lx = strtrim(t21);
H
hjxilinx 已提交
62 63

  EXPECT_STREQ("abc", t21);
H
Haojun Liao 已提交
64
  EXPECT_EQ(3, lx);
H
hjxilinx 已提交
65 66
}

H
Hui Li 已提交
67
#if 0
H
hjxilinx 已提交
68 69 70 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
TEST(testCase, string_replace_test) {
  char  t3[] = "abc01abc02abc";
  char* ret = strreplace(t3, "abc", "7");

  EXPECT_EQ(strlen(ret), 7);
  EXPECT_STREQ("7017027", ret);
  free(ret);

  char t4[] = "a01a02b03c04d05";
  ret = strreplace(t4, "0", "9999999999");

  EXPECT_EQ(strlen(ret), 5 * 10 + 10);
  EXPECT_STREQ("a99999999991a99999999992b99999999993c99999999994d99999999995", ret);
  free(ret);

  char t5[] = "abc";
  ret = strreplace(t5, "abc", "12345678901234567890");

  EXPECT_EQ(strlen(ret), 20);
  EXPECT_STREQ("12345678901234567890", ret);
  free(ret);

  char t6[] = "abc";
  ret = strreplace(t6, "def", "abc");

  EXPECT_EQ(strlen(ret), 3);
  EXPECT_STREQ("abc", ret);
  free(ret);

  char t7[] = "abcde000000000000001234";
  ret = strreplace(t7, "ab", "0000000");

  EXPECT_EQ(strlen(ret), 28);
  EXPECT_STREQ("0000000cde000000000000001234", ret);
  free(ret);

  char t8[] = "abc\ndef";
  char t[] = {10, 0};

  char    f1[] = "\\n";
  int32_t fx = strlen(f1);
  ret = strreplace(t8, "\n", "\\n");

  EXPECT_EQ(strlen(ret), 8);
  EXPECT_STREQ("abc\\ndef", ret);
  free(ret);

  char t9[] = "abc\\ndef";
  ret = strreplace(t9, "\\n", "\n");

  EXPECT_EQ(strlen(ret), 7);
  EXPECT_STREQ("abc\ndef", ret);
  free(ret);

  char t10[] = "abcdef";
  ret = strreplace(t10, "", "0");

  EXPECT_EQ(strlen(ret), 6);
  EXPECT_STREQ("abcdef", ret);
  free(ret);
}
H
Hui Li 已提交
129
#endif
H
hjxilinx 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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

TEST(testCase, string_tolower_test) {
  char t[1024] = {1};
  memset(t, 1, tListLen(t));

  const char* a1 = "ABC";
  strtolower(t, a1);
  EXPECT_STREQ(t, "abc");

  memset(t, 1, tListLen(t));
  const char* a2 = "ABC\'ABC\'D";
  strtolower(t, a2);
  EXPECT_STREQ(t, "abc\'ABC\'d");

  memset(t, 1, tListLen(t));
  const char* a3 = "";
  strtolower(t, a3);
  EXPECT_STREQ(t, "");

  memset(t, 1, tListLen(t));
  const char* a4 = "\"AbcDEF\"";
  strtolower(t, a4);
  EXPECT_STREQ(t, a4);

  memset(t, 1, tListLen(t));
  const char* a5 = "1234\"AbcDEF\"456";
  strtolower(t, a5);
  EXPECT_STREQ(t, a5);

  memset(t, 1, tListLen(t));
  const char* a6 = "1234";
  strtolower(t, a6);
  EXPECT_STREQ(t, a6);
}

TEST(testCase, string_strnchr_test) {
  char t[1024] = {0};
  memset(t, 1, tListLen(t));

  char a1[] = "AB.C";
  EXPECT_TRUE(strnchr(a1, '.', strlen(a1), true) != NULL);

  char a2[] = "abc.";
  EXPECT_TRUE(strnchr(a2, '.', strlen(a2), true) != NULL);

  char a8[] = "abc.";
  EXPECT_TRUE(strnchr(a8, '.', 1, true) == NULL);

  char a3[] = ".abc";
  EXPECT_TRUE(strnchr(a3, '.', strlen(a3), true) != NULL);

  char a4[] = "'.abc'";
  EXPECT_TRUE(strnchr(a4, '.', strlen(a4), true) == NULL);

  char a5[] = "'.abc.'abc";
  EXPECT_TRUE(strnchr(a5, '.', strlen(a5), true) == NULL);

  char a6[] = "0123456789.";
  EXPECT_TRUE(strnchr(a6, '.', strlen(a6), true) != NULL);

  char a7[] = "0123456789.";
  EXPECT_TRUE(strnchr(a7, '.', 3, true) == NULL);

  char a9[] = "0123456789.";
  EXPECT_TRUE(strnchr(a9, '.', 0, true) == NULL);

  char a10[] = "0123456789'.'";
  EXPECT_TRUE(strnchr(a10, '.', strlen(a10), true) == NULL);
}

H
TD-34  
hzcheng 已提交
200 201 202
// TEST(testCase, cache_resize_test) {
//   char a11[] = "abc'.'";
//   EXPECT_TRUE(strnchr(a11, '.', strlen(a11), false) != NULL);
H
hjxilinx 已提交
203

H
TD-34  
hzcheng 已提交
204 205
//   char a12[] = "abc'-'";
//   EXPECT_TRUE(strnchr(a12, '-', strlen(a12), false) != NULL);
H
hjxilinx 已提交
206

H
TD-34  
hzcheng 已提交
207 208
//   char a15[] = "abc'-'";
//   EXPECT_TRUE(strnchr(a15, '-', strlen(a15), true) == NULL);
H
hjxilinx 已提交
209

H
TD-34  
hzcheng 已提交
210 211
//   char a13[] = "'-'";
//   EXPECT_TRUE(strnchr(a13, '-', strlen(a13), false) != NULL);
H
hjxilinx 已提交
212

H
TD-34  
hzcheng 已提交
213 214
//   char a14[] = "'-'";
//   EXPECT_TRUE(strnchr(a14, '-', strlen(a14), true) == NULL);
H
hjxilinx 已提交
215

H
TD-34  
hzcheng 已提交
216 217 218
//   char a16[] = "'-'.";
//   EXPECT_TRUE(strnchr(a16, '.', strlen(a16), true) != NULL);
// }