1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Math\Test\Unit;
38
39use Hoa\Math\Context as CUT;
40use Hoa\Test;
41
42/**
43 * Class \Hoa\Math\Test\Unit\Context.
44 *
45 * Test suite of the Hoa\Math\Context class.
46 *
47 * @copyright  Copyright © 2007-2017 Hoa community
48 * @license    New BSD License
49 */
50class Context extends Test\Unit\Suite
51{
52    public function case_context_has_no_predefined_variable()
53    {
54        $this
55            ->given($context = new CUT())
56            ->when($result = $context->getVariables())
57            ->then
58                ->object($result)
59                    ->isInstanceOf('ArrayObject')
60                ->array(iterator_to_array($result))
61                    ->isEmpty();
62    }
63
64    public function case_context_exception_when_getting_unknown_variable()
65    {
66        $this
67            ->given(
68                $name    = 'foo',
69                $context = new CUT()
70            )
71            ->then
72                ->exception(function () use ($context, $name) {
73                    $context->getVariable($name);
74                })
75                    ->isInstanceOf('Hoa\Math\Exception\UnknownVariable');
76    }
77
78    public function case_context_returns_variable_value()
79    {
80        $this
81            ->given(
82                $name     = 'foo',
83                $value    = 42,
84                $callable = function () use ($value) { return $value; },
85                $context  = new CUT(),
86                $context->addVariable($name, $callable)
87            )
88            ->when($result = $context->getVariable($name))
89            ->then
90                ->integer($result)
91                    ->isEqualTo($value);
92    }
93
94    public function case_context_has_predefined_constants()
95    {
96        $this
97            ->given($context = new CUT())
98            ->when($result = $context->getConstants())
99            ->then
100                ->object($result)
101                    ->isInstanceOf('ArrayObject')
102                ->array(iterator_to_array($result))
103                    ->isEqualTo([
104                        'PI'               => M_PI,
105                        'PI_2'             => M_PI_2,
106                        'PI_4'             => M_PI_4,
107                        'E'                => M_E,
108                        'SQRT_PI'          => M_SQRTPI,
109                        'SQRT_2'           => M_SQRT2,
110                        'SQRT_3'           => M_SQRT3,
111                        'LN_PI'            => M_LNPI,
112                        'LOG_2E'           => M_LOG2E,
113                        'LOG_10E'          => M_LOG10E,
114                        'LN_2'             => M_LN2,
115                        'LN_10'            => M_LN10,
116                        'ONE_OVER_PI'      => M_1_PI,
117                        'TWO_OVER_PI'      => M_2_PI,
118                        'TWO_OVER_SQRT_PI' => M_2_SQRTPI,
119                        'ONE_OVER_SQRT_2'  => M_SQRT1_2,
120                        'EULER'            => M_EULER,
121                        'INFINITE'         => INF
122                    ]);
123    }
124
125    public function case_context_exception_when_getting_unknown_constant()
126    {
127        $this
128            ->given(
129                $name    = 'FOO',
130                $context = new CUT()
131            )
132            ->then
133                ->exception(function () use ($context, $name) {
134                    $context->getConstant($name);
135                })
136                    ->isInstanceOf('Hoa\Math\Exception\UnknownConstant');
137    }
138
139    public function case_context_exception_when_setting_already_defined_constant()
140    {
141        $this
142            ->given(
143                $name    = 'PI',
144                $context = new CUT()
145            )
146            ->then
147                ->exception(function () use ($context, $name) {
148                    $context->addConstant($name, 42);
149                })
150                    ->isInstanceOf('Hoa\Math\Exception\AlreadyDefinedConstant');
151    }
152
153    public function case_context_returns_constant_value()
154    {
155        $this
156            ->given(
157                $name     = 'FOO',
158                $value    = 42,
159                $context  = new CUT(),
160                $context->addConstant($name, $value)
161            )
162            ->when($result = $context->getConstant($name))
163            ->then
164                ->variable($result)
165                    ->isEqualTo($value);
166    }
167
168    public function case_context_has_predefined_functions()
169    {
170        $this
171            ->given($context = new CUT())
172            ->when($result = $context->getFunctions())
173            ->then
174                ->object($result)
175                    ->isInstanceOf('ArrayObject')
176                ->array(iterator_to_array($result))
177                    ->hasSize(23)
178                    ->hasKey('abs')
179                    ->hasKey('acos')
180                    ->hasKey('asin')
181                    ->hasKey('atan')
182                    ->hasKey('average')
183                    ->hasKey('avg')
184                    ->hasKey('ceil')
185                    ->hasKey('cos')
186                    ->hasKey('count')
187                    ->hasKey('deg2rad')
188                    ->hasKey('exp')
189                    ->hasKey('floor')
190                    ->hasKey('ln')
191                    ->hasKey('log')
192                    ->hasKey('max')
193                    ->hasKey('min')
194                    ->hasKey('pow')
195                    ->hasKey('rad2deg')
196                    ->hasKey('round')
197                    ->hasKey('round')
198                    ->hasKey('sin')
199                    ->hasKey('sqrt')
200                    ->hasKey('sum')
201                    ->hasKey('tan');
202    }
203
204    public function case_context_exception_when_getting_unknown_function()
205    {
206        $this
207            ->given(
208                $name    = 'foo',
209                $context = new CUT()
210            )
211            ->then
212                ->exception(function () use ($context, $name) {
213                    $context->getFunction($name);
214                })
215                    ->isInstanceOf('Hoa\Math\Exception\UnknownFunction');
216    }
217
218    public function case_context_exception_when_setting_unknown_function()
219    {
220        $this
221            ->given(
222                $name    = 'foo',
223                $context = new CUT()
224            )
225            ->then
226                ->exception(function () use ($context, $name) {
227                    $context->addFunction($name);
228                })
229                    ->isInstanceOf('Hoa\Math\Exception\UnknownFunction');
230    }
231
232    public function case_context_returns_function_callable()
233    {
234        $this
235            ->given(
236                $name     = 'foo',
237                $callable = function () {},
238                $context  = new CUT(),
239                $context->addFunction($name, $callable)
240            )
241            ->when($result = $context->getFunction($name))
242            ->then
243                ->object($result)
244                    ->isInstanceOf('Hoa\Consistency\Xcallable');
245    }
246
247    public function case_context_returns_the_right_function_callable()
248    {
249        $this
250            ->given(
251                $name     = 'foo',
252                $value    = 42,
253                $callable = function () use ($value) { return $value; },
254                $context  = new CUT(),
255                $context->addFunction($name, $callable)
256            )
257            ->when($result = $context->getFunction($name))
258            ->then
259                ->integer($result())
260                    ->isEqualTo($value);
261    }
262}
263