File: rexp-complex.t

package info (click to toggle)
libstatistics-r-io-perl 1.0002-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,824 kB
  • sloc: perl: 10,895; makefile: 2
file content (107 lines) | stat: -rw-r--r-- 4,531 bytes parent folder | download
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
#!perl -T
use 5.010;
use strict;
use warnings FATAL => 'all';

use Test::More tests => 32;
use Test::Fatal;

use Statistics::R::REXP::Complex;
use Statistics::R::REXP::List;

use Math::Complex qw(cplx);

use Data::Dumper;

my $empty_vec = new_ok('Statistics::R::REXP::Complex', [  ], 'new complex vector' );

is($empty_vec, $empty_vec, 'self equality');

my $empty_vec_2 = Statistics::R::REXP::Complex->new();
is($empty_vec, $empty_vec_2, 'empty complex vector equality');

my $vec = Statistics::R::REXP::Complex->new(elements => [cplx(3.3), cplx(4.7), cplx(11)]);
my $vec2 = Statistics::R::REXP::Complex->new([3.3, 4.7, 11]);
is($vec, $vec2, 'complex vector equality');

is(Statistics::R::REXP::Complex->new($vec2), $vec, 'copy constructor');
is(Statistics::R::REXP::Complex->new(Statistics::R::REXP::List->new([3.3, [4.7, 11]])),
   $vec, 'copy constructor from a vector');

## error checking in constructor arguments
like(exception {
        Statistics::R::REXP::Complex->new(sub {1+1})
     }, qr/Attribute 'elements' must be an array reference/,
     'error-check in single-arg constructor');
like(exception {
        Statistics::R::REXP::Complex->new(1, 2, 3)
     }, qr/odd number of arguments/,
     'odd constructor arguments');
like(exception {
        Statistics::R::REXP::Complex->new(elements => {foo => 1, bar => 2})
     }, qr/Attribute 'elements' must be an array reference/,
     'bad elements argument');

my $another_vec = Statistics::R::REXP::Complex->new(elements => [cplx(3.3), cplx(4.7, 1), 11]);
isnt($vec, $another_vec, 'complex vector inequality');

my $na_heavy_vec = Statistics::R::REXP::Complex->new(elements => [cplx(11.3, 3), '', undef, 0.0]);
my $na_heavy_vec2 = Statistics::R::REXP::Complex->new(elements => [cplx(11.3, 3), 0, undef, 0]);
is($na_heavy_vec, $na_heavy_vec, 'NA-heavy vector equality');
isnt($na_heavy_vec, $na_heavy_vec2, 'NA-heavy vector inequality');

is($empty_vec .'', 'complex()', 'empty complex vector text representation');
is($vec .'', 'complex(3.3, 4.7, 11)', 'complex vector text representation');
is(Statistics::R::REXP::Complex->new(elements => [undef]) .'',
   'complex(undef)', 'text representation of a singleton NA');
is($na_heavy_vec .'', 'complex(11.3+3i, undef, undef, 0)', 'empty numbers representation');

is_deeply($empty_vec->elements, [], 'empty complex vector contents');
is_deeply($vec->elements, [3.3, 4.7, 11], 'complex vector contents');
cmp_ok($vec->elements->[1], '==', 4.7, 'single element access');

is_deeply(Statistics::R::REXP::Complex->new(elements => [3.3, 4.0, '3x', 11])->elements,
          [3.3, 4, undef, 11], 'constructor with non-numeric values');

is_deeply(Statistics::R::REXP::Complex->new(elements => [3.3, 4.0, [7, [20.9, 44.1]], 11])->elements,
          [3.3, 4, 7, 20.9, 44.1, 11], 'constructor from nested arrays');

ok(! $empty_vec->is_null, 'is not null');
ok( $empty_vec->is_vector, 'is vector');


## attributes
is_deeply($vec->attributes, undef, 'default attributes');

my $vec_attr = Statistics::R::REXP::Complex->new(elements => [3.3, 4.7, 11],
                                                attributes => { foo => 'bar',
                                                                x => [40, 41, 42] });
is_deeply($vec_attr->attributes,
          { foo => 'bar', x => [40, 41, 42] }, 'constructed attributes');

my $vec_attr2 = Statistics::R::REXP::Complex->new(elements => [3.3, 4.7, 11],
                                                 attributes => { foo => 'bar',
                                                                 x => [40, 41, 42] });
my $another_vec_attr = Statistics::R::REXP::Complex->new(elements => [3.3, 4.7, 11],
                                                        attributes => { foo => 'bar',
                                                                        x => [40, 42, 42] });
is($vec_attr, $vec_attr2, 'equality considers attributes');
isnt($vec_attr, $vec, 'inequality considers attributes');
isnt($vec_attr, $another_vec_attr, 'inequality considers attributes deeply');

## attributes must be a hash
like(exception {
        Statistics::R::REXP::Complex->new(attributes => 1)
     }, qr/Attribute 'attributes' must be a hash reference/,
     'setting non-HASH attributes');

## Perl representation
is_deeply($empty_vec->to_pl,
          [], 'empty vector Perl representation');

is_deeply($vec->to_pl,
          [3.3, 4.7, 11], 'Perl representation');

is_deeply($na_heavy_vec->to_pl,
          [cplx(11.3, 3), undef, undef, 0], 'NA-heavy vector Perl representation');