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.
8 * Copyright (C) 2004 Martin Geisler.
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.
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.
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
26 use lsolesen\pel\PelDataWindow;
27 use lsolesen\pel\PelConvert;
28 use lsolesen\pel\PelDataWindowOffsetException;
30 class DataWindowTest extends \PHPUnit_Framework_TestCase
33 function testReadBytes()
35 $window = new PelDataWindow('abcdefgh');
37 $this->assertEquals($window->getSize(), 8);
38 $this->assertEquals($window->getBytes(), 'abcdefgh');
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), '');
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');
50 $clone = $window->getClone(2, 4);
51 $this->assertEquals($clone->getSize(), 4);
52 $this->assertEquals($clone->getBytes(), 'cdef');
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), '');
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');
66 $clone->getBytes(0, 6);
67 } catch (PelDataWindowOffsetException $e) {
70 $this->assertTrue($caught);
73 function testReadIntegers()
75 $window = new PelDataWindow("\x01\x02\x03\x04", PelConvert::BIG_ENDIAN);
77 $this->assertEquals($window->getSize(), 4);
78 $this->assertEquals($window->getBytes(), "\x01\x02\x03\x04");
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);
85 $this->assertEquals($window->getShort(0), 0x0102);
86 $this->assertEquals($window->getShort(1), 0x0203);
87 $this->assertEquals($window->getShort(2), 0x0304);
89 $this->assertEquals($window->getLong(0), 0x01020304);
91 $window->setByteOrder(PelConvert::LITTLE_ENDIAN);
92 $this->assertEquals($window->getSize(), 4);
93 $this->assertEquals($window->getBytes(), "\x01\x02\x03\x04");
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);
100 $this->assertEquals($window->getShort(0), 0x0201);
101 $this->assertEquals($window->getShort(1), 0x0302);
102 $this->assertEquals($window->getShort(2), 0x0403);
104 $this->assertEquals($window->getLong(0), 0x04030201);
107 function testReadBigIntegers()
109 $window = new PelDataWindow("\x89\xAB\xCD\xEF", PelConvert::BIG_ENDIAN);
111 $this->assertEquals($window->getSize(), 4);
112 $this->assertEquals($window->getBytes(), "\x89\xAB\xCD\xEF");
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);
119 $this->assertEquals($window->getShort(0), 0x89AB);
120 $this->assertEquals($window->getShort(1), 0xABCD);
121 $this->assertEquals($window->getShort(2), 0xCDEF);
123 $this->assertEquals($window->getLong(0), 0x89ABCDEF);
125 $window->setByteOrder(PelConvert::LITTLE_ENDIAN);
126 $this->assertEquals($window->getSize(), 4);
127 $this->assertEquals($window->getBytes(), "\x89\xAB\xCD\xEF");
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);
134 $this->assertEquals($window->getShort(0), 0xAB89);
135 $this->assertEquals($window->getShort(1), 0xCDAB);
136 $this->assertEquals($window->getShort(2), 0xEFCD);
138 $this->assertEquals($window->getLong(0), 0xEFCDAB89);