utilUT.cc 9.2 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7
#include <gtest/gtest.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include "index.h"
dengyihao's avatar
dengyihao 已提交
8
#include "indexCache.h"
dengyihao's avatar
dengyihao 已提交
9
#include "indexComm.h"
dengyihao's avatar
dengyihao 已提交
10 11
#include "indexFst.h"
#include "indexFstUtil.h"
dengyihao's avatar
dengyihao 已提交
12
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
13 14
#include "indexTfile.h"
#include "indexUtil.h"
dengyihao's avatar
dengyihao 已提交
15
#include "tcoding.h"
dengyihao's avatar
dengyihao 已提交
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
#include "tglobal.h"
#include "tskiplist.h"
#include "tutil.h"

class UtilEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
    src = (SArray *)taosArrayInit(2, sizeof(void *));
    for (int i = 0; i < 3; i++) {
      SArray *m = taosArrayInit(10, sizeof(uint64_t));
      taosArrayPush(src, &m);
    }

    rslt = (SArray *)taosArrayInit(10, sizeof(uint64_t));
  }
  virtual void TearDown() {
    for (int i = 0; i < taosArrayGetSize(src); i++) {
      SArray *m = (SArray *)taosArrayGetP(src, i);
      taosArrayDestroy(m);
    }
    taosArrayDestroy(src);
  }

  SArray *src;
  SArray *rslt;
};

