codingTests.cpp 7.0 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
Hongze Cheng 已提交
12 13
  void *ptr1 = taosEncodeFixedU16(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeFixedU16(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
14 15 16 17

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

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

  void *ptr1 = taosEncodeFixedI16(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeFixedI16(static_cast<void *>(buf), &value_check);

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

H
hzcheng 已提交
28 29 30 31
static bool test_fixed_uint32(uint32_t value) {
  char     buf[20] = "\0";
  uint32_t value_check = 0;

H
Hongze Cheng 已提交
32 33
  void *ptr1 = taosEncodeFixedU32(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeFixedU32(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
34 35 36 37

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

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

  void *ptr1 = taosEncodeFixedI32(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeFixedI32(static_cast<void *>(buf), &value_check);

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

H
hzcheng 已提交
48 49 50 51
static bool test_fixed_uint64(uint64_t value) {
  char     buf[20] = "\0";
  uint64_t value_check = 0;

H
Hongze Cheng 已提交
52 53
  void *ptr1 = taosEncodeFixedU64(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeFixedU64(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
54 55 56 57

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

H
Hongze Cheng 已提交
58 59 60 61 62 63 64 65 66 67
static bool test_fixed_int64(int64_t value) {
  char    buf[20] = "\0";
  int64_t value_check = 0;

  void *ptr1 = taosEncodeFixedI64(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeFixedI64(static_cast<void *>(buf), &value_check);

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

H
hzcheng 已提交
68 69 70 71
static bool test_variant_uint16(uint16_t value) {
  char     buf[20] = "\0";
  uint16_t value_check = 0;

H
Hongze Cheng 已提交
72 73
  void *ptr1 = taosEncodeVariantU16(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeVariantU16(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
74 75 76 77

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

H
Hongze Cheng 已提交
78 79 80 81 82 83 84 85 86 87
static bool test_variant_int16(int16_t value) {
  char    buf[20] = "\0";
  int16_t value_check = 0;

  void *ptr1 = taosEncodeVariantI16(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeVariantI16(static_cast<void *>(buf), &value_check);

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

H
hzcheng 已提交
88 89 90 91
static bool test_variant_uint32(uint32_t value) {
  char     buf[20] = "\0";
  uint32_t value_check = 0;

H
Hongze Cheng 已提交
92 93
  void *ptr1 = taosEncodeVariantU32(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeVariantU32(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
94 95 96 97

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

H
Hongze Cheng 已提交
98 99 100 101 102 103 104 105 106 107
static bool test_variant_int32(int32_t value) {
  char    buf[20] = "\0";
  int32_t value_check = 0;

  void *ptr1 = taosEncodeVariantI32(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeVariantI32(static_cast<void *>(buf), &value_check);

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

H
hzcheng 已提交
108 109 110 111
static bool test_variant_uint64(uint64_t value) {
  char     buf[20] = "\0";
  uint64_t value_check = 0;

H
Hongze Cheng 已提交
112 113
  void *ptr1 = taosEncodeVariantU64(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeVariantU64(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
114 115 116 117

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

H
Hongze Cheng 已提交
118 119 120 121 122 123 124 125 126 127
static bool test_variant_int64(int64_t value) {
  char    buf[20] = "\0";
  int64_t value_check = 0;

  void *ptr1 = taosEncodeVariantI64(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeVariantI64(static_cast<void *>(buf), &value_check);

  return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2));
}

H
hzcheng 已提交
128 129 130
TEST(codingTest, fixed_encode_decode) {
  srand(time(0));

H
Hongze Cheng 已提交
131
  // uint16_t
H
hzcheng 已提交
132 133 134 135 136
  for (uint16_t value = 0; value <= UINT16_MAX; value++) {
    ASSERT_TRUE(test_fixed_uint16(value));
    if (value == UINT16_MAX) break;
  }

H
Hongze Cheng 已提交
137 138 139 140 141 142 143 144
  // 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 已提交
145 146
  ASSERT_TRUE(test_fixed_uint32(0));
  ASSERT_TRUE(test_fixed_uint32(UINT32_MAX));
H
Hongze Cheng 已提交
147
  std::uniform_int_distribution<uint32_t> distr1(0, UINT32_MAX);
H
hzcheng 已提交
148 149

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

H
Hongze Cheng 已提交
153 154 155 156 157 158 159 160 161 162 163 164
  // 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 已提交
165 166 167 168

  ASSERT_TRUE(test_fixed_uint64(0));
  ASSERT_TRUE(test_fixed_uint64(UINT64_MAX));
  for (int i = 0; i < 1000000; i++) {
H
Hongze Cheng 已提交
169 170 171 172 173 174 175 176 177 178
    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 已提交
179 180 181 182 183 184
  }
}

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

H
Hongze Cheng 已提交
185
  // uint16_t
H
hzcheng 已提交
186 187 188 189 190
  for (uint16_t value = 0; value <= UINT16_MAX; value++) {
    ASSERT_TRUE(test_variant_uint16(value));
    if (value == UINT16_MAX) break;
  }

H
Hongze Cheng 已提交
191 192 193 194 195 196 197 198 199
  // 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 已提交
200 201 202 203
  ASSERT_TRUE(test_variant_uint32(0));
  ASSERT_TRUE(test_variant_uint32(UINT32_MAX));

  for (int i = 0; i < 5000000; i++) {
H
Hongze Cheng 已提交
204 205 206 207 208 209 210 211 212 213
    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 已提交
214 215
  }

H
Hongze Cheng 已提交
216 217 218
  std::mt19937_64 gen64(std::random_device{}());
  // uint64_t
  std::uniform_int_distribution<uint64_t> distr3(0, UINT64_MAX);
H
hzcheng 已提交
219 220 221 222

  ASSERT_TRUE(test_variant_uint64(0));
  ASSERT_TRUE(test_variant_uint64(UINT64_MAX));
  for (int i = 0; i < 5000000; i++) {
H
Hongze Cheng 已提交
223 224 225 226 227 228 229 230 231 232 233 234
    // 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 已提交
235
    // printf("%ull\n", value);
H
Hongze Cheng 已提交
236
    ASSERT_TRUE(test_variant_int64(distr4(gen64)));
H
hzcheng 已提交
237 238
  }
}