Changeset b16b0aa9 in rtems


Ignore:
Timestamp:
May 11, 2012, 11:19:45 AM (7 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
4.11, master
Children:
22506ae9, 9ee5b97
Parents:
a26ea56d
git-author:
Sebastian Huber <sebastian.huber@…> (05/11/12 11:19:45)
git-committer:
Sebastian Huber <sebastian.huber@…> (05/11/12 11:58:44)
Message:

fstests/fsrdwr: Add block read/write test case

File:
1 edited

Legend:

Unmodified
Added
Removed
  • testsuites/fstests/fsrdwr/init.c

    ra26ea56d rb16b0aa9  
    550550}
    551551
     552static void
     553random_fill (char *dst, size_t n)
     554{
     555  static uint32_t u = 0x12345678;
     556  uint32_t v = u;
     557  uint32_t w;
     558  size_t i = 0;
     559  int j = 0;
     560
     561  while (i < n) {
     562    if (j == 0) {
     563      v *= 1664525;
     564      v += 1013904223;
     565      w = v;
     566    } else {
     567      w >>= 8;
     568    }
     569
     570    dst [i] = (char) w;
     571
     572    ++i;
     573    j = (j + 1) % 4;
     574  }
     575
     576  u = v;
     577}
     578
     579static void
     580block_rw_lseek (int fd, size_t pos)
     581{
     582  off_t actual;
     583
     584  actual = lseek (fd, pos, SEEK_SET);
     585  rtems_test_assert (actual == pos);
     586}
     587
     588static void
     589block_rw_write (int fd, char *out, size_t pos, size_t size)
     590{
     591  ssize_t n;
     592
     593  random_fill (out + pos, size);
     594
     595  block_rw_lseek (fd, pos);
     596
     597  n = write (fd, out + pos, size);
     598  rtems_test_assert (n == (ssize_t) size);
     599}
     600
     601static void
     602block_rw_write_cont (int fd, char *out, size_t *pos, size_t size)
     603{
     604  ssize_t n;
     605
     606  random_fill (out + *pos, size);
     607
     608  n = write (fd, out + *pos, size);
     609  rtems_test_assert (n == (ssize_t) size);
     610
     611  *pos += size;
     612}
     613
     614static void
     615block_rw_check (int fd, const char *out, char *in, size_t size)
     616{
     617  ssize_t n;
     618  off_t file_size;
     619
     620  file_size = lseek (fd, 0, SEEK_END);
     621  rtems_test_assert (file_size == size);
     622
     623  block_rw_lseek (fd, 0);
     624
     625  n = read (fd, in, size);
     626  rtems_test_assert (n == (ssize_t) size);
     627
     628  rtems_test_assert (memcmp (out, in, size) == 0);
     629}
     630
     631static void
     632block_rw_prepare (const char *t, int fd, char *out, size_t size)
     633{
     634  int status;
     635
     636  printf ("test case: %s\n", t);
     637
     638  memset (out, 0, size);
     639
     640  status = ftruncate (fd, 0);
     641  rtems_test_assert (status == 0);
     642
     643  block_rw_lseek (fd, 0);
     644}
     645
     646static void
     647block_rw_case_0 (int fd, size_t block_size, char *out, char *in)
     648{
     649  const size_t size = 3 * block_size + 1;
     650
     651  block_rw_prepare (__func__, fd, out, size);
     652  block_rw_write (fd, out, 0, size);
     653  block_rw_check (fd, out, in, size);
     654}
     655
     656static void
     657block_rw_case_1 (int fd, size_t block_size, char *out, char *in)
     658{
     659  const size_t size = 2 * block_size;
     660
     661  block_rw_prepare (__func__, fd, out, size);
     662  block_rw_write (fd, out, block_size, block_size);
     663  block_rw_check (fd, out, in, size);
     664}
     665
     666static void
     667block_rw_case_2 (int fd, size_t block_size, char *out, char *in)
     668{
     669  const size_t size = (5 * block_size) / 2;
     670
     671  block_rw_prepare (__func__, fd, out, size);
     672  block_rw_write (fd, out, (3 * block_size) / 2, block_size);
     673  block_rw_check (fd, out, in, size);
     674}
     675
     676static void
     677block_rw_case_3 (int fd, size_t block_size, char *out, char *in)
     678{
     679  const size_t size = 2 * block_size;
     680
     681  block_rw_prepare (__func__, fd, out, size);
     682  block_rw_write (fd, out, block_size, block_size / 3);
     683  block_rw_write (fd, out, 2 * block_size - block_size / 3, block_size / 3);
     684  block_rw_check (fd, out, in, size);
     685}
     686
     687static void
     688block_rw_case_4 (int fd, size_t block_size, char *out, char *in)
     689{
     690  const size_t size = 3 * block_size + 1;
     691  size_t pos = 0;
     692
     693  block_rw_prepare (__func__, fd, out, size);
     694  block_rw_write_cont (fd, out, &pos, block_size);
     695  block_rw_write_cont (fd, out, &pos, block_size / 2);
     696  block_rw_write_cont (fd, out, &pos, block_size);
     697  block_rw_write_cont (fd, out, &pos, block_size / 2);
     698  block_rw_write_cont (fd, out, &pos, 1);
     699  block_rw_check (fd, out, in, size);
     700}
     701
     702static void
     703block_read_and_write (void)
     704{
     705  int fd;
     706  struct stat st;
     707  int status;
     708  size_t block_size;
     709  size_t size;
     710  char *out;
     711  char *in;
     712
     713  test_case_enter (__func__);
     714
     715  fd = open ("file", O_RDWR | O_CREAT | O_TRUNC, mode);
     716  rtems_test_assert (fd >= 0);
     717
     718  status = fstat (fd, &st);
     719  rtems_test_assert (status == 0);
     720
     721  block_size = st.st_blksize;
     722  size = 3 * block_size + 1;
     723
     724  out = malloc (size);
     725  rtems_test_assert (out != NULL);
     726
     727  in = malloc (size);
     728  rtems_test_assert (in != NULL);
     729
     730  block_rw_case_0 (fd, block_size, out, in);
     731  block_rw_case_1 (fd, block_size, out, in);
     732  block_rw_case_2 (fd, block_size, out, in);
     733  block_rw_case_3 (fd, block_size, out, in);
     734  block_rw_case_4 (fd, block_size, out, in);
     735
     736  status = close (fd);
     737  rtems_test_assert (status == 0);
     738
     739  free (out);
     740  free (in);
     741
     742  test_case_leave ();
     743}
     744
    552745void
    553746test (void)
     
    557750  truncate_test03 ();
    558751  truncate_to_zero ();
    559 }
     752  block_read_and_write ();
     753}
Note: See TracChangeset for help on using the changeset viewer.