stringTest.cpp 4.0 KB
Newer Older
H
hjxilinx 已提交
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 129 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
#include <gtest/gtest.h>
#include <limits.h>
#include <taosdef.h>
#include <iostream>

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

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 ";
  strtrim(t21);

  EXPECT_STREQ("abc", t21);
  EXPECT_EQ(3, strlen(t21));
}

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

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 已提交
160 161 162
// TEST(testCase, cache_resize_test) {
//   char a11[] = "abc'.'";
//   EXPECT_TRUE(strnchr(a11, '.', strlen(a11), false) != NULL);
H
hjxilinx 已提交
163

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

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

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

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

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