1 /* $Id: qio-t.c 6939 2004-06-10 22:04:58Z hkehoe $ */
2 /* Test suite for the Quick I/O library */
10 #include "inn/messages.h"
16 output(int fd, const void *data, size_t size)
18 if (xwrite(fd, data, size) < 0)
19 sysdie("Can't write to .testout");
25 unsigned char data[256], line[256], out[256];
37 for (c = 1, i = 0; i < 255; i++, c++)
41 memcpy(line, data, 255);
43 memcpy(out, data, 255);
45 fd = open(".testout", O_RDWR | O_CREAT | O_TRUNC, 0644);
46 if (fd < 0) sysdie("Can't create .testout");
49 /* Mostly duplicate the code from qio.c so that we can test with lines
50 exactly as large as the buffer. */
51 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
57 size += st.st_blksize;
59 #endif /* HAVE_ST_BLKSIZE */
61 /* Start with small, equally sized lines exactly equal to the buffer.
62 Then a line equal in size to the buffer, then a short line and
63 another line equal in size to the buffer, then a half line and lines
64 repeated to fill another buffer, then a line that's one character too
67 for (i = 0; i < count; i++)
68 output(fd, line, 256);
69 for (i = 0; i < count - 1; i++)
70 output(fd, data, 256);
71 output(fd, line, 256);
73 for (i = 0; i < count - 1; i++)
74 output(fd, data, 256);
75 output(fd, line, 256);
76 output(fd, data, 127);
78 for (i = 0; i < count; i++)
79 output(fd, line, 256);
80 for (i = 0; i < count; i++)
81 output(fd, data, 256);
87 /* Now make sure we can read all that back correctly. */
88 qio = QIOopen(".testout");
90 ok(2, !QIOerror(qio));
91 ok(3, QIOfileno(qio) > 0);
92 if (unlink(".testout") < 0)
93 sysdie("Can't unlink .testout");
94 for (success = true, i = 0; i < count; i++) {
95 result = QIOread(qio);
96 success = (success && !QIOerror(qio) && (QIOlength(qio) == 255)
97 && !strcmp(result, (char *) out));
100 ok(5, QIOtell(qio) == (off_t) size);
101 result = QIOread(qio);
102 if (strlen(result) < size - 1) {
105 for (success = true, i = 0; i < count - 1; i++)
106 success = success && !memcmp(result + i * 256, data, 256);
107 success = success && !memcmp(result + i * 256, data, 255);
110 ok(7, QIOtell(qio) == (off_t) (2 * size));
111 result = QIOread(qio);
112 ok(8, !QIOerror(qio));
113 ok(9, QIOlength(qio) == 0);
114 ok(10, *result == 0);
115 result = QIOread(qio);
116 if (strlen(result) < size - 1) {
119 for (success = true, i = 0; i < count - 1; i++)
120 success = success && !memcmp(result + i * 256, data, 256);
121 success = success && !memcmp(result + i * 256, data, 255);
124 ok(12, QIOtell(qio) == (off_t) (3 * size + 1));
125 result = QIOread(qio);
126 ok(13, !QIOerror(qio));
127 ok(14, QIOlength(qio) == 127);
128 ok(15, strlen(result) == 127);
129 ok(16, !memcmp(result, data, 127));
130 for (success = true, i = 0; i < count; i++) {
131 result = QIOread(qio);
132 success = (success && !QIOerror(qio) && (QIOlength(qio) == 255)
133 && !strcmp(result, (char *) out));
136 ok(18, QIOtell(qio) == (off_t) (4 * size + 129));
137 result = QIOread(qio);
139 ok(20, QIOerror(qio));
140 ok(21, QIOtoolong(qio));
141 ok(22, QIOrewind(qio) == 0);
142 ok(23, QIOtell(qio) == 0);
143 result = QIOread(qio);
144 ok(24, !QIOerror(qio));
145 ok(25, QIOlength(qio) == 255);
146 ok(26, strlen(result) == 255);
147 ok(27, !strcmp(result, (char *) out));
148 ok(28, QIOtell(qio) == 256);
151 ok(29, close(fd) < 0);
152 ok(30, errno == EBADF);