Completed
Push — master ( 1e7b7c...87d517 )
by Kyle
01:04
created

test_dijkstra()   B

Complexity

Conditions 8

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
dl 0
loc 21
rs 7.3333
c 0
b 0
f 0
1
#!/usr/bin/env python
2
3
import pytest
4
5
try:
6
    import numpy
7
except ImportError:
8
    numpy = None
9
10
import libtcodpy
11
12
def test_console_behaviour(console):
13
    assert not console
14
15
@pytest.mark.skip('takes too long')
16
def test_credits_long(console):
17
    libtcodpy.console_credits()
18
19
def test_credits(console):
20
    libtcodpy.console_credits_render(0, 0, True)
21
    libtcodpy.console_credits_reset()
22
23
def assert_char(console, x, y, ch=None, fg=None, bg=None):
24
    if ch is not None:
25
        try:
26
            ch = ord(ch)
27
        except TypeError:
28
            pass
29
        assert libtcodpy.console_get_char(console, x, y) == ch
30
    if fg is not None:
31
        assert libtcodpy.console_get_char_foreground(console, x, y) == fg
32
    if bg is not None:
33
        assert libtcodpy.console_get_char_background(console, x, y) == bg
34
35
def test_console_defaults(console, fg, bg):
36
    libtcodpy.console_set_default_foreground(console, fg)
37
    libtcodpy.console_set_default_background(console, bg)
38
    libtcodpy.console_clear(console)
39
    assert_char(console, 0, 0, None, fg, bg)
40
41
def test_console_set_char_background(console, bg):
42
    libtcodpy.console_set_char_background(console, 0, 0, bg, libtcodpy.BKGND_SET)
43
    assert_char(console, 0, 0, bg=bg)
44
45
def test_console_set_char_foreground(console, fg):
46
    libtcodpy.console_set_char_foreground(console, 0, 0, fg)
47
    assert_char(console, 0, 0, fg=fg)
48
49
def test_console_set_char(console, ch):
50
    libtcodpy.console_set_char(console, 0, 0, ch)
51
    assert_char(console, 0, 0, ch=ch)
52
53
def test_console_put_char(console, ch):
54
    libtcodpy.console_put_char(console, 0, 0, ch, libtcodpy.BKGND_SET)
55
    assert_char(console, 0, 0, ch=ch)
56
57
def console_put_char_ex(console, ch, fg, bg):
58
    libtcodpy.console_put_char_ex(console, 0, 0, ch, fg, bg)
59
    assert_char(console, 0, 0, ch=ch, fg=fg, bg=bg)
60
61
def test_console_printing(console, fg, bg):
62
    libtcodpy.console_set_background_flag(console,
63
                                          libtcodpy.BKGND_SET)
64
    assert (libtcodpy.console_get_background_flag(console) ==
65
                     libtcodpy.BKGND_SET)
66
67
    libtcodpy.console_set_alignment(console, libtcodpy.LEFT)
68
    assert (libtcodpy.console_get_alignment(console) ==
69
                     libtcodpy.LEFT)
70
71
    libtcodpy.console_print(console, 0, 0, 'print')
72
    libtcodpy.console_print_ex(console, 0, 0, libtcodpy.BKGND_SET,
73
                               libtcodpy.LEFT, 'print ex')
74
75
    assert (libtcodpy.console_print_rect(
76
        console, 0, 0, 8, 8, 'print rect') > 0
77
        )
78
    assert (libtcodpy.console_print_rect_ex(
79
        console, 0, 0, 8, 8, libtcodpy.BKGND_SET, libtcodpy.LEFT,
80
        'print rect ex') > 0
81
        )
82
    assert (libtcodpy.console_get_height_rect(
83
        console, 0, 0, 8, 8, 'get height') > 0
84
        )
85
86
    libtcodpy.console_set_color_control(libtcodpy.COLCTRL_1, fg, bg)
87
88
def test_console_rect(console):
89
    libtcodpy.console_rect(console, 0, 0, 4, 4, False, libtcodpy.BKGND_SET)
