1 | /* -*- Mode: C; c-basic-offset: 2; -*- */ |
2 | /* GdkPixbuf library - test loaders |
3 | * |
4 | * Copyright (C) 2001 Søren Sandmann (sandmann@daimi.au.dk) |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include "config.h" |
21 | #include "gdk-pixbuf/gdk-pixbuf.h" |
22 | #include "glib.h" |
23 | #include <stdio.h> |
24 | #include <stdlib.h> |
25 | #include <time.h> |
26 | #include <string.h> |
27 | |
28 | #ifdef HAVE_SYS_TIME_H |
29 | #include <sys/time.h> |
30 | #endif |
31 | |
32 | #ifdef HAVE_SYS_RESOURCE_H |
33 | #include <sys/resource.h> |
34 | #endif |
35 | |
36 | #include "test-common.h" |
37 | |
38 | static void |
39 | randomly_modify (const gchar *image, guint size) |
40 | { |
41 | int i, n; |
42 | |
43 | guchar *img_copy = g_malloc (n_bytes: size); |
44 | memmove (dest: img_copy, src: image, n: size); |
45 | |
46 | n = MIN (100, size / 4); |
47 | |
48 | for (i = 0; i < n; i++) |
49 | { |
50 | FILE *f; |
51 | GdkPixbufLoader *loader; |
52 | |
53 | guint index = g_test_rand_int_range (begin: 0, end: size); |
54 | guchar byte = g_test_rand_int_range (begin: 0, end: 256); |
55 | |
56 | img_copy[index] = byte; |
57 | f = fopen (filename: "pixbuf-randomly-modified-image" , modes: "w" ); |
58 | g_assert (f != NULL); |
59 | fwrite (ptr: img_copy, size: size, n: 1, s: f); |
60 | g_assert (!ferror (f)); |
61 | fclose (stream: f); |
62 | |
63 | loader = gdk_pixbuf_loader_new (); |
64 | gdk_pixbuf_loader_write (loader, buf: img_copy, count: size, NULL); |
65 | gdk_pixbuf_loader_close (loader, NULL); |
66 | g_object_unref (object: loader); |
67 | } |
68 | g_free (mem: img_copy); |
69 | } |
70 | |
71 | static void |
72 | test_randomly_modified (gconstpointer data) |
73 | { |
74 | GFile *file = G_FILE (data); |
75 | gchar *buffer; |
76 | gsize size; |
77 | gint iterations; |
78 | gint i; |
79 | GError *error = NULL; |
80 | |
81 | g_file_load_contents (file, NULL, contents: &buffer, length: &size, NULL, error: &error); |
82 | g_assert_no_error (error); |
83 | |
84 | if (g_test_thorough ()) |
85 | iterations = 1000; |
86 | else |
87 | iterations = 1; |
88 | |
89 | for (i = 0; i < iterations; i++) |
90 | randomly_modify (image: buffer, size); |
91 | |
92 | g_free (mem: buffer); |
93 | } |
94 | |
95 | int |
96 | main (int argc, char **argv) |
97 | { |
98 | gchar *base_dir; |
99 | GFile *base, *test_images; |
100 | #ifdef HAVE_SETRLIMIT |
101 | struct rlimit max_mem_size; |
102 | |
103 | max_mem_size.rlim_cur = 100 * 1024 * 1024; /* 100M */ |
104 | max_mem_size.rlim_max = max_mem_size.rlim_cur; |
105 | setrlimit (RLIMIT_DATA, rlimits: &max_mem_size); |
106 | #endif |
107 | |
108 | g_test_init (argc: &argc, argv: &argv, NULL); |
109 | |
110 | base_dir = g_build_filename (first_element: g_test_get_dir (file_type: G_TEST_DIST), "test-images" , NULL); |
111 | base = g_file_new_for_path (path: base_dir); |
112 | |
113 | test_images = g_file_get_child (file: base, name: "randomly-modified" ); |
114 | add_test_for_all_images (prefix: "/pixbuf" , base, file: test_images, test_func: test_randomly_modified, NULL); |
115 | g_object_unref (object: test_images); |
116 | |
117 | test_images = g_file_get_child (file: base, name: "fail" ); |
118 | add_test_for_all_images (prefix: "/pixbuf/randomly-modified" , base, file: test_images, test_func: test_randomly_modified, NULL); |
119 | g_object_unref (object: test_images); |
120 | |
121 | g_object_unref (object: base); |
122 | g_free (mem: base_dir); |
123 | |
124 | g_test_message (format: "Modified image is written to pixbuf-randomly-modified-image" ); |
125 | |
126 | return g_test_run (); |
127 | } |
128 | |