Skip to content

Commit

Permalink
Test unaligned access.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Apr 23, 2024
1 parent 27ae103 commit 0449ab1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/test/pegtl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions src/test/pegtl/unaligned_access.cpp
Original file line number Diff line number Diff line change
@@ -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 <tao/pegtl/uint16.hpp>
#include <tao/pegtl/uint32.hpp>
#include <tao/pegtl/uint64.hpp>
#include <tao/pegtl/uint8.hpp>

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"

0 comments on commit 0449ab1

Please sign in to comment.