File: map_test_case.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 (362 lines) | stat: -rw-r--r-- 9,708 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <algorithm>
#include <cstddef>
#include <iomanip>
#include <map>
#include <string>

#include "field.h"
#include "level_cache.h"
#include "map.h"
#include "map_test_case.h"

tripoint map_test_case::get_origin()
{
    if( origin ) {
        return *origin;
    }

    std::optional<point> res = std::nullopt;

    if( anchor_char ) {
        for_each_tile( tripoint_zero, [&]( map_test_case::tile & t ) {
            if( t.setup_c == *anchor_char ) {
                if( res ) {
                    FAIL( "Origin char '" << *anchor_char << "' is found more than once in setup" );
                }
                res = t.p.xy();
            }
        } );

        if( !res.has_value() ) {
            FAIL( "Origin char '" << *anchor_char << "' is not found in setup" );
        }
    } else {
        res = point_zero;
    }

    origin = anchor_map_pos - tripoint( *res, 0 );
    return *origin;
}

int map_test_case::get_width() const
{
    return setup[0].size();
}

int map_test_case::get_height() const
{
    return setup.size();
}

void map_test_case::for_each_tile( const tripoint &tmp_origin,
                                   const std::function<void( map_test_case::tile & )> &callback ) const
{
    int width = get_width();
    int height = get_height();

    tile tile;

    for( int y = 0; y < height; ++y ) {
        for( int x = 0; x < width; ++x ) {
            tile.setup_c = setup[y][x];
            tile.expect_c = expected_results[y][x];
            tile.p = tmp_origin + point( x, y );
            tile.p_local = point( x, y );
            callback( tile );
        }
    }
}

void map_test_case::for_each_tile( const std::function<void( tile )> &callback )
{
    do_internal_checks();
    if( !origin ) {
        origin = get_origin();
    }
    for_each_tile( *origin, callback );
}

std::vector<std::vector<std::string>> map_test_case::map_tiles_str(
                                       const std::function<void( tile, std::ostringstream & )> &callback )
{
    return map_tiles<std::string>( [&]( tile t ) {
        std::ostringstream out;
        callback( t, out );
        return out.str();
    } );
}

void map_test_case::do_internal_checks()
{
    if( checks_complete ) {
        return;
    }

    int height = get_height();
    REQUIRE( height > 0 );
    REQUIRE( static_cast<size_t>( height ) == expected_results.size() );
    int width = get_width();
    REQUIRE( width > 0 );

    for( const std::string &line : setup ) {
        CAPTURE( line );
        REQUIRE( line.size() == static_cast<size_t>( width ) );
    }

    for( const std::string &line : expected_results ) {
        CAPTURE( line );
        REQUIRE( line.size() == static_cast<size_t>( width ) );
    }

    checks_complete = true;
}

void map_test_case::transpose()
{
    origin = std::nullopt;
    checks_complete = false;

    auto transpose = []( std::vector<std::string> v ) {
        if( v.empty() ) {
            return;
        }
        std::vector<std::string> new_v( v[0].size() );

        for( const std::string &col : v ) {
            for( size_t y = 0; y < new_v.size(); ++y ) {
                new_v[y].push_back( col.at( y ) );
            }
        }

        v = new_v;
    };

    transpose( setup );
    transpose( expected_results );
}

void map_test_case::reflect_x()
{
    origin = std::nullopt;
    checks_complete = false;

    for( std::string &s : setup ) {
        std::reverse( s.begin(), s.end() );
    }
    for( std::string &s : expected_results ) {
        std::reverse( s.begin(), s.end() );
    }
}

void map_test_case::reflect_y()
{
    origin = std::nullopt;
    checks_complete = false;

    std::reverse( setup.begin(), setup.end() );
    std::reverse( expected_results.begin(), expected_results.end() );
}

void map_test_case::set_anchor_char_from( const std::set<char> &chars )
{
    for_each_tile( tripoint_zero, [&]( tile t ) {
        if( chars.count( t.setup_c ) ) {
            anchor_char = t.setup_c;
        }
    } );
}

std::string map_test_case::generate_transform_combinations()
{
    std::ostringstream out;
    if( GENERATE( false, true ) ) {
        out << "__transpose";
        transpose();
    }
    if( GENERATE( false, true ) ) {
        out << "__reflect_x";
        reflect_x();
    }
    if( GENERATE( false, true ) ) {
        out << "__reflect_y";
        reflect_y();
    }
    return out.str();
}

void map_test_case::validate_anchor_point( const tripoint &p )
{
    INFO( "checking point: " << p );
    tripoint origin_p = get_origin();
    REQUIRE( origin_p.z == p.z );
    REQUIRE( p == anchor_map_pos );
    // offset is anchor_map_pos in `map_test_case` local coords
    tripoint offset = p - origin_p;
    REQUIRE( offset.y >= 0 );
    REQUIRE( offset.y < get_height() );
    REQUIRE( offset.x >= 0 );
    REQUIRE( offset.x < get_width() );

    char setup_anchor_char = setup[offset.y][offset.x];
    REQUIRE( anchor_char );
    REQUIRE( setup_anchor_char == *anchor_char );
}

