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
5 Copyright (C) 1999 Free Software Foundation
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.
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.
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.
22 Author: Michael Meeks <michael@imaginator.com> */
27 #include <glib/gmessages.h>
28 #include <libgnomevfs/gnome-vfs-init.h>
29 #include <libgnomevfs/gnome-vfs-ops.h>
34 show_result (GnomeVFSResult result, const gchar *what, const gchar *text_uri)
36 fprintf (stderr, "%s `%s': %s\n",
37 what, text_uri, gnome_vfs_result_to_string (result));
38 if (result != GNOME_VFS_OK)
43 show_if_error (GnomeVFSResult result, const gchar *what)
45 if (result != GNOME_VFS_OK) {
46 fprintf (stderr, "%s: `%s'\n",
47 what, gnome_vfs_result_to_string (result));
54 translate_vfs_seek_pos (GnomeVFSSeekPosition whence, int *unix_whence)
60 case GNOME_VFS_SEEK_START:
62 ref_whence = SEEK_SET;
64 case GNOME_VFS_SEEK_CURRENT:
66 ref_whence = SEEK_CUR;
68 case GNOME_VFS_SEEK_END:
70 ref_whence = SEEK_END;
73 txt = "unknown seek type";
74 ref_whence = SEEK_SET;
75 g_warning ("Unknown seek type");
78 *unix_whence = ref_whence;
84 seek_test_chunk (GnomeVFSHandle *handle,
86 GnomeVFSFileOffset vfs_offset,
87 GnomeVFSSeekPosition whence,
88 GnomeVFSFileSize length)
90 GnomeVFSResult result;
93 translate_vfs_seek_pos (whence, &ref_whence);
95 { /* Preliminary tell */
96 GnomeVFSFileSize offset = 0;
98 result = gnome_vfs_tell (handle, &offset);
99 if (show_if_error (result, "head gnome_vfs_tell"))
102 ref_off = ftell (ref);
104 g_warning ("Wierd ftell failure");
108 if (ref_off != offset) {
109 g_warning ("Offset mismatch %d should be %d", (int)offset, (int)ref_off);
116 result = gnome_vfs_seek (handle, whence, vfs_offset);
117 fseekres = fseek (ref, vfs_offset, ref_whence);
120 result != GNOME_VFS_OK) {
121 g_warning ("seek success difference '%d - %d' - '%s'",
122 fseekres, errno, gnome_vfs_result_to_string (result));
127 { /* read - leaks like a sieve on error =] */
128 guint8 *data, *data_ref;
130 GnomeVFSFileSize bytes_read;
132 data = g_new (guint8, length);
133 data_ref = g_new (guint8, length);
135 result = gnome_vfs_read (handle, data, length, &bytes_read);
136 bytes_read_ref = fread (data_ref, 1, length, ref);
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));
144 if (result != GNOME_VFS_OK) {
145 g_warning ("VFS read failed with '%s'",
146 gnome_vfs_result_to_string (result));
150 { /* Compare the data */
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]);
165 GnomeVFSFileSize offset;
167 result = gnome_vfs_tell (handle, &offset);
168 if (show_if_error (result, "tail gnome_vfs_tell"))
171 ref_off = ftell (ref);
173 g_warning ("Wierd ftell failure");
177 if (ref_off != offset) {
178 g_warning ("Offset mismatch %d should be %d", (int)offset, (int)ref_off);
187 main (int argc, char **argv)
189 GnomeVFSResult result;
190 GnomeVFSHandle *handle;
194 if (! gnome_vfs_init ()) {
195 fprintf (stderr, "Cannot initialize gnome-vfs.\n");
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",
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]);
209 if (!(ref = fopen (argv[2], "r"))) {
210 fprintf (stderr, "Failed to open '%s' to compare seek history\n", argv[2]);
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));
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));
228 printf ("%d tests failed\n", failures);
230 printf ("All test successful\n");
232 result = gnome_vfs_close (handle);
233 show_result (result, "gnome_vfs_close", argv[1]);