1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
/*
** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "sfconfig.h"
#include <stdio.h>
#include <stdlib.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#include <errno.h>
#include "common.h"
static void make_data (int *data, int len, int seed) ;
static void file_open_test (const char *filename) ;
static void file_read_write_test (const char *filename) ;
static void file_truncate_test (const char *filename) ;
static void test_open_or_die (SF_PRIVATE *psf, int linenum) ;
static void test_close_or_die (SF_PRIVATE *psf, int linenum) ;
static void test_write_or_die (SF_PRIVATE *psf, void *data, sf_count_t bytes, sf_count_t items, sf_count_t new_position, int linenum) ;
static void test_read_or_die (SF_PRIVATE *psf, void *data, sf_count_t bytes, sf_count_t items, sf_count_t new_position, int linenum) ;
static void test_equal_or_die (int *array1, int *array2, int len, int linenum) ;
static void test_seek_or_die (SF_PRIVATE *psf, sf_count_t offset, int whence, sf_count_t new_position, int linenum) ;
int
main (void)
{ const char *filename = "file_io.dat" ;
file_open_test (filename) ;
file_read_write_test (filename) ;
file_truncate_test (filename) ;
unlink (filename) ;
return 0 ;
} /* main */
/*==============================================================================
** Actual test functions.
*/
static void
file_open_test (const char *filename)
{ SF_PRIVATE sf_data, *psf ;
int error ;
printf (" %-24s : ", "file_open_test") ;
fflush (stdout) ;
memset (&sf_data, 0, sizeof (sf_data)) ;
psf = &sf_data ;
/* Ensure that the file doesn't already exist. */
if (unlink (filename) != 0 && errno != ENOENT)
{ printf ("\n\nLine %d: unlink failed (%d) : %s\n\n", __LINE__, errno, strerror (errno)) ;
exit (1) ;
} ;
strncpy (psf->filename, filename, sizeof (psf->filename)) ;
/* Test that open for read fails if the file doesn't exist. */
error = psf_fopen (psf, psf->filename, SFM_READ) ;
if (error == 0)
{ printf ("\n\nLine %d: psf_fopen() should have failed.\n\n", __LINE__) ;
exit (1) ;
} ;
/* Reset error to zero. */
psf->error = SFE_NO_ERROR ;
/* Test file open in write mode. */
psf->mode = SFM_WRITE ;
test_open_or_die (psf, __LINE__) ;
test_close_or_die (psf, __LINE__) ;
unlink (psf->filename) ;
/* Test file open in read/write mode for a non-existant file. */
psf->mode = SFM_RDWR ;
test_open_or_die (psf, __LINE__) ;
test_close_or_die (psf, __LINE__) ;
/* Test file open in read/write mode for an existing file. */
psf->mode = SFM_RDWR ;
test_open_or_die (psf, __LINE__) ;
test_close_or_die (psf, __LINE__) ;
unlink (psf->filename) ;
puts ("ok") ;
} /* file_open_test */
static void
file_read_write_test (const char *filename)
{ static int data_out [512] ;
static int data_in [512] ;
SF_PRIVATE sf_data, *psf ;
sf_count_t retval ;
/*
** Open a new file and write two blocks of data to the file. After each
** write, test that psf_get_filelen() returns the new length.
*/
printf (" %-24s : ", "file_write_test") ;
fflush (stdout) ;
memset (&sf_data, 0, sizeof (sf_data)) ;
psf = &sf_data ;
strncpy (psf->filename, filename, sizeof (psf->filename)) ;
/* Test file open in write mode. */
psf->mode = SFM_WRITE ;
test_open_or_die (psf, __LINE__) ;
make_data (data_out, ARRAY_LEN (data_out), 1) ;
test_write_or_die (psf, data_out, sizeof (data_out [0]), ARRAY_LEN (data_out), sizeof (data_out), __LINE__) ;
if ((retval = psf_get_filelen (psf)) != sizeof (data_out))
{ printf ("\n\nLine %d: file length after write is not correct (%ld should be %d).\n\n", __LINE__, (long) retval, (int) sizeof (data_out)) ;
if (retval == 0)
printf ("An fsync() may be necessary before fstat() in psf_get_filelen().\n\n") ;
exit (1) ;
} ;
make_data (data_out, ARRAY_LEN (data_out), 2) ;
test_write_or_die (psf, data_out, ARRAY_LEN (data_out), sizeof (data_out [0]), 2 * sizeof (data_out), __LINE__) ;
if ((retval = psf_get_filelen (psf)) != 2 * sizeof (data_out))
{ printf ("\n\nLine %d: file length after write is not correct. (%ld should be %d)\n\n", __LINE__, (long) retval, 2 * ((int) sizeof (data_out))) ;
exit (1) ;
} ;
test_close_or_die (psf, __LINE__) ;
puts ("ok") ;
/*
** Now open the file in read mode, check the file length and check
** that the data is correct.
*/
printf (" %-24s : ", "file_read_test") ;
fflush (stdout) ;
/* Test file open in write mode. */
psf->mode = SFM_READ ;
test_open_or_die (psf, __LINE__) ;
make_data (data_out, ARRAY_LEN (data_out), 1) ;
test_read_or_die (psf, data_in, 1, sizeof (data_in), sizeof (data_in), __LINE__) ;
test_equal_or_die (data_out, data_in, ARRAY_LEN (data_out), __LINE__) ;
make_data (data_out, ARRAY_LEN (data_out), 2) ;
test_read_or_die (psf, data_in, sizeof (data_in [0]), ARRAY_LEN (data_in), 2 * sizeof (data_in), __LINE__) ;
test_equal_or_die (data_out, data_in, ARRAY_LEN (data_out), __LINE__) ;
test_close_or_die (psf, __LINE__) ;
puts ("ok") ;
/*
** Open the file in read/write mode, seek around a bit and then seek to
** the end of the file and write another block of data (3rd block). Then
** go back and check that all three blocks are correct.
*/
printf (" %-24s : ", "file_seek_test") ;
fflush (stdout) ;
/* Test file open in read/write mode. */
psf->mode = SFM_RDWR ;
test_open_or_die (psf, __LINE__) ;
test_seek_or_die (psf, 0, SEEK_SET, 0, __LINE__) ;
test_seek_or_die (psf, 0, SEEK_END, 2 * SIGNED_SIZEOF (data_out), __LINE__) ;
test_seek_or_die (psf, -1 * SIGNED_SIZEOF (data_out), SEEK_CUR, (sf_count_t) sizeof (data_out), __LINE__) ;
test_seek_or_die (psf, SIGNED_SIZEOF (data_out), SEEK_CUR, 2 * SIGNED_SIZEOF (data_out), __LINE__) ;
make_data (data_out, ARRAY_LEN (data_out), 3) ;
test_write_or_die (psf, data_out, sizeof (data_out [0]), ARRAY_LEN (data_out), 3 * sizeof (data_out), __LINE__) ;
test_seek_or_die (psf, 0, SEEK_SET, 0, __LINE__) ;
make_data (data_out, ARRAY_LEN (data_out), 1) ;
test_read_or_die (psf, data_in, 1, sizeof (data_in), sizeof (data_in), __LINE__) ;
test_equal_or_die (data_out, data_in, ARRAY_LEN (data_out), __LINE__) ;
test_seek_or_die (psf, 2 * SIGNED_SIZEOF (data_out), SEEK_SET, 2 * SIGNED_SIZEOF (data_out), __LINE__) ;
make_data (data_out, ARRAY_LEN (data_out), 3) ;
test_read_or_die (psf, data_in, 1, sizeof (data_in), 3 * sizeof (data_in), __LINE__) ;
test_equal_or_die (data_out, data_in, ARRAY_LEN (data_out), __LINE__) ;
test_seek_or_die (psf, SIGNED_SIZEOF (data_out), SEEK_SET, SIGNED_SIZEOF (data_out), __LINE__) ;
make_data (data_out, ARRAY_LEN (data_out), 2) ;
test_read_or_die (psf, data_in, 1, sizeof (data_in), 2 * sizeof (data_in), __LINE__) ;
test_equal_or_die (data_out, data_in, ARRAY_LEN (data_out), __LINE__) ;
test_close_or_die (psf, __LINE__) ;
puts ("ok") ;
/*
** Now test operations with a non-zero psf->fileoffset field. This field
** sets an artificial file start positions so that a seek to the start of
** the file will actually be a seek to the value given by psf->fileoffset.
*/
printf (" %-24s : ", "file_offset_test") ;
fflush (stdout) ;
/* Test file open in read/write mode. */
psf->mode = SFM_RDWR ;
psf->fileoffset = sizeof (data_out [0]) * ARRAY_LEN (data_out) ;
test_open_or_die (psf, __LINE__) ;
if ((retval = psf_get_filelen (psf)) != 3 * sizeof (data_out))
{ printf ("\n\nLine %d: file length after write is not correct. (%ld should be %d)\n\n", __LINE__, (long) retval, 3 * ((int) sizeof (data_out))) ;
exit (1) ;
} ;
test_seek_or_die (psf, SIGNED_SIZEOF (data_out), SEEK_SET, SIGNED_SIZEOF (data_out), __LINE__) ;
make_data (data_out, ARRAY_LEN (data_out), 5) ;
test_write_or_die (psf, data_out, sizeof (data_out [0]), ARRAY_LEN (data_out), 2 * sizeof (data_out), __LINE__) ;
test_close_or_die (psf, __LINE__) ;
/* final test with psf->fileoffset == 0. */
psf->mode = SFM_RDWR ;
psf->fileoffset = 0 ;
test_open_or_die (psf, __LINE__) ;
if ((retval = psf_get_filelen (psf)) != 3 * sizeof (data_out))
{ printf ("\n\nLine %d: file length after write is not correct. (%ld should be %d)\n\n", __LINE__, (long) retval, 3 * ((int) sizeof (data_out))) ;
exit (1) ;
} ;
make_data (data_out, ARRAY_LEN (data_out), 1) ;
test_read_or_die (psf, data_in, 1, sizeof (data_in), sizeof (data_in), __LINE__) ;
test_equal_or_die (data_out, data_in, ARRAY_LEN (data_out), __LINE__) ;
make_data (data_out, ARRAY_LEN (data_out), 2) ;
test_read_or_die (psf, data_in, 1, sizeof (data_in), 2 * sizeof (data_in), __LINE__) ;
test_equal_or_die (data_out, data_in, ARRAY_LEN (data_out), __LINE__) ;
make_data (data_out, ARRAY_LEN (data_out), 5) ;
test_read_or_die (psf, data_in, 1, sizeof (data_in), 3 * sizeof (data_in), __LINE__) ;
test_equal_or_die (data_out, data_in, ARRAY_LEN (data_out), __LINE__) ;
test_close_or_die (psf, __LINE__) ;
puts ("ok") ;
} /* file_read_write_test */
static void
file_truncate_test (const char *filename)
{ SF_PRIVATE sf_data, *psf ;
unsigned char buffer [256] ;
int k ;
/*
** Open a new file and write two blocks of data to the file. After each
** write, test that psf_get_filelen() returns the new length.
*/
printf (" %-24s : ", "file_truncate_test") ;
fflush (stdout) ;
memset (&sf_data, 0, sizeof (sf_data)) ;
memset (buffer, 0xEE, sizeof (buffer)) ;
psf = &sf_data ;
strncpy (psf->filename, filename, sizeof (psf->filename)) ;
/*
** Open the file write mode, write 0xEE data and then extend the file
** using truncate (the extended data should be 0x00).
*/
psf->mode = SFM_WRITE ;
test_open_or_die (psf, __LINE__) ;
test_write_or_die (psf, buffer, sizeof (buffer) / 2, 1, sizeof (buffer) / 2, __LINE__) ;
psf_ftruncate (psf, sizeof (buffer)) ;
test_close_or_die (psf, __LINE__) ;
/* Open the file in read mode and check the data. */
psf->mode = SFM_READ ;
test_open_or_die (psf, __LINE__) ;
test_read_or_die (psf, buffer, sizeof (buffer), 1, sizeof (buffer), __LINE__) ;
test_close_or_die (psf, __LINE__) ;
for (k = 0 ; k < SIGNED_SIZEOF (buffer) / 2 ; k++)
if (buffer [k] != 0xEE)
{ printf ("\n\nLine %d : buffer [%d] = %d (should be 0xEE)\n\n", __LINE__, k, buffer [k]) ;
exit (1) ;
} ;
for (k = SIGNED_SIZEOF (buffer) / 2 ; k < SIGNED_SIZEOF (buffer) ; k++)
if (buffer [k] != 0)
{ printf ("\n\nLine %d : buffer [%d] = %d (should be 0)\n\n", __LINE__, k, buffer [k]) ;
exit (1) ;
} ;
/* Open the file in read/write and shorten the file using truncate. */
psf->mode = SFM_RDWR ;
test_open_or_die (psf, __LINE__) ;
psf_ftruncate (psf, sizeof (buffer) / 4) ;
test_close_or_die (psf, __LINE__) ;
/* Check the file length. */
psf->mode = SFM_READ ;
test_open_or_die (psf, __LINE__) ;
test_seek_or_die (psf, 0, SEEK_END, SIGNED_SIZEOF (buffer) / 4, __LINE__) ;
test_close_or_die (psf, __LINE__) ;
puts ("ok") ;
} /* file_truncate_test */
/*==============================================================================
** Testing helper functions.
*/
static void
test_open_or_die (SF_PRIVATE *psf, int linenum)
{ int error ;
/* Test that open for read fails if the file doesn't exist. */
error = psf_fopen (psf, psf->filename, psf->mode) ;
if (error)
{ printf ("\n\nLine %d: psf_fopen() failed : %s\n\n", linenum, strerror (errno)) ;
exit (1) ;
} ;
} /* test_open_or_die */
static void
test_close_or_die (SF_PRIVATE *psf, int linenum)
{
psf_fclose (psf) ;
if (psf_file_valid (psf))
{ printf ("\n\nLine %d: psf->filedes should not be valid.\n\n", linenum) ;
exit (1) ;
} ;
} /* test_close_or_die */
static void
test_write_or_die (SF_PRIVATE *psf, void *data, sf_count_t bytes, sf_count_t items, sf_count_t new_position, int linenum)
{ sf_count_t retval ;
retval = psf_fwrite (data, bytes, items, psf) ;
if (retval != items)
{ printf ("\n\nLine %d: psf_write() returned %ld (should be %ld)\n\n", linenum, (long) retval, (long) items) ;
exit (1) ;
} ;
if ((retval = psf_ftell (psf)) != new_position)
{ printf ("\n\nLine %d: file length after write is not correct. (%ld should be %ld)\n\n", linenum, (long) retval, (long) new_position) ;
exit (1) ;
} ;
return ;
} /* test_write_or_die */
static void
test_read_or_die (SF_PRIVATE *psf, void *data, sf_count_t bytes, sf_count_t items, sf_count_t new_position, int linenum)
{ sf_count_t retval ;
retval = psf_fread (data, bytes, items, psf) ;
if (retval != items)
{ printf ("\n\nLine %d: psf_write() returned %ld (should be %ld)\n\n", linenum, (long) retval, (long) items) ;
exit (1) ;
} ;
if ((retval = psf_ftell (psf)) != new_position)
{ printf ("\n\nLine %d: file length after write is not correct. (%ld should be %ld)\n\n", linenum, (long) retval, (long) new_position) ;
exit (1) ;
} ;
return ;
} /* test_write_or_die */
static void
test_seek_or_die (SF_PRIVATE *psf, sf_count_t offset, int whence, sf_count_t new_position, int linenum)
{ sf_count_t retval ;
retval = psf_fseek (psf, offset, whence) ;
if (retval != new_position)
{ printf ("\n\nLine %d: psf_fseek() failed. New position is %ld (should be %ld).\n\n",
linenum, (long) retval, (long) new_position) ;
exit (1) ;
} ;
} /* test_seek_or_die */
static void
test_equal_or_die (int *array1, int *array2, int len, int linenum)
{ int k ;
for (k = 0 ; k < len ; k++)
if (array1 [k] != array2 [k])
printf ("\n\nLine %d: error at index %d (%d != %d).\n\n",
linenum, k, array1 [k], array2 [k]) ;
return ;
} /* test_equal_or_die */
static void
make_data (int *data, int len, int seed)
{ int k ;
srand (seed * 3333333 + 14756123) ;
for (k = 0 ; k < len ; k++)
data [k] = rand () ;
} /* make_data */
/*
** Do not edit or modify anything in this comment block.
** The arch-tag line is a file identity tag for the GNU Arch
** revision control system.
**
** arch-tag: 0a21fb93-78dd-4f72-8338-4bca9e77b8c8
*/
|