L4Re/departure

Annotated tests/dstest_block_client_simple.cc

455:8929c3922e96
2022-10-29 Paul Boddie Introduced format specifier macros to avoid output formatting difficulties.
paul@90 1
/*
paul@90 2
 * Test dataspace operations.
paul@90 3
 *
paul@237 4
 * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk>
paul@90 5
 *
paul@90 6
 * This program is free software; you can redistribute it and/or
paul@90 7
 * modify it under the terms of the GNU General Public License as
paul@90 8
 * published by the Free Software Foundation; either version 2 of
paul@90 9
 * the License, or (at your option) any later version.
paul@90 10
 *
paul@90 11
 * This program is distributed in the hope that it will be useful,
paul@90 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@90 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@90 14
 * GNU General Public License for more details.
paul@90 15
 *
paul@90 16
 * You should have received a copy of the GNU General Public License
paul@90 17
 * along with this program; if not, write to the Free Software
paul@90 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@90 19
 * Boston, MA  02110-1301, USA
paul@90 20
 */
paul@90 21
paul@90 22
#include <systypes/fcntl.h>
paul@455 23
#include <systypes/format.h>
paul@455 24
paul@455 25
#define FMToffsetd "%" pFMToffset "d"
paul@90 26
paul@90 27
#include <stdio.h>
paul@106 28
#include <stdlib.h>
paul@151 29
#include <string.h>
paul@90 30
paul@94 31
#include <fsclient/client.h>
paul@90 32
paul@90 33
paul@90 34
paul@90 35
int main(int argc, char *argv[])
paul@90 36
{
paul@90 37
  if (argc < 2)
paul@90 38
  {
paul@237 39
    printf("Need a filename and optional repetition.\n");
paul@90 40
    return 1;
paul@90 41
  }
paul@90 42
paul@90 43
  /* Obtain filename and access parameters. */
paul@90 44
paul@90 45
  char *filename = argv[1];
paul@237 46
  int repetition = argc > 2 ? atoi(argv[2]) : 10;
paul@144 47
  file_t *file;
paul@144 48
paul@237 49
  file = client_open(filename, O_RDWR);
paul@90 50
paul@402 51
  if (!client_opened(file))
paul@90 52
  {
paul@402 53
    client_close(file);
paul@90 54
    printf("Could not obtain file.\n");
paul@90 55
    return 1;
paul@90 56
  }
paul@90 57
paul@90 58
  /* Copy the sampled data to another file region. */
paul@90 59
paul@90 60
  offset_t size = file->size;
paul@402 61
  offset_t new_size = size * (repetition + 1);
paul@402 62
paul@90 63
  char buffer[size];
paul@90 64
  offset_t nread = client_read(file, buffer, size);
paul@90 65
paul@90 66
  if (nread != size)
paul@90 67
  {
paul@455 68
    printf("Could not read entire file: " FMToffsetd " out of " FMToffsetd " bytes.\n", nread, size);
paul@90 69
    return 1;
paul@90 70
  }
paul@90 71
paul@90 72
  printf("File contents...\n");
paul@90 73
paul@90 74
  fwrite(buffer, sizeof(char), nread, stdout);
paul@90 75
paul@455 76
  printf("Copy " FMToffsetd " bytes to end...\n", nread);
paul@90 77
paul@106 78
  for (int times = 0; times < repetition; times++)
paul@90 79
  {
paul@90 80
    printf("Copy #%d...\n", times);
paul@90 81
paul@90 82
    offset_t nwritten = client_write(file, buffer, nread);
paul@90 83
paul@90 84
    if (nwritten != nread)
paul@90 85
    {
paul@455 86
      printf("Could not write file section: " FMToffsetd " instead of " FMToffsetd " bytes.\n",
paul@90 87
             nwritten, nread);
paul@90 88
      return 1;
paul@90 89
    }
paul@90 90
  }
paul@90 91
paul@455 92
  printf("File now has size " FMToffsetd ".\n", file->size);
paul@455 93
  printf("File has expected size " FMToffsetd ": %s\n", new_size, new_size == file->size ? "True" : "False");
paul@90 94
paul@90 95
  printf("Seek to start...\n");
paul@90 96
paul@90 97
  client_seek(file, 0, SEEK_SET);
paul@90 98
paul@455 99
  printf("At mapped region from " FMToffsetd " to " FMToffsetd " with data at " FMToffsetd " to " FMToffsetd ".\n",
paul@90 100
         file->start_pos, file->end_pos, file->data_current, file->data_end);
paul@90 101
paul@90 102
  printf("File contents...\n");
paul@90 103
paul@90 104
  offset_t position = 0;
paul@90 105
paul@402 106
  while ((nread = client_read(file, buffer, size)) > 0)
paul@90 107
  {
paul@90 108
    if ((nread != size) && (nread != file->size - position))
paul@90 109
    {
paul@455 110
      printf("Could not read file section: " FMToffsetd " instead of " FMToffsetd " or remaining " FMToffsetd " bytes.\n",
paul@90 111
             nread, size, file->size - position);
paul@90 112
      return 1;
paul@90 113
    }
paul@90 114
paul@90 115
    fwrite(buffer, sizeof(char), nread, stdout);
paul@90 116
    position += nread;
paul@90 117
  }
paul@90 118
paul@407 119
  printf("\nFile shown in its entirety: %s\n", position == file->size ? "True" : "False");
paul@402 120
  printf("End of test.\n");
paul@90 121
  return 0;
paul@90 122
}
paul@90 123
paul@90 124
// vim: tabstop=2 expandtab shiftwidth=2