From 129a9e3532e6999e488f34135d66f0b932170392 Mon Sep 17 00:00:00 2001 From: eyalz800 Date: Mon, 4 Nov 2024 21:11:53 +0200 Subject: [PATCH] Fixed const detection in containers assuming subscript operator --- test/src/test_list.cpp | 162 +++++++++++++++++++++++++++++++++++++ test/src/test_stdarray.cpp | 20 +++++ zpp_bits.h | 8 +- 3 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 test/src/test_list.cpp diff --git a/test/src/test_list.cpp b/test/src/test_list.cpp new file mode 100644 index 0000000..813cfaf --- /dev/null +++ b/test/src/test_list.cpp @@ -0,0 +1,162 @@ +#include "test.h" + +#include + +namespace test_list +{ + +TEST(list, integer) +{ + auto [data, in, out] = zpp::bits::data_in_out(); + out(std::list{1,2,3,4}).or_throw(); + + EXPECT_EQ(encode_hex(data), + "04000000" + "01000000" + "02000000" + "03000000" + "04000000"); + + std::list v; + in(v).or_throw(); + + EXPECT_EQ(v, (std::list{1,2,3,4})); +} + +TEST(list, const_integer) +{ + auto [data, in, out] = zpp::bits::data_in_out(); + const std::list o{1,2,3,4}; + out(o).or_throw(); + + EXPECT_EQ(encode_hex(data), + "04000000" + "01000000" + "02000000" + "03000000" + "04000000"); + + std::list v; + in(v).or_throw(); + + EXPECT_EQ(v, (std::list{1,2,3,4})); +} + +TEST(list, string) +{ + using namespace std::string_literals; + auto [data, in, out] = zpp::bits::data_in_out(); + out(std::list{"1"s,"2"s,"3"s,"4"s}).or_throw(); + + EXPECT_EQ(encode_hex(data), + "04000000" + "01000000" + "31" + "01000000" + "32" + "01000000" + "33" + "01000000" + "34"); + + std::list v; + in(v).or_throw(); + + EXPECT_EQ(v, (std::list{"1"s,"2"s,"3"s,"4"s})); +} + +TEST(list, const_string) +{ + using namespace std::string_literals; + auto [data, in, out] = zpp::bits::data_in_out(); + const std::list o{"1"s, "2"s, "3"s, "4"s}; + out(o).or_throw(); + + EXPECT_EQ(encode_hex(data), + "04000000" + "01000000" + "31" + "01000000" + "32" + "01000000" + "33" + "01000000" + "34"); + + std::list v; + in(v).or_throw(); + + EXPECT_EQ(v, (std::list{"1"s,"2"s,"3"s,"4"s})); +} + +TEST(list, sized_1b_integer) +{ + auto [data, in, out] = zpp::bits::data_in_out(); + out(zpp::bits::sized(std::list{1,2,3,4})).or_throw(); + + EXPECT_EQ(encode_hex(data), + "04" + "01000000" + "02000000" + "03000000" + "04000000"); + + std::list v; + in(zpp::bits::sized(v)).or_throw(); + + EXPECT_EQ(v, (std::list{1,2,3,4})); +} + +TEST(list, unsized_integer) +{ + auto [data, in, out] = zpp::bits::data_in_out(); + out(zpp::bits::unsized(std::list{1,2,3,4})).or_throw(); + + EXPECT_EQ(encode_hex(data), + "01000000" + "02000000" + "03000000" + "04000000"); + + std::list v(4); + in(zpp::bits::unsized(v)).or_throw(); + + EXPECT_EQ(v, (std::list{1,2,3,4})); +} + +TEST(list, sized_t_1b_integer) +{ + auto [data, in, out] = zpp::bits::data_in_out(); + out(zpp::bits::sized_t, std::uint8_t>{1,2,3,4}).or_throw(); + + EXPECT_EQ(encode_hex(data), + "04" + "01000000" + "02000000" + "03000000" + "04000000"); + + zpp::bits::sized_t, std::uint8_t> v; + in(v).or_throw(); + + EXPECT_EQ(v, (std::list{1,2,3,4})); +} + +TEST(list, unsized_t_integer) +{ + auto [data, in, out] = zpp::bits::data_in_out(); + out(zpp::bits::unsized_t>{1,2,3,4}).or_throw(); + + EXPECT_EQ(encode_hex(data), + "01000000" + "02000000" + "03000000" + "04000000"); + + zpp::bits::unsized_t> v(4); + in(v).or_throw(); + + EXPECT_EQ(v, (std::list{1,2,3,4})); +} + +} // namespace test_list diff --git a/test/src/test_stdarray.cpp b/test/src/test_stdarray.cpp index 0741619..0227fa1 100644 --- a/test/src/test_stdarray.cpp +++ b/test/src/test_stdarray.cpp @@ -20,6 +20,26 @@ TEST(stdarray, integer) } } +TEST(stdarray, const_integer) +{ + auto [data, in, out] = zpp::bits::data_in_out(); + const std::array a1 = {1,2,3,4}; + out(a1).or_throw(); + + EXPECT_EQ(encode_hex(data), + "01000000" + "02000000" + "03000000" + "04000000"); + + std::array a2{}; + in(a2).or_throw(); + + for (std::size_t i = 0; i < std::extent_v; ++i) { + EXPECT_EQ(a1[i], a2[i]); + } +} + TEST(stdarray, string) { using namespace std::string_literals; diff --git a/zpp_bits.h b/zpp_bits.h index 3f52d44..b319aa8 100644 --- a/zpp_bits.h +++ b/zpp_bits.h @@ -2680,8 +2680,12 @@ class in { using type = std::remove_cvref_t; using value_type = typename type::value_type; - constexpr auto is_const = std::is_const_v< - std::remove_reference_t>; + constexpr auto is_const = + std::is_const_v> || + requires { + requires std::is_const_v< + std::remove_reference_t>; + }; if constexpr (!std::is_void_v && (requires(type container) { container.resize(1); } ||