ans_common_test.cc (1431B)
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 range) { 18 constexpr size_t log_alpha_size = 8; 19 AliasTable::Entry table[1 << log_alpha_size]; 20 InitAliasTable(distribution, range, log_alpha_size, table); 21 std::vector<std::vector<uint32_t>> offsets(distribution.size()); 22 for (uint32_t i = 0; i < range; i++) { 23 AliasTable::Symbol s = AliasTable::Lookup( 24 table, i, ANS_LOG_TAB_SIZE - 8, (1 << (ANS_LOG_TAB_SIZE - 8)) - 1); 25 offsets[s.value].push_back(s.offset); 26 } 27 for (uint32_t i = 0; i < distribution.size(); i++) { 28 ASSERT_EQ(static_cast<size_t>(distribution[i]), offsets[i].size()); 29 std::sort(offsets[i].begin(), offsets[i].end()); 30 for (uint32_t j = 0; j < offsets[i].size(); j++) { 31 ASSERT_EQ(offsets[i][j], j); 32 } 33 } 34 } 35 36 TEST(ANSCommonTest, AliasDistributionSmoke) { 37 VerifyAliasDistribution({ANS_TAB_SIZE / 2, ANS_TAB_SIZE / 2}, ANS_TAB_SIZE); 38 VerifyAliasDistribution({ANS_TAB_SIZE}, ANS_TAB_SIZE); 39 VerifyAliasDistribution({0, 0, 0, ANS_TAB_SIZE, 0}, ANS_TAB_SIZE); 40 } 41 42 } // namespace 43 } // namespace jxl