diff --git a/src/test/pegtl/CMakeLists.txt b/src/test/pegtl/CMakeLists.txt index 0e70393d..c2d95ddb 100644 --- a/src/test/pegtl/CMakeLists.txt +++ b/src/test/pegtl/CMakeLists.txt @@ -199,6 +199,7 @@ set(test_sources uint32_general.cpp uint64_general.cpp uint8_general.cpp + unaligned_access.cpp utf16_general.cpp utf32_general.cpp utf8_general.cpp diff --git a/src/test/pegtl/unaligned_access.cpp b/src/test/pegtl/unaligned_access.cpp new file mode 100644 index 00000000..f0e163d8 --- /dev/null +++ b/src/test/pegtl/unaligned_access.cpp @@ -0,0 +1,71 @@ +// Copyright (c) 2024 Dr. Colin Hirsch and Daniel Frey +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt) + +#include "test.hpp" + +#include +#include +#include +#include + +namespace TAO_PEGTL_NAMESPACE +{ + void test16() + { + const char data[] = { 2, 2, 1, 2, 2 }; + base_input<> in( data, sizeof( data ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint16::one< 0x0202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint16::one< 0x0202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( in.empty() ); + } + + void test32() + { + const char data[] = { 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2 }; + base_input<> in( data, sizeof( data ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint32::one< 0x02020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint32::one< 0x02020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint32::one< 0x02020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint32::one< 0x02020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint32::one< 0x02020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( in.empty() ); + } + + void test64() + { + const char data[] = { 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2 }; + base_input<> in( data, sizeof( data ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint64::one< 0x0202020202020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint64::one< 0x0202020202020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint64::one< 0x0202020202020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint64::one< 0x0202020202020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint64::one< 0x0202020202020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint64::one< 0x0202020202020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint64::one< 0x0202020202020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint8::one< 0x01 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( parse< uint64::one< 0x0202020202020202 > >( in ) ); + TAO_PEGTL_TEST_ASSERT( in.empty() ); + } + + void unit_test() + { + test16(); + test32(); + test64(); + } + +} // namespace TAO_PEGTL_NAMESPACE + +#include "main.hpp"