1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 11;
7
8use Statistics::Descriptive::Smoother;
9
10local $SIG{__WARN__} = sub { };
11
12{
13
14    #Test factory pattern
15    my $smoother = Statistics::Descriptive::Smoother->instantiate(
16        {
17            method  => 'exponential',
18            coeff   => 0,
19            data    => [ 1,   2,   3 ],
20            samples => [ 100, 100, 100 ],
21        }
22    );
23
24    # TEST
25    isa_ok(
26        $smoother,
27        'Statistics::Descriptive::Smoother::Exponential',
28        'Exponential class correctly created'
29    );
30}
31
32{
33
34    my $smoother = Statistics::Descriptive::Smoother->instantiate(
35        {
36            method  => 'weightedExponential',
37            coeff   => 0,
38            data    => [ 1,   2,   3 ],
39            samples => [ 100, 100, 100 ],
40        }
41    );
42
43    # TEST
44    isa_ok(
45        $smoother,
46        'Statistics::Descriptive::Smoother::Weightedexponential',
47        'Weightedexponential class correctly created'
48    );
49
50}
51
52{
53
54    # Test invalid smoothing method
55    eval {
56        Statistics::Descriptive::Smoother->instantiate(
57            {
58                method  => 'invalid_method',
59                coeff   => 0,
60                data    => [ 1,   2,   3 ],
61                samples => [ 100, 100, 100 ],
62            }
63        );
64    };
65
66    # TEST
67    ok( $@, 'Invalid method' );
68
69}
70
71{
72
73    #TODO get output from Carp
74    #Test invalid coefficient
75    my $smoother_neg = Statistics::Descriptive::Smoother->instantiate(
76        {
77            method  => 'exponential',
78            coeff   => -123,
79            data    => [ 1,   2,   3 ],
80            samples => [ 100, 100, 100 ],
81        }
82    );
83
84    # TEST
85    is( $smoother_neg, undef, 'Invalid coefficient: < 0' );
86
87    my $smoother_pos = Statistics::Descriptive::Smoother->instantiate(
88        {
89            method  => 'exponential',
90            coeff   => 123,
91            data    => [ 1,   2,   3 ],
92            samples => [ 100, 100, 100 ],
93        }
94    );
95
96    # TEST
97    is( $smoother_pos, undef, 'Invalid coefficient: > 1' );
98
99}
100
101{
102
103    #Test unsufficient number of samples
104    my $smoother = Statistics::Descriptive::Smoother->instantiate(
105        {
106            method  => 'exponential',
107            coeff   => 0,
108            data    => [1],
109            samples => [100],
110        }
111    );
112
113    # TEST
114    is( $smoother, undef, 'Insufficient number of samples' );
115
116}
117
118{
119
120    #Test smoothing coefficient accessors
121    my $smoother = Statistics::Descriptive::Smoother->instantiate(
122        {
123            method  => 'exponential',
124            coeff   => 0.5,
125            data    => [ 1,   2,   3 ],
126            samples => [ 100, 100, 100 ],
127        }
128    );
129
130    # TEST
131    is( $smoother->get_smoothing_coeff(), 0.5, 'get_smoothing_coeff' );
132
133    my $ok = $smoother->set_smoothing_coeff(0.7);
134
135    # TEST
136    ok( $ok, 'set_smoothing_coeff: set went fine' );
137
138    # TEST
139    is( $smoother->get_smoothing_coeff(),
140        0.7, 'set_smoothing_coeff: correct value set' );
141
142    my $ok2 = $smoother->set_smoothing_coeff(123);
143
144    # TEST
145    is( $ok2, undef, 'set_smoothing_coeff: set failed' );
146
147    # TEST
148    is( $smoother->get_smoothing_coeff(),
149        0.7, 'set_smoothing_coeff: value not modified after failure' );
150
151}
152
1531;
154
155=pod
156
157=head1 AUTHOR
158
159Fabio Ponciroli
160
161=head1 COPYRIGHT
162
163Copyright(c) 2012 by Fabio Ponciroli.
164
165=head1 LICENSE
166
167This file is licensed under the MIT/X11 License:
168http://www.opensource.org/licenses/mit-license.php.
169
170Permission is hereby granted, free of charge, to any person obtaining a copy of
171this software and associated documentation files (the "Software"), to deal in
172the Software without restriction, including without limitation the rights to
173use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
174of the Software, and to permit persons to whom the Software is furnished to do
175so, subject to the following conditions:
176
177The above copyright notice and this permission notice shall be included in all
178copies or substantial portions of the Software.
179
180THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
181IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
182FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
183AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
184LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
185OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
186SOFTWARE.
187
188=cut
189