File: make_static_test.cpp

package info (click to toggle)
cataclysm-dda 0.H-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 710,808 kB
  • sloc: cpp: 524,019; python: 11,580; sh: 1,228; makefile: 1,169; xml: 507; javascript: 150; sql: 56; exp: 41; perl: 37
file content (72 lines) | stat: -rw-r--r-- 2,271 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iosfwd>
#include <string>
#include <type_traits>

#include "cata_catch.h"
#include "make_static.h"
#include "string_id.h"

TEST_CASE( "make_static_macro_test", "[make_static_macro]" )
{
    using test_id_type = string_id<int>;

    CHECK( 1 == STATIC( 1 ) );
    CHECK_FALSE( 2 == STATIC( 1 ) );

    // NOLINTNEXTLINE(cata-static-string_id-constants)
    CHECK( test_id_type( "test1" ) == STATIC( test_id_type( "test1" ) ) );
    // NOLINTNEXTLINE(cata-static-string_id-constants)
    CHECK_FALSE( test_id_type( "test1" ) == STATIC( test_id_type( "test2" ) ) );

    // check the static scope
    auto get_static = []() -> const auto & {
        return STATIC( std::string( "test" ) );
    };

    // entity with the same address should be returned
    CHECK( &get_static() == &get_static() );

    // NOLINTNEXTLINE(cata-almost-never-auto)
    const auto test11 = STATIC( "test11" );
    static_assert( std::is_same_v<std::decay_t<decltype( test11 )>, std::string>,
                   "type must be std::string" );

    CHECK( test11 == "test11" );
    CHECK( STATIC( "test11" ) == "test11" );
    CHECK( STATIC( "test11" ) == STATIC( "test11" ) );
    CHECK_FALSE( STATIC( "test11" ) == STATIC( "test12" ) );
}

TEST_CASE( "make_static_macro_benchmark_string_id", "[.][make_static_macro][benchmark]" )
{
    using test_id_type = string_id<int>;

    // NOLINTNEXTLINE(cata-static-string_id-constants)
    static const test_id_type test_id( "test" );
    BENCHMARK( "static variable outside" ) {
        return test_id.is_empty();
    };

    BENCHMARK( "static variable inside" ) {
        // NOLINTNEXTLINE(cata-static-string_id-constants)
        static const test_id_type test_id( "test" );
        return test_id.is_empty();
    };

    BENCHMARK( "inline const" ) {
        // NOLINTNEXTLINE(cata-static-string_id-constants)
        return test_id_type( "test" ).is_empty();
    };

    BENCHMARK( "static in a lambda" ) {
        return ( []() -> const auto & {
            // NOLINTNEXTLINE(cata-static-string_id-constants)
            static const test_id_type test_id( "test" );
            return test_id;
        } )().is_empty();
    };

    BENCHMARK( "make_static macro" ) {
        return STATIC( test_id_type( "test" ) ).is_empty();
    };
}