90
91
def test_console_lines(console):
92
    libtcodpy.console_hline(console, 0, 0, 4)
93
    libtcodpy.console_vline(console, 0, 0, 4)
94
95
def test_console_print_frame(console):
96
    libtcodpy.console_print_frame(console, 0, 0, 9, 9)
97
98
def test_console_fade(console):
99
    libtcodpy.console_set_fade(0, libtcodpy.Color(0, 0, 0))
100
    libtcodpy.console_get_fade()
101
    libtcodpy.console_get_fading_color()
102
103
def assertConsolesEqual(a, b):
104
    for y in range(libtcodpy.console_get_height(a)):
105
        for x in range(libtcodpy.console_get_width(a)):
106
            assert libtcodpy.console_get_char(a, x, y) == \
107
                libtcodpy.console_get_char(b, x, y)
108
            assert libtcodpy.console_get_char_foreground(a, x, y) == \
109
                libtcodpy.console_get_char_foreground(b, x, y)
110
            assert libtcodpy.console_get_char_background(a, x, y) == \
111
                libtcodpy.console_get_char_background(b, x, y)
112
113
def test_console_blit(console, offscreen):
114
    libtcodpy.console_print(offscreen, 0, 0, 'test')
115
    libtcodpy.console_blit(offscreen, 0, 0, 0, 0, console, 0, 0, 1, 1)
116
    assertConsolesEqual(console, offscreen)
117
    libtcodpy.console_set_key_color(offscreen, libtcodpy.black)
118
119
def test_console_asc_read_write(console, offscreen, tmpdir):
120
    libtcodpy.console_print(console, 0, 0, 'test')
121
122
    asc_file = tmpdir.join('test.asc').strpath
123
    assert libtcodpy.console_save_asc(console, asc_file)
124
    assert libtcodpy.console_load_asc(offscreen, asc_file)
125
    assertConsolesEqual(console, offscreen)
126
127
def test_console_apf_read_write(console, offscreen, tmpdir):
128
    libtcodpy.console_print(console, 0, 0, 'test')
129
130
    apf_file = tmpdir.join('test.apf').strpath
131
    assert libtcodpy.console_save_apf(console, apf_file)
132
    assert libtcodpy.console_load_apf(offscreen, apf_file)
133
    assertConsolesEqual(console, offscreen)
134
135
def test_console_rexpaint_load_test_file(console):
136
    xp_console = libtcodpy.console_from_xp('libtcod/data/rexpaint/test.xp')
137
    assert xp_console
138
    assert libtcodpy.console_get_char(xp_console, 0, 0) == ord('T')
139
    assert libtcodpy.console_get_char(xp_console, 1, 0) == ord('e')
140
    assert (libtcodpy.console_get_char_background(xp_console, 0, 1) ==
141
            libtcodpy.Color(255, 0, 0))
142
    assert (libtcodpy.console_get_char_background(xp_console, 1, 1) ==
143
            libtcodpy.Color(0, 255, 0))
144
    assert (libtcodpy.console_get_char_background(xp_console, 2, 1) ==
145
            libtcodpy.Color(0, 0, 255))
146
147
def test_console_rexpaint_save_load(console, tmpdir, ch, fg, bg):
148
    libtcodpy.console_print(console, 0, 0, 'test')
149
    libtcodpy.console_put_char_ex(console, 1, 1, ch, fg, bg)
150
    xp_file = tmpdir.join('test.xp').strpath
151
    assert libtcodpy.console_save_xp(console, xp_file, 1)
152
    xp_console = libtcodpy.console_from_xp(xp_file)
153
    assert xp_console
154
    assertConsolesEqual(console, xp_console)
155
    assert libtcodpy.console_load_xp(None, xp_file)
156
    assertConsolesEqual(console, xp_console)
157
158
def test_console_rexpaint_list_save_load(console, tmpdir):
159
    con1 = libtcodpy.console_new(8, 2)
160
    con2 = libtcodpy.console_new(8, 2)
