OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [test0/] [src/] [fileio.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
 
2
#include <stdio.h>
3
#include <unistd.h>
4
#include <sys/types.h>
5
#include <sys/stat.h>
6
#include <fcntl.h>
7
#include <string.h>
8
#include <tests.h>
9
#include <errno.h>
10
#include INC_GLUE(memory.h)
11
 
12
int small_io_test(void)
13
{
14
        int fd1, fd2;
15
        char *string = "abcdefg";
16
        char strbuf[30];
17
        char strbuf2[30];
18
        char *path = "/text.txt";
19
        char stackbuf[PAGE_SIZE*2];
20
 
21
        fd1 = open(path, O_TRUNC | O_RDWR | O_CREAT, S_IRWXU);
22
        fd2 = open(path, O_RDWR, 0);
23
 
24
        test_printf("%s: fd1: %d, fd2: %d\n", __FUNCTION__, fd1, fd2);
25
        perror("OPEN");
26
 
27
        for (int i = 0; i < 4; i++) {
28
                sprintf(strbuf, "%s%d", string, i);
29
                test_printf("Writing to %s offset %x, string: %s\n",
30
                            path, i*PAGE_SIZE, strbuf);
31
                lseek(fd1, i*PAGE_SIZE, SEEK_SET);
32
                write(fd1, strbuf, strlen(strbuf) + 1);
33
        }
34
        close(fd1);
35
 
36
        memset(strbuf, 0, 30);
37
        for (int i = 0; i < 4; i++) {
38
                sprintf(strbuf2, "%s%d", string, i);
39
                lseek(fd2, i*PAGE_SIZE, SEEK_SET);
40
                read(fd2, strbuf, 30);
41
                test_printf("Read %s, offset %x as %s\n",
42
                            path, i*PAGE_SIZE, strbuf);
43
                if (strcmp(strbuf, strbuf2))
44
                        goto out_err;
45
        }
46
 
47
        /* Now read into an unaligned buffer larger than page size */
48
        lseek(fd2, 0, SEEK_SET);
49
 
50
        read(fd2, stackbuf, PAGE_SIZE * 2);
51
 
52
        sprintf(strbuf2, "%s%d", string, 0);
53
        if (strcmp(stackbuf, strbuf2))
54
                goto out_err;
55
        sprintf(strbuf2, "%s%d", string, 1);
56
        if (strcmp(&stackbuf[PAGE_SIZE], strbuf2))
57
                goto out_err;
58
 
59
        close(fd2);
60
 
61
        printf("MINI IO TEST        -- PASSED --\n");
62
        return 0;
63
 
64
out_err:
65
        printf("MINI IO TEST        -- FAILED --\n");
66
        return 0;
67
}
68
 
69
int fileio(void)
70
{
71
        int fd;
72
        ssize_t cnt;
73
        int err;
74
        char buf[128];
75
        off_t offset;
76
        int tid = getpid();
77
        char *str = "I WROTE TO THIS FILE\n";
78
        char filename[128];
79
 
80
        memset(buf, 0, 128);
81
        memset(filename, 0, 128);
82
        sprintf(filename, "/home/bahadir/newfile%d.txt", tid);
83
        if ((fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0) {
84
                test_printf("OPEN: %d", -errno);
85
                test_printf("While trying to open %s\n", filename);
86
                goto out_err;
87
        }
88
        test_printf("%d: Created %s\n", tid, filename);
89
 
90
        test_printf("%d: write.\n", tid);
91
        if ((int)(cnt = write(fd, str, strlen(str))) < 0) {
92
                test_printf("WRITE: %d", errno);
93
                goto out_err;
94
        }
95
 
96
        test_printf("%d: lseek.\n", tid);
97
        if ((int)(offset = lseek(fd, 0, SEEK_SET)) < 0) {
98
                test_printf("LSEEK: %d", errno);
99
                goto out_err;
100
        }
101
        test_printf("%d: read.\n", tid);
102
        if ((int)(cnt = read(fd, buf, strlen(str))) < 0) {
103
                test_printf("READ: %d", errno);
104
                goto out_err;
105
        } else if (cnt != strlen(str)) {
106
                test_printf("%d: Read: %d bytes from file.\n", tid, cnt);
107
                goto out_err;
108
        }
109
 
110
        test_printf("%d: Read: %d bytes from file.\n", tid, cnt);
111
        if (cnt) {
112
                test_printf("%d: Read string: %s\n", tid, buf);
113
                if (strcmp(buf, str)) {
114
                        test_printf("Strings not the same:\n");
115
                        test_printf("Str: %s\n", str);
116
                        test_printf("Buf: %s\n", buf);
117
                        goto out_err;
118
                }
119
        }
120
 
121
        if ((err = close(fd)) < 0) {
122
                test_printf("CLOSE: %d", errno);
123
                goto out_err;
124
        }
125
 
126
        if (getpid() == parent_of_all)
127
                printf("FILE IO TEST        -- PASSED --\n");
128
        return 0;
129
 
130
out_err:
131
        printf("FILE IO TEST        -- FAILED --\n");
132
        return 0;
133
}
134
 
135
#if defined(HOST_TESTS)
136
int main(void)
137
{
138
        test_printf("File IO test 1:\n");
139
        if (fileio() == 0)
140
                test_printf("-- PASSED --\n");
141
        else
142
                test_printf("-- FAILED --\n");
143
 
144
        test_printf("File IO test 2:\n");
145
        if (fileio2() == 0)
146
                test_printf("-- PASSED --\n");
147
        else
148
                test_printf("-- FAILED --\n");
149
 
150
 
151
        return 0;
152
}
153
#endif
154
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.