11
11
vers = "0.1" ) ] ;
12
12
#[ crate_type = "lib" ] ;
13
13
14
+ #[ cfg( test) ]
15
+ extern mod extra;
16
+
14
17
extern mod std;
15
18
use std:: cast;
16
19
use std:: io;
@@ -25,6 +28,7 @@ pub mod ffi;
25
28
#[ link_args="-L. -lpng -lz -lshim" ]
26
29
extern { }
27
30
31
+ #[ deriving( Eq ) ]
28
32
pub enum ColorType {
29
33
K1 , K2 , K4 , K8 , K16 ,
30
34
KA8 , KA16 ,
@@ -249,15 +253,19 @@ pub fn store_png(img: &Image, path: &Path) -> Result<(),~str> {
249
253
250
254
#[cfg(test)]
251
255
mod test {
256
+ use extra::test::{bench, fmt_bench_samples};
252
257
use std::io;
253
258
use std::io::File;
254
259
use std::vec;
255
- use super::{ffi, load_png, store_png, RGB8, Image};
260
+
261
+ use super::{ffi, load_png, load_png_from_memory, store_png};
262
+ use super::{ColorType, RGB8, RGBA8, KA8, Image};
256
263
257
264
#[test]
258
265
#[fixed_stack_segment]
259
266
fn test_valid_png() {
260
- let mut reader = match File::open_mode(&Path::new(" test. png"), io::Open, io::Read) {
267
+ let file = " test/servo-screenshot. png";
268
+ let mut reader = match File::open_mode(&Path::new(file), io::Open, io::Read) {
261
269
Some(r) => r,
262
270
None => fail!(" could not open file"),
263
271
};
@@ -271,10 +279,49 @@ mod test {
271
279
}
272
280
}
273
281
282
+ fn load_rgba8(file: &'static str, w: u32, h: u32) {
283
+ match load_png(&Path::new(file)) {
284
+ Err(m) => fail!(m),
285
+ Ok(image) => {
286
+ assert_eq!(image.color_type, RGBA8);
287
+ assert_eq!(image.width, w);
288
+ assert_eq!(image.height, h);
289
+ }
290
+ }
291
+ }
292
+
274
293
#[test]
275
294
fn test_load() {
276
- let res = load_png(&Path::new(" test. png"));
277
- assert!(res.is_ok());
295
+ load_rgba8(" test/servo-screenshot. png", 831, 624);
296
+
297
+ test_store();
298
+ load_rgba8(" test/store. png", 10, 10);
299
+ }
300
+
301
+ fn bench_file_from_memory(file: &'static str, w: u32, h: u32, c: ColorType) {
302
+ let mut reader = match File::open_mode(&Path::new(file), io::Open, io::Read) {
303
+ Some(r) => r,
304
+ None => fail!(" could not open ' { } ' ", file)
305
+ };
306
+ let buf = reader.read_to_end();
307
+ let bs = bench::benchmark(|b| b.iter(|| {
308
+ match load_png_from_memory(buf) {
309
+ Err(m) => fail!(m),
310
+ Ok(image) => {
311
+ assert_eq!(image.color_type, c);
312
+ assert_eq!(image.width, w);
313
+ assert_eq!(image.height, h);
314
+ }
315
+ }
316
+ }));
317
+ println!(" libpng load ' { } ' : { } ", file, fmt_bench_samples(&bs));
318
+ }
319
+
320
+ #[test]
321
+ fn test_load_perf() {
322
+ bench_file_from_memory(" test/servo-screenshot. png", 831, 624, RGBA8);
323
+ bench_file_from_memory(" test/mozilla-dinosaur-head-logo. png", 1300, 929, RGBA8);
324
+ bench_file_from_memory(" test/rust-huge-logo. png", 4000, 4000, KA8);
278
325
}
279
326
280
327
#[test]
@@ -285,7 +332,7 @@ mod test {
285
332
color_type: RGB8,
286
333
pixels: vec::from_elem(10 * 10 * 3, 100u8),
287
334
};
288
- let res = store_png(&img, &Path::new(" test_store . png") ) ;
335
+ let res = store_png(&img, &Path::new(" test/store . png") ) ;
289
336
assert!( res. is_ok( ) ) ;
290
337
}
291
338
}
0 commit comments