ported to test::output
[language-befunge.git] / t / 5-befunge / c-maths.t
blobe87e898af6e469718cc806794c47992be2ad6487
1 #!perl
3 # This file is part of Language::Befunge.
4 # Copyright (c) 2001-2008 Jerome Quelin, all rights reserved.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the same terms as Perl itself.
11 # -- math functions
13 use strict;
14 use warnings;
16 use Test::More tests => 21;
17 use Test::Exception;
18 use Test::Output;
20 use Language::Befunge;
21 my $bef = Language::Befunge->new;
24 # multiplication
25 $bef->store_code( '49*.q' );
26 stdout_is { $bef->run_code } '36 ', 'regular multiplication';
27 $bef->store_code( '4*.q' );
28 stdout_is { $bef->run_code } '0 ', 'multiplication with empty stack';
29 $bef->store_code( 'aaa** aaa** * aaa** aaa** *  * . q' );
30 throws_ok { $bef->run_code } qr/program overflow while performing multiplication/,
31     'program overflow during multiplication';
32 $bef->store_code( '1- aaa*** aaa** * aaa** aaa** *  * . q' );
33 throws_ok { $bef->run_code } qr/program underflow while performing multiplication/,
34     'program underflow during multiplication';
37 # addition
38 $bef->store_code( '35+.q' );
39 stdout_is { $bef->run_code } '8 ', 'regular addition';
40 $bef->store_code( 'f+.q' );
41 stdout_is { $bef->run_code } '15 ', 'addition with empty stack';
42 $bef->store_code( '2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ f+ .q' );
43 throws_ok { $bef->run_code } qr/program overflow while performing addition/,
44     'program overflow during addition';
45 $bef->store_code( '2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ - 0f- + .q' );
46 throws_ok { $bef->run_code } qr/program underflow while performing addition/,
47     'program underflow during addition';
50 # subtraction
51 $bef->store_code( '93-.q' );
52 stdout_is { $bef->run_code } '6 ', 'regular subtraction';
53 $bef->store_code( '35-.q' );
54 stdout_is { $bef->run_code } '-2 ', 'regular subtraction, negative';
55 $bef->store_code( 'f-.q' );
56 stdout_is { $bef->run_code } '-15 ', 'subtraction with empty stack';
57 $bef->store_code( '2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ 0f- - .q' );
58 throws_ok { $bef->run_code } qr/program overflow while performing substraction/,
59     'program overflow during subtraction';
60 $bef->store_code( '2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ - f- .q' );
61 throws_ok { $bef->run_code } qr/program underflow while performing substraction/,
62     'program underflow during subtraction';
65 # division
66 $bef->store_code( '93/.q' );
67 stdout_is { $bef->run_code } '3 ', 'regular division';
68 $bef->store_code( '54/.q' );
69 stdout_is { $bef->run_code } '1 ', 'regular division, non-integer';
70 $bef->store_code( 'f/.q' );
71 stdout_is { $bef->run_code } '0 ', 'division with empty stack';
72 $bef->store_code( 'a0/.q' );
73 stdout_is { $bef->run_code } '0 ', 'division by zero';
74 # can't over/underflow integer division
77 # remainder
78 $bef->store_code( '93%.q' );
79 stdout_is { $bef->run_code } '0 ', 'regular remainder';
80 $bef->store_code( '54/.q' );
81 stdout_is { $bef->run_code } '1 ', 'regular remainder, non-integer';
82 $bef->store_code( 'f%.q' );
83 stdout_is { $bef->run_code } '0 ', 'remainder with empty stack';
84 $bef->store_code( 'a0%.q' );
85 stdout_is { $bef->run_code } '0 ', 'remainder by zero';
86 # can't over/underflow integer remainder