161
    libtcodpy.console_print(con1, 0, 0, 'hello')
162
    libtcodpy.console_print(con2, 0, 0, 'world')
163
    xp_file = tmpdir.join('test.xp').strpath
164
    assert libtcodpy.console_list_save_xp([con1, con2], xp_file, 1)
165
    for a, b in zip([con1, con2], libtcodpy.console_list_load_xp(xp_file)):
166
        assertConsolesEqual(a, b)
167
        libtcodpy.console_delete(a)
168
        libtcodpy.console_delete(b)
169
170
def test_console_fullscreen(console):
171
    libtcodpy.console_set_fullscreen(False)
172
173
def test_console_key_input(console):
174
    libtcodpy.console_check_for_keypress()
175
    libtcodpy.console_is_key_pressed(libtcodpy.KEY_ENTER)
176
177
def test_console_fill_errors(console):
178
    with pytest.raises(TypeError):
179
        libtcodpy.console_fill_background(console, [0], [], [])
180
    with pytest.raises(TypeError):
181
        libtcodpy.console_fill_foreground(console, [0], [], [])
182
183
def test_console_fill(console):
184
    width = libtcodpy.console_get_width(console)
185
    height = libtcodpy.console_get_height(console)
186
    fill = [i % 256 for i in range(width * height)]
187
    libtcodpy.console_fill_background(console, fill, fill, fill)
188
    libtcodpy.console_fill_foreground(console, fill, fill, fill)
189
    libtcodpy.console_fill_char(console, fill)
190
191
    # verify fill
192
    bg, fg, ch = [], [], []
193
    for y in range(height):
194
        for x in range(width):
195
            bg.append(libtcodpy.console_get_char_background(console, x, y)[0])
196
            fg.append(libtcodpy.console_get_char_foreground(console, x, y)[0])
197
            ch.append(libtcodpy.console_get_char(console, x, y))
198
    assert fill == bg
199
    assert fill == fg
200
    assert fill == ch
201
202
@pytest.mark.skipif(not numpy, reason='requires numpy module')
203
def test_console_fill_numpy(console):
204
    width = libtcodpy.console_get_width(console)
205
    height = libtcodpy.console_get_height(console)
206
    fill = numpy.zeros((height, width), dtype=numpy.intc)
207
    for y in range(height):
208
        fill[y, :] = y % 256
209
210
    libtcodpy.console_fill_background(console, fill, fill, fill)
211
    libtcodpy.console_fill_foreground(console, fill, fill, fill)
212
    libtcodpy.console_fill_char(console, fill)
213
214
    # verify fill
215
    bg = numpy.zeros((height, width), dtype=numpy.intc)
216
    fg = numpy.zeros((height, width), dtype=numpy.intc)
217
    ch = numpy.zeros((height, width), dtype=numpy.intc)
218
    for y in range(height):
219
        for x in range(width):
220
            bg[y, x] = libtcodpy.console_get_char_background(console, x, y)[0]
221
            fg[y, x] = libtcodpy.console_get_char_foreground(console, x, y)[0]
222
            ch[y, x] = libtcodpy.console_get_char(console, x, y)
223
    fill = fill.tolist()
224
    assert fill == bg.tolist()
225
    assert fill == fg.tolist()
226
    assert fill == ch.tolist()
227
228
def test_console_buffer(console):
229
    buffer = libtcodpy.ConsoleBuffer(
230
        libtcodpy.console_get_width(console),
231
        libtcodpy.console_get_height(console),
232
    )
233
    buffer = buffer.copy()
234
    buffer.set_fore(0, 0, 0, 0, 0, '@')
235
    buffer.set_back(0, 0, 0, 0, 0)
236
    buffer.set(0, 0, 0, 0, 0, 0, 0, 0, '@')
237
    buffer.blit(console)
238
239
def test_console_buffer_error(console):
240
    buffer = libtcodpy.ConsoleBuffer(0, 0)
241
    with pytest.raises(ValueError):
242
        buffer.blit(console)