static void clearSourceArray(SArray *p) {
  for (int i = 0; i < taosArrayGetSize(p); i++) {
    SArray *m = (SArray *)taosArrayGetP(p, i);
    taosArrayClear(m);
  }
}
static void clearFinalArray(SArray *p) { taosArrayClear(p); }
TEST_F(UtilEnv, intersectionSimpleResult) {
  SArray *f = (SArray *)taosArrayGetP(src, 0);
  for (int i = 0; i < 10; i++) {
    uint64_t val = i;
    taosArrayPush(f, &val);
  }
  f = (SArray *)taosArrayGetP(src, 1);
  for (int i = 0; i < 10; i++) {
    uint64_t val = i;
    taosArrayPush(f, &val);
  }
  f = (SArray *)taosArrayGetP(src, 2);
  for (int i = 0; i < 10; i++) {
    uint64_t val = i;
    taosArrayPush(f, &val);
  }
  iIntersection(src, rslt);
  assert(taosArrayGetSize(rslt) == 10);

  clearSourceArray(src);
  clearFinalArray(rslt);
}
TEST_F(UtilEnv, intersectMultiEmptyResult) {
  SArray *f = (SArray *)taosArrayGetP(src, 0);
  for (int i = 10; i < 20; i++) {
    uint64_t val = i;
    taosArrayPush(f, &val);
  }
  f = (SArray *)taosArrayGetP(src, 1);
  for (int i = 0; i < 10; i++) {
    uint64_t val = i;
    taosArrayPush(f, &val);
  }
  f = (SArray *)taosArrayGetP(src, 2);
  for (int i = 20; i < 30; i++) {
    uint64_t val = i;
    taosArrayPush(f, &val);
  }
  // empty source
  iIntersection(src, rslt);
  assert(taosArrayGetSize(rslt) == 0);
  clearSourceArray(src);
  clearFinalArray(rslt);
}
TEST_F(UtilEnv, intersectSimpleEmpty) {
  clearSourceArray(src);
  clearFinalArray(rslt);

  iIntersection(src, rslt);
  assert(taosArrayGetSize(rslt) == 0);
}
TEST_F(UtilEnv, intersect01) {
  clearSourceArray(src);
  clearFinalArray(rslt);

  uint64_t arr1[] = {2, 3, 4, 5};
  SArray * f = (SArray *)taosArrayGetP(src, 0);
  for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
    taosArrayPush(f, &arr1[i]);
  }

  uint64_t arr2[] = {1, 2, 3, 5};
  f = (SArray *)taosArrayGetP(src, 1);
  for (int i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++) {
    taosArrayPush(f, &arr2[i]);
  }

  uint64_t arr3[] = {3, 5, 10, 11};
  f = (SArray *)taosArrayGetP(src, 2);
  for (int i = 0; i < sizeof(arr3) / sizeof(arr3[0]); i++) {
    taosArrayPush(f, &arr3[i]);
  }

  iIntersection(src, rslt);
  assert(taosArrayGetSize(rslt) == 2);
}
TEST_F(UtilEnv, intersect02) {
  clearSourceArray(src);
  clearFinalArray(rslt);

  uint64_t arr1[] = {13, 14, 15};
  SArray * f = (SArray *)taosArrayGetP(src, 0);
  for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
    taosArrayPush(f, &arr1[i]);
  }

  uint64_t arr2[] = {8, 10, 12, 13};
  f = (SArray *)taosArrayGetP(src, 1);
  for (int i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++) {
    taosArrayPush(f, &arr2[i]);
  }

  uint64_t arr3[] = {9, 10, 11};
  f = (SArray *)taosArrayGetP(src, 2);
  for (int i = 0; i < sizeof(arr3) / sizeof(arr3[0]); i++) {
    taosArrayPush(f, &arr3[i]);
  }

  iIntersection(src, rslt);
  assert(taosArrayGetSize(rslt) == 0);
}
dengyihao's avatar
dengyihao 已提交
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
TEST_F(UtilEnv, 01union) {
  clearSourceArray(src);
  clearFinalArray(rslt);

  uint64_t arr1[] = {13, 14, 15};
  SArray * f = (SArray *)taosArrayGetP(src, 0);
  for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
    taosArrayPush(f, &arr1[i]);
  }
  iUnion(src, rslt);
  assert(taosArrayGetSize(rslt) == 3);
}
TEST_F(UtilEnv, 02union) {
  clearSourceArray(src);
  clearFinalArray(rslt);

  uint64_t arr1[] = {13, 14, 15};
  SArray * f = (SArray *)taosArrayGetP(src, 0);
  for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
    taosArrayPush(f, &arr1[i]);
  }

  uint64_t arr2[] = {13, 14, 15};
  f = (SArray *)taosArrayGetP(src, 1);
  for (int i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++) {
    taosArrayPush(f, &arr2[i]);
  }
  iUnion(src, rslt);
  assert(taosArrayGetSize(rslt) == 3);
}
TEST_F(UtilEnv, 03union) {
  clearSourceArray(src);
  clearFinalArray(rslt);

  uint64_t arr1[] = {13, 16, 18, 20};
  SArray * f = (SArray *)taosArrayGetP(src, 0);
  for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
    taosArrayPush(f, &arr1[i]);
  }

  uint64_t arr2[] = {0, 12, 13, 20, 23};
  f = (SArray *)taosArrayGetP(src, 1);
  for (int i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++) {
    taosArrayPush(f, &arr2[i]);
  }
  uint64_t arr3[] = {1, 12, 13, 16, 17};
  f = (SArray *)taosArrayGetP(src, 2);
  for (int i = 0; i < sizeof(arr3) / sizeof(arr3[0]); i++) {
    taosArrayPush(f, &arr3[i]);
  }
  iUnion(src, rslt);
  assert(taosArrayGetSize(rslt) == 9);
}
dengyihao's avatar
dengyihao 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
TEST_F(UtilEnv, 04union) {
  clearSourceArray(src);
  clearFinalArray(rslt);

  uint64_t arr1[] = {1, 4, 5, 6};
  SArray * f = (SArray *)taosArrayGetP(src, 0);
  for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
    taosArrayPush(f, &arr1[i]);
  }

  uint64_t arr2[] = {7, 8, 10};
  f = (SArray *)taosArrayGetP(src, 1);
  for (int i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++) {
    taosArrayPush(f, &arr2[i]);
  }

dengyihao's avatar
dengyihao 已提交
220
  uint64_t arr3[] = {20, 21, 30, 100, 120};
dengyihao's avatar
dengyihao 已提交
221 222 223 224 225
  f = (SArray *)taosArrayGetP(src, 2);
  for (int i = 0; i < sizeof(arr3) / sizeof(arr3[0]); i++) {
    taosArrayPush(f, &arr3[i]);
  }
  iUnion(src, rslt);
dengyihao's avatar
dengyihao 已提交
226
  assert(taosArrayGetSize(rslt) == 12);
