Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Dec 9, 2024
1 parent 480c615 commit 3d97f7c
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 97 deletions.
5 changes: 0 additions & 5 deletions include/tao/pegtl/internal/mmap_file_posix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,6 @@ namespace TAO_PEGTL_NAMESPACE::internal
mmap_file_posix& operator=( const mmap_file_posix& ) = delete;
mmap_file_posix& operator=( mmap_file_posix&& ) = delete;

[[nodiscard]] bool empty() const noexcept
{
return m_size == 0;
}

[[nodiscard]] std::size_t size() const noexcept
{
return m_size;
Expand Down
5 changes: 0 additions & 5 deletions include/tao/pegtl/internal/mmap_file_win32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,6 @@ namespace TAO_PEGTL_NAMESPACE::internal
mmap_file_win32& operator=( const mmap_file_win32& ) = delete;
mmap_file_win32& operator=( mmap_file_win32&& ) = delete;

[[nodiscard]] bool empty() const noexcept
{
return m_size == 0;
}

[[nodiscard]] std::size_t size() const noexcept
{
return m_size;
Expand Down
1 change: 1 addition & 0 deletions src/test/pegtl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ set(test_sources
internal_base_inputs.cpp
internal_byteswap.cpp
internal_char_scan_traits.cpp
internal_constexpr.cpp
internal_copy_input.cpp
internal_data_and_size.cpp
internal_endian.cpp
Expand Down
10 changes: 6 additions & 4 deletions src/test/pegtl/debug_coverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace TAO_PEGTL_NAMESPACE
}

#if defined( __cpp_exceptions )
using grammar = seq< sor< try_catch_return_false< must< one< 'a' > > >, one< 'F' > >, eof >;
using grammar = seq< sor< try_catch_return_false< must< one< 'a' > > >, seq< at< one< 'F' > >, one< 'F' > >, digit >, eof >;

void unit_test()
{
Expand All @@ -35,14 +35,16 @@ namespace TAO_PEGTL_NAMESPACE
const bool success = coverage< grammar >( in, result );
std::cout << result; // To manually see that printing does the right thing, too.
TAO_PEGTL_TEST_ASSERT( success );
TAO_PEGTL_TEST_ASSERT( result.coverage.size() == 7 );
TAO_PEGTL_TEST_ASSERT( result.coverage.size() == 10 );
TAO_PEGTL_TEST_ASSERT( equals< grammar >( result, coverage_info{ 1, 1, 0, 0, 0 } ) );
TAO_PEGTL_TEST_ASSERT( equals< one< 'a' > >( result, coverage_info{ 1, 0, 1, 0, 1 } ) ); // TODO: Should this really be counted as both failure and raise?
TAO_PEGTL_TEST_ASSERT( equals< one< 'F' > >( result, coverage_info{ 1, 1, 0, 0, 0 } ) );
TAO_PEGTL_TEST_ASSERT( equals< one< 'F' > >( result, coverage_info{ 2, 2, 0, 0, 0 } ) );
TAO_PEGTL_TEST_ASSERT( equals< at< one< 'F' > > >( result, coverage_info{ 1, 1, 0, 0, 0 } ) );
TAO_PEGTL_TEST_ASSERT( equals< eof >( result, coverage_info{ 1, 1, 0, 0, 0 } ) );
TAO_PEGTL_TEST_ASSERT( equals< try_catch_return_false< must< one< 'a' > > > >( result, coverage_info{ 1, 0, 1, 0, 0 } ) );
TAO_PEGTL_TEST_ASSERT( equals< must< one< 'a' > > >( result, coverage_info{ 1, 0, 0, 1, 0 } ) );
TAO_PEGTL_TEST_ASSERT( equals< sor< try_catch_return_false< must< one< 'a' > > >, one< 'F' > > >( result, coverage_info{ 1, 1, 0, 0, 0 } ) );
TAO_PEGTL_TEST_ASSERT( equals< digit >( result, coverage_info{ 0, 0, 0, 0, 0 } ) );
TAO_PEGTL_TEST_ASSERT( equals< sor< try_catch_return_false< must< one< 'a' > > >, seq< at< one< 'F' > >, one< 'F' > >, digit > >( result, coverage_info{ 1, 1, 0, 0, 0 } ) );
}
#else
using grammar = seq< sor< one< 'a' >, one< 'F' > >, eof >;
Expand Down
173 changes: 173 additions & 0 deletions src/test/pegtl/internal_constexpr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// 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/utf16.hpp>
#include <tao/pegtl/utf32.hpp>

#include <tao/pegtl/int8.hpp>
#include <tao/pegtl/int16.hpp>
#include <tao/pegtl/int32.hpp>
#include <tao/pegtl/int64.hpp>

#include <tao/pegtl/uint8.hpp>
#include <tao/pegtl/uint16.hpp>
#include <tao/pegtl/uint32.hpp>
#include <tao/pegtl/uint64.hpp>

#include <tao/pegtl/enums.hpp>
#include <tao/pegtl/member.hpp>

namespace TAO_PEGTL_NAMESPACE
{
struct foo
{
int bar;
};

void unit_test()
{
using in8 = internal::view_input< std::uint8_t >;
using in16 = internal::view_input< std::uint16_t >;
using in32 = internal::view_input< std::uint32_t >;
using in64 = internal::view_input< std::uint64_t >;

TAO_PEGTL_TEST_ASSERT( internal::peek_char::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_char::size< in8 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_seven::bulk< in8 >() == false );
TAO_PEGTL_TEST_ASSERT( internal::peek_seven::size< in8 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_utf8::bulk< in8 >() == false );
TAO_PEGTL_TEST_ASSERT( internal::peek_utf8::size< in8 >() == 0 );

TAO_PEGTL_TEST_ASSERT( internal::peek_utf16_be::bulk< in8 >() == false );
TAO_PEGTL_TEST_ASSERT( internal::peek_utf16_be::size< in8 >() == 0 );

TAO_PEGTL_TEST_ASSERT( internal::peek_utf16_le::bulk< in16 >() == false );
TAO_PEGTL_TEST_ASSERT( internal::peek_utf16_le::size< in16 >() == 0 );

TAO_PEGTL_TEST_ASSERT( internal::peek_utf32_be::bulk< in8 >() == false );
TAO_PEGTL_TEST_ASSERT( internal::peek_utf32_be::size< in8 >() == 4 );

TAO_PEGTL_TEST_ASSERT( internal::peek_utf32_le::bulk< in32 >() == false );
TAO_PEGTL_TEST_ASSERT( internal::peek_utf32_le::size< in32 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_int8::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_int8::size< in8 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_int16_be::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_int16_be::size< in8 >() == 2 );

TAO_PEGTL_TEST_ASSERT( internal::peek_int16_le::bulk< in16 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_int16_le::size< in16 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_int32_be::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_int32_be::size< in8 >() == 4 );

TAO_PEGTL_TEST_ASSERT( internal::peek_int32_le::bulk< in32 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_int32_le::size< in32 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_int64_be::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_int64_be::size< in8 >() == 8 );

TAO_PEGTL_TEST_ASSERT( internal::peek_int64_le::bulk< in64 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_int64_le::size< in64 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_uint8::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_uint8::size< in8 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_uint16_be::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_uint16_be::size< in8 >() == 2 );

TAO_PEGTL_TEST_ASSERT( internal::peek_uint16_le::bulk< in16 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_uint16_le::size< in16 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_uint32_be::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_uint32_be::size< in8 >() == 4 );

TAO_PEGTL_TEST_ASSERT( internal::peek_uint32_le::bulk< in32 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_uint32_le::size< in32 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_uint64_be::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_uint64_be::size< in8 >() == 8 );

TAO_PEGTL_TEST_ASSERT( internal::peek_uint64_le::bulk< in64 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_uint64_le::size< in64 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int8< 42 >::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int8< 43 >::size< in8 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int16_be< 42 >::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int16_be< 43 >::size< in8 >() == 2 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int16_le< 42 >::bulk< in16 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int16_le< 43 >::size< in16 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int32_be< 42 >::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int32_be< 43 >::size< in8 >() == 4 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int32_le< 42 >::bulk< in32 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int32_le< 43 >::size< in32 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int64_be< 42 >::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int64_be< 43 >::size< in8 >() == 8 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int64_le< 42 >::bulk< in64 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_int64_le< 43 >::size< in64 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint8< 42 >::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint8< 43 >::size< in8 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint16_be< 42 >::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint16_be< 43 >::size< in8 >() == 2 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint16_le< 42 >::bulk< in16 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint16_le< 43 >::size< in16 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint32_be< 42 >::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint32_be< 43 >::size< in8 >() == 4 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint32_le< 42 >::bulk< in32 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint32_le< 43 >::size< in32 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint64_be< 42 >::bulk< in8 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint64_be< 43 >::size< in8 >() == 8 );

TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint64_le< 42 >::bulk< in64 >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_mask_uint64_le< 43 >::size< in64 >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::peek_member< &foo::bar >::bulk< void >() == true );
TAO_PEGTL_TEST_ASSERT( internal::peek_member< &foo::bar >::size< void >() == 1 );

TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_1( 0x0000 ) == true );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_1( 0x007f ) == true );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_1( 0x0080 ) == false );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_1( 0xffff ) == false );

TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_2( 0x007f ) == false );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_2( 0x0080 ) == true );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_2( 0x07ff ) == true );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_2( 0x0800 ) == false );

TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_3( 0x007ff ) == false );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_3( 0x00800 ) == true );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_3( 0x0ffff ) == true );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_3( 0x10000 ) == false );

TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_3( 0x0d7ff ) == true );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_3( 0x0d800 ) == false );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_3( 0x0dfff ) == false );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_3( 0x0e000 ) == true );

TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_4( 0x00ffff ) == false );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_4( 0x010000 ) == true );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_4( 0x10ffff ) == true );
TAO_PEGTL_TEST_ASSERT( internal::has_utf8_length_4( 0x110000 ) == false );
}

} // namespace TAO_PEGTL_NAMESPACE

#include "main.hpp"
45 changes: 16 additions & 29 deletions src/test/pegtl/internal_file_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,37 @@
// 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)

#if !defined( __cpp_exceptions ) || !defined( _POSIX_MAPPED_FILES )
#include <iostream>
int main()
{
std::cout << "Exception support disabled, skipping test..." << std::endl;
}
#else

#include <tao/pegtl/file_input.hpp>

#include "test.hpp"

#if defined( __cpp_exceptions ) && defined( _POSIX_MAPPED_FILES )