243
244
def test_console_font_mapping(console):
245
    libtcodpy.console_map_ascii_code_to_font('@', 1, 1)
246
    libtcodpy.console_map_ascii_codes_to_font('@', 1, 0, 0)
247
    libtcodpy.console_map_string_to_font('@', 0, 0)
248
249
def test_mouse(console):
250
    libtcodpy.mouse_show_cursor(True)
251
    libtcodpy.mouse_is_cursor_visible()
252
    mouse = libtcodpy.mouse_get_status()
253
    repr(mouse)
254
    libtcodpy.mouse_move(0, 0)
255
256
def test_sys_time(console):
257
    libtcodpy.sys_set_fps(0)
258
    libtcodpy.sys_get_fps()
259
    libtcodpy.sys_get_last_frame_length()
260
    libtcodpy.sys_sleep_milli(0)
261
    libtcodpy.sys_elapsed_milli()
262
    libtcodpy.sys_elapsed_seconds()
263
264
def test_sys_screenshot(console, tmpdir):
265
    libtcodpy.sys_save_screenshot(tmpdir.join('test.png').strpath)
266
267
def test_sys_custom_render(console):
268
    if libtcodpy.sys_get_renderer() != libtcodpy.RENDERER_SDL:
269
        pytest.xfail(reason='Only supports SDL')
270
271
    escape = []
272
    def sdl_callback(sdl_surface):
273
        escape.append(True)
274
        libtcodpy.console_set_dirty(0, 0, 0, 0)
275
    libtcodpy.sys_register_SDL_renderer(sdl_callback)
276
    libtcodpy.console_flush()
277
    assert escape, 'proof that sdl_callback was called'
278
279
def test_image(console, tmpdir):
280
    img = libtcodpy.image_new(16, 16)
281
    libtcodpy.image_clear(img, libtcodpy.Color(0, 0, 0))
282
    libtcodpy.image_invert(img)
283
    libtcodpy.image_hflip(img)
284
    libtcodpy.image_rotate90(img)
285
    libtcodpy.image_vflip(img)
286
    libtcodpy.image_scale(img, 24, 24)
287
    libtcodpy.image_set_key_color(img, libtcodpy.Color(255, 255, 255))
288
    libtcodpy.image_get_alpha(img, 0, 0)
289
    libtcodpy.image_is_pixel_transparent(img, 0, 0)
290
    libtcodpy.image_get_size(img)
291
    libtcodpy.image_get_pixel(img, 0, 0)
292
    libtcodpy.image_get_mipmap_pixel(img, 0, 0, 1, 1)
293
    libtcodpy.image_put_pixel(img, 0, 0, libtcodpy.Color(255, 255, 255))
294
    libtcodpy.image_blit(img, console, 0, 0,
295
                         libtcodpy.BKGND_SET, 1, 1, 0)
296
    libtcodpy.image_blit_rect(img, console, 0, 0, 16, 16,
297
                              libtcodpy.BKGND_SET)
298
    libtcodpy.image_blit_2x(img, console, 0, 0)
299
    libtcodpy.image_save(img, tmpdir.join('test.png').strpath)
300
    libtcodpy.image_delete(img)
301
302
    img = libtcodpy.image_from_console(console)
303
    libtcodpy.image_refresh_console(img, console)
304
    libtcodpy.image_delete(img)
305
306
    libtcodpy.image_delete(libtcodpy.image_load('libtcod/data/img/circle.png'))
307
308
@pytest.mark.parametrize('sample', ['@', u'\u2603']) # Unicode snowman
309
def test_clipboard(console, sample):
310
    saved = libtcodpy.sys_clipboard_get()
311
    try:
312
        libtcodpy.sys_clipboard_set(sample)
313
        assert libtcodpy.sys_clipboard_get() == sample
314
    finally:
315
        libtcodpy.sys_clipboard_set(saved)
316
317
318
# arguments to test with and the results expected from these arguments
319
LINE_ARGS = (-5, 0, 5, 10)
320
EXCLUSIVE_RESULTS = [(-4, 1), (-3, 2), (-2, 3), (-1, 4), (0, 5), (1, 6),
321
                     (2, 7), (3, 8), (4, 9), (5, 10)]
