-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pw_allocator: Make Layout constructor explicit
This CL adds the explicit keyword to the Layout constructor. Without this, passing a size_t to a method that requires a Layput would silently convert it with a default alignment. This is potentially error-prone, as calls passing just an alignment would build but not behave as exepcted. Change-Id: I7b35dc3881ba93934c019b0c852cbcfbf969aaa3 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/211915 Lint: Lint 🤖 <[email protected]> Commit-Queue: Aaron Green <[email protected]> Presubmit-Verified: CQ Bot Account <[email protected]> Reviewed-by: Taylor Cramer <[email protected]>
- Loading branch information
1 parent
a0b08ff
commit 446fcc3
Showing
7 changed files
with
143 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2024 The Pigweed Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
#include "pw_allocator/layout.h" | ||
|
||
#include <cstddef> | ||
|
||
#include "pw_result/result.h" | ||
#include "pw_status/status.h" | ||
#include "pw_unit_test/framework.h" | ||
|
||
namespace { | ||
|
||
using ::pw::allocator::Layout; | ||
|
||
TEST(LayoutTest, DefaultConstructor) { | ||
Layout layout; | ||
EXPECT_EQ(layout.size(), 0U); | ||
EXPECT_EQ(layout.alignment(), alignof(std::max_align_t)); | ||
} | ||
|
||
TEST(LayoutTest, SizeOnlyConstructor) { | ||
constexpr size_t kSize = 512; | ||
Layout layout(kSize); | ||
EXPECT_EQ(layout.size(), kSize); | ||
EXPECT_EQ(layout.alignment(), alignof(std::max_align_t)); | ||
} | ||
|
||
TEST(LayoutTest, FullConstructor) { | ||
constexpr size_t kSize = 2048; | ||
constexpr size_t kAlignment = 4; | ||
Layout layout(kSize, kAlignment); | ||
EXPECT_EQ(layout.size(), kSize); | ||
EXPECT_EQ(layout.alignment(), kAlignment); | ||
} | ||
|
||
TEST(LayoutTest, ConstructUsingInitializer) { | ||
constexpr size_t kSize = 1024; | ||
constexpr size_t kAlignment = 8; | ||
Layout layout = {kSize, kAlignment}; | ||
EXPECT_EQ(layout.size(), kSize); | ||
EXPECT_EQ(layout.alignment(), kAlignment); | ||
} | ||
|
||
TEST(LayoutTest, ConstructFromType) { | ||
struct Values { | ||
uint8_t u8; | ||
uint16_t u16; | ||
uint32_t u32; | ||
}; | ||
Layout layout = Layout::Of<Values>(); | ||
EXPECT_EQ(layout.size(), sizeof(Values)); | ||
EXPECT_EQ(layout.alignment(), alignof(Values)); | ||
} | ||
|
||
TEST(LayoutTest, Extend) { | ||
constexpr size_t kSize1 = 2048; | ||
constexpr size_t kSize2 = 1024; | ||
constexpr size_t kAlignment = 2; | ||
Layout layout1 = {kSize1, kAlignment}; | ||
EXPECT_EQ(layout1.size(), kSize1); | ||
EXPECT_EQ(layout1.alignment(), kAlignment); | ||
|
||
Layout layout2 = layout1.Extend(kSize2); | ||
EXPECT_EQ(layout2.size(), kSize1 + kSize2); | ||
EXPECT_EQ(layout2.alignment(), kAlignment); | ||
} | ||
|
||
TEST(LayoutTest, UnwrapOk) { | ||
constexpr size_t kSize = 1024; | ||
constexpr size_t kAlignment = 8; | ||
pw::Result<Layout> result = Layout(kSize, kAlignment); | ||
Layout layout = Layout::Unwrap(result); | ||
EXPECT_EQ(layout.size(), kSize); | ||
EXPECT_EQ(layout.alignment(), kAlignment); | ||
} | ||
|
||
TEST(LayoutTest, UnwrapError) { | ||
pw::Result<Layout> result = pw::Status::Unimplemented(); | ||
Layout layout = Layout::Unwrap(result); | ||
EXPECT_EQ(layout.size(), 0U); | ||
EXPECT_EQ(layout.alignment(), alignof(std::max_align_t)); | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters