commonTests.cpp 8.1 KB
Newer Older
1 2 3
#include <gtest/gtest.h>
#include <iostream>

S
Shengliang Guan 已提交
4 5
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
6 7 8 9 10
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "os.h"

H
Haojun Liao 已提交
11 12
#include "tcommon.h"
#include "tdatablock.h"
13
#include "tcommon.h"
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
#include "taos.h"
#include "tvariant.h"
#include "tdef.h"

namespace {
//
}  // namespace

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

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

  int64_t val = 0;
  bool sign = true;

  int32_t ret = toInteger(s, strlen(s), 10, &val, &sign);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(val, 123);
  ASSERT_EQ(sign, true);

  s = "9223372036854775807";
  ret = toInteger(s, strlen(s), 10, &val, &sign);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(val, 9223372036854775807);
  ASSERT_EQ(sign, true);

  s = "9323372036854775807";
  ret = toInteger(s, strlen(s), 10, &val, &sign);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(val, 9323372036854775807u);
  ASSERT_EQ(sign, false);

  s = "-9323372036854775807";
  ret = toInteger(s, strlen(s), 10, &val, &sign);
  ASSERT_EQ(ret, -1);

  s = "-1";
  ret = toInteger(s, strlen(s), 10, &val, &sign);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(val, -1);
  ASSERT_EQ(sign, true);

  s = "-9223372036854775807";
  ret = toInteger(s, strlen(s), 10, &val, &sign);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(val, -9223372036854775807);
  ASSERT_EQ(sign, true);

  s = "1000u";
  ret = toInteger(s, strlen(s), 10, &val, &sign);
  ASSERT_EQ(ret, -1);

  s = "0x10";
  ret = toInteger(s, strlen(s), 16, &val, &sign);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(val, 16);
  ASSERT_EQ(sign, true);

  s = "110";
  ret = toInteger(s, strlen(s), 2, &val, &sign);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(val, 6);
  ASSERT_EQ(sign, true);

  s = "110";
  ret = toInteger(s, strlen(s), 8, &val, &sign);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(val, 72);
  ASSERT_EQ(sign, true);

  //18446744073709551615  UINT64_MAX
  s = "18446744073709551615";
  ret = toInteger(s, strlen(s), 10, &val, &sign);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(val, 18446744073709551615u);
  ASSERT_EQ(sign, false);

  s = "18446744073709551616";
  ret = toInteger(s, strlen(s), 10, &val, &sign);
  ASSERT_EQ(ret, -1);
}
S
Shengliang Guan 已提交
100

H
Haojun Liao 已提交
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 160 161 162 163 164 165
TEST(testCase, Datablock_test) {
  SSDataBlock* b = static_cast<SSDataBlock*>(calloc(1, sizeof(SSDataBlock)));
  b->info.numOfCols = 2;
  b->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData));

  SColumnInfoData infoData = {0};
  infoData.info.bytes = 4;
  infoData.info.type = TSDB_DATA_TYPE_INT;
  infoData.info.colId = 1;

  infoData.pData = (char*) calloc(40, infoData.info.bytes);
  infoData.nullbitmap = (char*) calloc(1, sizeof(char) * (40/8));
  taosArrayPush(b->pDataBlock, &infoData);

  SColumnInfoData infoData1 = {0};
  infoData1.info.bytes = 40;
  infoData1.info.type = TSDB_DATA_TYPE_BINARY;
  infoData1.info.colId = 2;

  infoData1.varmeta.offset = (int32_t*) calloc(40, sizeof(uint32_t));
  taosArrayPush(b->pDataBlock, &infoData1);

  char* str = "the value of: %d";
  char buf[128] = {0};
  char varbuf[128] = {0};

  for(int32_t i = 0; i < 40; ++i) {
    SColumnInfoData* p0 = (SColumnInfoData *) taosArrayGet(b->pDataBlock, 0);
    SColumnInfoData* p1 = (SColumnInfoData *) taosArrayGet(b->pDataBlock, 1);

    if (i&0x01) {
      int32_t len = sprintf(buf, str, i);
      STR_TO_VARSTR(varbuf, buf)
      colDataAppend(p0, i, (const char*) &i, false);
      colDataAppend(p1, i, (const char*) varbuf, false);

      memset(varbuf, 0, sizeof(varbuf));
      memset(buf, 0, sizeof(buf));
    } else {
      colDataAppend(p0, i, (const char*) &i, true);
      colDataAppend(p1, i, (const char*) varbuf, true);
    }

    b->info.rows++;
  }

  SColumnInfoData* p0 = (SColumnInfoData *) taosArrayGet(b->pDataBlock, 0);
  SColumnInfoData* p1 = (SColumnInfoData *) taosArrayGet(b->pDataBlock, 1);
  for(int32_t i = 0; i < 40; ++i) {
    if (i & 0x01) {
      ASSERT_EQ(colDataIsNull_f(p0->nullbitmap, i), false);
      ASSERT_EQ(colDataIsNull(p1, b->info.rows, i, nullptr), false);
    } else {
      ASSERT_EQ(colDataIsNull_f(p0->nullbitmap, i), true);

      ASSERT_EQ(colDataIsNull(p0, b->info.rows, i, nullptr), true);
      ASSERT_EQ(colDataIsNull(p1, b->info.rows, i, nullptr), true);
    }
  }

  printf("binary column length:%d\n", *(int32_t*) p1->pData);

  ASSERT_EQ(colDataGetNumOfCols(b), 2);
  ASSERT_EQ(colDataGetNumOfRows(b), 40);

