1 //
2 // gsl-lite is based on GSL: Guideline Support Library.
3 // For more information see https://github.com/martinmoene/gsl-lite
4 //
5 // Copyright (c) 2015 Martin Moene
6 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
7 //
8 // This code is licensed under the MIT License (MIT).
9 //
10 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16 // THE SOFTWARE.
17 
18 #include "gsl-lite.t.h"
19 
20 CASE( "span<>: free comparation functions fail for different const-ness" "[.issue #32]" )
21 {
22 #if gsl_CONFIG_ALLOWS_NONSTRICT_SPAN_COMPARISON
23     char data[] = { 'a', 'b' };
24     string_span  a = make_span( data );
25     cstring_span b = make_span( data ).last( 1 );
26 
27     // worked as expected before 0.9.0, in 0.9.[0,1] converts to bool and compares equal!
28 
29     EXPECT( a != b );
30 #else
31     EXPECT( !!"span<>: cannot compare different types (gsl_CONFIG_ALLOWS_NONSTRICT_SPAN_COMPARISON=0)" );
32 #endif
33 }
34 
35 CASE( "byte: aliasing rules lead to undefined behaviour when using enum class" "[.issue #34](GSL #313)" )
36 {
37     struct F {
fF38         static int f( int & i, gsl::byte & r )
39         {
40            i = 7;
41            r <<= 1;
42            return i;
43         }
44     };
45 
46    int i;
47    EXPECT( 14 == F::f( i, reinterpret_cast<gsl::byte&>( i ) ) );
48 }
49 
50 CASE( "string_span<>: must not include terminating '\\0'" "[.issue #53]" )
51 {
52     char const data[] = "ab";
53     char const * text = "ab";
54     cstring_span span = "ab";
55 
56     cstring_span a( data );
57     cstring_span b( ensure_z( text ) );
58 
59     // may include terminating '\0'
60     // works differently from M-GSL and basic_string_span proposal:
61 
62     EXPECT( span.length() == 2u );
63     EXPECT(    a.length() == 2u );
64     EXPECT(    b.length() == 2u );
65 
66     EXPECT( a == span );
67     EXPECT( a == b );
68 }
69 
70 CASE( "string_span<>: to_string triggers SFINAE errors on basic_string_span's move & copy constructor with Clang-3.9 (define gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS)" "[.issue #53a]" )
71 {
72 #if gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS
73     cstring_span span = "Hello world";
74     std::string str = to_string( span );
75 #endif
76 }
77 // end of file
78