summaryrefslogtreecommitdiff
path: root/cpan/Math-BigInt/t/objectify_mbi.t
blob: 8bb357196998626ecfc6137e3cdb867ade3d6a62 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!perl
#
# Verify that objectify() is able to convert a "foreign" object into what we
# want, when what we want is Math::BigInt or subclass thereof.

use strict;
use warnings;

package main;

use Test::More tests => 10;

use Math::BigInt;

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

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

    # This object defines what we want.

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

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

    my $int_percent1 = My::Percent::Int1 -> new(100);
    is($int * $int_percent1, 10,
       qq|\$class -> new(10);|
       . q| $int_percent1 = My::Percent::Int1 -> new(100);|
       . q| $int * $int_percent1|);

    my $int_percent2 = My::Percent::Int2 -> new(100);
    is($int * $int_percent2, 10,
       qq|\$class -> new(10);|
       . q| $int_percent2 = My::Percent::Int2 -> new(100);|
       . q| $int * $int_percent2|);

    my $int_percent3 = My::Percent::Int3 -> new(100);
    is($int * $int_percent3, 10,
       qq|\$class -> new(10);|
       . q| $int_percent3 = My::Percent::Int3 -> new(100);|
       . q| $int * $int_percent3|);

    my $int_percent4 = My::Percent::Int4 -> new(100);
    is($int * $int_percent4, 10,
       qq|\$class -> new(10);|
       . q| $int_percent4 = My::Percent::Int4 -> new(100);|
       . q| $int * $int_percent4|);

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

###############################################################################
# Class supports as_int(), which returns a Math::BigInt.

package My::Percent::Int1;

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

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

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

package My::Percent::Int2;

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

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

###############################################################################
# Class does not support as_int(), but supports as_number(), which returns a
# Math::BigInt.

package My::Percent::Int3;

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

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

###############################################################################
# Class does  not support as_int(),  but supports as_number(), which  returns a
# scalar.

package My::Percent::Int4;

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

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

###############################################################################
# Class supports neither as_int() or as_number().

package My::Percent::Int5;

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::BigInt::Subclass;

use base 'Math::BigInt';