13fa38318SNico Weber //===-- stats_test.cpp ------------------------------------------*- C++ -*-===//
23fa38318SNico Weber //
33fa38318SNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43fa38318SNico Weber // See https://llvm.org/LICENSE.txt for license information.
53fa38318SNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63fa38318SNico Weber //
73fa38318SNico Weber //===----------------------------------------------------------------------===//
83fa38318SNico Weber
9*0d3d4d3bSKostya Kortchinsky #include "tests/scudo_unit_test.h"
10*0d3d4d3bSKostya Kortchinsky
11*0d3d4d3bSKostya Kortchinsky #include "stats.h"
123fa38318SNico Weber
TEST(ScudoStatsTest,LocalStats)133fa38318SNico Weber TEST(ScudoStatsTest, LocalStats) {
143fa38318SNico Weber scudo::LocalStats LStats;
153fa38318SNico Weber LStats.init();
163fa38318SNico Weber for (scudo::uptr I = 0; I < scudo::StatCount; I++)
173fa38318SNico Weber EXPECT_EQ(LStats.get(static_cast<scudo::StatType>(I)), 0U);
183fa38318SNico Weber LStats.add(scudo::StatAllocated, 4096U);
193fa38318SNico Weber EXPECT_EQ(LStats.get(scudo::StatAllocated), 4096U);
203fa38318SNico Weber LStats.sub(scudo::StatAllocated, 4096U);
213fa38318SNico Weber EXPECT_EQ(LStats.get(scudo::StatAllocated), 0U);
223fa38318SNico Weber LStats.set(scudo::StatAllocated, 4096U);
233fa38318SNico Weber EXPECT_EQ(LStats.get(scudo::StatAllocated), 4096U);
243fa38318SNico Weber }
253fa38318SNico Weber
TEST(ScudoStatsTest,GlobalStats)263fa38318SNico Weber TEST(ScudoStatsTest, GlobalStats) {
273fa38318SNico Weber scudo::GlobalStats GStats;
283fa38318SNico Weber GStats.init();
293fa38318SNico Weber scudo::uptr Counters[scudo::StatCount] = {};
303fa38318SNico Weber GStats.get(Counters);
313fa38318SNico Weber for (scudo::uptr I = 0; I < scudo::StatCount; I++)
323fa38318SNico Weber EXPECT_EQ(Counters[I], 0U);
333fa38318SNico Weber scudo::LocalStats LStats;
343fa38318SNico Weber LStats.init();
353fa38318SNico Weber GStats.link(&LStats);
363fa38318SNico Weber for (scudo::uptr I = 0; I < scudo::StatCount; I++)
373fa38318SNico Weber LStats.add(static_cast<scudo::StatType>(I), 4096U);
383fa38318SNico Weber GStats.get(Counters);
393fa38318SNico Weber for (scudo::uptr I = 0; I < scudo::StatCount; I++)
403fa38318SNico Weber EXPECT_EQ(Counters[I], 4096U);
413fa38318SNico Weber // Unlinking the local stats move numbers to the global stats.
423fa38318SNico Weber GStats.unlink(&LStats);
433fa38318SNico Weber GStats.get(Counters);
443fa38318SNico Weber for (scudo::uptr I = 0; I < scudo::StatCount; I++)
453fa38318SNico Weber EXPECT_EQ(Counters[I], 4096U);
463fa38318SNico Weber }
47