5bb06e7d13c35af96e6e7300a8ca5bdcea31e36d
[yaffs-website] / vendor / lsolesen / pel / test / DataWindowTest.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 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\PelDataWindow;
27 use lsolesen\pel\PelConvert;
28 use lsolesen\pel\PelDataWindowOffsetException;
29
30 class DataWindowTest extends \PHPUnit_Framework_TestCase
31 {
32
33     function testReadBytes()
34     {
35         $window = new PelDataWindow('abcdefgh');
36
37         $this->assertEquals($window->getSize(), 8);
38         $this->assertEquals($window->getBytes(), 'abcdefgh');
39
40         $this->assertEquals($window->getBytes(0), 'abcdefgh');
41         $this->assertEquals($window->getBytes(1), 'bcdefgh');
42         $this->assertEquals($window->getBytes(7), 'h');
43         // $this->assertEquals($window->getBytes(8), '');
44
45         $this->assertEquals($window->getBytes(- 1), 'h');
46         $this->assertEquals($window->getBytes(- 2), 'gh');
47         $this->assertEquals($window->getBytes(- 7), 'bcdefgh');
48         $this->assertEquals($window->getBytes(- 8), 'abcdefgh');
49
50         $clone = $window->getClone(2, 4);
51         $this->assertEquals($clone->getSize(), 4);
52         $this->assertEquals($clone->getBytes(), 'cdef');
53
54         $this->assertEquals($clone->getBytes(0), 'cdef');
55         $this->assertEquals($clone->getBytes(1), 'def');
56         $this->assertEquals($clone->getBytes(3), 'f');
57         // $this->assertEquals($clone->getBytes(4), '');
58
59         $this->assertEquals($clone->getBytes(- 1), 'f');
60         $this->assertEquals($clone->getBytes(- 2), 'ef');
61         $this->assertEquals($clone->getBytes(- 3), 'def');
62         $this->assertEquals($clone->getBytes(- 4), 'cdef');
63
64         $caught = false;
65         try {
66             $clone->getBytes(0, 6);
67         } catch (PelDataWindowOffsetException $e) {
68             $caught = true;
69         }
70         $this->assertTrue($caught);
71     }
72
73     function testReadIntegers()
74     {
75         $window = new PelDataWindow("\x01\x02\x03\x04", PelConvert::BIG_ENDIAN);
76
77         $this->assertEquals($window->getSize(), 4);
78         $this->assertEquals($window->getBytes(), "\x01\x02\x03\x04");
79
80         $this->assertEquals($window->getByte(0), 0x01);
81         $this->assertEquals($window->getByte(1), 0x02);
82         $this->assertEquals($window->getByte(2), 0x03);
83         $this->assertEquals($window->getByte(3), 0x04);
84
85         $this->assertEquals($window->getShort(0), 0x0102);
86         $this->assertEquals($window->getShort(1), 0x0203);
87         $this->assertEquals($window->getShort(2), 0x0304);
88
89         $this->assertEquals($window->getLong(0), 0x01020304);
90
91         $window->setByteOrder(PelConvert::LITTLE_ENDIAN);
92         $this->assertEquals($window->getSize(), 4);
93         $this->assertEquals($window->getBytes(), "\x01\x02\x03\x04");
94
95         $this->assertEquals($window->getByte(0), 0x01);
96         $this->assertEquals($window->getByte(1), 0x02);
97         $this->assertEquals($window->getByte(2), 0x03);
98         $this->assertEquals($window->getByte(3), 0x04);
99
100         $this->assertEquals($window->getShort(0), 0x0201);
101         $this->assertEquals($window->getShort(1), 0x0302);
102         $this->assertEquals($window->getShort(2), 0x0403);
103
104         $this->assertEquals($window->getLong(0), 0x04030201);
105     }
106
107     function testReadBigIntegers()
108     {
109         $window = new PelDataWindow("\x89\xAB\xCD\xEF", PelConvert::BIG_ENDIAN);
110
111         $this->assertEquals($window->getSize(), 4);
112         $this->assertEquals($window->getBytes(), "\x89\xAB\xCD\xEF");
113
114         $this->assertEquals($window->getByte(0), 0x89);
115         $this->assertEquals($window->getByte(1), 0xAB);
116         $this->assertEquals($window->getByte(2), 0xCD);
117         $this->assertEquals($window->getByte(3), 0xEF);
118
119         $this->assertEquals($window->getShort(0), 0x89AB);
120         $this->assertEquals($window->getShort(1), 0xABCD);
121         $this->assertEquals($window->getShort(2), 0xCDEF);
122
123         $this->assertEquals($window->getLong(0), 0x89ABCDEF);
124
125         $window->setByteOrder(PelConvert::LITTLE_ENDIAN);
126         $this->assertEquals($window->getSize(), 4);
127         $this->assertEquals($window->getBytes(), "\x89\xAB\xCD\xEF");
128
129         $this->assertEquals($window->getByte(0), 0x89);
130         $this->assertEquals($window->getByte(1), 0xAB);
131         $this->assertEquals($window->getByte(2), 0xCD);
132         $this->assertEquals($window->getByte(3), 0xEF);
133
134         $this->assertEquals($window->getShort(0), 0xAB89);
135         $this->assertEquals($window->getShort(1), 0xCDAB);
136         $this->assertEquals($window->getShort(2), 0xEFCD);
137
138         $this->assertEquals($window->getLong(0), 0xEFCDAB89);
139     }
140 }