mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 01:01:35 +01:00
Add read_file to testlib.c.
This commit is contained in:
1 parent
6283389bc8
commit
f9de8950e4
3 files changed
+58
-7
No files matched your search
@@ -86,6 +86,7 @@ func do(dirname string, filename string) (failed bool, err error) {
|
|||||||
outCmd := exec.Command(out)
|
outCmd := exec.Command(out)
|
||||||
outCmd.Stdout = os.Stdout
|
outCmd.Stdout = os.Stdout
|
||||||
outCmd.Stderr = os.Stderr
|
outCmd.Stderr = os.Stderr
|
||||||
|
outCmd.Dir = dirname
|
||||||
if err := outCmd.Run(); err == nil {
|
if err := outCmd.Run(); err == nil {
|
||||||
// No-op.
|
// No-op.
|
||||||
} else if _, ok := err.(*exec.ExitError); ok {
|
} else if _, ok := err.(*exec.ExitError); ok {
|
||||||
|
|||||||
+19
-7
@@ -23,23 +23,35 @@ uint8_t global_dst_buffer[BUFFER_SIZE];
|
|||||||
uint8_t global_src_buffer[BUFFER_SIZE];
|
uint8_t global_src_buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
void test_lzw_decode() {
|
void test_lzw_decode() {
|
||||||
puffs_gif_lzw_decoder dec;
|
|
||||||
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0);
|
|
||||||
|
|
||||||
puffs_base_buf1 dst = {.ptr = global_dst_buffer, .cap = BUFFER_SIZE};
|
puffs_base_buf1 dst = {.ptr = global_dst_buffer, .cap = BUFFER_SIZE};
|
||||||
puffs_base_buf1 src = {.ptr = global_src_buffer, .cap = BUFFER_SIZE};
|
puffs_base_buf1 src = {.ptr = global_src_buffer, .cap = BUFFER_SIZE};
|
||||||
global_src_buffer[src.wi++] = 0x80;
|
if (!read_file(&src, "../../testdata/bricks-nodither.giflzw")) {
|
||||||
global_src_buffer[src.wi++] = 0x81;
|
goto cleanup0;
|
||||||
|
}
|
||||||
|
// That .giflzw file should be 13382 bytes long.
|
||||||
|
if (src.wi != 13382) {
|
||||||
|
FAIL("test_lzw_decode: file size: got %d, want %d", (int)(src.wi), 13382);
|
||||||
|
goto cleanup0;
|
||||||
|
}
|
||||||
|
// The first byte in that file, the LZW literal width, should be 0x08.
|
||||||
|
if (src.ptr[0] != 0x08) {
|
||||||
|
FAIL("test_lzw_decode: LZW literal width: got %d, want %d",
|
||||||
|
(int)(src.ptr[0]), 0x08);
|
||||||
|
goto cleanup0;
|
||||||
|
}
|
||||||
|
|
||||||
|
puffs_gif_lzw_decoder dec;
|
||||||
|
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0);
|
||||||
puffs_gif_status status =
|
puffs_gif_status status =
|
||||||
puffs_gif_lzw_decoder_decode(&dec, &dst, &src, false);
|
puffs_gif_lzw_decoder_decode(&dec, &dst, &src, false);
|
||||||
if (status != puffs_gif_status_ok) {
|
if (status != puffs_gif_status_ok) {
|
||||||
FAIL("test_lzw_decode: status: got %d, want %d", status,
|
FAIL("test_lzw_decode: status: got %d, want %d", status,
|
||||||
puffs_gif_status_ok);
|
puffs_gif_status_ok);
|
||||||
goto cleanup0;
|
goto cleanup1;
|
||||||
}
|
}
|
||||||
cleanup0:
|
cleanup1:
|
||||||
puffs_gif_lzw_decoder_destructor(&dec);
|
puffs_gif_lzw_decoder_destructor(&dec);
|
||||||
|
cleanup0:;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The empty comments forces clang-format to place one element per line.
|
// The empty comments forces clang-format to place one element per line.
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be found
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
// in the LICENSE file.
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
char fail_msg[65536] = {0};
|
char fail_msg[65536] = {0};
|
||||||
|
|
||||||
@@ -38,3 +40,39 @@ int main(int argc, char** argv) {
|
|||||||
printf("%-16s%-8sPASS (%d tests run)\n", test_filename, cc, tests_run);
|
printf("%-16s%-8sPASS (%d tests run)\n", test_filename, cc, tests_run);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef PUFFS_BASE_HEADER_H
|
||||||
|
// PUFFS_BASE_HEADER_H is where puffs_base_buf1 is defined.
|
||||||
|
bool read_file(puffs_base_buf1* dst, const char* path) {
|
||||||
|
FILE* f = fopen(path, "r");
|
||||||
|
if (!f) {
|
||||||
|
FAIL("read_file(\"%s\"): %s (errno=%d)", path, strerror(errno), errno);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
uint8_t* ptr = dst->ptr + dst->wi;
|
||||||
|
size_t cap = dst->cap - dst->wi;
|
||||||
|
while (true) {
|
||||||
|
size_t n = fread(ptr, 1, cap, f);
|
||||||
|
ptr += n;
|
||||||
|
cap -= n;
|
||||||
|
dst->wi += n;
|
||||||
|
if (feof(f)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int err = ferror(f);
|
||||||
|
if (err == EINTR) {
|
||||||
|
clearerr(f);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (err) {
|
||||||
|
FAIL("read_file(\"%s\"): %s (errno=%d)", path, strerror(err), err);
|
||||||
|
} else {
|
||||||
|
FAIL("read_file(\"%s\"): EOF not reached", path);
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif // PUFFS_BASE_HEADER_H
|
||||||
Reference in new issue
Block a user