From cf1acba45b1cf3d3aec0adf507d8fb0ba2826373 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 29 May 2020 14:59:21 +0000 Subject: [PATCH] add more encoding tests --- src/util/tests/codingTests.cpp | 144 ++++++++++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 11 deletions(-) diff --git a/src/util/tests/codingTests.cpp b/src/util/tests/codingTests.cpp index 036ed0bf83..57e21a828c 100644 --- a/src/util/tests/codingTests.cpp +++ b/src/util/tests/codingTests.cpp @@ -15,6 +15,16 @@ static bool test_fixed_uint16(uint16_t value) { return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2)); } +static bool test_fixed_int16(int16_t value) { + char buf[20] = "\0"; + int16_t value_check = 0; + + void *ptr1 = taosEncodeFixedI16(static_cast(buf), value); + void *ptr2 = taosDecodeFixedI16(static_cast(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; @@ -25,6 +35,16 @@ static bool test_fixed_uint32(uint32_t value) { return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2)); } +static bool test_fixed_int32(int32_t value) { + char buf[20] = "\0"; + int32_t value_check = 0; + + void *ptr1 = taosEncodeFixedI32(static_cast(buf), value); + void *ptr2 = taosDecodeFixedI32(static_cast(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; @@ -35,6 +55,16 @@ static bool test_fixed_uint64(uint64_t value) { return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2)); } +static bool test_fixed_int64(int64_t value) { + char buf[20] = "\0"; + int64_t value_check = 0; + + void *ptr1 = taosEncodeFixedI64(static_cast(buf), value); + void *ptr2 = taosDecodeFixedI64(static_cast(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; @@ -45,6 +75,16 @@ static bool test_variant_uint16(uint16_t value) { return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2)); } +static bool test_variant_int16(int16_t value) { + char buf[20] = "\0"; + int16_t value_check = 0; + + void *ptr1 = taosEncodeVariantI16(static_cast(buf), value); + void *ptr2 = taosDecodeVariantI16(static_cast(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; @@ -55,6 +95,16 @@ static bool test_variant_uint32(uint32_t value) { return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2)); } +static bool test_variant_int32(int32_t value) { + char buf[20] = "\0"; + int32_t value_check = 0; + + void *ptr1 = taosEncodeVariantI32(static_cast(buf), value); + void *ptr2 = taosDecodeVariantI32(static_cast(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; @@ -65,52 +115,124 @@ static bool test_variant_uint64(uint64_t value) { return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2)); } +static bool test_variant_int64(int64_t value) { + char buf[20] = "\0"; + int64_t value_check = 0; + + void *ptr1 = taosEncodeVariantI64(static_cast(buf), value); + void *ptr2 = taosDecodeVariantI64(static_cast(buf), &value_check); + + return ((ptr2 != NULL) && (value == value_check) && (ptr1 == ptr2)); +} + TEST(codingTest, fixed_encode_decode) { srand(time(0)); + // uint16_t 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())); + // 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_64 gen (std::random_device{}()); + std::mt19937 gen32(std::random_device{}()); + // uint32_t + ASSERT_TRUE(test_fixed_uint32(0)); + ASSERT_TRUE(test_fixed_uint32(UINT32_MAX)); + std::uniform_int_distribution distr1(0, UINT32_MAX); + + for (int i = 0; i < 1000000; i++) { + ASSERT_TRUE(test_fixed_uint32(distr1(gen32))); + } + + // int32_t + ASSERT_TRUE(test_fixed_int32(INT32_MIN)); + ASSERT_TRUE(test_fixed_int32(INT32_MAX)); + std::uniform_int_distribution 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 distr3(0, UINT64_MAX); 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())); + ASSERT_TRUE(test_fixed_uint64(distr3(gen64))); + } + + // int64_t + std::uniform_int_distribution 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))); } } TEST(codingTest, variant_encode_decode) { srand(time(0)); + // uint16_t for (uint16_t value = 0; value <= UINT16_MAX; value++) { ASSERT_TRUE(test_variant_uint16(value)); if (value == UINT16_MAX) break; } + // 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 distr1(0, UINT32_MAX); 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())); + ASSERT_TRUE(test_variant_uint32(distr1(gen32))); } - std::mt19937_64 gen (std::random_device{}()); + // int32_t + std::uniform_int_distribution 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))); + } + + std::mt19937_64 gen64(std::random_device{}()); + // uint64_t + std::uniform_int_distribution distr3(0, UINT64_MAX); ASSERT_TRUE(test_variant_uint64(0)); ASSERT_TRUE(test_variant_uint64(UINT64_MAX)); for (int i = 0; i < 5000000; i++) { - uint64_t value = gen(); + // uint64_t value = gen(); // printf("%ull\n", value); - ASSERT_TRUE(test_variant_uint64(value)); + ASSERT_TRUE(test_variant_uint64(distr3(gen64))); + } + + // int64_t + std::uniform_int_distribution 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(); + // printf("%ull\n", value); + ASSERT_TRUE(test_variant_int64(distr4(gen64))); } } \ No newline at end of file