diff options
author | Lenny Maiorani <lenny@colorado.edu> | 2020-11-16 17:41:46 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-11-20 21:18:14 +0100 |
commit | bdf3baa8ac76ebdabd7826a8453fefe07f3f9dc9 (patch) | |
tree | e821970b71b01b661cfe58efb6da15ea62522499 /AK/Tests/TestMACAddress.cpp | |
parent | 700fe315cf18836452c9157d004e4b869f7f2ae3 (diff) | |
download | serenity-bdf3baa8ac76ebdabd7826a8453fefe07f3f9dc9.zip |
MACAddress: AK::Array as member variable instead of C-array
Problem:
- C-style arrays do not automatically provide bounds checking and are
less type safe overall.
- `__builtin_memcmp` is not a constant expression in the current gcc.
Solution:
- Change private m_data to be AK::Array.
- Eliminate constructor from C-style array.
- Change users of the C-style array constructor to use the default
constructor.
- Change `operator==()` to be a hand-written comparison loop and let
the optimizer figure out to use `memcmp`.
Diffstat (limited to 'AK/Tests/TestMACAddress.cpp')
-rw-r--r-- | AK/Tests/TestMACAddress.cpp | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/AK/Tests/TestMACAddress.cpp b/AK/Tests/TestMACAddress.cpp index 3b6e4a0fac..cfc4455431 100644 --- a/AK/Tests/TestMACAddress.cpp +++ b/AK/Tests/TestMACAddress.cpp @@ -43,14 +43,6 @@ TEST_CASE(should_braces_construct) EXPECT(!sut.is_zero()); } -TEST_CASE(should_construct_from_c_array) -{ - constexpr u8 addr[6] = { 1, 2, 3, 4, 5, 6 }; - constexpr MACAddress sut(addr); - static_assert(!sut.is_zero()); - EXPECT(!sut.is_zero()); -} - TEST_CASE(should_construct_from_6_octets) { constexpr MACAddress sut(1, 2, 3, 4, 5, 6); @@ -58,7 +50,7 @@ TEST_CASE(should_construct_from_6_octets) EXPECT(!sut.is_zero()); } -TEST_CASE(should_provide_access_to_octet_by_index) +TEST_CASE(should_provide_read_access_to_octet_by_index) { constexpr auto is_all_expected = [](auto& sut) { for (auto i = 0u; i < sizeof(MACAddress); ++i) { @@ -78,6 +70,21 @@ TEST_CASE(should_provide_access_to_octet_by_index) } } +TEST_CASE(should_provide_write_access_to_octet_by_index) +{ + constexpr auto sut = [] { + MACAddress m {}; + for (auto i = 0u; i < sizeof(MACAddress); ++i) { + m[i] = i + 1; + } + return m; + }(); + + constexpr MACAddress expected(1, 2, 3, 4, 5, 6); + + static_assert(expected == sut); +} + TEST_CASE(should_equality_compare) { constexpr MACAddress a(1, 2, 3, 4, 5, 6); |