From 1d6d06e2f5a09b7bd6d32dfb4cdd807b02ecfe55 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Quelin?= Date: Mon, 25 May 2009 19:54:05 +0200 Subject: [PATCH] ported to test::output and test::exception --- t/5-befunge/c-maths.t | 305 +++++++++++++++----------------------------------- 1 file changed, 88 insertions(+), 217 deletions(-) rewrite t/5-befunge/c-maths.t (92%) diff --git a/t/5-befunge/c-maths.t b/t/5-befunge/c-maths.t dissimilarity index 92% index 3439002..e87e898 100644 --- a/t/5-befunge/c-maths.t +++ b/t/5-befunge/c-maths.t @@ -1,217 +1,88 @@ -#!perl -# -# This file is part of Language::Befunge. -# Copyright (c) 2001-2008 Jerome Quelin, all rights reserved. -# -# This program is free software; you can redistribute it and/or modify -# it under the same terms as Perl itself. -# -# - -#---------------------------------------# -# The math functions. # -#---------------------------------------# - -use strict; -use Language::Befunge; -use POSIX qw! tmpnam !; -use Test; - -# Vars. -my $file; -my $fh; -my $tests; -my $out; -my $bef = Language::Befunge->new; -BEGIN { $tests = 0 }; - -# In order to see what happens... -sub sel () { - $file = tmpnam(); - open OUT, ">$file" or die $!; - $fh = select OUT; -} -sub slurp () { - select $fh; - close OUT; - open OUT, "<$file" or die $!; - my $content; - { - local $/; - $content = ; - } - close OUT; - unlink $file; - return $content; -} - -# Multiplication. -sel; # regular multiplication. -$bef->store_code( <<'END_OF_CODE' ); -49*.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "36 " ); -sel; # empty stack. -$bef->store_code( <<'END_OF_CODE' ); -4*.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "0 " ); -sel; # program overflow. -$bef->store_code( <<'END_OF_CODE' ); -aaa** aaa** * aaa** aaa** * * . q -END_OF_CODE -eval { $bef->run_code; }; -$out = slurp; -ok( $@, qr/program overflow while performing multiplication/ ); -sel; # program underflow. -$bef->store_code( <<'END_OF_CODE' ); -1- aaa*** aaa** * aaa** aaa** * * . q -END_OF_CODE -eval { $bef->run_code; }; -$out = slurp; -ok( $@, qr/program underflow while performing multiplication/ ); -BEGIN { $tests += 4 }; - - -# Addition. -sel; # regular addition. -$bef->store_code( <<'END_OF_CODE' ); -35+.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "8 " ); -sel; # empty stack. -$bef->store_code( <<'END_OF_CODE' ); -f+.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "15 " ); -sel; # program overflow. -$bef->store_code( <<'END_OF_CODE' ); -2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ f+ .q -END_OF_CODE -eval { $bef->run_code; }; -$out = slurp; -ok( $@, qr/program overflow while performing addition/ ); -sel; # program underflow. -$bef->store_code( <<'END_OF_CODE' ); -2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ - 0f- + .q -END_OF_CODE -eval { $bef->run_code; }; -$out = slurp; -ok( $@, qr/program underflow while performing addition/ ); -BEGIN { $tests += 4 }; - - -# Substraction. -sel; # regular substraction. -$bef->store_code( <<'END_OF_CODE' ); -93-.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "6 " ); -sel; # regular substraction (negative). -$bef->store_code( <<'END_OF_CODE' ); -35-.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "-2 " ); -sel; # empty stack. -$bef->store_code( <<'END_OF_CODE' ); -f-.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "-15 " ); -sel; # program overflow. -$bef->store_code( <<'END_OF_CODE' ); -2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ 0f- - .q -END_OF_CODE -eval { $bef->run_code; }; -$out = slurp; -ok( $@, qr/program overflow while performing substraction/ ); -sel; # program underflow. -$bef->store_code( <<'END_OF_CODE' ); -2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ - f- .q -END_OF_CODE -eval { $bef->run_code; }; -$out = slurp; -ok( $@, qr/program underflow while performing substraction/ ); -BEGIN { $tests += 5 }; - - -# Division. -sel; # regular division. -$bef->store_code( <<'END_OF_CODE' ); -93/.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "3 " ); -sel; # regular division (non-integer). -$bef->store_code( <<'END_OF_CODE' ); -54/.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "1 " ); -sel; # empty stack. -$bef->store_code( <<'END_OF_CODE' ); -f/.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "0 " ); -sel; # division by zero. -$bef->store_code( <<'END_OF_CODE' ); -a0/.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "0 " ); -# Can't over/underflow integer division. -BEGIN { $tests += 4 }; - -# Remainder. -sel; # regular remainder. -$bef->store_code( <<'END_OF_CODE' ); -93%.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "0 " ); -sel; # regular remainder (non-integer). -$bef->store_code( <<'END_OF_CODE' ); -54/.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "1 " ); -sel; # empty stack. -$bef->store_code( <<'END_OF_CODE' ); -f%.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "0 " ); -sel; # remainder by zero. -$bef->store_code( <<'END_OF_CODE' ); -a0%.q -END_OF_CODE -$bef->run_code; -$out = slurp; -ok( $out, "0 " ); -# Can't over/underflow integer remainder. -BEGIN { $tests += 4 }; - - -BEGIN { plan tests => $tests }; - +#!perl +# +# This file is part of Language::Befunge. +# Copyright (c) 2001-2008 Jerome Quelin, all rights reserved. +# +# This program is free software; you can redistribute it and/or modify +# it under the same terms as Perl itself. +# +# + +# -- math functions + +use strict; +use warnings; + +use Test::More tests => 21; +use Test::Exception; +use Test::Output; + +use Language::Befunge; +my $bef = Language::Befunge->new; + + +# multiplication +$bef->store_code( '49*.q' ); +stdout_is { $bef->run_code } '36 ', 'regular multiplication'; +$bef->store_code( '4*.q' ); +stdout_is { $bef->run_code } '0 ', 'multiplication with empty stack'; +$bef->store_code( 'aaa** aaa** * aaa** aaa** * * . q' ); +throws_ok { $bef->run_code } qr/program overflow while performing multiplication/, + 'program overflow during multiplication'; +$bef->store_code( '1- aaa*** aaa** * aaa** aaa** * * . q' ); +throws_ok { $bef->run_code } qr/program underflow while performing multiplication/, + 'program underflow during multiplication'; + + +# addition +$bef->store_code( '35+.q' ); +stdout_is { $bef->run_code } '8 ', 'regular addition'; +$bef->store_code( 'f+.q' ); +stdout_is { $bef->run_code } '15 ', 'addition with empty stack'; +$bef->store_code( '2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ f+ .q' ); +throws_ok { $bef->run_code } qr/program overflow while performing addition/, + 'program overflow during addition'; +$bef->store_code( '2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ - 0f- + .q' ); +throws_ok { $bef->run_code } qr/program underflow while performing addition/, + 'program underflow during addition'; + + +# subtraction +$bef->store_code( '93-.q' ); +stdout_is { $bef->run_code } '6 ', 'regular subtraction'; +$bef->store_code( '35-.q' ); +stdout_is { $bef->run_code } '-2 ', 'regular subtraction, negative'; +$bef->store_code( 'f-.q' ); +stdout_is { $bef->run_code } '-15 ', 'subtraction with empty stack'; +$bef->store_code( '2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ 0f- - .q' ); +throws_ok { $bef->run_code } qr/program overflow while performing substraction/, + 'program overflow during subtraction'; +$bef->store_code( '2+a* 1+a* 4+a* 7+a* 4+a* 8+a* 3+a* 6+a* 4+a* 6+ - f- .q' ); +throws_ok { $bef->run_code } qr/program underflow while performing substraction/, + 'program underflow during subtraction'; + + +# division +$bef->store_code( '93/.q' ); +stdout_is { $bef->run_code } '3 ', 'regular division'; +$bef->store_code( '54/.q' ); +stdout_is { $bef->run_code } '1 ', 'regular division, non-integer'; +$bef->store_code( 'f/.q' ); +stdout_is { $bef->run_code } '0 ', 'division with empty stack'; +$bef->store_code( 'a0/.q' ); +stdout_is { $bef->run_code } '0 ', 'division by zero'; +# can't over/underflow integer division + + +# remainder +$bef->store_code( '93%.q' ); +stdout_is { $bef->run_code } '0 ', 'regular remainder'; +$bef->store_code( '54/.q' ); +stdout_is { $bef->run_code } '1 ', 'regular remainder, non-integer'; +$bef->store_code( 'f%.q' ); +stdout_is { $bef->run_code } '0 ', 'remainder with empty stack'; +$bef->store_code( 'a0%.q' ); +stdout_is { $bef->run_code } '0 ', 'remainder by zero'; +# can't over/underflow integer remainder + + -- 2.11.4.GIT