1// Copyright 2020, 2021 Peter Dimov
2// Distributed under the Boost Software License, Version 1.0.
3// http://www.boost.org/LICENSE_1_0.txt
4
5#include <boost/assert/source_location.hpp>
6#include <boost/core/lightweight_test.hpp>
7#include <sstream>
8#include <cstring>
9
10static char const* adjust_filename( char const* file )
11{
12#if defined(__INTEL_LLVM_COMPILER) && __INTEL_LLVM_COMPILER >= 20210300
13
14 char const* fn = std::strrchr( file, '/' );
15 return fn? fn + 1: file;
16
17#else
18
19 return file;
20
21#endif
22}
23
24int main()
25{
26 {
27 boost::source_location loc;
28 BOOST_TEST_EQ( loc.to_string(), std::string( "(unknown source location)" ) );
29
30 std::ostringstream os;
31 os << loc;
32
33 BOOST_TEST_EQ( os.str(), loc.to_string() );
34 }
35
36 {
37 boost::source_location loc( "file", 5, "" );
38 BOOST_TEST_EQ( loc.to_string(), std::string( "file:5" ) );
39
40 std::ostringstream os;
41 os << loc;
42
43 BOOST_TEST_EQ( os.str(), loc.to_string() );
44 }
45
46 {
47 boost::source_location loc( "file", 7, "main()" );
48 BOOST_TEST_EQ( loc.to_string(), std::string( "file:7 in function 'main()'" ) );
49
50 std::ostringstream os;
51 os << loc;
52
53 BOOST_TEST_EQ( os.str(), loc.to_string() );
54 }
55
56 {
57 boost::source_location loc( "file", 11, "main()", 13 );
58 BOOST_TEST_EQ( loc.to_string(), std::string( "file:11:13 in function 'main()'" ) );
59
60 std::ostringstream os;
61 os << loc;
62
63 BOOST_TEST_EQ( os.str(), loc.to_string() );
64 }
65
66 {
67 boost::source_location loc( "file", 17, "", 19 );
68 BOOST_TEST_EQ( loc.to_string(), std::string( "file:17:19" ) );
69
70 std::ostringstream os;
71 os << loc;
72
73 BOOST_TEST_EQ( os.str(), loc.to_string() );
74 }
75
76 {
77 boost::source_location loc = BOOST_CURRENT_LOCATION;
78
79 std::string prefix = std::string( adjust_filename(__FILE__) ) + ":77";
80 BOOST_TEST_EQ( loc.to_string().substr( 0, prefix.size() ), prefix );
81
82 std::ostringstream os;
83 os << loc;
84
85 BOOST_TEST_EQ( os.str(), loc.to_string() );
86 }
87
88 return boost::report_errors();
89}
90

source code of boost/libs/assert/test/source_location_test3.cpp