1#!/usr/bin/perl -w
2
3use strict;
4use Test::More tests => 51;
5use Test::Exception;
6
7use Time::HiRes::Value;
8
9use Carp ();
10
11dies_ok( sub { Time::HiRes::Value->new( "Hello" ); }, 
12         'Exception (not convertable)' );
13dies_ok( sub { Time::HiRes::Value->new( "15.pineapple" ); },
14         'Exception (not convertable, leading digits)' );
15dies_ok( sub { Time::HiRes::Value->new( "hello", "world" ); },
16         'Exception (not convertable pair)' );
17
18my $t1 = Time::HiRes::Value->new( 1 );
19ok( defined $t1, 'defined $t1' );
20is( ref $t1, "Time::HiRes::Value", 'ref $t1' );
21
22is( "$t1", "1.000000", 'Stringify' );
23is( ref( $t1->NUMBER ), "", 'Numerify returns plain scalar' );
24
25my $neg = Time::HiRes::Value->new( -4 );
26is( "$neg", "-4.000000", 'Stringify negative' );
27
28$neg = Time::HiRes::Value->new( -4.1 );
29is( "$neg", "-4.100000", 'Stringify negative non-integer' );
30
31$neg = Time::HiRes::Value->new( -0.5 );
32is( "$neg", "-0.500000", 'Stringify negative non-integer > -1' );
33
34my $t2 = Time::HiRes::Value->new( 1.5 );
35is( "$t2", "1.500000", 'Non-integer constructor' );
36
37my $t3 = Time::HiRes::Value->new( 2, 500 );
38is( "$t3", "2.000500", 'Array' );
39
40cmp_ok( $t1, '==', 1, 'Compare == scalar 1' );
41cmp_ok( $t1, '<=', 2, 'Compare <= scalar 2' );
42cmp_ok( $t2, '==', 1.5, 'Compare == scalar 1.5' );
43cmp_ok( $t2, '!=', 1.6, 'Compare != scalar 1.6' );
44
45cmp_ok( $t1, '!=', $t3, 'Compare != Value3' );
46cmp_ok( $t3, '>', $t2, 'Compare > Value2' );
47
48my $t4 = $t1 + 1;
49cmp_ok( $t4, '==', 2, 'add scalar 1' );
50is( ref($t4), "Time::HiRes::Value", 'add scalar 1 reftype' );
51$t4 = $t2 + 2.3;
52cmp_ok( $t4, '==', 3.8, 'add scalar 2.3' );
53$t4 = 1 + $t1;
54cmp_ok( $t4, '==', 2, 'add scalar 1 swapped' );
55is( ref($t4), "Time::HiRes::Value", 'add scalar 1 swapped reftype' );
56
57$t4 = $t1 + -1;
58cmp_ok( $t4, '==', 0, 'inverse of addition' );
59
60$t4 = $t1 + $t2;
61cmp_ok( $t4, '==', 2.5, 'add Value2' );
62
63cmp_ok( $t1 + 0, '==', $t1, 'identity of addition' );
64cmp_ok( $t1 + 3, '==', 3 + $t1, 'commutativity of addition' );
65
66$t4 = $t3 - 2;
67cmp_ok( $t4, '==', 0.0005, 'subtract scalar 2' );
68is( ref($t4), "Time::HiRes::Value", 'subtrace scalar 2 reftype' );
69$t4 = 4 - $t2;
70cmp_ok( $t4, '==', 2.5, 'subtract scalar 4 swapped' );
71is( ref($t4), "Time::HiRes::Value", 'subtrace scalar 4 swapped reftype' );
72
73$t4 = $t1 - 3.1;
74cmp_ok( $t4, '==', -2.1, 'subtract scalar 3.1, negative result' );
75
76cmp_ok( $t1 - 0, '==', $t1, 'identity of subtraction' );
77
78cmp_ok( $t1 * 1, '==', "1.000000", 'multiply t1 * 1' );
79cmp_ok( $t1 * 250, '==', "250.000000", 'multiply t1 * 250' );
80
81cmp_ok( $t2 * 2, '==', "3.000000", 'multiply t2 * 2' );
82cmp_ok( $t2 * 4.2, '==', "6.300000", 'multiply t2 * 4.2' );
83cmp_ok( $t2 * -4.2, '==', "-6.300000", 'multiply t2 * -4.2' );
84
85cmp_ok( $t1 * 1, '==', $t1, 'identity of multiplication' );
86cmp_ok( $t1 * 3, '==', 3 * $t1, 'commutativity of multiplication' );
87
88cmp_ok( $t1 * 0, '==', 0, 'nullability of multiplication' );
89
90dies_ok( sub { $t1 * $t2 },
91         'multiply t1 * t2 fails' );
92
93cmp_ok( $t1 / 1, '==', 1, 'divide t1 / 1' );
94cmp_ok( $t1 / 20, '==', 0.05, 'divide t1 / 20' );
95
96cmp_ok( $t2 / 2, '==', 0.75, 'divide t2 / 2' );
97cmp_ok( $t2 / 1.5, '==', 1, 'divide t2 / 1.5' );
98cmp_ok( $t2 / -4, '==', -0.375, 'divide t2 / -4' );
99
100cmp_ok( $t1 / 1, '==', $t1, 'identity of division' );
101
102dies_ok( sub { 15 / $t1 },
103         'divide 15 / t1 fails' );
104dies_ok( sub { $t1 / $t2 },
105         'divide t1 / t2 fails' );
106
107# Ensure division by zero appears to come from the right place
108# Test::Exception seems to mess this one up via Carp, so we'll do it the old-
109# fashioned way
110$_ = eval { $t1 / 0 };
111like( $@, qr/^Illegal division by zero at $0 line/,
112          'divide t1 / 0 fails' );
113