xref: /openbsd/gnu/usr.bin/perl/cpan/bignum/t/bigrat.t (revision 898184e3)
1#!/usr/bin/perl -w
2
3###############################################################################
4
5use Test;
6use strict;
7
8BEGIN
9  {
10  $| = 1;
11  chdir 't' if -d 't';
12  unshift @INC, '../lib';
13  plan tests => 40;
14  }
15
16use bigrat qw/oct hex/;
17
18###############################################################################
19# general tests
20
21my $x = 5; ok (ref($x) =~ /^Math::BigInt/);		# :constant
22
23# todo:  ok (2 + 2.5,4.5);				# should still work
24# todo: $x = 2 + 3.5; ok (ref($x),'Math::BigFloat');
25
26$x = 2 ** 255; ok (ref($x) =~ /^Math::BigInt/);
27
28# see if Math::BigRat constant works
29ok (1/3, '1/3');
30ok (1/4+1/3,'7/12');
31ok (5/7+3/7,'8/7');
32
33ok (3/7+1,'10/7');
34ok (3/7+1.1,'107/70');
35ok (3/7+3/7,'6/7');
36
37ok (3/7-1,'-4/7');
38ok (3/7-1.1,'-47/70');
39ok (3/7-2/7,'1/7');
40
41# fails ?
42# ok (1+3/7,'10/7');
43
44ok (1.1+3/7,'107/70');
45ok (3/7*5/7,'15/49');
46ok (3/7 / (5/7),'3/5');
47ok (3/7 / 1,'3/7');
48ok (3/7 / 1.5,'2/7');
49
50###############################################################################
51# accurarcy and precision
52
53ok_undef (bigrat->accuracy());
54ok (bigrat->accuracy(12),12);
55ok (bigrat->accuracy(),12);
56
57ok_undef (bigrat->precision());
58ok (bigrat->precision(12),12);
59ok (bigrat->precision(),12);
60
61ok (bigrat->round_mode(),'even');
62ok (bigrat->round_mode('odd'),'odd');
63ok (bigrat->round_mode(),'odd');
64
65###############################################################################
66# hex() and oct()
67
68my $c = 'Math::BigInt';
69
70ok (ref(hex(1)), $c);
71ok (ref(hex(0x1)), $c);
72ok (ref(hex("af")), $c);
73ok (hex("af"), Math::BigInt->new(0xaf));
74ok (ref(hex("0x1")), $c);
75
76ok (ref(oct("0x1")), $c);
77ok (ref(oct("01")), $c);
78ok (ref(oct("0b01")), $c);
79ok (ref(oct("1")), $c);
80ok (ref(oct(" 1")), $c);
81ok (ref(oct(" 0x1")), $c);
82
83ok (ref(oct(0x1)), $c);
84ok (ref(oct(01)), $c);
85ok (ref(oct(0b01)), $c);
86ok (ref(oct(1)), $c);
87
88###############################################################################
89###############################################################################
90# Perl 5.005 does not like ok ($x,undef)
91
92sub ok_undef
93  {
94  my $x = shift;
95
96  ok (1,1) and return if !defined $x;
97  ok ($x,'undef');
98  }
99