Skip to content

Commit

Permalink
Implement buddyinfo suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfin authored and dtrugman committed Jan 1, 2025
1 parent 2709212 commit 9e2b9e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/parsers/buddyinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ zone parse_buddyinfo_line(const std::string& line)

zn.name = tokens[ZONE_NAME];

// std::distance can be negative, but the conditional on line 46
// and the nature of the begin() and end() iterators means num_chunks
// should never be less than 0.
size_t num_chunks =
std::distance(tokens.begin() + FIRST_CHUNK, tokens.end());
// This line is protected from underflow thanks to the tokens size
// conditional above.
size_t num_chunks = tokens.size() - FIRST_CHUNK;

zn.chunks.resize(num_chunks);

Expand Down
7 changes: 3 additions & 4 deletions test/test_buddyinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TEST_CASE("Parse buddyinfo", "[procfs][buddyinfo]")
pfs::zone expected;
std::string line;

SECTION("Sample 1")
SECTION("Default size - Sample 1")
{
line = "Node 0, zone Normal 216 55 189 101 84 38 37 "
" 27 5 3 587";
Expand All @@ -19,7 +19,7 @@ TEST_CASE("Parse buddyinfo", "[procfs][buddyinfo]")
expected.chunks = {216, 55, 189, 101, 84, 38, 37, 27, 5, 3, 587};
}

SECTION("Sample 2")
SECTION("Default size - Sample 2")
{
line = "Node 1, zone DMA 1 1 1 0 2 1 1 "
" 0 1 1 3";
Expand All @@ -29,8 +29,7 @@ TEST_CASE("Parse buddyinfo", "[procfs][buddyinfo]")
expected.chunks = {1, 1, 1, 0, 2, 1, 1, 0, 1, 1, 3};
}

// 9 chunks instead of the default of 11
SECTION("Sample 3")
SECTION("Custom size - Smaller than default")
{
line = "Node 0, zone Normal 189 101 84 38 37 "
" 27 5 3 587";
Expand Down

0 comments on commit 9e2b9e6

Please sign in to comment.