1 //===-- stats_test.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "scudo/standalone/stats.h" 10 #include "gtest/gtest.h" 11 12 TEST(ScudoStatsTest, LocalStats) { 13 scudo::LocalStats LStats; 14 LStats.init(); 15 for (scudo::uptr I = 0; I < scudo::StatCount; I++) 16 EXPECT_EQ(LStats.get(static_cast<scudo::StatType>(I)), 0U); 17 LStats.add(scudo::StatAllocated, 4096U); 18 EXPECT_EQ(LStats.get(scudo::StatAllocated), 4096U); 19 LStats.sub(scudo::StatAllocated, 4096U); 20 EXPECT_EQ(LStats.get(scudo::StatAllocated), 0U); 21 LStats.set(scudo::StatAllocated, 4096U); 22 EXPECT_EQ(LStats.get(scudo::StatAllocated), 4096U); 23 } 24 25 TEST(ScudoStatsTest, GlobalStats) { 26 scudo::GlobalStats GStats; 27 GStats.init(); 28 scudo::uptr Counters[scudo::StatCount] = {}; 29 GStats.get(Counters); 30 for (scudo::uptr I = 0; I < scudo::StatCount; I++) 31 EXPECT_EQ(Counters[I], 0U); 32 scudo::LocalStats LStats; 33 LStats.init(); 34 GStats.link(&LStats); 35 for (scudo::uptr I = 0; I < scudo::StatCount; I++) 36 LStats.add(static_cast<scudo::StatType>(I), 4096U); 37 GStats.get(Counters); 38 for (scudo::uptr I = 0; I < scudo::StatCount; I++) 39 EXPECT_EQ(Counters[I], 4096U); 40 // Unlinking the local stats move numbers to the global stats. 41 GStats.unlink(&LStats); 42 GStats.get(Counters); 43 for (scudo::uptr I = 0; I < scudo::StatCount; I++) 44 EXPECT_EQ(Counters[I], 4096U); 45 } 46