namespace TAO_PEGTL_NAMESPACE
{
void unit_test()
{
try {
internal::file_mapper dummy( "include" );
// LCOV_EXCL_START
std::cerr << "pegtl: unit test failed for [ internal::file_mapper ]" << std::endl;
++failed;
// LCOV_EXCL_STOP
}
catch( const std::filesystem::filesystem_error& ) {
}
// LCOV_EXCL_START
catch( ... ) {
std::cerr << "pegtl: unit test failed for [ internal::file_mapper ] with unexpected exception" << std::endl;
++failed;
}
// LCOV_EXCL_STOP
TAO_PEGTL_TEST_THROWS( internal::mmap_file_impl( "include" ) );

const std::string s = "dummy content\n";
const std::string dummy_content = s + s + s + s + s + s + s + s + s + s + s;

internal::file_mapper mapper( "src/test/pegtl/data/test_data.txt" );
TAO_PEGTL_TEST_ASSERT( !mapper.empty() );
internal::mmap_file_impl mapper( "src/test/pegtl/data/test_data.txt" );
TAO_PEGTL_TEST_ASSERT( mapper.size() == 154 );
TAO_PEGTL_TEST_ASSERT( std::string_view( mapper.data(), mapper.size() ) == dummy_content );
TAO_PEGTL_TEST_ASSERT( std::string_view( mapper.begin(), mapper.end() - mapper.begin() ) == dummy_content );
}

} // namespace TAO_PEGTL_NAMESPACE

#include "main.hpp"
#else

namespace TAO_PEGTL_NAMESPACE
{
void unit_test()
{
std::cerr << "Skipping mmap test..." << std::endl;
}

} // namespace TAO_PEGTL_NAMESPACE

#endif

#include "main.hpp"
39 changes: 0 additions & 39 deletions src/test/pegtl/internal_file_opener.cpp

This file was deleted.

7 changes: 6 additions & 1 deletion src/test/pegtl/internal_lazy_scan_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace TAO_PEGTL_NAMESPACE
{
text_position pos;
internal::scan_input< char > in( str.data(), str.size() );
internal::lazy_scan_traits< typename Eol::rule_t, internal::peek_char >::scan( pos, in );
internal::lazy_scan_traits< typename Eol::rule_t, internal::peek_seven >::scan( pos, in );
TAO_PEGTL_TEST_ASSERT( test::equal( pos, line, column, count ) );
}

Expand Down Expand Up @@ -77,6 +77,11 @@ namespace TAO_PEGTL_NAMESPACE
lazy_test< ascii::lf_crlf >( "\n\r", 2, 2, 2 );
lazy_test< ascii::lf_crlf >( "\r\n", 2, 1, 2 );
lazy_test< ascii::lf_crlf >( " \r\n ", 2, 3, 6 );

#if defined( __cpp_exceptions )
const auto c = char( 0x80 );
TAO_PEGTL_TEST_THROWS( lazy_test< ascii::lf_crlf >( std::string( c, 1 ), 0, 0, 0 ) );
#endif
}

} // namespace TAO_PEGTL_NAMESPACE
Expand Down
Loading

0 comments on commit 3d97f7c

Please sign in to comment.