codingTests.cpp 3.2 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#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;

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

  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;

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

  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;

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

  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;

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

  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;

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

  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;

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

  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));
  }
}