package util

import (
	"bytes"
	"fmt"
	"hash/crc32"
	"io"
	"os"
	"testing"

	"gordenko.dev/dima/textutil"
)

var page = make([]byte, 4096)
var ieeeTable = crc32.MakeTable(crc32.IEEE)

func BenchmarkCRC32C(b *testing.B) {
	for i := 0; i < b.N; i++ {
		crc32.Checksum(page, castagnoliTable)
	}
}

func BenchmarkCRC32IEEE(b *testing.B) {
	for i := 0; i < b.N; i++ {
		crc32.Checksum(page, ieeeTable)
	}
}

func TestHash(t *testing.T) {
	raw, err := os.ReadFile("../b")
	if err != nil {
		t.Fatal(err)
	}

	buf, err := textutil.ParseHex(string(raw))
	if err != nil {
		t.Fatal(err)
	}

	fmt.Println(CalculateCRC32(buf))

	w := bytes.NewBuffer(nil)
	w.Write([]byte{
		0, 0, 0, 0, 0, 0, 0, 0, 0, // size (max 9 byte)
	})

	hasher := NewHasher()

	multi := io.MultiWriter(w, hasher)

	i := 0
	for i < len(buf) {
		end := i + 50
		if end >= len(buf) {
			end = len(buf)
		}
		multi.Write(buf[i:end])
		i += 50
	}

	fmt.Println(hasher.Sum32())
}