322
INCLUSIVE_RESULTS = [(-5, 0)] + EXCLUSIVE_RESULTS
323
324
def test_line_step():
325
    """
326
    libtcodpy.line_init and libtcodpy.line_step
327
    """
328
    libtcodpy.line_init(*LINE_ARGS)
329
    for expected_xy in EXCLUSIVE_RESULTS:
330
        assert libtcodpy.line_step() == expected_xy
331
    assert libtcodpy.line_step() == (None, None)
332
333
def test_line():
334
    """
335
    tests normal use, lazy evaluation, and error propagation
336
    """
337
    # test normal results
338
    test_result = []
339
    def line_test(*test_xy):
340
        test_result.append(test_xy)
341
        return 1
342
    assert libtcodpy.line(*LINE_ARGS, py_callback=line_test) == 1
343
    assert test_result == INCLUSIVE_RESULTS
344
345
    # test lazy evaluation
346
    test_result = []
347
    def return_false(*test_xy):
348
        test_result.append(test_xy)
349
        return False
350
    assert libtcodpy.line(*LINE_ARGS, py_callback=return_false) == 0
351
    assert test_result == INCLUSIVE_RESULTS[:1]
352
353
def test_line_iter():
354
    """
355
    libtcodpy.line_iter
356
    """
357
    assert list(libtcodpy.line_iter(*LINE_ARGS)) == INCLUSIVE_RESULTS
358
359
def test_bsp():
360
    """
361
    commented out statements work in libtcod-cffi
362
    """
363
    bsp = libtcodpy.bsp_new_with_size(0, 0, 64, 64)
364
    repr(bsp) # test __repr__ on leaf
365
    libtcodpy.bsp_resize(bsp, 0, 0, 32, 32)
366
    assert bsp != None
367
368
    # test getter/setters
369
    bsp.x = bsp.x
370
    bsp.y = bsp.y
371
    bsp.w = bsp.w
372
    bsp.h = bsp.h
373
    bsp.position = bsp.position
374
    bsp.horizontal = bsp.horizontal
375
    bsp.level = bsp.level
376
377
    # cover functions on leaf
378
    #self.assertFalse(libtcodpy.bsp_left(bsp))
379
    #self.assertFalse(libtcodpy.bsp_right(bsp))
380
    #self.assertFalse(libtcodpy.bsp_father(bsp))
381
    assert libtcodpy.bsp_is_leaf(bsp)
382
383
    assert libtcodpy.bsp_contains(bsp, 1, 1)
384
    #self.assertFalse(libtcodpy.bsp_contains(bsp, -1, -1))
385
    #self.assertEqual(libtcodpy.bsp_find_node(bsp, 1, 1), bsp)
386
    #self.assertFalse(libtcodpy.bsp_find_node(bsp, -1, -1))
387
388
    libtcodpy.bsp_split_once(bsp, False, 4)
389
    repr(bsp) # test __repr__ with parent
390
    libtcodpy.bsp_split_once(bsp, True, 4)
391
    repr(bsp)
392
393
    # cover functions on parent
394
    assert libtcodpy.bsp_left(bsp)
395
    assert libtcodpy.bsp_right(bsp)
396
    #self.assertFalse(libtcodpy.bsp_father(bsp))
397
    assert not libtcodpy.bsp_is_leaf(bsp)
398
    #self.assertEqual(libtcodpy.bsp_father(libtcodpy.bsp_left(bsp)), bsp)
399
    #self.assertEqual(libtcodpy.bsp_father(libtcodpy.bsp_right(bsp)), bsp)
400
401
    libtcodpy.bsp_split_recursive(bsp, None, 4, 2, 2, 1.0, 1.0)
402
403
    # cover bsp_traverse
404
    def traverse(node, user_data):
405
        return True
406
407
    libtcodpy.bsp_traverse_pre_order(bsp, traverse)
