Add Adler32 and CRC32/IEEE tests for pi's digits

This commit is contained in:
Nigel Tao committed 2018-03-25 21:54:07 +11:00
1 parent 56a767ede3
commit a2949fe28d
5 files changed
+163 -41

No files matched your search

+42 -10
View File
@@ -16,8 +16,8 @@
package main
// checksum.go prints a checksum of stdin's bytes. Checksum algorithms include
// "adler32" and "crc32/ieee".
// checksum.go prints a checksum of stdin's bytes, or of the opening digits of
// π. Checksum algorithms include "adler32" and "crc32/ieee".
//
// Usage: go run checksum.go -algorithm=crc32/ieee < foo.bar
@@ -29,10 +29,12 @@ import (
"hash/crc32"
"io"
"os"
"strings"
)
var (
algorithm = flag.String("algorithm", "adler32", "checksum algorithm")
pi = flag.Bool("pi", false, "checksum the digits of pi instead of stdin")
)
func main() {
@@ -45,21 +47,51 @@ func main() {
func main1() error {
flag.Parse()
h32 := hash.Hash32(nil)
if *pi {
const digits = "3.1415926535897932384626433832795028841971693993751058209749445"
if len(digits) != 63 {
panic("bad len(digits)")
}
for i := 0; i < 64; i++ {
if err := do(strings.NewReader(digits[:i])); err != nil {
return err
}
fmt.Print(",")
if i&7 == 7 {
fmt.Println()
}
}
} else {
if err := do(os.Stdin); err != nil {
return err
}
fmt.Println()
}
return nil
}
func do(r io.Reader) error {
h := io.Writer(nil)
switch *algorithm {
case "adler32":
h32 = adler32.New()
h = adler32.New()
case "crc32/ieee":
h32 = crc32.NewIEEE()
h = crc32.NewIEEE()
default:
return fmt.Errorf("unknown algorithm %q", *algorithm)
}
if h32 != nil {
if _, err := io.Copy(h32, os.Stdin); err != nil {
return err
}
fmt.Printf("0x%08X\n", h32.Sum32())
if _, err := io.Copy(h, r); err != nil {
return err
}
switch h := h.(type) {
case hash.Hash32:
fmt.Printf("0x%08X", h.Sum32())
case hash.Hash64:
fmt.Printf("0x%016X", h.Sum64())
default:
return fmt.Errorf("algorithm %q is not a Hash32 or Hash64", *algorithm)
}
return nil
}
+9 -9
View File
@@ -25,11 +25,11 @@ const char* mimic_bench_adler32(wuffs_base__buf1* dst,
return "miniz does not independently compute Adler32";
}
const char* mimic_bench_crc32(wuffs_base__buf1* dst,
wuffs_base__buf1* src,
uint64_t wlimit,
uint64_t rlimit) {
return "miniz does not implement CRC32";
const char* mimic_bench_crc32_ieee(wuffs_base__buf1* dst,
wuffs_base__buf1* src,
uint64_t wlimit,
uint64_t rlimit) {
return "miniz does not implement CRC32/IEEE";
}
const char* mimic_deflate_zlib_decode(wuffs_base__buf1* dst,
@@ -94,10 +94,10 @@ const char* mimic_bench_adler32(wuffs_base__buf1* dst,
return NULL;
}
const char* mimic_bench_crc32(wuffs_base__buf1* dst,
wuffs_base__buf1* src,
uint64_t wlimit,
uint64_t rlimit) {
const char* mimic_bench_crc32_ieee(wuffs_base__buf1* dst,
wuffs_base__buf1* src,
uint64_t wlimit,
uint64_t rlimit) {
// TODO: don't ignore wlimit and rlimit.
uint8_t* ptr = src->ptr + src->ri;
size_t len = src->wi - src->ri;
+63 -18
View File
@@ -58,7 +58,7 @@ golden_test crc32_pi_gt = {
// ---------------- CRC32 Tests
void test_wuffs_crc32() {
void test_wuffs_crc32_ieee_golden() {
CHECK_FOCUS(__func__);
struct {
@@ -117,14 +117,58 @@ void test_wuffs_crc32() {
}
}
void test_wuffs_crc32_ieee_pi() {
CHECK_FOCUS(__func__);
const char* digits =
"3.1415926535897932384626433832795028841971693993751058209749445";
if (strlen(digits) != 63) {
FAIL("strlen(digits): got %d, want 63", (int)(strlen(digits)));
return;
}
// The want values are determined by script/checksum.go.
//
// wants[i] is the checksum of the first i bytes of the digits string.
uint32_t wants[64] = {
0x00000000, 0x6DD28E9B, 0x69647A00, 0x83B58BCD, 0x16E010BE, 0xAF13912C,
0xB6C654DC, 0x02D43F2E, 0xC60167FD, 0xDE72F5D2, 0xECB2EAA3, 0x22E1CE23,
0x26F4BB12, 0x099FD2E0, 0x2D041A2F, 0xC14373C1, 0x61A5D6D0, 0xEB60F999,
0x93EDF514, 0x779BB713, 0x7EC98D7A, 0x43184A97, 0x739064B9, 0xA81B2541,
0x1CCB1037, 0x4B177527, 0xC8932C85, 0xF0C86A18, 0xE99C072F, 0xC6EA2FC5,
0xF11D621D, 0x09483B39, 0xD20BA7B6, 0xA66136B0, 0x3F1C0D9B, 0x7D37E8CC,
0x68AFEE60, 0xB7DA99A5, 0x55BD96C6, 0xF18E35A4, 0x5C4D8E41, 0x6B38760A,
0x63623EDF, 0x0BB7D76F, 0x5001AC9B, 0x0A5FC5FB, 0xA76213D4, 0x0C1E135B,
0x916718F4, 0xD0FE1B9F, 0xE4D15B60, 0xCE8A5FB4, 0x381922EB, 0xB351097C,
0xA3003B0D, 0x64C7C28B, 0x8ED5424B, 0x6C872ADF, 0x7CBF02ED, 0x2D713AFF,
0xA028F932, 0x3BC16241, 0xF256AB5C, 0xE69E60DA,
};
int i;
for (i = 0; i < 64; i++) {
wuffs_crc32__ieee checksum;
wuffs_crc32__ieee__initialize(&checksum, WUFFS_VERSION, 0);
uint32_t got =
wuffs_crc32__ieee__update(&checksum, ((wuffs_base__slice_u8){
.ptr = (uint8_t*)(digits),
.len = i,
}));
if (got != wants[i]) {
FAIL("i=%d: got 0x%08" PRIX32 ", want 0x%08" PRIX32 "\n", i, got,
wants[i]);
return;
}
}
}
// ---------------- CRC32 Benches
uint32_t global_wuffs_crc32_unused_u32;
const char* wuffs_bench_crc32(wuffs_base__buf1* dst,
wuffs_base__buf1* src,
uint64_t wlimit,
uint64_t rlimit) {
const char* wuffs_bench_crc32_ieee(wuffs_base__buf1* dst,
wuffs_base__buf1* src,
uint64_t wlimit,
uint64_t rlimit) {
// TODO: don't ignore wlimit and rlimit.
wuffs_crc32__ieee checksum;
wuffs_crc32__ieee__initialize(&checksum, WUFFS_VERSION, 0);
@@ -137,30 +181,30 @@ const char* wuffs_bench_crc32(wuffs_base__buf1* dst,
return NULL;
}
void bench_wuffs_crc32_10k() {
void bench_wuffs_crc32_ieee_10k() {
CHECK_FOCUS(__func__);
do_bench_buf1_buf1(wuffs_bench_crc32, tc_src, &crc32_midsummer_gt, 0, 0,
do_bench_buf1_buf1(wuffs_bench_crc32_ieee, tc_src, &crc32_midsummer_gt, 0, 0,
150000);
}
void bench_wuffs_crc32_100k() {
void bench_wuffs_crc32_ieee_100k() {
CHECK_FOCUS(__func__);
do_bench_buf1_buf1(wuffs_bench_crc32, tc_src, &crc32_pi_gt, 0, 0, 15000);
do_bench_buf1_buf1(wuffs_bench_crc32_ieee, tc_src, &crc32_pi_gt, 0, 0, 15000);
}
// ---------------- Mimic Benches
#ifdef WUFFS_MIMIC
void bench_mimic_crc32_10k() {
void bench_mimic_crc32_ieee_10k() {
CHECK_FOCUS(__func__);
do_bench_buf1_buf1(mimic_bench_crc32, tc_src, &crc32_midsummer_gt, 0, 0,
do_bench_buf1_buf1(mimic_bench_crc32_ieee, tc_src, &crc32_midsummer_gt, 0, 0,
150000);
}
void bench_mimic_crc32_100k() {
void bench_mimic_crc32_ieee_100k() {
CHECK_FOCUS(__func__);
do_bench_buf1_buf1(mimic_bench_crc32, tc_src, &crc32_pi_gt, 0, 0, 15000);
do_bench_buf1_buf1(mimic_bench_crc32_ieee, tc_src, &crc32_pi_gt, 0, 0, 15000);
}
#endif // WUFFS_MIMIC
@@ -170,7 +214,8 @@ void bench_mimic_crc32_100k() {
// The empty comments forces clang-format to place one element per line.
proc tests[] = {
test_wuffs_crc32, //
test_wuffs_crc32_ieee_golden, //
test_wuffs_crc32_ieee_pi, //
NULL,
};
@@ -178,13 +223,13 @@ proc tests[] = {
// The empty comments forces clang-format to place one element per line.
proc benches[] = {
bench_wuffs_crc32_10k, //
bench_wuffs_crc32_100k, //
bench_wuffs_crc32_ieee_10k, //
bench_wuffs_crc32_ieee_100k, //
#ifdef WUFFS_MIMIC
bench_mimic_crc32_10k, //
bench_mimic_crc32_100k, //
bench_mimic_crc32_ieee_10k, //
bench_mimic_crc32_ieee_100k, //
#endif // WUFFS_MIMIC
+2 -2
View File
@@ -634,13 +634,13 @@ void bench_wuffs_deflate_decode_100k() {
void bench_mimic_crc32_10k() {
CHECK_FOCUS(__func__);
do_bench_buf1_buf1(mimic_bench_crc32, tc_src, &crc32_midsummer_gt, 0, 0,
do_bench_buf1_buf1(mimic_bench_crc32_ieee, tc_src, &crc32_midsummer_gt, 0, 0,
100000);
}
void bench_mimic_crc32_100k() {
CHECK_FOCUS(__func__);
do_bench_buf1_buf1(mimic_bench_crc32, tc_src, &crc32_pi_gt, 0, 0, 10000);
do_bench_buf1_buf1(mimic_bench_crc32_ieee, tc_src, &crc32_pi_gt, 0, 0, 10000);
}
void bench_mimic_deflate_decode_1k() {
+47 -2
View File
@@ -84,7 +84,7 @@ void test_basic_status_used_package() {
// ---------------- Adler32 Tests
void test_wuffs_adler32() {
void test_wuffs_adler32_golden() {
CHECK_FOCUS(__func__);
struct {
@@ -143,6 +143,50 @@ void test_wuffs_adler32() {
}
}
void test_wuffs_adler32_pi() {
CHECK_FOCUS(__func__);
const char* digits =
"3.1415926535897932384626433832795028841971693993751058209749445";
if (strlen(digits) != 63) {
FAIL("strlen(digits): got %d, want 63", (int)(strlen(digits)));
return;
}
// The want values are determined by script/checksum.go.
//
// wants[i] is the checksum of the first i bytes of the digits string.
uint32_t wants[64] = {
0x00000001, 0x00340034, 0x00960062, 0x01290093, 0x01F000C7, 0x02E800F8,
0x0415012D, 0x057B0166, 0x07130198, 0x08E101CE, 0x0AE40203, 0x0D1A0236,
0x0F85026B, 0x122802A3, 0x150402DC, 0x18170313, 0x1B63034C, 0x1EE2037F,
0x229303B1, 0x267703E4, 0x2A93041C, 0x2EE30450, 0x33690486, 0x382104B8,
0x3D0F04EE, 0x42310522, 0x47860555, 0x4D0E0588, 0x52CE05C0, 0x58C105F3,
0x5EE60625, 0x6542065C, 0x6BD70695, 0x72A106CA, 0x799B06FA, 0x80C7072C,
0x882B0764, 0x8FC7079C, 0x979707D0, 0x9F980801, 0xA7D2083A, 0xB0430871,
0xB8E508A2, 0xC1BD08D8, 0xCACE0911, 0xD4120944, 0xDD8F097D, 0xE74509B6,
0xF12E09E9, 0xFB4E0A20, 0x05B20A55, 0x10380A86, 0x1AEE0AB6, 0x25D90AEB,
0x30FC0B23, 0x3C510B55, 0x47D60B85, 0x53940BBE, 0x5F890BF5, 0x6BB20C29,
0x78140C62, 0x84AA0C96, 0x91740CCA, 0x9E730CFF,
};
int i;
for (i = 0; i < 64; i++) {
wuffs_zlib__adler32 checksum;
wuffs_zlib__adler32__initialize(&checksum, WUFFS_VERSION, 0);
uint32_t got =
wuffs_zlib__adler32__update(&checksum, ((wuffs_base__slice_u8){
.ptr = (uint8_t*)(digits),
.len = i,
}));
if (got != wants[i]) {
FAIL("i=%d: got 0x%08" PRIX32 ", want 0x%08" PRIX32 "\n", i, got,
wants[i]);
return;
}
}
}
// ---------------- Zlib Tests
const char* wuffs_zlib_decode(wuffs_base__buf1* dst,
@@ -365,7 +409,8 @@ proc tests[] = {
// other place.
test_basic_status_used_package, //
test_wuffs_adler32, //
test_wuffs_adler32_golden, //
test_wuffs_adler32_pi, //
test_wuffs_zlib_checksum_ignore, //
test_wuffs_zlib_checksum_verify_bad, //