File: 05unary_minus.t

package info (click to toggle)
libmath-symbolic-perl 0.613-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,268 kB
  • sloc: perl: 12,638; makefile: 9
file content (67 lines) | stat: -rw-r--r-- 1,732 bytes parent folder | download | duplicates (6)
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
#!perl
use strict;
use warnings;

use Test::More tests => 6;

BEGIN {
	use_ok('Math::Symbolic');
}

if ($ENV{TEST_YAPP_PARSER}) {
	require Math::Symbolic::Parser::Yapp;
	$Math::Symbolic::Parser = Math::Symbolic::Parser::Yapp->new();
}

use Math::Symbolic::ExportConstants qw/:all/;

my $var = Math::Symbolic::Variable->new();
my $a   = $var->new( 'a' => 2 );

print "Vars: a=" . $a->value() . " (Values are optional)\n\n";

my $op = Math::Symbolic::Operator->new();
my $umi;
undef $@;
eval <<'HERE';
$umi = $op->new({type=>U_MINUS, operands=>[ $a ]});
HERE
ok( !$@, 'Unary minus creation' );

print "prefix notation and evaluation:\n";

undef $@;
eval <<'HERE';
print $umi->to_string('prefix') . " = " . $umi->value() . "\n\n";
HERE
ok( !$@, 'Unary minus to prefix' );

undef $@;
eval <<'HERE';
print $umi->to_string('infix') . " = " . $umi->value() . "\n\n";
HERE
ok( !$@, 'Unary minus to infix' );

undef $@;
eval <<'HERE';
$umi = Math::Symbolic::Operator->new('neg', Math::Symbolic::Operator->new('-', Math::Symbolic::Variable->new('a'), Math::Symbolic::Variable->new('b')));
$umi = $umi->new('neg', $umi);
HERE
$umi = $umi->simplify();
my $str = $umi->to_string('prefix');
$str =~ s/\s+//g;
ok( ( !$@ and $str eq 'subtract(a,b)' ), 'Unary minus simplification' );

undef $@;
eval <<'HERE';
$umi = Math::Symbolic::Operator->new('neg', Math::Symbolic::Operator->new('-', Math::Symbolic::Variable->new('a'), Math::Symbolic::Variable->new('b')));
$umi = $umi->new('neg', $umi);
$umi = $umi->new('neg', $umi);
$umi = $umi->new('neg', $umi);
$umi = $umi->new('neg', $umi);
HERE
$umi = $umi->simplify();
$str = $umi->to_string('prefix');
$str =~ s/\s+//g;
ok( ( !$@ and $str eq 'subtract(b,a)' ), 'More unary minus simplification' );