408
    libtcodpy.bsp_traverse_in_order(bsp, traverse)
409
    libtcodpy.bsp_traverse_post_order(bsp, traverse)
410
    libtcodpy.bsp_traverse_level_order(bsp, traverse)
411
    libtcodpy.bsp_traverse_inverted_level_order(bsp, traverse)
412
413
    # test __repr__ on deleted node
414
    son = libtcodpy.bsp_left(bsp)
415
    libtcodpy.bsp_remove_sons(bsp)
416
    repr(son)
417
418
    libtcodpy.bsp_delete(bsp)
419
420
def test_map():
421
    map = libtcodpy.map_new(16, 16)
422
    assert libtcodpy.map_get_width(map) == 16
423
    assert libtcodpy.map_get_height(map) == 16
424
    libtcodpy.map_copy(map, map)
425
    libtcodpy.map_clear(map)
426
    libtcodpy.map_set_properties(map, 0, 0, True, True)
427
    assert libtcodpy.map_is_transparent(map, 0, 0)
428
    assert libtcodpy.map_is_walkable(map, 0, 0)
429
    libtcodpy.map_is_in_fov(map, 0, 0)
430
    libtcodpy.map_delete(map)
431
432
def test_color():
433
    color_a = libtcodpy.Color(0, 1, 2)
434
    assert list(color_a) == [0, 1, 2]
435
    assert color_a[0] == color_a.r
436
    assert color_a[1] == color_a.g
437
    assert color_a[2] == color_a.b
438
439
    color_a[1] = 3
440
    color_a['b'] = color_a['b']
441
    assert list(color_a) == [0, 3, 2]
442
443
    assert color_a == color_a
444
445
    color_b = libtcodpy.Color(255, 255, 255)
446
    assert color_a != color_b
447
448
    color = libtcodpy.color_lerp(color_a, color_b, 0.5)
449
    libtcodpy.color_set_hsv(color, 0, 0, 0)
450
    libtcodpy.color_get_hsv(color)
451
    libtcodpy.color_scale_HSV(color, 0, 0)
452
453
def test_color_repr():
454
    Color = libtcodpy.Color
455
    col = Color(0, 1, 2)
456
    assert eval(repr(col)) == col
457
458
def test_color_math():
459
    color_a = libtcodpy.Color(0, 1, 2)
460
    color_b = libtcodpy.Color(0, 10, 20)
461
462
    assert color_a + color_b == libtcodpy.Color(0, 11, 22)
463
    assert color_b - color_a == libtcodpy.Color(0, 9, 18)
464
    assert libtcodpy.Color(255, 255, 255) * color_a == color_a
465
    assert color_a * 100 == libtcodpy.Color(0, 100, 200)
466
467
def test_color_gen_map():
468
    colors = libtcodpy.color_gen_map([(0, 0, 0), (255, 255, 255)], [0, 8])
469
    assert colors[0] == libtcodpy.Color(0, 0, 0)
470
    assert colors[-1] == libtcodpy.Color(255, 255, 255)
471
472
def test_namegen_parse():
473
    libtcodpy.namegen_parse('libtcod/data/namegen/jice_celtic.cfg')
474
    assert libtcodpy.namegen_generate('Celtic female')
475
    assert libtcodpy.namegen_get_sets()
476
    libtcodpy.namegen_destroy()
477
478
def test_noise():
479
    noise = libtcodpy.noise_new(1)
480
    libtcodpy.noise_set_type(noise, libtcodpy.NOISE_SIMPLEX)
481
    libtcodpy.noise_get(noise, [0])
482
    libtcodpy.noise_get_fbm(noise, [0], 4)
483
    libtcodpy.noise_get_turbulence(noise, [0], 4)
484
    libtcodpy.noise_delete(noise)
485
486
def test_random():
487
    rand = libtcodpy.random_get_instance()
488
    rand = libtcodpy.random_new()
489
    libtcodpy.random_delete(rand)
490
    rand = libtcodpy.random_new_from_seed(42)