dengyihao's avatar
dengyihao 已提交
227
}
dengyihao's avatar
dengyihao 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
TEST_F(UtilEnv, 05unionExcept) {
  clearSourceArray(src);
  clearFinalArray(rslt);

  uint64_t arr2[] = {7};
  SArray * f = (SArray *)taosArrayGetP(src, 1);
  for (int i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++) {
    taosArrayPush(f, &arr2[i]);
  }

  iUnion(src, rslt);

  SArray *ept = taosArrayInit(0, sizeof(uint64_t));
  iExcept(rslt, ept);
  EXPECT_EQ(taosArrayGetSize(rslt), 1);
}
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
TEST_F(UtilEnv, 01Except) {
  SArray *total = taosArrayInit(4, sizeof(uint64_t));
  {
    uint64_t arr1[] = {1, 4, 5, 6};
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
      taosArrayPush(total, &arr1[i]);
    }
  }

  SArray *except = taosArrayInit(4, sizeof(uint64_t));
  {
    uint64_t arr1[] = {1, 4, 5, 6};
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
      taosArrayPush(except, &arr1[i]);
    }
  }
  iExcept(total, except);
  ASSERT_EQ(taosArrayGetSize(total), 0);

  taosArrayClear(total);
  taosArrayClear(except);

  {
    uint64_t arr1[] = {1, 4, 5, 6, 7, 8};
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
      taosArrayPush(total, &arr1[i]);
    }
  }

  {
    uint64_t arr1[] = {2, 4, 5, 6};
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
      taosArrayPush(except, &arr1[i]);
    }
  }
  iExcept(total, except);
  ASSERT_EQ(taosArrayGetSize(total), 3);

  taosArrayClear(total);
  taosArrayClear(except);
  {
    uint64_t arr1[] = {1, 4, 5, 6, 7, 8, 10, 100};
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
      taosArrayPush(total, &arr1[i]);
    }
  }

  {
    uint64_t arr1[] = {2, 4, 5, 6};
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
      taosArrayPush(except, &arr1[i]);
    }
  }
  iExcept(total, except);
  ASSERT_EQ(taosArrayGetSize(total), 5);
  ASSERT_EQ(*(uint64_t *)taosArrayGet(total, 0), 1);
  ASSERT_EQ(*(uint64_t *)taosArrayGet(total, 1), 7);
  ASSERT_EQ(*(uint64_t *)taosArrayGet(total, 2), 8);
  ASSERT_EQ(*(uint64_t *)taosArrayGet(total, 3), 10);
  ASSERT_EQ(*(uint64_t *)taosArrayGet(total, 4), 100);

  taosArrayClear(total);
  taosArrayClear(except);
  {
    uint64_t arr1[] = {1, 100};
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
      taosArrayPush(total, &arr1[i]);
    }
  }

  {
    uint64_t arr1[] = {2, 4, 5, 6};
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
      taosArrayPush(except, &arr1[i]);
    }
  }
  iExcept(total, except);
  ASSERT_EQ(taosArrayGetSize(total), 2);
  ASSERT_EQ(*(uint64_t *)taosArrayGet(total, 0), 1);
  ASSERT_EQ(*(uint64_t *)taosArrayGet(total, 1), 100);
}
dengyihao's avatar
dengyihao 已提交
325
TEST_F(UtilEnv, testFill) {
dengyihao's avatar
dengyihao 已提交
326
  for (int i = 0; i < 1000000; i++) {
327 328
    int64_t val = i;
    char    buf[65] = {0};
dengyihao's avatar
dengyihao 已提交
329
    idxInt2str(val, buf, 1);
330 331
    EXPECT_EQ(val, taosStr2int64(buf));
  }
dengyihao's avatar
dengyihao 已提交
332
  for (int i = 0; i < 1000000; i++) {
333 334
    int64_t val = 0 - i;
    char    buf[65] = {0};
dengyihao's avatar
dengyihao 已提交
335
    idxInt2str(val, buf, -1);
336
    EXPECT_EQ(val, taosStr2int64(buf));
dengyihao's avatar
dengyihao 已提交
337 338
  }
}
dengyihao's avatar
dengyihao 已提交
339
TEST_F(UtilEnv, TempResult) {
dengyihao's avatar
dengyihao 已提交
340
  SIdxTRslt *relt = idxTRsltCreate();
dengyihao's avatar
dengyihao 已提交
341 342 343 344

  SArray *f = taosArrayInit(0, sizeof(uint64_t));

  uint64_t val = UINT64_MAX - 1;
dengyihao's avatar
dengyihao 已提交
345 346 347 348 349 350 351 352 353 354 355 356
  taosArrayPush(relt->add, &val);
  idxTRsltMergeTo(relt, f);
  EXPECT_EQ(taosArrayGetSize(f), 1);
}
TEST_F(UtilEnv, TempResultExcept) {
  SIdxTRslt *relt = idxTRsltCreate();

  SArray *f = taosArrayInit(0, sizeof(uint64_t));

  uint64_t val = UINT64_MAX;
  taosArrayPush(relt->add, &val);
  idxTRsltMergeTo(relt, f);
dengyihao's avatar
dengyihao 已提交
357 358
  EXPECT_EQ(taosArrayGetSize(f), 1);
}