codingTests.cpp 3.2 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 18 19 20 21

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

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

H
Hongze Cheng 已提交
22 23
  void *ptr1 = taosEncodeFixedU32(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeFixedU32(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
24 25 26 27 28 29 30 31

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

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

H
Hongze Cheng 已提交
32 33
  void *ptr1 = taosEncodeFixedU64(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeFixedU64(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
34 35 36 37 38 39 40 41

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

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

H
Hongze Cheng 已提交
42 43
  void *ptr1 = taosEncodeVariantU16(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeVariantU16(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
44 45 46 47 48 49 50 51

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

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

H
Hongze Cheng 已提交
52 53
  void *ptr1 = taosEncodeVariantU32(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeVariantU32(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
54 55 56 57 58 59 60 61

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

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

H
Hongze Cheng 已提交
62 63
  void *ptr1 = taosEncodeVariantU64(static_cast<void *>(buf), value);
  void *ptr2 = taosDecodeVariantU64(static_cast<void *>(buf), &value_check);
H
hzcheng 已提交
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

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

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

  for (uint16_t value = 0; value <= UINT16_MAX; value++) {
    ASSERT_TRUE(test_fixed_uint16(value));
    if (value == UINT16_MAX) break;
  }

  ASSERT_TRUE(test_fixed_uint32(0));
  ASSERT_TRUE(test_fixed_uint32(UINT32_MAX));

  for (int i = 0; i < 1000000; i++) {
    ASSERT_TRUE(test_fixed_uint32(rand()));
  }

  std::mt19937_64 gen (std::random_device{}());

  ASSERT_TRUE(test_fixed_uint64(0));
  ASSERT_TRUE(test_fixed_uint64(UINT64_MAX));
  for (int i = 0; i < 1000000; i++) {
    ASSERT_TRUE(test_fixed_uint64(gen()));
  }
}

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

  for (uint16_t value = 0; value <= UINT16_MAX; value++) {
    ASSERT_TRUE(test_variant_uint16(value));
    if (value == UINT16_MAX) break;
  }

  ASSERT_TRUE(test_variant_uint32(0));
  ASSERT_TRUE(test_variant_uint32(UINT32_MAX));

  for (int i = 0; i < 5000000; i++) {
    ASSERT_TRUE(test_variant_uint32(rand()));
  }

  std::mt19937_64 gen (std::random_device{}());

  ASSERT_TRUE(test_variant_uint64(0));
  ASSERT_TRUE(test_variant_uint64(UINT64_MAX));
  for (int i = 0; i < 5000000; i++) {
    uint64_t value = gen();
    // printf("%ull\n", value);
    ASSERT_TRUE(test_variant_uint64(value));
  }
}