491
    libtcodpy.random_set_distribution(rand, libtcodpy.DISTRIBUTION_LINEAR)
492
    libtcodpy.random_get_int(rand, 0, 1)
493
    libtcodpy.random_get_int_mean(rand, 0, 1, 0)
494
    libtcodpy.random_get_float(rand, 0, 1)
495
    libtcodpy.random_get_double(rand, 0, 1)
496
    libtcodpy.random_get_float_mean(rand, 0, 1, 0)
497
    libtcodpy.random_get_double_mean(rand, 0, 1, 0)
498
499
    backup = libtcodpy.random_save(rand)
500
    libtcodpy.random_restore(rand, backup)
501
502
    libtcodpy.random_delete(rand)
503
    libtcodpy.random_delete(backup)
504
505
def test_heightmap():
506
    hmap = libtcodpy.heightmap_new(16, 16)
507
    repr(hmap)
508
    noise = libtcodpy.noise_new(2)
509
510
    # basic operations
511
    libtcodpy.heightmap_set_value(hmap, 0, 0, 1)
512
    libtcodpy.heightmap_add(hmap, 1)
513
    libtcodpy.heightmap_scale(hmap, 1)
514
    libtcodpy.heightmap_clear(hmap)
515
    libtcodpy.heightmap_clamp(hmap, 0, 0)
516
    libtcodpy.heightmap_copy(hmap, hmap)
517
    libtcodpy.heightmap_normalize(hmap)
518
    libtcodpy.heightmap_lerp_hm(hmap, hmap, hmap, 0)
519
    libtcodpy.heightmap_add_hm(hmap, hmap, hmap)
520
    libtcodpy.heightmap_multiply_hm(hmap, hmap, hmap)
521
522
    # modifying the heightmap
523
    libtcodpy.heightmap_add_hill(hmap, 0, 0, 4, 1)
524
    libtcodpy.heightmap_dig_hill(hmap, 0, 0, 4, 1)
525
    libtcodpy.heightmap_rain_erosion(hmap, 1, 1, 1)
526
    libtcodpy.heightmap_kernel_transform(hmap, 3, [-1, 1, 0], [0, 0, 0],
527
                                    [.33, .33, .33], 0, 1)
528
    libtcodpy.heightmap_add_voronoi(hmap, 10, 3, [1,3,5])
529
    libtcodpy.heightmap_add_fbm(hmap, noise, 1, 1, 1, 1, 4, 1, 1)
530
    libtcodpy.heightmap_scale_fbm(hmap, noise, 1, 1, 1, 1, 4, 1, 1)
531
    libtcodpy.heightmap_dig_bezier(hmap, [0, 16, 16, 0], [0, 0, 16, 16],
532
                              1, 1, 1, 1)
533
534
    # read data
535
    libtcodpy.heightmap_get_value(hmap, 0, 0)
536
    libtcodpy.heightmap_get_interpolated_value(hmap, 0, 0)
537
538
    libtcodpy.heightmap_get_slope(hmap, 0, 0)
539
    libtcodpy.heightmap_get_normal(hmap, 0, 0, 0)
540
    libtcodpy.heightmap_count_cells(hmap, 0, 0)
541
    libtcodpy.heightmap_has_land_on_border(hmap, 0)
542
    libtcodpy.heightmap_get_minmax(hmap)
543
544
    libtcodpy.noise_delete(noise)
545
    libtcodpy.heightmap_delete(hmap)
546
547
MAP = (
548
       '############',
549
       '#   ###    #',
550
       '#   ###    #',
551
       '#   ### ####',
552
       '## #### # ##',
553
       '##      ####',
554
       '############',
555
       )
556
557
MAP_WIDTH = len(MAP[0])
558
MAP_HEIGHT = len(MAP)
559
560
POINT_A = (2, 2)
561
POINT_B = (9, 2)
562
POINT_C = (9, 4)
563
564
POINTS_AB = POINT_A + POINT_B # valid path
565
POINTS_AC = POINT_A + POINT_C # invalid path
566
567
@pytest.fixture()
568
def map_():
569
    map_ = libtcodpy.map_new(MAP_WIDTH, MAP_HEIGHT)
