summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/perl/cpan/Math-BigInt/t/objectify_mbf.t
blob: 258ed7b196b6dd9b981ce6e0b30d4235d68215a7 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!perl
#
# Verify that objectify() is able to convert a "foreign" object into what we
# want, when what we want is Math::BigFloat or subclass thereof.

use strict;
use warnings;

package main;

use Test::More tests => 6;

use Math::BigFloat;

###############################################################################

for my $class ('Math::BigFloat', 'Math::BigFloat::Subclass') {

    # This object defines what we want.

    my $float = $class -> new(10);

    # Create various objects that should work with the object above after
    # objectify() has done its thing.

    my $float_percent1 = My::Percent::Float1 -> new(100);
    is($float * $float_percent1, 10,
       qq|\$float = $class -> new(10);|
       . q| $float_percent1 = My::Percent::Float1 -> new(100);|
       . q| $float * $float_percent1;|);

    my $float_percent2 = My::Percent::Float2 -> new(100);
    is($float * $float_percent2, 10,
       qq|\$float = $class -> new(10);|
       . q| $float_percent2 = My::Percent::Float2 -> new(100);|
       . q| $float * $float_percent2;|);

    my $float_percent3 = My::Percent::Float3 -> new(100);
    is($float * $float_percent3, 10,
       qq|\$float = $class -> new(10);|
       . q| $float_percent3 = My::Percent::Float3 -> new(100);|
       . q| $float * $float_percent3;|);
}

###############################################################################
# Class supports as_float(), which returns a Math::BigFloat.

package My::Percent::Float1;

sub new {
    my $class = shift;
    my $num = shift;
    return bless \$num, $class;
}

sub as_float {
    my $self = shift;
    return Math::BigFloat -> new($$self / 100);
}

###############################################################################
# Class supports as_float(), which returns a scalar.

package My::Percent::Float2;

sub new {
    my $class = shift;
    my $num = shift;
    return bless \$num, $class;
}

sub as_float {
    my $self = shift;
    return $$self / 100;
}

###############################################################################
# Class does not support as_float().

package My::Percent::Float3;

use overload '""' => sub { $_[0] -> as_string(); };

sub new {
    my $class = shift;
    my $num = shift;
    return bless \$num, $class;
}

sub as_string {
    my $self = shift;
    return $$self / 100;
}

###############################################################################

package Math::BigFloat::Subclass;

use base 'Math::BigFloat';