mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Add crc32 ieee test cases
This commit is contained in:
1 parent
008bce65f2
commit
0ebfb75699
3 files changed
+84
-9
No files matched your search
@@ -16,17 +16,25 @@
|
||||
|
||||
package main
|
||||
|
||||
// adler32sum.go prints the Adler-32 checksum of stdin's bytes.
|
||||
// checksum.go prints a checksum of stdin's bytes. Checksum algorithms include
|
||||
// "adler32" and "crc32/ieee".
|
||||
//
|
||||
// Usage: go run adler32sum.go < foo.bar
|
||||
// Usage: go run checksum.go -algorithm=crc32/ieee < foo.bar
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"hash"
|
||||
"hash/adler32"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
algorithm = flag.String("algorithm", "adler32", "checksum algorithm")
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := main1(); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
@@ -35,10 +43,23 @@ func main() {
|
||||
}
|
||||
|
||||
func main1() error {
|
||||
a := adler32.New()
|
||||
if _, err := io.Copy(a, os.Stdin); err != nil {
|
||||
return err
|
||||
flag.Parse()
|
||||
|
||||
h32 := hash.Hash32(nil)
|
||||
switch *algorithm {
|
||||
case "adler32":
|
||||
h32 = adler32.New()
|
||||
case "crc32/ieee":
|
||||
h32 = 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())
|
||||
}
|
||||
fmt.Printf("0x%08X\n", a.Sum32())
|
||||
return nil
|
||||
}
|
||||
+56
-2
@@ -60,7 +60,61 @@ golden_test crc32_pi_gt = {
|
||||
|
||||
void test_wuffs_crc32() {
|
||||
CHECK_FOCUS(__func__);
|
||||
// TODO: implement.
|
||||
|
||||
struct {
|
||||
const char* filename;
|
||||
// The want values are determined by script/checksum.go.
|
||||
uint32_t want;
|
||||
} test_cases[] = {
|
||||
{
|
||||
.filename = "../../data/hat.bmp",
|
||||
.want = 0xA95A578B,
|
||||
},
|
||||
{
|
||||
.filename = "../../data/hat.gif",
|
||||
.want = 0xD9743B6A,
|
||||
},
|
||||
{
|
||||
.filename = "../../data/hat.jpeg",
|
||||
.want = 0x7F1A90CD,
|
||||
},
|
||||
{
|
||||
.filename = "../../data/hat.lossless.webp",
|
||||
.want = 0x485AA040,
|
||||
},
|
||||
{
|
||||
.filename = "../../data/hat.lossy.webp",
|
||||
.want = 0x89F53B4E,
|
||||
},
|
||||
{
|
||||
.filename = "../../data/hat.png",
|
||||
.want = 0xD5DA5C2F,
|
||||
},
|
||||
{
|
||||
.filename = "../../data/hat.tiff",
|
||||
.want = 0xBEF54503,
|
||||
},
|
||||
};
|
||||
|
||||
int i;
|
||||
for (i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); i++) {
|
||||
wuffs_base__buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE};
|
||||
if (!read_file(&src, test_cases[i].filename)) {
|
||||
return;
|
||||
}
|
||||
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 = src.ptr + src.ri,
|
||||
.len = src.wi - src.ri,
|
||||
}));
|
||||
if (got != test_cases[i].want) {
|
||||
FAIL("i=%d, filename=\"%s\": got 0x%08" PRIX32 ", want 0x%08" PRIX32 "\n",
|
||||
i, test_cases[i].filename, got, test_cases[i].want);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- CRC32 Benches
|
||||
@@ -116,7 +170,7 @@ 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, // TODO: uncomment.
|
||||
|
||||
NULL,
|
||||
};
|
||||
|
||||
+1
-1
@@ -89,7 +89,7 @@ void test_wuffs_adler32() {
|
||||
|
||||
struct {
|
||||
const char* filename;
|
||||
// The want values are determined by script/adler32sum.go.
|
||||
// The want values are determined by script/checksum.go.
|
||||
uint32_t want;
|
||||
} test_cases[] = {
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user