xref: /aosp_15_r20/external/zucchini/disassembler_dex_unittest.cc (revision a03ca8b91e029cd15055c20c78c2e087c84792e4)
1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/zucchini/disassembler_dex.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <algorithm>
11 #include <set>
12 #include <vector>
13 
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace zucchini {
17 
18 namespace {
19 
20 template <typename T>
CountDistinct(const std::vector<T> & v)21 size_t CountDistinct(const std::vector<T>& v) {
22   return std::set<T>(v.begin(), v.end()).size();
23 }
24 
25 }  // namespace
26 
27 // Ensures that ReferenceGroups from DisassemblerDex::MakeReferenceGroups()
28 // cover each non-sentinel element in ReferenceType in order, exactly once. Also
29 // ensures that the ReferenceType elements are grouped by ReferencePool, and
30 // listed in increasing order.
TEST(DisassemblerDexTest,ReferenceGroups)31 TEST(DisassemblerDexTest, ReferenceGroups) {
32   std::vector<uint32_t> pool_list;
33   std::vector<uint32_t> type_list;
34   DisassemblerDex dis;
35   for (ReferenceGroup group : dis.MakeReferenceGroups()) {
36     pool_list.push_back(static_cast<uint32_t>(group.pool_tag().value()));
37     type_list.push_back(static_cast<uint32_t>(group.type_tag().value()));
38   }
39 
40   // Check ReferenceByte coverage.
41   constexpr size_t kNumTypes = DisassemblerDex::kNumTypes;
42   EXPECT_EQ(kNumTypes, type_list.size());
43   EXPECT_EQ(kNumTypes, CountDistinct(type_list));
44   EXPECT_TRUE(std::is_sorted(type_list.begin(), type_list.end()));
45 
46   // Check that ReferenceType elements are grouped by ReferencePool. Note that
47   // repeats can occur, and pools can be skipped.
48   EXPECT_TRUE(std::is_sorted(pool_list.begin(), pool_list.end()));
49 }
50 
51 }  // namespace zucchini
52