codingTests.cpp 7.8 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11
#include <gtest/gtest.h>
#include <stdlib.h>
#include <time.h>
#include <random>

#include "tcoding.h"

static bool test_fixed_uint16(uint16_t value) {
  char     buf[20] = "\0";
  uint16_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
12
  void *pBuf = (void *)buf;
H
hzcheng 已提交
13

H
TD-353  
Hongze Cheng 已提交
14 15 16 17
  int   tlen = taosEncodeFixedU16(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeFixedU16(static_cast<void *>(buf), &value_check);

  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
hzcheng 已提交
18 19
}

H
Hongze Cheng 已提交
20 21 22 23
static bool test_fixed_int16(int16_t value) {
  char    buf[20] = "\0";
  int16_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
24 25 26 27
  void *pBuf = (void *)buf;

  int   tlen = taosEncodeFixedI16(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeFixedI16(static_cast<void *>(buf), &value_check);
H
Hongze Cheng 已提交
28

H
TD-353  
Hongze Cheng 已提交
29
  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
Hongze Cheng 已提交
30 31
}

H
hzcheng 已提交
32 33 34 35
static bool test_fixed_uint32(uint32_t value) {
  char     buf[20] = "\0";
  uint32_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
36 37 38 39
  void *pBuf = (void *)buf;

  int   tlen = taosEncodeFixedU32(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeFixedU32(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
40

H
TD-353  
Hongze Cheng 已提交
41
  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
hzcheng 已提交
42 43
}

H
Hongze Cheng 已提交
44 45 46 47
static bool test_fixed_int32(int32_t value) {
  char    buf[20] = "\0";
  int32_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
48
  void *pBuf = (void *)buf;
H
Hongze Cheng 已提交
49

H
TD-353  
Hongze Cheng 已提交
50 51 52 53
  int   tlen = taosEncodeFixedI32(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeFixedI32(static_cast<void *>(buf), &value_check);

  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
Hongze Cheng 已提交
54 55
}

H
hzcheng 已提交
56 57 58 59
static bool test_fixed_uint64(uint64_t value) {
  char     buf[20] = "\0";
  uint64_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
60 61 62 63
  void *pBuf = (void *)buf;

  int   tlen = taosEncodeFixedU64(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeFixedU64(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
64

H
TD-353  
Hongze Cheng 已提交
65
  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
hzcheng 已提交
66 67
}

H
Hongze Cheng 已提交
68 69 70 71
static bool test_fixed_int64(int64_t value) {
  char    buf[20] = "\0";
  int64_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
72 73 74 75
  void *pBuf = (void *)buf;

  int   tlen = taosEncodeFixedI64(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeFixedI64(static_cast<void *>(buf), &value_check);
H
Hongze Cheng 已提交
76

H
TD-353  
Hongze Cheng 已提交
77
  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
Hongze Cheng 已提交
78 79
}

H
hzcheng 已提交
80 81 82 83
static bool test_variant_uint16(uint16_t value) {
  char     buf[20] = "\0";
  uint16_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
84
  void *pBuf = (void *)buf;
H
hzcheng 已提交
85

H
TD-353  
Hongze Cheng 已提交
86 87 88 89
  int   tlen = taosEncodeVariantU16(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeVariantU16(static_cast<void *>(buf), &value_check);

  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
hzcheng 已提交
90 91
}

H
Hongze Cheng 已提交
92 93 94 95
static bool test_variant_int16(int16_t value) {
  char    buf[20] = "\0";
  int16_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
96 97 98 99
  void *pBuf = (void *)buf;

  int   tlen = taosEncodeVariantI16(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeVariantI16(static_cast<void *>(buf), &value_check);
H
Hongze Cheng 已提交
100

H
TD-353  
Hongze Cheng 已提交
101
  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
Hongze Cheng 已提交
102 103
}

H
hzcheng 已提交
104 105 106 107
static bool test_variant_uint32(uint32_t value) {
  char     buf[20] = "\0";
  uint32_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
108 109 110 111
  void *pBuf = (void *)buf;

  int   tlen = taosEncodeVariantU32(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeVariantU32(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
112

H
TD-353  
Hongze Cheng 已提交
113
  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
hzcheng 已提交
114 115
}

H
Hongze Cheng 已提交
116 117 118 119
static bool test_variant_int32(int32_t value) {
  char    buf[20] = "\0";
  int32_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
120
  void *pBuf = (void *)buf;
H
Hongze Cheng 已提交
121

H
TD-353  
Hongze Cheng 已提交
122 123 124 125
  int   tlen = taosEncodeVariantI32(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeVariantI32(static_cast<void *>(buf), &value_check);

  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
Hongze Cheng 已提交
126 127
}

H
hzcheng 已提交
128 129 130 131
static bool test_variant_uint64(uint64_t value) {
  char     buf[20] = "\0";
  uint64_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
132 133 134 135
  void *pBuf = (void *)buf;

  int   tlen = taosEncodeVariantU64(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeVariantU64(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
136

H
TD-353  
Hongze Cheng 已提交
137
  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
hzcheng 已提交
138 139
}

H
Hongze Cheng 已提交
140 141 142 143
static bool test_variant_int64(int64_t value) {
  char    buf[20] = "\0";
  int64_t value_check = 0;

H
TD-353  
Hongze Cheng 已提交
144 145 146 147
  void *pBuf = (void *)buf;

  int   tlen = taosEncodeVariantI64(static_cast<void **>(&pBuf), value);
  void *ptr = taosDecodeVariantI64(static_cast<void *>(buf), &value_check);
H
Hongze Cheng 已提交
148

H
TD-353  
Hongze Cheng 已提交
149
  return ((ptr != NULL) && (value == value_check) && (pBuf == ptr) && POINTER_DISTANCE(pBuf, buf) == tlen);
H
Hongze Cheng 已提交
150 151
}

H
hzcheng 已提交
152 153 154
TEST(codingTest, fixed_encode_decode) {
  srand(time(0));

H
Hongze Cheng 已提交
155
  // uint16_t
H
hzcheng 已提交
156 157 158 159 160
  for (uint16_t value = 0; value <= UINT16_MAX; value++) {
    ASSERT_TRUE(test_fixed_uint16(value));
    if (value == UINT16_MAX) break;
  }

H
Hongze Cheng 已提交
161 162 163 164 165 166 167 168
  // int16_t
  for (int16_t value = INT16_MIN; value <= INT16_MAX; value++) {
    ASSERT_TRUE(test_fixed_int16(value));
    if (value == INT16_MAX) break;
  }

  std::mt19937 gen32(std::random_device{}());
  // uint32_t
H
hzcheng 已提交
169 170
  ASSERT_TRUE(test_fixed_uint32(0));
  ASSERT_TRUE(test_fixed_uint32(UINT32_MAX));
H
Hongze Cheng 已提交
171
  std::uniform_int_distribution<uint32_t> distr1(0, UINT32_MAX);
H
hzcheng 已提交
172 173

  for (int i = 0; i < 1000000; i++) {
H
Hongze Cheng 已提交
174
    ASSERT_TRUE(test_fixed_uint32(distr1(gen32)));
H
hzcheng 已提交
175 176
  }

H
Hongze Cheng 已提交
177 178 179 180 181 182 183 184 185 186 187 188
  // int32_t
  ASSERT_TRUE(test_fixed_int32(INT32_MIN));
  ASSERT_TRUE(test_fixed_int32(INT32_MAX));
  std::uniform_int_distribution<int32_t> distr2(INT32_MIN, INT32_MAX);

  for (int i = 0; i < 1000000; i++) {
    ASSERT_TRUE(test_fixed_int32(distr2(gen32)));
  }

  std::mt19937_64 gen64(std::random_device{}());
  // uint64_t
  std::uniform_int_distribution<uint64_t> distr3(0, UINT64_MAX);
H
hzcheng 已提交
189 190 191 192

  ASSERT_TRUE(test_fixed_uint64(0));
  ASSERT_TRUE(test_fixed_uint64(UINT64_MAX));
  for (int i = 0; i < 1000000; i++) {
H
Hongze Cheng 已提交
193 194 195 196 197 198 199 200 201 202
    ASSERT_TRUE(test_fixed_uint64(distr3(gen64)));
  }

  // int64_t
  std::uniform_int_distribution<int64_t> distr4(INT64_MIN, INT64_MAX);

  ASSERT_TRUE(test_fixed_int64(INT64_MIN));
  ASSERT_TRUE(test_fixed_int64(INT64_MAX));
  for (int i = 0; i < 1000000; i++) {
    ASSERT_TRUE(test_fixed_int64(distr4(gen64)));
H
hzcheng 已提交
203 204 205 206 207 208
  }
}

TEST(codingTest, variant_encode_decode) {
  srand(time(0));

H
Hongze Cheng 已提交
209
  // uint16_t
H
hzcheng 已提交
210 211 212 213 214
  for (uint16_t value = 0; value <= UINT16_MAX; value++) {
    ASSERT_TRUE(test_variant_uint16(value));
    if (value == UINT16_MAX) break;
  }

H
Hongze Cheng 已提交
215 216 217 218 219 220 221 222 223
  // int16_t
  for (int16_t value = INT16_MIN; value <= INT16_MAX; value++) {
    ASSERT_TRUE(test_variant_int16(value));
    if (value == INT16_MAX) break;
  }

  std::mt19937 gen32(std::random_device{}());
  // uint32_t
  std::uniform_int_distribution<uint32_t> distr1(0, UINT32_MAX);
H
hzcheng 已提交
224 225 226 227
  ASSERT_TRUE(test_variant_uint32(0));
  ASSERT_TRUE(test_variant_uint32(UINT32_MAX));

  for (int i = 0; i < 5000000; i++) {
H
Hongze Cheng 已提交
228 229 230 231 232 233 234 235 236 237
    ASSERT_TRUE(test_variant_uint32(distr1(gen32)));
  }

  // int32_t
  std::uniform_int_distribution<int32_t> distr2(INT32_MIN, INT32_MAX);
  ASSERT_TRUE(test_variant_int32(INT32_MIN));
  ASSERT_TRUE(test_variant_int32(INT32_MAX));

  for (int i = 0; i < 5000000; i++) {
    ASSERT_TRUE(test_variant_int32(distr2(gen32)));
H
hzcheng 已提交
238 239
  }

H
Hongze Cheng 已提交
240 241 242
  std::mt19937_64 gen64(std::random_device{}());
  // uint64_t
  std::uniform_int_distribution<uint64_t> distr3(0, UINT64_MAX);
H
hzcheng 已提交
243 244 245 246

  ASSERT_TRUE(test_variant_uint64(0));
  ASSERT_TRUE(test_variant_uint64(UINT64_MAX));
  for (int i = 0; i < 5000000; i++) {
H
Hongze Cheng 已提交
247 248 249 250 251 252 253 254 255 256 257 258
    // uint64_t value = gen();
    // printf("%ull\n", value);
    ASSERT_TRUE(test_variant_uint64(distr3(gen64)));
  }

  // int64_t
  std::uniform_int_distribution<int64_t> distr4(INT64_MIN, INT64_MAX);

  ASSERT_TRUE(test_variant_int64(INT64_MIN));
  ASSERT_TRUE(test_variant_int64(INT64_MAX));
  for (int i = 0; i < 5000000; i++) {
    // uint64_t value = gen();
H
hzcheng 已提交
259
    // printf("%ull\n", value);
H
Hongze Cheng 已提交
260
    ASSERT_TRUE(test_variant_int64(distr4(gen64)));
H
hzcheng 已提交
261 262
  }
}