mirror of
https://github.com/fumiama/base16384.git
synced 2026-06-07 19:40:23 +08:00
feat(file): add custom stream api
This commit is contained in:
@@ -100,7 +100,7 @@ static char tstbuf[BASE16384_ENCBUFSZ];
|
||||
int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND); \
|
||||
loop_ok(!fdout, i, "open"); \
|
||||
\
|
||||
err = base16384_encode_fd_detailed(fd, fdout, encbuf, decbuf, 0); \
|
||||
err = base16384_encode_fd_detailed(fd, fdout, encbuf, decbuf, flag); \
|
||||
base16384_loop_ok(err); \
|
||||
loop_ok(close(fd), i, "close"); \
|
||||
\
|
||||
@@ -109,7 +109,46 @@ static char tstbuf[BASE16384_ENCBUFSZ];
|
||||
\
|
||||
loop_ok(lseek(fdout, 0, SEEK_SET), i, "lseek"); \
|
||||
\
|
||||
err = base16384_decode_fd_detailed(fdout, fdval, encbuf, decbuf, 0); \
|
||||
err = base16384_decode_fd_detailed(fdout, fdval, encbuf, decbuf, flag); \
|
||||
base16384_loop_ok(err); \
|
||||
\
|
||||
loop_ok(close(fdout), i, "close"); \
|
||||
loop_ok(close(fdval), i, "close"); \
|
||||
\
|
||||
validate_result(); \
|
||||
}
|
||||
|
||||
#define test_stream_detailed(flag) \
|
||||
fputs("testing base16384_en/decode_stream with flag "#flag"...\n", stderr); \
|
||||
init_input_file(); \
|
||||
for(i = TEST_SIZE; i > 0; i--) { \
|
||||
reset_and_truncate(fd, i); \
|
||||
\
|
||||
int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND); \
|
||||
loop_ok(!fdout, i, "open"); \
|
||||
\
|
||||
err = base16384_encode_stream_detailed(&(base16384_stream_t){ \
|
||||
.client_data = (void*)(uintptr_t)fd, \
|
||||
.f.reader = base16384_test_file_reader, \
|
||||
}, &(base16384_stream_t){ \
|
||||
.client_data = (void*)(uintptr_t)fdout, \
|
||||
.f.writer = base16384_test_file_writer, \
|
||||
}, encbuf, decbuf, flag); \
|
||||
base16384_loop_ok(err); \
|
||||
loop_ok(close(fd), i, "close"); \
|
||||
\
|
||||
int fdval = open(TEST_VALIDATE_FILENAME, O_WRONLY|O_TRUNC|O_CREAT); \
|
||||
loop_ok(!fdval, i, "open"); \
|
||||
\
|
||||
loop_ok(lseek(fdout, 0, SEEK_SET), i, "lseek"); \
|
||||
\
|
||||
err = base16384_decode_stream_detailed(&(base16384_stream_t){ \
|
||||
.client_data = (void*)(uintptr_t)fdout, \
|
||||
.f.reader = base16384_test_file_reader, \
|
||||
}, &(base16384_stream_t){ \
|
||||
.client_data = (void*)(uintptr_t)fdval, \
|
||||
.f.writer = base16384_test_file_writer, \
|
||||
}, encbuf, decbuf, flag); \
|
||||
base16384_loop_ok(err); \
|
||||
\
|
||||
loop_ok(close(fdout), i, "close"); \
|
||||
@@ -148,6 +187,7 @@ int main() {
|
||||
test_detailed(file);
|
||||
test_detailed(fp);
|
||||
test_detailed(fd);
|
||||
test_detailed(stream);
|
||||
|
||||
remove_test_files();
|
||||
|
||||
|
||||
@@ -92,4 +92,36 @@
|
||||
ok(fclose(fp), "fclose"); \
|
||||
fputs("input file created.\n", stderr);
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
static ssize_t base16384_test_file_reader(const void *client_data, void *buffer, size_t count) {
|
||||
int fd = (int)((uintptr_t)client_data);
|
||||
ssize_t ret = read(fd, buffer, count);
|
||||
if(ret < 0) return ret;
|
||||
for(ssize_t i = 0; i < ret; i++) {
|
||||
((uint8_t*)(buffer))[i] = ~((uint8_t*)(buffer))[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t base16384_test_file_writer(const void *client_data, const void *buffer, size_t count) {
|
||||
int fd = (int)((uintptr_t)client_data);
|
||||
if(count <= 0) {
|
||||
errno = EINVAL;
|
||||
return -100;
|
||||
}
|
||||
uint8_t* wbuf = (uint8_t*)malloc(count);
|
||||
if(!wbuf) return -200;
|
||||
for(ssize_t i = 0; i < count; i++) {
|
||||
wbuf[i] = ~((uint8_t*)(buffer))[i];
|
||||
}
|
||||
ssize_t ret = write(fd, buffer, count);
|
||||
int errnobak = errno;
|
||||
free(wbuf);
|
||||
errno = errnobak;
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -122,6 +122,44 @@ int main() {
|
||||
validate_result();
|
||||
}
|
||||
|
||||
fputs("testing base16384_en/decode_stream...\n", stderr);
|
||||
init_input_file();
|
||||
for(i = TEST_SIZE; i > 0; i--) {
|
||||
reset_and_truncate(fd, i);
|
||||
|
||||
int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND);
|
||||
loop_ok(!fdout, i, "open");
|
||||
|
||||
err = base16384_encode_stream(&(base16384_stream_t){
|
||||
.client_data = (void*)(uintptr_t)fd,
|
||||
.f.reader = base16384_test_file_reader,
|
||||
}, &(base16384_stream_t){
|
||||
.client_data = (void*)(uintptr_t)fdout,
|
||||
.f.writer = base16384_test_file_writer,
|
||||
}, encbuf, decbuf);
|
||||
base16384_loop_ok(err);
|
||||
loop_ok(close(fd), i, "close");
|
||||
|
||||
int fdval = open(TEST_VALIDATE_FILENAME, O_WRONLY|O_TRUNC|O_CREAT);
|
||||
loop_ok(!fdval, i, "open");
|
||||
|
||||
loop_ok(lseek(fdout, 0, SEEK_SET), i, "lseek");
|
||||
|
||||
err = base16384_decode_stream(&(base16384_stream_t){
|
||||
.client_data = (void*)(uintptr_t)fdout,
|
||||
.f.reader = base16384_test_file_reader,
|
||||
}, &(base16384_stream_t){
|
||||
.client_data = (void*)(uintptr_t)fdval,
|
||||
.f.writer = base16384_test_file_writer,
|
||||
}, encbuf, decbuf);
|
||||
base16384_loop_ok(err);
|
||||
|
||||
loop_ok(close(fdout), i, "close");
|
||||
loop_ok(close(fdval), i, "close");
|
||||
|
||||
validate_result();
|
||||
}
|
||||
|
||||
remove(TEST_INPUT_FILENAME);
|
||||
remove(TEST_OUTPUT_FILENAME);
|
||||
remove(TEST_VALIDATE_FILENAME);
|
||||
|
||||
Reference in New Issue
Block a user