hash_test.cc (4518B)
1 /* 2 * Copyright (c) 2018, Alliance for Open Media. All rights reserved. 3 * 4 * This source code is subject to the terms of the BSD 2 Clause License and 5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 6 * was not distributed with this source code in the LICENSE file, you can 7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open 8 * Media Patent License 1.0 was not distributed with this source code in the 9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 10 */ 11 12 #include <cstdlib> 13 #include <new> 14 #include <tuple> 15 16 #include "config/aom_config.h" 17 #include "config/av1_rtcd.h" 18 19 #include "aom_ports/aom_timer.h" 20 #include "av1/encoder/hash.h" 21 #include "gtest/gtest.h" 22 #include "test/acm_random.h" 23 #include "test/util.h" 24 25 namespace { 26 27 using get_crc32c_value_func = uint32_t (*)(void *calculator, const uint8_t *p, 28 size_t length); 29 30 using HashParam = std::tuple<get_crc32c_value_func, int>; 31 32 class AV1Crc32cHashTest : public ::testing::TestWithParam<HashParam> { 33 public: 34 ~AV1Crc32cHashTest() override; 35 void SetUp() override; 36 37 void TearDown() override; 38 39 protected: 40 void RunCheckOutput(get_crc32c_value_func test_impl); 41 void RunSpeedTest(get_crc32c_value_func test_impl); 42 43 void RunZeroTest(get_crc32c_value_func test_impl); 44 45 libaom_test::ACMRandom rnd_; 46 CRC32C calc_; 47 uint8_t *buffer_; 48 int bsize_; 49 size_t length_; 50 }; 51 52 AV1Crc32cHashTest::~AV1Crc32cHashTest() = default; 53 54 void AV1Crc32cHashTest::SetUp() { 55 rnd_.Reset(libaom_test::ACMRandom::DeterministicSeed()); 56 av1_crc32c_calculator_init(&calc_); 57 58 bsize_ = GET_PARAM(1); 59 length_ = bsize_ * bsize_ * sizeof(uint16_t); 60 buffer_ = new uint8_t[length_]; 61 ASSERT_NE(buffer_, nullptr); 62 for (size_t i = 0; i < length_; ++i) { 63 buffer_[i] = rnd_.Rand8(); 64 } 65 } 66 67 void AV1Crc32cHashTest::TearDown() { delete[] buffer_; } 68 69 void AV1Crc32cHashTest::RunCheckOutput(get_crc32c_value_func test_impl) { 70 get_crc32c_value_func ref_impl = av1_get_crc32c_value_c; 71 // for the same buffer crc should be the same 72 uint32_t crc0 = test_impl(&calc_, buffer_, length_); 73 uint32_t crc1 = test_impl(&calc_, buffer_, length_); 74 uint32_t crc2 = ref_impl(&calc_, buffer_, length_); 75 ASSERT_EQ(crc0, crc1); 76 ASSERT_EQ(crc0, crc2); // should equal to software version 77 // modify buffer 78 buffer_[0] += 1; 79 uint32_t crc3 = test_impl(&calc_, buffer_, length_); 80 uint32_t crc4 = ref_impl(&calc_, buffer_, length_); 81 ASSERT_NE(crc0, crc3); // crc shoud not equal to previous one 82 ASSERT_EQ(crc3, crc4); 83 } 84 85 void AV1Crc32cHashTest::RunSpeedTest(get_crc32c_value_func test_impl) { 86 get_crc32c_value_func impls[] = { av1_get_crc32c_value_c, test_impl }; 87 const int repeat = 10000000 / (bsize_ + bsize_); 88 89 aom_usec_timer timer; 90 double time[2]; 91 for (int i = 0; i < 2; ++i) { 92 aom_usec_timer_start(&timer); 93 for (int j = 0; j < repeat; ++j) { 94 impls[i](&calc_, buffer_, length_); 95 } 96 aom_usec_timer_mark(&timer); 97 time[i] = static_cast<double>(aom_usec_timer_elapsed(&timer)); 98 } 99 printf("hash %3dx%-3d:%7.2f/%7.2fus", bsize_, bsize_, time[0], time[1]); 100 printf("(%3.2f)\n", time[0] / time[1]); 101 } 102 103 void AV1Crc32cHashTest::RunZeroTest(get_crc32c_value_func test_impl) { 104 uint8_t buffer0[1024] = { 0 }; 105 // for buffer with different size the crc should not be the same 106 const uint32_t crc0 = test_impl(&calc_, buffer0, 32); 107 const uint32_t crc1 = test_impl(&calc_, buffer0, 128); 108 const uint32_t crc2 = test_impl(&calc_, buffer0, 1024); 109 ASSERT_NE(crc0, crc1); 110 ASSERT_NE(crc0, crc2); 111 ASSERT_NE(crc1, crc2); 112 } 113 114 TEST_P(AV1Crc32cHashTest, CheckOutput) { RunCheckOutput(GET_PARAM(0)); } 115 116 TEST_P(AV1Crc32cHashTest, CheckZero) { RunZeroTest(GET_PARAM(0)); } 117 118 TEST_P(AV1Crc32cHashTest, DISABLED_Speed) { RunSpeedTest(GET_PARAM(0)); } 119 120 const int kValidBlockSize[] = { 64, 32, 8, 4 }; 121 122 INSTANTIATE_TEST_SUITE_P( 123 C, AV1Crc32cHashTest, 124 ::testing::Combine(::testing::Values(&av1_get_crc32c_value_c), 125 ::testing::ValuesIn(kValidBlockSize))); 126 127 #if HAVE_SSE4_2 128 INSTANTIATE_TEST_SUITE_P( 129 SSE4_2, AV1Crc32cHashTest, 130 ::testing::Combine(::testing::Values(&av1_get_crc32c_value_sse4_2), 131 ::testing::ValuesIn(kValidBlockSize))); 132 #endif 133 134 #if HAVE_ARM_CRC32 135 INSTANTIATE_TEST_SUITE_P( 136 ARM_CRC32, AV1Crc32cHashTest, 137 ::testing::Combine(::testing::Values(&av1_get_crc32c_value_arm_crc32), 138 ::testing::ValuesIn(kValidBlockSize))); 139 #endif 140 141 } // namespace