166
  char* pData = colDataGetData(p1, 3);
H
Haojun Liao 已提交
167 168 169 170 171 172 173
  printf("the second row of binary:%s, length:%d\n", (char*)varDataVal(pData), varDataLen(pData));

  SArray* pOrderInfo = taosArrayInit(3, sizeof(SBlockOrderInfo));
  SBlockOrderInfo order = {.order = TSDB_ORDER_ASC, .colIndex = 0};
  taosArrayPush(pOrderInfo, &order);

  blockDataSort(b, pOrderInfo, true);
H
Haojun Liao 已提交
174
  blockDataDestroy(b);
H
Haojun Liao 已提交
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 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

  taosArrayDestroy(pOrderInfo);
}

#if 0
TEST(testCase, non_var_dataBlock_split_test) {
  SSDataBlock* b = static_cast<SSDataBlock*>(calloc(1, sizeof(SSDataBlock)));
  b->info.numOfCols = 2;
  b->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData));

  SColumnInfoData infoData = {0};
  infoData.info.bytes = 4;
  infoData.info.type = TSDB_DATA_TYPE_INT;
  infoData.info.colId = 1;

  int32_t numOfRows = 1000000;

  infoData.pData = (char*) calloc(numOfRows, infoData.info.bytes);
  infoData.nullbitmap = (char*) calloc(1, sizeof(char) * (numOfRows/8));
  taosArrayPush(b->pDataBlock, &infoData);

  SColumnInfoData infoData1 = {0};
  infoData1.info.bytes = 1;
  infoData1.info.type = TSDB_DATA_TYPE_TINYINT;
  infoData1.info.colId = 2;

  infoData1.pData = (char*) calloc(numOfRows, infoData.info.bytes);
  infoData1.nullbitmap = (char*) calloc(1, sizeof(char) * (numOfRows/8));
  taosArrayPush(b->pDataBlock, &infoData1);

  for(int32_t i = 0; i < numOfRows; ++i) {
    SColumnInfoData* p0 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 0);
    SColumnInfoData* p1 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 1);

    int8_t v = i;
    colDataAppend(p0, i, (const char*)&i, false);
    colDataAppend(p1, i, (const char*)&v, false);
    b->info.rows++;
  }

  int32_t pageSize = 64 * 1024;

  int32_t startIndex= 0;
  int32_t stopIndex = 0;
  int32_t count = 1;
  while(1) {
    blockDataSplitRows(b, false, startIndex, &stopIndex, pageSize);
    printf("the %d split, from: %d to %d\n", count++, startIndex, stopIndex);

    if (stopIndex == numOfRows - 1) {
      break;
    }

    startIndex = stopIndex + 1;
  }

}

#endif

TEST(testCase, var_dataBlock_split_test) {
  SSDataBlock* b = static_cast<SSDataBlock*>(calloc(1, sizeof(SSDataBlock)));
  b->info.numOfCols = 2;
  b->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData));

  int32_t numOfRows = 1000000;

  SColumnInfoData infoData = {0};
  infoData.info.bytes = 4;
  infoData.info.type = TSDB_DATA_TYPE_INT;
  infoData.info.colId = 1;

  infoData.pData = (char*) calloc(numOfRows, infoData.info.bytes);
  infoData.nullbitmap = (char*) calloc(1, sizeof(char) * (numOfRows/8));
  taosArrayPush(b->pDataBlock, &infoData);

  SColumnInfoData infoData1 = {0};
  infoData1.info.bytes = 40;
  infoData1.info.type = TSDB_DATA_TYPE_BINARY;
  infoData1.info.colId = 2;

  infoData1.varmeta.offset = (int32_t*) calloc(numOfRows, sizeof(uint32_t));
  taosArrayPush(b->pDataBlock, &infoData1);

  char buf[41] = {0};
  char buf1[100] = {0};

  for(int32_t i = 0; i < numOfRows; ++i) {
    SColumnInfoData* p0 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 0);
    SColumnInfoData* p1 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 1);

    int8_t v = i;
    colDataAppend(p0, i, (const char*)&i, false);

    sprintf(buf, "the number of row:%d", i);
    int32_t len = sprintf(buf1, buf, i);
    STR_TO_VARSTR(buf1, buf)
    colDataAppend(p1, i, buf1, false);
    b->info.rows++;

    memset(buf, 0, sizeof(buf));
    memset(buf1, 0, sizeof(buf1));
  }

  int32_t pageSize = 64 * 1024;

  int32_t startIndex= 0;
  int32_t stopIndex = 0;
  int32_t count = 1;
  while(1) {
    blockDataSplitRows(b, true, startIndex, &stopIndex, pageSize);
    printf("the %d split, from: %d to %d\n", count++, startIndex, stopIndex);

    if (stopIndex == numOfRows - 1) {
      break;
    }

    startIndex = stopIndex + 1;
  }
}

S
Shengliang Guan 已提交
296
#pragma GCC diagnostic pop