1# This file is a Tcl script to test out the code in tkImgFmtPPM.c,
2# which reads and write PPM-format image files for photo widgets.
3# The files is organized in the standard fashion for Tcl tests.
4#
5# Copyright (c) 1994-1997 Sun Microsystems, Inc.
6# Copyright (c) 1998-1999 by Scriptics Corporation.
7# All rights reserved.
8#
9# RCS: @(#) $Id$
10
11package require tcltest 2.1
12eval tcltest::configure $argv
13tcltest::loadTestedCommands
14
15eval image delete [image names]
16
17# Note that we do not use [tcltest::makeFile] because it is
18# only suitable for text files
19proc put {file data} {
20    set f [open $file w]
21    fconfigure $f -translation lf
22    puts -nonewline $f $data
23    close $f
24}
25
26test imgPPM-1.1 {FileReadPPM procedure} {
27    put test.ppm "P6\n0 256\n255\nabcdef"
28    list [catch {image create photo p1 -file test.ppm} msg] $msg
29} {1 {PPM image file "test.ppm" has dimension(s) <= 0}}
30test imgPPM-1.2 {FileReadPPM procedure} {
31    put test.ppm "P6\n-2 256\n255\nabcdef"
32    list [catch {image create photo p1 -file test.ppm} msg] $msg
33} {1 {PPM image file "test.ppm" has dimension(s) <= 0}}
34test imgPPM-1.3 {FileReadPPM procedure} {
35    put test.ppm "P6\n10 0\n255\nabcdef"
36    list [catch {image create photo p1 -file test.ppm} msg] $msg
37} {1 {PPM image file "test.ppm" has dimension(s) <= 0}}
38test imgPPM-1.4 {FileReadPPM procedure} {
39    put test.ppm "P6\n10 -2\n255\nabcdef"
40    list [catch {image create photo p1 -file test.ppm} msg] $msg
41} {1 {PPM image file "test.ppm" has dimension(s) <= 0}}
42test imgPPM-1.5 {FileReadPPM procedure} {
43    put test.ppm "P6\n10 20\n256\nabcdef"
44    list [catch {image create photo p1 -file test.ppm} msg] $msg
45} {1 {PPM image file "test.ppm" has bad maximum intensity value 256}}
46test imgPPM-1.6 {FileReadPPM procedure} {
47    put test.ppm "P6\n10 20\n0\nabcdef"
48    list [catch {image create photo p1 -file test.ppm} msg] $msg
49} {1 {PPM image file "test.ppm" has bad maximum intensity value 0}}
50test imgPPM-1.7 {FileReadPPM procedure} {
51    put test.ppm "P6\n10 10\n255\nabcdef"
52    list [catch {image create photo p1 -file test.ppm} msg] $msg
53} {1 {error reading PPM image file "test.ppm": not enough data}}
54test imgPPM-1.8 {FileReadPPM procedure} {
55    put test.ppm "P6\n5 4\n255\n01234567890123456789012345678901234567890123456789012345678"
56    list [catch {image create photo p1 -file test.ppm} msg] $msg
57} {1 {error reading PPM image file "test.ppm": not enough data}}
58test imgPPM-1.9 {FileReadPPM procedure} {
59    put test.ppm "P6\n5 4\n150\n012345678901234567890123456789012345678901234567890123456789"
60    list [catch {image create photo p1 -file test.ppm} msg] $msg \
61	    [image width p1] [image height p1]
62} {0 p1 5 4}
63
64catch {image delete p1}
65put test.ppm "P6\n5 4\n255\n012345678901234567890123456789012345678901234567890123456789"
66image create photo p1 -file test.ppm
67test imgPPM-2.1 {FileWritePPM procedure} {
68    list [catch {p1 write not_a_dir/bar/baz/gorp} msg] [string tolower $msg] \
69	    [string tolower $errorCode]
70} {1 {couldn't open "not_a_dir/bar/baz/gorp": no such file or directory} {posix enoent {no such file or directory}}}
71test imgPPM-2.2 {FileWritePPM procedure} {
72    catch {unset data}
73    p1 write -format ppm test.ppm
74    set fd [open test.ppm]
75    set data [read $fd]
76    close $fd
77    set data
78} {P6
795 4
80255
81012345678901234567890123456789012345678901234567890123456789}
82
83test imgPPM-3.1 {ReadPPMFileHeader procedure} {
84    catch {image delete p1}
85    put test.ppm "#   \n#\n#\nP6\n#\n##\n5 4\n255\n012345678901234567890123456789012345678901234567890123456789"
86    list [catch {image create photo p1 -file test.ppm} msg] $msg
87} {0 p1}
88test imgPPM-3.2 {ReadPPMFileHeader procedure} {
89    catch {image delete p1}
90    put test.ppm "P6\n5\n 4   	                                                                 255\n012345678901234567890123456789012345678901234567890123456789"
91    list [catch {image create photo p1 -file test.ppm} msg] $msg
92} {0 p1}
93test imgPPM-3.3 {ReadPPMFileHeader procedure} {
94    catch {image delete p1}
95    put test.ppm "P6\n#                                                                      asdfasdf\n5 4\n255\n012345678901234567890123456789012345678901234567890123456789"
96    list [catch {image create photo p1 -file test.ppm} msg] $msg
97} {0 p1}
98test imgPPM-3.4 {ReadPPMFileHeader procedure} {
99    catch {image delete p1}
100    put test.ppm "P6 \n5 4\n255\n012345678901234567890123456789012345678901234567890123456789"
101    list [catch {image create photo p1 -file test.ppm} msg] $msg
102} {0 p1}
103test imgPPM-3.5 {ReadPPMFileHeader procedure} {
104    catch {image delete p1}
105    put test.ppm "P5\n5 4\n255\n01234567890123456789"
106    list [catch {image create photo p1 -file test.ppm} msg] $msg
107} {0 p1}
108test imgPPM-3.6 {ReadPPMFileHeader procedure} {
109    catch {image delete p1}
110    put test.ppm "P3\n5 4\n255\n012345678901234567890123456789012345678901234567890123456789"
111    list [catch {image create photo p1 -file test.ppm} msg] $msg
112} {1 {couldn't recognize data in image file "test.ppm"}}
113test imgPPM-3.7 {ReadPPMFileHeader procedure} {
114    catch {image delete p1}
115    put test.ppm "P6x\n5 4\n255\n012345678901234567890123456789012345678901234567890123456789"
116    list [catch {image create photo p1 -file test.ppm} msg] $msg
117} {1 {couldn't recognize data in image file "test.ppm"}}
118test imgPPM-3.8 {ReadPPMFileHeader procedure} {
119    catch {image delete p1}
120    put test.ppm "P6\nxy5 4\n255\n012345678901234567890123456789012345678901234567890123456789"
121    list [catch {image create photo p1 -file test.ppm} msg] $msg
122} {1 {couldn't recognize data in image file "test.ppm"}}
123test imgPPM-3.9 {ReadPPMFileHeader procedure} {
124    catch {image delete p1}
125    put test.ppm "P6\n5\n255\n!012345678901234567890123456789012345678901234567890123456789"
126    list [catch {image create photo p1 -file test.ppm} msg] $msg
127} {1 {couldn't recognize data in image file "test.ppm"}}
128test imgPPM-3.10 {ReadPPMFileHeader procedure} {
129    catch {image delete p1}
130    put test.ppm "P6\n5 4\nzz255\n012345678901234567890123456789012345678901234567890123456789"
131    list [catch {image create photo p1 -file test.ppm} msg] $msg
132} {1 {couldn't recognize data in image file "test.ppm"}}
133test imgPPM-3.11 {ReadPPMFileHeader procedure, empty file} {
134    catch {image delete p1}
135    put test.ppm "     "
136    list [catch {image create photo p1 -file test.ppm} msg] $msg
137} {1 {couldn't recognize data in image file "test.ppm"}}
138test imgPPM-3.12 {ReadPPMFileHeader procedure, file ends too soon} {
139    catch {image delete p1}
140    put test.ppm "P6\n566"
141    list [catch {image create photo p1 -file test.ppm} msg] $msg
142} {1 {couldn't recognize data in image file "test.ppm"}}
143test imgPPM-3.13 {ReadPPMFileHeader procedure, file ends too soon} {
144    catch {image delete p1}
145    put test.ppm "P6\n566\n#asdf"
146    list [catch {image create photo p1 -file test.ppm} msg] $msg
147} {1 {couldn't recognize data in image file "test.ppm"}}
148
149test imgPPM-4.1 {StringReadPPM procedure, data too short [Bug 1822391]} \
150    -setup {
151	image create photo I -width 1103 -height 997
152    } \
153    -cleanup {
154	image delete I
155    } \
156    -body {
157	I put "P5\n1103 997\n255\n"
158    } \
159    -returnCodes error \
160    -result {truncated PPM data}
161
162eval image delete [image names]
163
164# cleanup
165catch {file delete test.ppm}
166cleanupTests
167return
168