Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / lsolesen / pel / test / NumberTest.php
1 <?php
2
3 /**
4  * PEL: PHP Exif Library.
5  * A library with support for reading and
6  * writing all Exif headers in JPEG and TIFF images using PHP.
7  *
8  * Copyright (C) 2004, 2005, 2006 Martin Geisler.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program in the file COPYING; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301 USA
24  */
25
26 use \lsolesen\pel\PelEntryByte;
27 use \lsolesen\pel\Pel;
28 use \lsolesen\pel\PelEntrySByte;
29 use \lsolesen\pel\PelEntryShort;
30 use \lsolesen\pel\PelEntrySShort;
31 use \lsolesen\pel\PelEntryLong;
32 use \lsolesen\pel\PelEntrySLong;
33 use \lsolesen\pel\PelEntryRational;
34 use \lsolesen\pel\PelEntrySRational;
35 use \lsolesen\pel\PelOverflowException;
36
37 abstract class NumberTest extends \PHPUnit_Framework_TestCase
38 {
39     private $min;
40     private $max;
41     protected $num;
42
43     public function __construct($min, $max)
44     {
45         Pel::setStrictParsing(true);
46         $this->min = $min;
47         $this->max = $max;
48         parent::__construct('PEL Exif Number Tests');
49     }
50
51     public function testOverflow()
52     {
53         $this->num->setValue(0);
54         $this->assertEquals($this->num->getValue(), 0);
55
56         $caught = false;
57         try {
58             $this->num->setValue($this->min - 1);
59         } catch (PelOverflowException $e) {
60             $caught = true;
61         }
62         $this->assertTrue($caught);
63         $this->assertEquals($this->num->getValue(), 0);
64
65         $caught = false;
66         try {
67             $this->num->setValue($this->max + 1);
68         } catch (PelOverflowException $e) {
69             $caught = true;
70         }
71         $this->assertTrue($caught);
72         $this->assertEquals($this->num->getValue(), 0);
73
74         $caught = false;
75         try {
76             $this->num->setValue(0, $this->max + 1);
77         } catch (PelOverflowException $e) {
78             $caught = true;
79         }
80         $this->assertTrue($caught);
81         $this->assertEquals($this->num->getValue(), 0);
82
83         $caught = false;
84         try {
85             $this->num->setValue(0, $this->min - 1);
86         } catch (PelOverflowException $e) {
87             $caught = true;
88         }
89         $this->assertTrue($caught);
90         $this->assertEquals($this->num->getValue(), 0);
91     }
92
93     /**
94      * @expectedException              PHPUnit_Framework_Error
95      * @expectedExceptionMessageRegExp /Missing argument 1 for lsolesen.pel.PelEntryNumber::setValue()/
96      */
97     public function testSetValueWithNoArgument()
98     {
99         $this->num->setValue();
100     }
101
102     public function testReturnValues()
103     {
104         $this->num->setValue(1, 2, 3);
105         $this->assertEquals($this->num->getValue(), array(
106             1,
107             2,
108             3
109         ));
110         $this->assertEquals($this->num->getText(), '1, 2, 3');
111
112         $this->num->setValue(1);
113         $this->assertEquals($this->num->getValue(), 1);
114         $this->assertEquals($this->num->getText(), '1');
115
116         $this->num->setValue($this->max);
117         $this->assertEquals($this->num->getValue(), $this->max);
118         $this->assertEquals($this->num->getText(), $this->max);
119
120         $this->num->setValue($this->min);
121         $this->assertEquals($this->num->getValue(), $this->min);
122         $this->assertEquals($this->num->getText(), $this->min);
123     }
124 }
125
126 class ByteTestCase extends NumberTest
127 {
128
129     public function __construct()
130     {
131         $this->num = new PelEntryByte(42);
132         parent::__construct(0, 255);
133     }
134 }
135
136 class SByteTestCase extends NumberTest
137 {
138
139     public function __construct()
140     {
141         $this->num = new PelEntrySByte(42);
142         parent::__construct(- 128, 127);
143     }
144 }
145
146 class ShortTestCase extends NumberTest
147 {
148
149     public function __construct()
150     {
151         $this->num = new PelEntryShort(42);
152         parent::__construct(0, 65535);
153     }
154 }
155
156 class SShortTestCase extends NumberTest
157 {
158
159     public function __construct()
160     {
161
162         $this->num = new PelEntrySShort(42);
163         parent::__construct(- 32768, 32767);
164     }
165 }
166
167 class LongTestCase extends NumberTest
168 {
169
170     public function __construct()
171     {
172
173         $this->num = new PelEntryLong(42);
174         parent::__construct(0, 4294967295);
175     }
176 }
177
178 class SLongTestCase extends NumberTest
179 {
180
181     public function __construct()
182     {
183
184         $this->num = new PelEntrySLong(42);
185         parent::__construct(- 2147483648, 2147483647);
186     }
187 }
188
189 class RationalTestCase extends \PHPUnit_Framework_TestCase
190 {
191     public function testOverflow()
192     {
193         $entry = new PelEntryRational(42, array(
194             1,
195             2
196         ));
197         $this->assertEquals($entry->getValue(), array(
198             1,
199             2
200         ));
201
202         $caught = false;
203         try {
204             $entry->setValue(array(
205                 3,
206                 4
207             ), array(
208                 - 1,
209                 2
210             ), array(
211                 7,
212                 8
213             ));
214         } catch (PelOverflowException $e) {
215             $caught = true;
216         }
217         $this->assertTrue($caught);
218         $this->assertEquals($entry->getValue(), array(
219             1,
220             2
221         ));
222
223         $caught = false;
224         try {
225             $entry->setValue(array(
226                 3,
227                 4
228             ), array(
229                 1,
230                 4294967296
231             ));
232         } catch (PelOverflowException $e) {
233             $caught = true;
234         }
235         $this->assertTrue($caught);
236         $this->assertEquals($entry->getValue(), array(
237             1,
238             2
239         ));
240
241         $caught = false;
242         try {
243             $entry->setValue(array(
244                 3,
245                 4
246             ), array(
247                 4294967296,
248                 1
249             ));
250         } catch (PelOverflowException $e) {
251             $caught = true;
252         }
253         $this->assertTrue($caught);
254         $this->assertEquals($entry->getValue(), array(
255             1,
256             2
257         ));
258     }
259
260     /**
261      * @expectedException              PHPUnit_Framework_Error
262      * @expectedMessage                Undefined variable: tag
263      * @expectedExceptionMessageRegExp /Missing argument 1 for lsolesen.pel.PelEntryRational::__construct()/
264      */
265     public function testPelEntryWithoutArguments()
266     {
267         $entry = new PelEntryRational();
268     }
269
270     public function testReturnValues()
271     {
272         $entry = new PelEntryRational(42);
273         $this->assertEquals($entry->getValue(), array());
274         $this->assertEquals($entry->getText(), '');
275
276         $entry->setValue(array(
277             1,
278             2
279         ), array(
280             3,
281             4
282         ), array(
283             5,
284             6
285         ));
286         $this->assertEquals($entry->getValue(), array(
287             array(
288                 1,
289                 2
290             ),
291             array(
292                 3,
293                 4
294             ),
295             array(
296                 5,
297                 6
298             )
299         ));
300         $this->assertEquals($entry->getText(), '1/2, 3/4, 5/6');
301
302         $entry->setValue(array(
303             7,
304             8
305         ));
306         $this->assertEquals($entry->getValue(), array(
307             7,
308             8
309         ));
310         $this->assertEquals($entry->getText(), '7/8');
311
312         $pattern = new PatternExpectation('/Missing argument 1 for lsolesen.pel.PelEntryNumber::setValue()/');
313         $this->expectError($pattern);
314         $entry->setValue();
315
316         $entry->setValue(array(
317             0,
318             4294967295
319         ));
320         $this->assertEquals($entry->getValue(), array(
321             0,
322             4294967295
323         ));
324         $this->assertEquals($entry->getText(), '0/4294967295');
325     }
326 }
327
328 class SRationalTestCase extends \PHPUnit_Framework_TestCase
329 {
330
331     public function __construct()
332     {
333
334         parent::__construct('PEL Exif SRational Tests');
335     }
336
337     public function testOverflow()
338     {
339         $entry = new PelEntrySRational(42, array(
340             - 1,
341             2
342         ));
343         $this->assertEquals($entry->getValue(), array(
344             - 1,
345             2
346         ));
347
348         $caught = false;
349         try {
350             $entry->setValue(array(
351                 - 10,
352                 - 20
353             ), array(
354                 - 1,
355                 - 2147483649
356             ));
357         } catch (PelOverflowException $e) {
358             $caught = true;
359         }
360         $this->assertTrue($caught);
361         $this->assertEquals($entry->getValue(), array(
362             - 1,
363             2
364         ));
365
366         $caught = false;
367         try {
368             $entry->setValue(array(
369                 3,
370                 4
371             ), array(
372                 1,
373                 2147483648
374             ));
375         } catch (PelOverflowException $e) {
376             $caught = true;
377         }
378         $this->assertTrue($caught);
379         $this->assertEquals($entry->getValue(), array(
380             - 1,
381             2
382         ));
383
384         $caught = false;
385         try {
386             $entry->setValue(array(
387                 3,
388                 4
389             ), array(
390                 4294967296,
391                 1
392             ));
393         } catch (PelOverflowException $e) {
394             $caught = true;
395         }
396         $this->assertTrue($caught);
397         $this->assertEquals($entry->getValue(), array(
398             - 1,
399             2
400         ));
401     }
402
403     public function testReturnValues()
404     {
405         $pattern = new PatternExpectation('/Missing argument 1 for lsolesen.pel.PelEntrySRational::__construct()/');
406         $this->expectError($pattern);
407         $pattern = new PatternExpectation('/Undefined variable: tag/');
408         $this->expectError($pattern);
409         $entry = new PelEntrySRational();
410
411         $entry = new PelEntrySRational(42);
412         $this->assertEquals($entry->getValue(), array());
413
414         $entry->setValue(array(
415             - 1,
416             2
417         ), array(
418             3,
419             4
420         ), array(
421             5,
422             - 6
423         ));
424         $this->assertEquals($entry->getValue(), array(
425             array(
426                 - 1,
427                 2
428             ),
429             array(
430                 3,
431                 4
432             ),
433             array(
434                 5,
435                 - 6
436             )
437         ));
438         $this->assertEquals($entry->getText(), '-1/2, 3/4, -5/6');
439
440         $entry->setValue(array(
441             - 7,
442             - 8
443         ));
444         $this->assertEquals($entry->getValue(), array(
445             - 7,
446             - 8
447         ));
448         $this->assertEquals($entry->getText(), '7/8');
449
450         $pattern = new PatternExpectation('/Missing argument 1 for lsolesen.pel.PelEntryNumber::setValue()/');
451         $this->expectError($pattern);
452         $entry->setValue();
453
454         $entry->setValue(array(
455             0,
456             2147483647
457         ));
458         $this->assertEquals($entry->getValue(), array(
459             0,
460             2147483647
461         ));
462         $this->assertEquals($entry->getText(), '0/2147483647');
463     }
464 }