570
    for y, line in enumerate(MAP):
571
        for x, ch in enumerate(line):
572
            libtcodpy.map_set_properties(map_, x, y, ch == ' ', ch == ' ')
573
    yield map_
574
    libtcodpy.map_delete(map_)
575
576
@pytest.fixture()
577
def path_callback(map_):
578
    def callback(ox, oy, dx, dy, user_data):
579
        if libtcodpy.map_is_walkable(map_, dx, dy):
580
            return 1
581
        return 0
582
    return callback
583
584
def test_map_fov(map_):
585
    libtcodpy.map_compute_fov(map_, *POINT_A)
586
587
def test_astar(map_):
588
    astar = libtcodpy.path_new_using_map(map_)
589
590
    assert not  libtcodpy.path_compute(astar, *POINTS_AC)
591
    assert  libtcodpy.path_size(astar) == 0
592
    assert  libtcodpy.path_compute(astar, *POINTS_AB)
593
    assert  libtcodpy.path_get_origin(astar) == POINT_A
594
    assert  libtcodpy.path_get_destination(astar) == POINT_B
595
    libtcodpy.path_reverse(astar)
596
    assert libtcodpy.path_get_origin(astar) == POINT_B
597
    assert libtcodpy.path_get_destination(astar) == POINT_A
598
599
    assert libtcodpy.path_size(astar) != 0
600
    assert libtcodpy.path_size(astar) > 0
601
    assert not libtcodpy.path_is_empty(astar)
602
603
    for i in range(libtcodpy.path_size(astar)):
604
        x, y = libtcodpy.path_get(astar, i)
605
606
    while (x, y) != (None, None):
607
        x, y = libtcodpy.path_walk(astar, False)
608
609
    libtcodpy.path_delete(astar)
610
611
def test_astar_callback(map_, path_callback):
612
    astar = libtcodpy.path_new_using_function(
613
        libtcodpy.map_get_width(map_),
614
        libtcodpy.map_get_height(map_),
615
        path_callback,
616
        )
617
    libtcodpy.path_compute(astar, *POINTS_AB)
618
    libtcodpy.path_delete(astar)
619
620
def test_dijkstra(map_):
621
    path = libtcodpy.dijkstra_new(map_)
622
623
    libtcodpy.dijkstra_compute(path, *POINT_A)
624
625
    assert not libtcodpy.dijkstra_path_set(path, *POINT_C)
626
    assert libtcodpy.dijkstra_get_distance(path, *POINT_C) == -1
627
628
    assert libtcodpy.dijkstra_path_set(path, *POINT_B)
629
    assert libtcodpy.dijkstra_size(path)
630
    assert not libtcodpy.dijkstra_is_empty(path)
631
632
    libtcodpy.dijkstra_reverse(path)
633
634
    for i in range(libtcodpy.dijkstra_size(path)):
635
        x, y = libtcodpy.dijkstra_get(path, i)
636
637
    while (x, y) != (None, None):
638
        x, y = libtcodpy.dijkstra_path_walk(path)
639
640
    libtcodpy.dijkstra_delete(path)
641
642
def test_dijkstra_callback(map_, path_callback):
643
    path = libtcodpy.dijkstra_new_using_function(
644
        libtcodpy.map_get_width(map_),
645
        libtcodpy.map_get_height(map_),
646
        path_callback,
647
        )
648
    libtcodpy.dijkstra_compute(path, *POINT_A)
649
    libtcodpy.dijkstra_delete(path)
650
651
652
def test_alpha_blend(console):
653
    for i in range(256):
654
        libtcodpy.console_put_char(console, 0, 0, 'x',
655
                                   libtcodpy.BKGND_ALPHA(i))
656
        libtcodpy.console_put_char(console, 0, 0, 'x',
657
                                   libtcodpy.BKGND_ADDALPHA(i))
658
659
660
if __name__ == '__main__':
661
    pytest.main()
662