ans_common_test.cc (1525B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #include "lib/jxl/ans_common.h" 7 8 #include <vector> 9 10 #include "lib/jxl/ans_params.h" 11 #include "lib/jxl/testing.h" 12 13 namespace jxl { 14 namespace { 15 16 void VerifyAliasDistribution(const std::vector<int>& distribution, 17 uint32_t log_range) { 18 constexpr size_t log_alpha_size = 8; 19 AliasTable::Entry table[1 << log_alpha_size]; 20 ASSERT_TRUE(InitAliasTable(distribution, log_range, log_alpha_size, table)); 21 uint32_t range = 1 << log_range; 22 std::vector<std::vector<uint32_t>> offsets(distribution.size()); 23 for (uint32_t i = 0; i < range; i++) { 24 AliasTable::Symbol s = AliasTable::Lookup( 25 table, i, ANS_LOG_TAB_SIZE - 8, (1 << (ANS_LOG_TAB_SIZE - 8)) - 1); 26 offsets[s.value].push_back(s.offset); 27 } 28 for (uint32_t i = 0; i < distribution.size(); i++) { 29 ASSERT_EQ(static_cast<size_t>(distribution[i]), offsets[i].size()); 30 std::sort(offsets[i].begin(), offsets[i].end()); 31 for (uint32_t j = 0; j < offsets[i].size(); j++) { 32 ASSERT_EQ(offsets[i][j], j); 33 } 34 } 35 } 36 37 TEST(ANSCommonTest, AliasDistributionSmoke) { 38 VerifyAliasDistribution({ANS_TAB_SIZE / 2, ANS_TAB_SIZE / 2}, 39 ANS_LOG_TAB_SIZE); 40 VerifyAliasDistribution({ANS_TAB_SIZE}, ANS_LOG_TAB_SIZE); 41 VerifyAliasDistribution({0, 0, 0, ANS_TAB_SIZE, 0}, ANS_LOG_TAB_SIZE); 42 } 43 44 } // namespace 45 } // namespace jxl