I'm trying to test a little buffer template with boost::test
.
This is my template.
template <size_t N, EndianType endianType = EndianType::NativeEndianType>
class Buffer {
public:
Buffer();
template <typename T>
Buffer(T* data);
template <typename T>
Buffer(T* data, size_t maxSize);
Buffer(const Buffer&) = default;
Buffer(Buffer&&) = default;
~Buffer() = default;
template <typename T>
void setValue(const T& data, size_t byteIndex);
template<typename T, size_t Index>
void setValue(const T& data);
template <typename T>
T getValue(size_t byteIndex) const noexcept;
template <typename T, size_t Index>
T getValue() const noexcept;
public:
Buffer& operator=(const Buffer&) = default;
Buffer& operator=(Buffer&&) = default;
private:
using ByteArray = std::array<std::byte, N>;
private:
ByteArray m_data;
};
template <size_t N, EndianType endianType>
Buffer<N, endianType>::Buffer() {
memset(m_data.data(), 0, N);
}
template <size_t N, EndianType endianType>
template <typename T>
Buffer<N, endianType>::Buffer(T* data) {
memcpy(m_data.data(), data, N);
}
template <size_t N, EndianType endianType>
template <typename T>
Buffer<N, endianType>::Buffer(T* data, size_t maxSize) {
memset(m_data.data(), 0, N);
memcpy(m_data.data(), data, std::min(maxSize, N));
}
template <size_t N, EndianType endianType>
template <typename T>
void Buffer<N, endianType>::setValue(const T& data, size_t byteIndex) {
memcpy(m_data.data() + byteIndex, &data, sizeof(data));
}
template <size_t N, EndianType endianType>
template <typename T, size_t Index>
void Buffer<N, endianType>::setValue(const T& data) {
static_assert(Index < N - sizeof(T), "Cannot read data: can go in overflow");
memcpy(m_data.data() + Index, &data, sizeof(data));
}
template <size_t N, EndianType endianType>
template <typename T>
T Buffer<N, endianType>::getValue(size_t byteIndex) const noexcept {
return *reinterpret_cast<const T*>(m_data.data() + byteIndex);
}
template <size_t N, EndianType endianType>
template <typename T, size_t Index>
T Buffer<N, endianType>::getValue() const noexcept {
static_assert(Index < N - sizeof(T), "Cannot read data: can go in overflow");
return *reinterpret_cast<const T*>(m_data.data() + Index);
}
The idea to set/get a value in the buffer is to have two methods for them: one that set the index as method parameter (i.e. if you need to calculate the index at runtime), and one that use the index as template parameter (for using static_assert
for example).
This is my test case file:
#include "stdafx.hpp"
#include "XXX/Buffer.hpp"
BOOST_AUTO_TEST_SUITE(BufferTest);
BOOST_AUTO_TEST_CASE(BufferConstructor) {
constexpr auto data{ "string" };
Buffer<6> buffer(data);
BOOST_CHECK_EQUAL(buffer.getValue<char>(0), 's');
BOOST_CHECK_EQUAL(buffer.getValue<char>(1), 't');
BOOST_CHECK_EQUAL(buffer.getValue<char>(2), 'r');
BOOST_CHECK_EQUAL(buffer.getValue<char>(3), 'i');
BOOST_CHECK_EQUAL(buffer.getValue<char>(4), 'n');
BOOST_CHECK_EQUAL(buffer.getValue<char>(5), 'g');
BOOST_CHECK_EQUAL(buffer.getValue<char, 0>(), 's');
}
BOOST_AUTO_TEST_SUITE_END();
In the test case I've some errors in the last row (I'm using Visual Studio 2022):
>------ Build All started: Project: wcigi, Configuration: debug-Debug ------
[1/2] Building CXX object tests\WCIGI\CMakeFiles\WCIGITest.dir\BufferTest.cpp.obj
FAILED: tests/WCIGI/CMakeFiles/WCIGITest.dir/BufferTest.cpp.obj
C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1436~1.325\bin\Hostx64\x64\cl.exe /nologo /TP -IP:\Projects\wcigi\tests\WCIGI\..\..\src -IP:\Projects\wcigi\build\debug\vcpkg_installed\x64-windows\include -IP:\Projects\wcigi\tests\WCIGI /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 -std:c++20 /showIncludes /Fotests\WCIGI\CMakeFiles\WCIGITest.dir\BufferTest.cpp.obj /Fdtests\WCIGI\CMakeFiles\WCIGITest.dir\ /FS -c P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp
P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp(16): warning C4002: too many arguments for function-like macro invocation 'BOOST_CHECK_EQUAL'
P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp(16): error C2672: 'WCIGI::Buffer<6,WCIGI::EndianType::Little>::getValue': no matching overloaded function found
P:\Projects\wcigi\src\WCIGI/Buffer.hpp(33): note: could be 'T WCIGI::Buffer<6,WCIGI::EndianType::Little>::getValue(void) noexcept const'
P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp(16): note: 'WCIGI::Buffer<6,WCIGI::EndianType::Little>::getValue': too many template arguments
P:\Projects\wcigi\src\WCIGI/Buffer.hpp(30): note: or 'T WCIGI::Buffer<6,WCIGI::EndianType::Little>::getValue(size_t) noexcept const'
P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp(16): note: 'WCIGI::Buffer<6,WCIGI::EndianType::Little>::getValue': too many template arguments
P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp(16): error C2672: 'boost::test_tools::tt_detail::check_frwd': no matching overloaded function found
P:\Projects\wcigi\build\debug\vcpkg_installed\x64-windows\include\boost/test/tools/old/impl.hpp(92): note: could be 'bool boost::test_tools::tt_detail::check_frwd(Pred,const boost::unit_test::lazy_ostream &,boost::unit_test::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const Arg0 &,const char *,const Arg1 &,const char *,const Arg2 &,const char *,const Arg3 &,const char *,const Arg4 &,const char *)'
P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp(16): note: 'bool boost::test_tools::tt_detail::check_frwd(Pred,const boost::unit_test::lazy_ostream &,boost::unit_test::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const Arg0 &,const char *,const Arg1 &,const char *,const Arg2 &,const char *,const Arg3 &,const char *,const Arg4 &,const char *)': expects 16 arguments - 7 provided
P:\Projects\wcigi\build\debug\vcpkg_installed\x64-windows\include\boost/test/tools/old/impl.hpp(92): note: or 'bool boost::test_tools::tt_detail::check_frwd(Pred,const boost::unit_test::lazy_ostream &,boost::unit_test::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const Arg0 &,const char *,const Arg1 &,const char *,const Arg2 &,const char *,const Arg3 &,const char *)'
P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp(16): note: 'bool boost::test_tools::tt_detail::check_frwd(Pred,const boost::unit_test::lazy_ostream &,boost::unit_test::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const Arg0 &,const char *,const Arg1 &,const char *,const Arg2 &,const char *,const Arg3 &,const char *)': expects 14 arguments - 7 provided
P:\Projects\wcigi\build\debug\vcpkg_installed\x64-windows\include\boost/test/tools/old/impl.hpp(92): note: or 'bool boost::test_tools::tt_detail::check_frwd(Pred,const boost::unit_test::lazy_ostream &,boost::unit_test::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const Arg0 &,const char *,const Arg1 &,const char *,const Arg2 &,const char *)'
P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp(16): note: 'bool boost::test_tools::tt_detail::check_frwd(Pred,const boost::unit_test::lazy_ostream &,boost::unit_test::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const Arg0 &,const char *,const Arg1 &,const char *,const Arg2 &,const char *)': expects 12 arguments - 7 provided
P:\Projects\wcigi\build\debug\vcpkg_installed\x64-windows\include\boost/test/tools/old/impl.hpp(92): note: or 'bool boost::test_tools::tt_detail::check_frwd(Pred,const boost::unit_test::lazy_ostream &,boost::unit_test::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const Arg0 &,const char *,const Arg1 &,const char *)'
P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp(16): note: 'bool boost::test_tools::tt_detail::check_frwd(Pred,const boost::unit_test::lazy_ostream &,boost::unit_test::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const Arg0 &,const char *,const Arg1 &,const char *)': expects 10 arguments - 7 provided
P:\Projects\wcigi\build\debug\vcpkg_installed\x64-windows\include\boost/test/tools/old/impl.hpp(92): note: or 'bool boost::test_tools::tt_detail::check_frwd(Pred,const boost::unit_test::lazy_ostream &,boost::unit_test::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const Arg0 &,const char *)'
P:\Projects\wcigi\tests\WCIGI\BufferTest.cpp(16): note: 'bool boost::test_tools::tt_detail::check_frwd(Pred,const boost::unit_test::lazy_ostream &,boost::unit_test::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const Arg0 &,const char *)': expects 8 arguments - 7 provided
ninja: build stopped: subcommand failed.
Build All failed.
I'm not sure that the problem is my code, because if I write the test in the following way, it compiles and runs correctly:
BOOST_AUTO_TEST_CASE(BufferConstructor) {
constexpr auto data{ "string" };
Buffer<6> buffer(data);
BOOST_CHECK_EQUAL(buffer.getValue<char>(0), 's');
BOOST_CHECK_EQUAL(buffer.getValue<char>(1), 't');
BOOST_CHECK_EQUAL(buffer.getValue<char>(2), 'r');
BOOST_CHECK_EQUAL(buffer.getValue<char>(3), 'i');
BOOST_CHECK_EQUAL(buffer.getValue<char>(4), 'n');
BOOST_CHECK_EQUAL(buffer.getValue<char>(5), 'g');
auto x = buffer.getValue<char, 0>();
BOOST_CHECK_EQUAL(x, 's');
}
Or even this way:
BOOST_AUTO_TEST_CASE(BufferConstructor) {
constexpr auto data{ "string" };
Buffer<6> buffer(data);
BOOST_CHECK_EQUAL(buffer.getValue<char>(0), 's');
BOOST_CHECK_EQUAL(buffer.getValue<char>(1), 't');
BOOST_CHECK_EQUAL(buffer.getValue<char>(2), 'r');
BOOST_CHECK_EQUAL(buffer.getValue<char>(3), 'i');
BOOST_CHECK_EQUAL(buffer.getValue<char>(4), 'n');
BOOST_CHECK_EQUAL(buffer.getValue<char>(5), 'g');
BOOST_CHECK_EQUAL((buffer.getValue<char, 0>()), 's'); // Look at extra parenthesis
}
So I don't know if the error is related to my code or if it's specific to boost::test
, because I cannot see any difference in the logic of the boost macro between the row with the error and other ones.
What I'm doing wrong?