# Copyright (c) 2007-2009 Martin Becker. All rights reserved. # This package is free software; you can redistribute it and/or modify it # under the same terms as Perl itself. # # $Id: 07_strings.t 36 2009-06-08 11:51:03Z demetri $ # Checking stringification and stringification parameters. # Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl t/07_strings.t' ######################### use strict; use warnings; use Test; BEGIN { plan tests => 65 }; use Math::Polynomial 1.000; ok(1); ######################### my @var_conf = qw( with_variable fold_sign fold_zero fold_one fold_exp_zero fold_exp_one ); my $std_conf = { convert_coeff => sub { "[$_[0]]" }, plus => '<+>', minus => '<->', leading_plus => '{+}', leading_minus => '{-}', times => '<*>', power => '<^>', variable => '', prefix => '<(>', suffix => '<)>', }; Math::Polynomial->string_config($std_conf); my $p = Math::Polynomial->new(-1, 2, 0, 1); my $zp = $p->new; my @expected = ( { with_variable => [ '<(>{+}[1]<+>[0]<+>[2]<+>[-1]<)>', '<(>{+}<^>3<+>[2]<*><+>[-1]<)>', ], fold_sign => [ '<(>{+}<^>3<+>[2]<*><+>[-1]<)>', '<(>{+}<^>3<+>[2]<*><->[1]<)>', ], fold_zero => [ '<(>{+}<^>3<+>[0]<*><^>2<+>[2]<*><+>[-1]<)>', '<(>{+}<^>3<+>[2]<*><+>[-1]<)>', ], fold_one => [ '<(>{+}[1]<*><^>3<+>[2]<*><+>[-1]<)>', '<(>{+}<^>3<+>[2]<*><+>[-1]<)>', ], fold_exp_zero => [ '<(>{+}<^>3<+>[2]<*><+>[-1]<*><^>0<)>', '<(>{+}<^>3<+>[2]<*><+>[-1]<)>', ], fold_exp_one => [ '<(>{+}<^>3<+>[2]<*><^>1<+>[-1]<)>', '<(>{+}<^>3<+>[2]<*><+>[-1]<)>', ], }, { with_variable => [ '<(>{+}[-1]<+>[2]<+>[0]<+>[1]<)>', '<(>{+}[-1]<+>[2]<*><+><^>3<)>', ], fold_sign => [ '<(>{+}[-1]<+>[2]<*><+><^>3<)>', '<(>{-}[1]<+>[2]<*><+><^>3<)>', ], fold_zero => [ '<(>{+}[-1]<+>[2]<*><+>[0]<*><^>2<+><^>3<)>', '<(>{+}[-1]<+>[2]<*><+><^>3<)>', ], fold_one => [ '<(>{+}[-1]<+>[2]<*><+>[1]<*><^>3<)>', '<(>{+}[-1]<+>[2]<*><+><^>3<)>', ], fold_exp_zero => [ '<(>{+}[-1]<*><^>0<+>[2]<*><+><^>3<)>', '<(>{+}[-1]<+>[2]<*><+><^>3<)>', ], fold_exp_one => [ '<(>{+}[-1]<+>[2]<*><^>1<+><^>3<)>', '<(>{+}[-1]<+>[2]<*><+><^>3<)>', ], }, { with_variable => [ '<(>{+}[0]<)>', '<(>{+}[0]<)>', ], fold_sign => [ '<(>{+}[0]<)>', '<(>{+}[0]<)>', ], fold_zero => [ '<(>{+}[0]<)>', '<(>{+}[0]<)>', ], fold_one => [ '<(>{+}[0]<)>', '<(>{+}[0]<)>', ], fold_exp_zero => [ '<(>{+}[0]<*><^>0<)>', '<(>{+}[0]<)>', ], fold_exp_one => [ '<(>{+}[0]<)>', '<(>{+}[0]<)>', ], }, ); foreach my $ascending (0, 1) { foreach my $param (@var_conf) { foreach my $value (0, 1) { my $conf = { %{$std_conf}, 'ascending' => $ascending, $param => $value, }; ok( $p->as_string($conf), $expected[$ascending]->{$param}->[$value], "ascending=$ascending, $param=$value", ); ok( $zp->as_string($conf), $expected[2]->{$param}->[$value], "zero polynomial, $param=$value", ); } } } my $config1 = {'variable' => 'X'}; my $config2 = {'fold_sign' => 1}; my $config3 = {'ascending' => 1}; my $config4 = {'with_variable' => 0, 'plus' => q[, ]}; Math::Polynomial->string_config($config1); $p->string_config($config2); ok($p->string_config, $config2, 'p with config2'); ok($p->as_string, '(x^3 + 2 x - 1)', '"p" with config2'); ok(Math::Polynomial->string_config, $config1, 'global config1'); my $pp = $p->clone; ok($pp->string_config, $config2, 'pp with config2'); ok($pp->as_string, '(x^3 + 2 x - 1)', '"pp" with config2'); my $q = $p - 1; ok($q->string_config, $config2, 'q with config2'); ok($q->as_string, '(x^3 + 2 x - 2)', '"q" with config2'); $p->string_config($config3); ok($p->string_config, $config3, 'p with config3'); ok($p->as_string, '(-1 + 2 x + x^3)', '"p" with config3'); ok($pp->string_config, $config2, 'pp still with config2'); ok($q->string_config, $config2, 'q still with config2'); $q->string_config(undef); ok($q->as_string, '(X^3 + 2 X + -2)', '"q" with global config1'); my $r = $q + 1; Math::Polynomial->string_config($config4); ok($r->as_string, '(1, 0, 2, -1)', '"r" with global config4'); ok($r == $p); Math::Polynomial->string_config(undef); my $config5 = Math::Polynomial->string_config; ok(defined($config5) && 0 == keys %{$config5}); ok($r->as_string, '(x^3 + 2 x + -1)', '"r" with global defaults'); __END__