1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* test-seek.c - Test for the seek emulation functionality of the GNOME Virtual |
---|
3 | File System library. |
---|
4 | |
---|
5 | Copyright (C) 1999 Free Software Foundation |
---|
6 | |
---|
7 | The Gnome Library is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU Library General Public License as |
---|
9 | published by the Free Software Foundation; either version 2 of the |
---|
10 | License, or (at your option) any later version. |
---|
11 | |
---|
12 | The Gnome Library is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | Library General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Library General Public |
---|
18 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
19 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
20 | Boston, MA 02111-1307, USA. |
---|
21 | |
---|
22 | Author: Michael Meeks <michael@imaginator.com> */ |
---|
23 | |
---|
24 | #include <config.h> |
---|
25 | |
---|
26 | #include <errno.h> |
---|
27 | #include <glib/gmessages.h> |
---|
28 | #include <libgnomevfs/gnome-vfs-init.h> |
---|
29 | #include <libgnomevfs/gnome-vfs-ops.h> |
---|
30 | #include <stdio.h> |
---|
31 | #include <stdlib.h> |
---|
32 | |
---|
33 | static void |
---|
34 | show_result (GnomeVFSResult result, const gchar *what, const gchar *text_uri) |
---|
35 | { |
---|
36 | fprintf (stderr, "%s `%s': %s\n", |
---|
37 | what, text_uri, gnome_vfs_result_to_string (result)); |
---|
38 | if (result != GNOME_VFS_OK) |
---|
39 | exit (1); |
---|
40 | } |
---|
41 | |
---|
42 | static gboolean |
---|
43 | show_if_error (GnomeVFSResult result, const gchar *what) |
---|
44 | { |
---|
45 | if (result != GNOME_VFS_OK) { |
---|
46 | fprintf (stderr, "%s: `%s'\n", |
---|
47 | what, gnome_vfs_result_to_string (result)); |
---|
48 | return TRUE; |
---|
49 | } else |
---|
50 | return FALSE; |
---|
51 | } |
---|
52 | |
---|
53 | static const char * |
---|
54 | translate_vfs_seek_pos (GnomeVFSSeekPosition whence, int *unix_whence) |
---|
55 | { |
---|
56 | const char *txt; |
---|
57 | int ref_whence; |
---|
58 | |
---|
59 | switch (whence) { |
---|
60 | case GNOME_VFS_SEEK_START: |
---|
61 | txt = "seek_start"; |
---|
62 | ref_whence = SEEK_SET; |
---|
63 | break; |
---|
64 | case GNOME_VFS_SEEK_CURRENT: |
---|
65 | txt = "seek_current"; |
---|
66 | ref_whence = SEEK_CUR; |
---|
67 | break; |
---|
68 | case GNOME_VFS_SEEK_END: |
---|
69 | txt = "seek_end"; |
---|
70 | ref_whence = SEEK_END; |
---|
71 | break; |
---|
72 | default: |
---|
73 | txt = "unknown seek type"; |
---|
74 | ref_whence = SEEK_SET; |
---|
75 | g_warning ("Unknown seek type"); |
---|
76 | } |
---|
77 | if (unix_whence) |
---|
78 | *unix_whence = ref_whence; |
---|
79 | |
---|
80 | return txt; |
---|
81 | } |
---|
82 | |
---|
83 | static gboolean |
---|
84 | seek_test_chunk (GnomeVFSHandle *handle, |
---|
85 | FILE *ref, |
---|
86 | GnomeVFSFileOffset vfs_offset, |
---|
87 | GnomeVFSSeekPosition whence, |
---|
88 | GnomeVFSFileSize length) |
---|
89 | { |
---|
90 | GnomeVFSResult result; |
---|
91 | int ref_whence; |
---|
92 | |
---|
93 | translate_vfs_seek_pos (whence, &ref_whence); |
---|
94 | |
---|
95 | { /* Preliminary tell */ |
---|
96 | GnomeVFSFileSize offset = 0; |
---|
97 | long ref_off; |
---|
98 | result = gnome_vfs_tell (handle, &offset); |
---|
99 | if (show_if_error (result, "head gnome_vfs_tell")) |
---|
100 | return FALSE; |
---|
101 | |
---|
102 | ref_off = ftell (ref); |
---|
103 | if (ref_off < 0) { |
---|
104 | g_warning ("Wierd ftell failure"); |
---|
105 | return FALSE; |
---|
106 | } |
---|
107 | |
---|
108 | if (ref_off != offset) { |
---|
109 | g_warning ("Offset mismatch %d should be %d", (int)offset, (int)ref_off); |
---|
110 | return FALSE; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | { /* seek */ |
---|
115 | int fseekres; |
---|
116 | result = gnome_vfs_seek (handle, whence, vfs_offset); |
---|
117 | fseekres = fseek (ref, vfs_offset, ref_whence); |
---|
118 | |
---|
119 | if (fseekres == 0 && |
---|
120 | result != GNOME_VFS_OK) { |
---|
121 | g_warning ("seek success difference '%d - %d' - '%s'", |
---|
122 | fseekres, errno, gnome_vfs_result_to_string (result)); |
---|
123 | return FALSE; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | { /* read - leaks like a sieve on error =] */ |
---|
128 | guint8 *data, *data_ref; |
---|
129 | int bytes_read_ref; |
---|
130 | GnomeVFSFileSize bytes_read; |
---|
131 | |
---|
132 | data = g_new (guint8, length); |
---|
133 | data_ref = g_new (guint8, length); |
---|
134 | |
---|
135 | result = gnome_vfs_read (handle, data, length, &bytes_read); |
---|
136 | bytes_read_ref = fread (data_ref, 1, length, ref); |
---|
137 | |
---|
138 | if (bytes_read_ref != bytes_read) { |
---|
139 | g_warning ("read failure: vfs read %d and fread %d bytes ('%s')", |
---|
140 | (int)bytes_read, bytes_read_ref, |
---|
141 | gnome_vfs_result_to_string (result)); |
---|
142 | return FALSE; |
---|
143 | } |
---|
144 | if (result != GNOME_VFS_OK) { |
---|
145 | g_warning ("VFS read failed with '%s'", |
---|
146 | gnome_vfs_result_to_string (result)); |
---|
147 | return FALSE; |
---|
148 | } |
---|
149 | |
---|
150 | { /* Compare the data */ |
---|
151 | int i; |
---|
152 | for (i = 0; i < bytes_read; i++) |
---|
153 | if (data[i] != data_ref[i]) { |
---|
154 | g_warning ("vfs read data mismatch at byte %d, '%d' != '%d'", |
---|
155 | i, data[i], data_ref[i]); |
---|
156 | return FALSE; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | g_free (data_ref); |
---|
161 | g_free (data); |
---|
162 | } |
---|
163 | |
---|
164 | { /* Tail tell */ |
---|
165 | GnomeVFSFileSize offset; |
---|
166 | long ref_off; |
---|
167 | result = gnome_vfs_tell (handle, &offset); |
---|
168 | if (show_if_error (result, "tail gnome_vfs_tell")) |
---|
169 | return FALSE; |
---|
170 | |
---|
171 | ref_off = ftell (ref); |
---|
172 | if (ref_off < 0) { |
---|
173 | g_warning ("Wierd ftell failure"); |
---|
174 | return FALSE; |
---|
175 | } |
---|
176 | |
---|
177 | if (ref_off != offset) { |
---|
178 | g_warning ("Offset mismatch %d should be %d", (int)offset, (int)ref_off); |
---|
179 | return FALSE; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | return TRUE; |
---|
184 | } |
---|
185 | |
---|
186 | int |
---|
187 | main (int argc, char **argv) |
---|
188 | { |
---|
189 | GnomeVFSResult result; |
---|
190 | GnomeVFSHandle *handle; |
---|
191 | FILE *ref; |
---|
192 | int i, failures; |
---|
193 | |
---|
194 | if (! gnome_vfs_init ()) { |
---|
195 | fprintf (stderr, "Cannot initialize gnome-vfs.\n"); |
---|
196 | return 1; |
---|
197 | } |
---|
198 | |
---|
199 | if (argc != 3) { |
---|
200 | fprintf (stderr, "This is a program to test seek emulation on linear filesystems\n"); |
---|
201 | fprintf (stderr, "Usage: %s <source file uri> <seekable local reference fname>\n", |
---|
202 | argv[0]); |
---|
203 | return 1; |
---|
204 | } |
---|
205 | |
---|
206 | result = gnome_vfs_open (&handle, argv[1], GNOME_VFS_OPEN_READ|GNOME_VFS_OPEN_RANDOM); |
---|
207 | show_result (result, "gnome_vfs_open", argv[1]); |
---|
208 | |
---|
209 | if (!(ref = fopen (argv[2], "r"))) { |
---|
210 | fprintf (stderr, "Failed to open '%s' to compare seek history\n", argv[2]); |
---|
211 | exit (1); |
---|
212 | } |
---|
213 | |
---|
214 | failures = 0; |
---|
215 | for (i = 0; i < 10; i++) { |
---|
216 | GnomeVFSFileSize length = (1000.0 * rand () / (RAND_MAX + 1.0)); |
---|
217 | GnomeVFSFileOffset seekpos = (1000.0 * rand () / (RAND_MAX + 1.0)); |
---|
218 | GnomeVFSSeekPosition w = (int)(2.0 * rand () / (RAND_MAX + 1.0)); |
---|
219 | |
---|
220 | if (!seek_test_chunk (handle, ref, seekpos, w, length)) { |
---|
221 | printf ("Failed: seek (offset %d, whence '%s'), read (length %d), tell = %ld\n", |
---|
222 | (int)seekpos, translate_vfs_seek_pos (w, NULL), |
---|
223 | (int)length, ftell (ref)); |
---|
224 | failures++; |
---|
225 | } |
---|
226 | } |
---|
227 | if (failures) |
---|
228 | printf ("%d tests failed\n", failures); |
---|
229 | else |
---|
230 | printf ("All test successful\n"); |
---|
231 | |
---|
232 | result = gnome_vfs_close (handle); |
---|
233 | show_result (result, "gnome_vfs_close", argv[1]); |
---|
234 | |
---|
235 | return 0; |
---|
236 | } |
---|