Commit 38bd57e8 authored by nagayama15's avatar nagayama15

Add deflate/inflate tests

parent 400efaf4
......@@ -66,8 +66,6 @@ async function opt(input, output, optflags, cwd) {
}
const srcs = [
'zlib-exports',
'zlib/adler32',
'zlib/compress',
'zlib/crc32',
......@@ -93,9 +91,19 @@ const srcs = [
srcs.map(src => clang(`../src/${src}.c`, `./${src}.bc`, cflags, buildDir)),
);
// *.bc -> zlib.bc
// *.c -> *.bc
await clang(`../src/zlib-exports.c`, `./zlib-exports.bc`, cflags, buildDir);
// *.bc -> zlib-lib.bc
await link([
...srcs.map(s => `./${s}.bc`),
], `./zlib-lib.bc`, [
], buildDir);
// zlib-exports.bc zlib-lib.bc -> zlib.bc
await link([
...srcs.map(src => `./${src}.bc`),
`./zlib-exports.bc`,
`./zlib-lib.bc`,
`${cli.basedir}/lib/webassembly.bc`,
], 'zlib.bc', [
'-only-needed',
......
#pragma once
void print(int);
#include <webassembly.h>
#include <stdlib.h>
#include <memory.h>
#include <zlib.h>
#include "import.h"
export int add2(int a, int b) {
return a + b;
#define RAW_DATA_SIZE ((uInt)2 * 1024 * 1024)
static unsigned char *make_random_bytes(uInt size) {
unsigned char *buffer = malloc(size);
for (uInt i = 0; i < size; i++) {
buffer[i] = rand() >> 1;
}
return buffer;
}
export int run_test(void) {
// Make a random byte sequence
srand(0);
unsigned char *raw_bytes = make_random_bytes(RAW_DATA_SIZE);
int level = 9;
// Deflate the random sequence
uLongf deflated_size = compressBound(RAW_DATA_SIZE);
unsigned char *deflated_bytes = malloc(deflated_size);
compress2(deflated_bytes, &deflated_size, raw_bytes, RAW_DATA_SIZE, level);
// Inflate the random sequence
uLongf inflated_size = RAW_DATA_SIZE;
unsigned char *inflated_bytes = malloc(RAW_DATA_SIZE);
uncompress(inflated_bytes, &inflated_size, deflated_bytes, deflated_size);
// Validation
if (inflated_size != RAW_DATA_SIZE || memcmp(inflated_bytes, raw_bytes, RAW_DATA_SIZE) != 0) {
return 1;
}
// Release the buffers
free(deflated_bytes);
free(raw_bytes);
return 0;
}
......@@ -2,7 +2,11 @@ const assert = require('assert');
const wasm = require('webassembly');
(async () => {
const zlib = await wasm.load(`${__dirname}/build/zlib.wasm`);
const zlib = await wasm.load(`${__dirname}/build/zlib.wasm`, {
imports: {
print: console.log,
},
});
assert.strictEqual(zlib.exports.add2(8, 5), 13);
assert.strictEqual(zlib.exports.run_test(), 0);
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment