Add std/flate tests.

This commit is contained in:
Nigel Tao committed 2017-07-07 12:06:54 +10:00
1 parent 4efce30bfc
commit 0b9f9162a5
7 files changed
+799 -7

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
#include "zlib.h"
unsigned int flate_mimic_read_func(void* ctx, unsigned char** buf) {
puffs_base_buf1* src = (puffs_base_buf1*)(ctx);
*buf = src->ptr + src->ri;
return src->wi - src->ri;
}
int flate_mimic_write_func(void* ctx, unsigned char* ptr, unsigned int len) {
puffs_base_buf1* dst = (puffs_base_buf1*)(ctx);
size_t n = len;
if (n > dst->len - dst->wi) {
return 1;
}
memmove(dst->ptr + dst->wi, ptr, n);
dst->wi += n;
return 0;
}
const char* flate_mimic_decode(puffs_base_buf1* dst, puffs_base_buf1* src) {
const char* ret = NULL;
uint8_t window[32 * 1024];
z_stream z = {0};
int ibi_err = inflateBackInit(&z, 15, window);
if (ibi_err != Z_OK) {
ret = "inflateBackInit failed";
goto cleanup0;
}
int ib_err =
inflateBack(&z, flate_mimic_read_func, src, flate_mimic_write_func, dst);
if (ib_err != Z_STREAM_END) {
ret = "inflateBack failed";
goto cleanup1;
}
size_t readable = src->wi - src->ri;
size_t remaining = z.avail_in;
if (readable < remaining) {
ret = "inconsistent avail_in";
goto cleanup1;
}
src->ri += readable - remaining;
cleanup1:;
int ibe_err = inflateBackEnd(&z);
if ((ibe_err != Z_OK) && !ret) {
ret = "inflateBackEnd failed";
}
cleanup0:;
return ret;
}
+195
View File
@@ -0,0 +1,195 @@
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
/*
This test program is typically run indirectly, by the "puffs test" or "puffs
bench" commands. These commands take an optional "-mimic" flag to check that
Puffs' output mimics (i.e. exactly matches) other libraries' output, such as
giflib for GIF, libpng for PNG, etc.
To manually run this test:
for cc in clang gcc; do
$cc -std=c99 -Wall -Werror flate.c && ./a.out
rm -f a.out
done
Each edition should print "PASS", amongst other information, and exit(0).
Add the "puffs mimic cflags" (everything after the colon below) to the C
compiler flags (after the .c file) to run the mimic tests.
To manually run the benchmarks, replace "-Wall -Werror" with "-O3" and replace
"./a.out" with "./a.out -bench". Combine these changes with the "puffs mimic
cflags" to run the mimic benchmarks.
*/
// !! puffs mimic cflags: -DPUFFS_MIMIC -lz
#include "../../../gen/c/std/flate.c"
#include "../testlib/testlib.c"
const char* proc_filename = "std/flate.c";
// ---------------- Golden Tests
// The src_offset0 and src_offset1 magic numbers come from:
//
// go run script/extract-flate-offsets.go test/testdata/*.gz
//
// The empty comments forces clang-format to place one element per line.
golden_test midsummer_gt = {
.want_filename = "../../testdata/midsummer.txt", //
.src_filename = "../../testdata/midsummer.txt.gz", //
.src_offset0 = 24, //
.src_offset1 = 5166, //
};
golden_test pi_gt = {
.want_filename = "../../testdata/pi.txt", //
.src_filename = "../../testdata/pi.txt.gz", //
.src_offset0 = 17, //
.src_offset1 = 48335, //
};
golden_test romeo_gt = {
.want_filename = "../../testdata/romeo.txt", //
.src_filename = "../../testdata/romeo.txt.gz", //
.src_offset0 = 20, //
.src_offset1 = 550, //
};
// ---------------- Flate Tests
const char* flate_puffs_decode(puffs_base_buf1* dst, puffs_base_buf1* src) {
puffs_flate_decoder dec;
puffs_flate_decoder_constructor(&dec, PUFFS_VERSION, 0);
puffs_base_writer1 dst_writer = {.buf = dst};
puffs_base_reader1 src_reader = {.buf = src};
puffs_flate_status s =
puffs_flate_decoder_decode(&dec, dst_writer, src_reader);
puffs_flate_decoder_destructor(&dec);
if (s) {
return puffs_flate_status_string(s);
}
return NULL;
}
void test_flate_puffs_decode_midsummer() {
proc_funcname = __func__;
test_buf1_buf1(flate_puffs_decode, &midsummer_gt);
}
void test_flate_puffs_decode_pi() {
proc_funcname = __func__;
test_buf1_buf1(flate_puffs_decode, &pi_gt);
}
void test_flate_puffs_decode_romeo() {
proc_funcname = __func__;
test_buf1_buf1(flate_puffs_decode, &romeo_gt);
}
// ---------------- Mimic Tests
#ifdef PUFFS_MIMIC
#include "../mimiclib/flate.c"
void test_flate_mimic_decode_midsummer() {
proc_funcname = __func__;
test_buf1_buf1(flate_mimic_decode, &midsummer_gt);
}
void test_flate_mimic_decode_pi() {
proc_funcname = __func__;
test_buf1_buf1(flate_mimic_decode, &pi_gt);
}
void test_flate_mimic_decode_romeo() {
proc_funcname = __func__;
test_buf1_buf1(flate_mimic_decode, &romeo_gt);
}
#endif // PUFFS_MIMIC
// ---------------- Flate Benches
void bench_flate_puffs_decode_1k() {
proc_funcname = __func__;
bench_buf1_buf1(flate_puffs_decode, &romeo_gt, 200000);
}
void bench_flate_puffs_decode_10k() {
proc_funcname = __func__;
bench_buf1_buf1(flate_puffs_decode, &midsummer_gt, 30000);
}
void bench_flate_puffs_decode_100k() {
proc_funcname = __func__;
bench_buf1_buf1(flate_puffs_decode, &pi_gt, 3000);
}
// ---------------- Mimic Benches
#ifdef PUFFS_MIMIC
void bench_flate_mimic_decode_1k() {
proc_funcname = __func__;
bench_buf1_buf1(flate_mimic_decode, &romeo_gt, 200000);
}
void bench_flate_mimic_decode_10k() {
proc_funcname = __func__;
bench_buf1_buf1(flate_mimic_decode, &midsummer_gt, 30000);
}
void bench_flate_mimic_decode_100k() {
proc_funcname = __func__;
bench_buf1_buf1(flate_mimic_decode, &pi_gt, 3000);
}
#endif // PUFFS_MIMIC
// ---------------- Manifest
// The empty comments forces clang-format to place one element per line.
proc tests[] = {
// Flate Tests
/* TODO: implement flate_puffs, then uncomment these.
test_flate_puffs_decode_midsummer, //
test_flate_puffs_decode_pi, //
test_flate_puffs_decode_romeo, //
*/
#ifdef PUFFS_MIMIC
// Mimic Tests
test_flate_mimic_decode_midsummer, //
test_flate_mimic_decode_pi, //
test_flate_mimic_decode_romeo, //
#endif // PUFFS_MIMIC
NULL,
};
// The empty comments forces clang-format to place one element per line.
proc benches[] = {
// Flate Benches
bench_flate_puffs_decode_1k, //
bench_flate_puffs_decode_10k, //
bench_flate_puffs_decode_100k, //
#ifdef PUFFS_MIMIC
// Mimic Benches
bench_flate_mimic_decode_1k, //
bench_flate_mimic_decode_10k, //
bench_flate_mimic_decode_100k, //
#endif // PUFFS_MIMIC
NULL,
};
-7
View File
@@ -31,13 +31,6 @@ cflags" to run the mimic benchmarks.
const char* proc_filename = "std/gif.c";
#define BUFFER_SIZE (1024 * 1024)
uint8_t global_got_buffer[BUFFER_SIZE];
uint8_t global_want_buffer[BUFFER_SIZE];
uint8_t global_src_buffer[BUFFER_SIZE];
uint8_t global_palette_buffer[3 * 256];
// ---------------- Basic Tests
void test_basic_bad_argument_out_of_range() {
+89
View File
@@ -7,6 +7,13 @@
#include <string.h>
#include <sys/time.h>
#define BUFFER_SIZE (1024 * 1024)
uint8_t global_got_buffer[BUFFER_SIZE];
uint8_t global_want_buffer[BUFFER_SIZE];
uint8_t global_src_buffer[BUFFER_SIZE];
uint8_t global_palette_buffer[3 * 256];
char fail_msg[65536] = {0};
#define FAIL(...) snprintf(fail_msg, sizeof(fail_msg), ##__VA_ARGS__)
@@ -34,6 +41,13 @@ const char* cc = "cl";
const char* cc = "cc";
#endif
typedef struct {
const char* want_filename;
const char* src_filename;
size_t src_offset0;
size_t src_offset1;
} golden_test;
bool bench_warm_up;
struct timeval bench_start_tv;
@@ -249,4 +263,79 @@ bool buf1s_equal(const char* prefix,
return false;
}
void proc_buf1_buf1(const char* (*codec_func)(puffs_base_buf1*,
puffs_base_buf1*),
golden_test* gt,
uint64_t reps,
bool bench) {
if (!codec_func) {
FAIL("NULL codec_func");
return;
}
if (!gt) {
FAIL("NULL golden_test");
return;
}
puffs_base_buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE};
puffs_base_buf1 got = {.ptr = global_got_buffer, .len = BUFFER_SIZE};
puffs_base_buf1 want = {.ptr = global_want_buffer, .len = BUFFER_SIZE};
if (!read_file(&src, gt->src_filename)) {
return;
}
if (gt->src_offset0 || gt->src_offset1) {
if (gt->src_offset0 > gt->src_offset1) {
FAIL("inconsistent src_offsets");
return;
}
if (gt->src_offset1 > src.wi) {
FAIL("src_offset1 too large");
return;
}
src.ri = gt->src_offset0;
src.wi = gt->src_offset1;
}
if (bench) {
bench_start();
}
uint64_t n_bytes = 0;
uint64_t i;
for (i = 0; i < reps; i++) {
got.wi = 0;
src.ri = gt->src_offset0;
const char* s = codec_func(&got, &src);
if (s) {
FAIL("%s", s);
return;
}
n_bytes += got.wi;
}
if (bench) {
bench_finish(reps, n_bytes);
return;
}
if (!read_file(&want, gt->want_filename)) {
return;
}
if (!buf1s_equal("", &got, &want)) {
return;
}
}
void bench_buf1_buf1(const char* (*codec_func)(puffs_base_buf1*,
puffs_base_buf1*),
golden_test* gt,
uint64_t reps) {
proc_buf1_buf1(codec_func, gt, reps, true);
}
void test_buf1_buf1(const char* (*codec_func)(puffs_base_buf1*,
puffs_base_buf1*),
golden_test* gt) {
proc_buf1_buf1(codec_func, gt, 1, false);
}
#endif // PUFFS_BASE_HEADER_H