std::string map_test_case_common::printers::format_2d_array( const
        std::vector<std::vector<std::string>> &info )
{
    size_t max = 0;
    for( const auto &row : info ) {
        for( const std::string &el : row ) {
            max = std::max( max, el.size() );
        }
    }
    std::ostringstream buf;

    for( const auto &row : info ) {
        for( const std::string &el : row ) {
            buf << std::setw( max ) << el;
        }
        buf << '\n';
    }

    return buf.str();
}

static std::string print_and_format_helper( map_test_case &t, int zshift,
        std::function<void( const tripoint &p, std::ostringstream &out )> print_tile )
{
    tripoint shift = tripoint( point_zero, zshift );
    return map_test_case_common::printers::format_2d_array(
    t.map_tiles_str( [&]( map_test_case::tile t, std::ostringstream & out ) {
        print_tile( t.p + shift, out );
    } ) );
}

std::string map_test_case_common::printers::fields( map_test_case &t, int zshift )
{
    map &here = get_map();
    return print_and_format_helper( t, zshift, [&]( auto p, auto & out ) {
        bool first = true;
        for( auto &pr : here.field_at( p ) ) {
            out << ( first ? " " : "," ) << pr.second.name();
            first = false;
        }
    } );
}

std::string map_test_case_common::printers::transparency( map_test_case &t, int zshift )
{
    const level_cache &cache = get_map().access_cache( t.get_origin().z + zshift );
    return print_and_format_helper( t, zshift, [&]( auto p, auto & out ) {
        out << std::setprecision( 3 ) << cache.transparency_cache[p.x][p.y] << ' ';
    } );
}

std::string map_test_case_common::printers::seen( map_test_case &t, int zshift )
{
    const auto &cache = get_map().access_cache( t.get_origin().z + zshift ).seen_cache;
    return print_and_format_helper( t, zshift, [&]( auto p, auto & out ) {
        out << std::setprecision( 3 ) << cache[p.x][p.y] << ' ';
    } );
}

std::string map_test_case_common::printers::lm( map_test_case &t, int zshift )
{
    const level_cache &cache = get_map().access_cache( t.get_origin().z + zshift );
    return print_and_format_helper( t, zshift, [&]( auto p, auto & out ) {
        out << cache.lm[p.x][p.y].to_string() << ' ';
    } );
}

std::string map_test_case_common::printers::apparent_light( map_test_case &t, int zshift )
{
    const level_cache &cache = get_map().access_cache( t.get_origin().z + zshift );
    return print_and_format_helper( t, zshift, [&]( auto p, auto & out ) {
        out << std::setprecision( 3 ) << map::apparent_light_helper( cache, p ).apparent_light << ' ';
    } );
}

std::string map_test_case_common::printers::obstructed( map_test_case &t, int zshift )
{
    const level_cache &cache = get_map().access_cache( t.get_origin().z + zshift );
    return print_and_format_helper( t, zshift, [&]( auto p, auto & out ) {
        bool obs = map::apparent_light_helper( cache, p ).obstructed;
        out << ( obs ? '#' : '.' );
    } );
}

std::string map_test_case_common::printers::floor( map_test_case &t, int zshift )
{
    const level_cache &cache = get_map().access_cache( t.get_origin().z + zshift );
    return print_and_format_helper( t, zshift, [&]( auto p, auto & out ) {
        out << ( cache.floor_cache[p.x][p.y] ? '#' : '.' );
    } );
}

std::string map_test_case_common::printers::expected( map_test_case &t )
{
    return format_2d_array( t.map_tiles_str( [&]( map_test_case::tile t, std::ostringstream & out ) {
        out << t.expect_c;
    } ) );
}

// common helpers, used together with map_test_case
namespace map_test_case_common
{

tile_predicate operator+(
    const std::function<void( map_test_case::tile )> &f,
    const std::function<void( map_test_case::tile )> &g )
{
    return [ = ]( map_test_case::tile t ) {
        f( t );
        g( t );
        return true;
    };
}

tile_predicate operator&&( const tile_predicate &f, const tile_predicate &g )
{
    return [ = ]( map_test_case::tile t ) {
        return f( t ) && g( t );
    };
}

tile_predicate operator||( const tile_predicate &f, const tile_predicate &g )
{
    return [ = ]( map_test_case::tile t ) {
        return f( t ) || g( t );
    };
}

namespace tiles
{

tile_predicate ifchar( char c, const tile_predicate &f )
{
    return [ = ]( map_test_case::tile t ) {
        if( t.setup_c == c ) {
            f( t );
            return true;
        }
        return false;
    };
}

tile_predicate ter_set(
    ter_str_id ter,
    tripoint shift
)
{
    return [ = ]( map_test_case::tile t ) {
        REQUIRE( ter.is_valid() );
        tripoint p = t.p + shift;
        get_map().ter_set( p, ter );
        return true;
    };
}

} // namespace tiles

} // namespace map_test_case_common