1/** \file
2 *  \brief Simple test to check functionality of fread
3 */
4
5/*
6 * Copyright (c) 2010, 2011, ETH Zurich.
7 * All rights reserved.
8 *
9 * This file is distributed under the terms in the attached LICENSE file.
10 * If you do not find this file, copies can be found by writing to:
11 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
12 */
13
14#include <stdio.h>
15#include <barrelfish/barrelfish.h>
16#include <vfs/vfs.h>
17
18/* ------------------------------ MAIN ------------------------------ */
19
20#define AMOUNT 100000
21
22int main(int argc, char *argv[])
23{
24    errval_t err;
25
26    vfs_init();
27
28    err = vfs_mkdir("/filetests");
29    if (err_is_fail(err)) {
30        USER_PANIC_ERR(err, "vfs_mkdir failed");
31    }
32
33    /* Create a file with a lot of data */
34    FILE *fh = fopen("/filetests/fread_test.dat", "w");
35    if (!fh) {
36        USER_PANIC("fopen failed");
37    }
38
39    for (int i = 0; i < AMOUNT; i++) {
40        fprintf(fh, "h");
41    }
42    fclose(fh);
43
44    /* Read out the data in chunks */
45    fh = fopen("/filetests/fread_test.dat", "r");
46    if (!fh) {
47        USER_PANIC("fopen failed");
48    }
49    char *ptr = malloc(AMOUNT);
50    assert(ptr);
51
52    size_t size = fread(ptr, 10, 1, fh);
53    if (size != 10) {
54        USER_PANIC("fread did not read full amount");
55    }
56
57    size = fread(ptr, AMOUNT - 10, 1, fh);
58    if (size != AMOUNT - 10) {
59        USER_PANIC("fread did not read full amount");
60    }
61
62    size = fread(ptr, AMOUNT, 1, fh);
63    if (size != 0) {
64        USER_PANIC("fread did not read full amount");
65    }
66
67    printf("client done\n");
68    return 0;
69}
70