]> git.donarmstrong.com Git - deb_pkgs/libstatistics-r-perl.git/blob - t/06-get-set.t
New upstream version 0.34
[deb_pkgs/libstatistics-r-perl.git] / t / 06-get-set.t
1 #! perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Statistics::R;
7
8 my ($R, $input, $output);
9
10
11 ok $R = Statistics::R->new();
12
13
14 $input = undef;
15 ok $R->set('x', $input), 'undef';
16 is $R->get('x'), undef;
17
18
19 $input = 123;
20 ok $R->set('x', $input);
21 ok $output = $R->get('x'), 'integer';
22 is ref($output), '';
23 is $output, 123;
24
25
26 # R default number of digits is 7
27 $input = 0.93945768644;
28 ok $R->set('x', $input), 'real number';
29 ok $output = $R->get('x');
30 is ref($output), '';
31 is $output, sprintf("%.7f", $input);
32
33
34 $input = "apocalypse";
35 ok $R->set('x', $input), 'string';
36 ok $output = $R->get('x');
37 is ref($output), '';
38 is $output, "apocalypse";
39
40
41 $input = "a string";
42 ok $R->set('x', $input), 'string with witespace';
43 ok $output = $R->get('x');
44 is ref($output), '';
45 is $output, "a string";
46
47
48 $input = 'gi|57116681|ref|NC_000962.2|';
49 ok $R->set('x', $input), 'number-containing string';
50 ok $output = $R->get('x');
51 is ref($output), '';
52 is $output, 'gi|57116681|ref|NC_000962.2|';
53
54
55 # Mixed arrays are considered as string arrays by R, thus there is no digit limit
56 $input = [123, "a string", 'two strings', 0.93945768644];
57 ok $R->set('x', $input), 'mixed array';
58 ok $output = $R->get('x');
59 is ref($output), 'ARRAY';
60 is $$output[0], 123;
61 is $$output[1], "a string";
62 is $$output[2], "two strings";
63 is $$output[3], 0.93945768644;
64
65
66 # RT bug #71988
67 $input = [ q{statistics-r-0.22}, "abc 123 xyz", 'gi|57116681|ref|NC_000962.2|'];
68 ok $R->set('x', $input), 'array of number-containing strings';
69 ok $output = $R->get('x');
70 is ref($output), 'ARRAY';
71 is $$output[0], q{statistics-r-0.22};
72 is $$output[1], "abc 123 xyz";
73 is $$output[2], 'gi|57116681|ref|NC_000962.2|';
74
75
76 $input = [123,142,147,153,145,151,165,129,133,150,142,154,131,146,151,136,147,156,141,155,147,165,168,146,148,146,142,145,161,157,154,137,130,161,130,156,140,145,154];
77 ok $R->set('x', $input), 'large array of integers';
78 ok $output = $R->get('x');
79 is ref($output), 'ARRAY';
80 for (my $i = 0; $i < scalar @$input; $i++) {
81     is $$output[$i], $$input[$i];
82 }
83
84
85 $input = [1, 2, 3];
86 ok $R->set('x', $input), 'data frame';
87 is $R->run(q`a <- data.frame(first=x)`), '';
88 ok $output = $R->get('a$first');
89 is ref($output), 'ARRAY';
90 is $$output[0], 1;
91 is $$output[1], 2;
92 is $$output[2], 3;
93
94
95 # Bug reported by Manuel A. Alonso Tarajano
96 is $R->run(q`mydat = seq(1:4)`), '';
97 ok $output = $R->get('mydat');
98 is $$output[0], 1;
99 is $$output[1], 2;
100 is $$output[2], 3;
101 is $$output[3], 4;
102
103
104 # Strings containing quotes and escaped quotes
105 $input = q{He said: "Let's go \"home\" now!\n"};
106 ok $R->set('x', $input), 'string';
107 ok $output = $R->get('x');
108 is ref($output), '';
109 is $output, q{He said: "Let's go \"home\" now!\n"};
110
111
112 $input = q{He said: "Let's go \\\\\\\\\\\\\"home\\\\\\\\\\\\\" now!\n"};
113 # because \ is a special char that needs to be escaped, this string really is:
114 #          He said: "Let's go \\\\\\\"home\\\\\\\" now!\n
115 ok $R->set('x', $input), 'string';
116 ok $output = $R->get('x');
117 is ref($output), '';
118 is $output, q{He said: "Let's go \\\\\\\\\\\\\"home\\\\\\\\\\\\\" now!\n"};
119
120
121 ok $R->stop();
122
123 done_testing;