<commit_before>package chunker_test

import (
	"strconv"
	"testing"

	"github.com/restic/restic/chunker"
)

var polAddTests = []struct {
	x, y chunker.Pol
	sum  chunker.Pol
}{
	{23, 16, 23 ^ 16},
	{0x9a7e30d1e855e0a0, 0x670102a1f4bcd414, 0xfd7f32701ce934b4},
	{0x9a7e30d1e855e0a0, 0x9a7e30d1e855e0a0, 0},
}

func TestPolAdd(t *testing.T) {
	for _, test := range polAddTests {
		equals(t, test.sum, test.x.Add(test.y))
		equals(t, test.sum, test.y.Add(test.x))
	}
}

func parseBin(s string) chunker.Pol {
	i, err := strconv.ParseUint(s, 2, 64)
	if err != nil {
		panic(err)
	}

	return chunker.Pol(i)
}

var polMulTests = []struct {
	x, y chunker.Pol
	res  chunker.Pol
}{
	{1, 2, 2},
	{
		parseBin("1101"),
		parseBin("10"),
		parseBin("11010"),
	},
	{
		parseBin("1101"),
		parseBin("11"),
		parseBin("10111"),
	},
	{
		0x40000000,
		0x40000000,
		0x1000000000000000,
	},
	{
		parseBin("1010"),
		parseBin("100100"),
		parseBin("101101000"),
	},
	{
		parseBin("100"),
		parseBin("11"),
		parseBin("1100"),
	},
	{
		parseBin("11"),
		parseBin("110101"),
		parseBin("1011111"),
	},
	{
		parseBin("10011"),
		parseBin("110101"),
		parseBin("1100001111"),
	},
}

func TestPolMul(t *testing.T) {
	for i, test := range polMulTests {
		m := test.x.Mul(test.y)
		assert(t, test.res == m,
			"TestPolMul failed for test %d: %v * %v: want %v, got %v",
			i, test.x, test.y, test.res, m)
		m = test.y.Mul(test.x)
		assert(t, test.res == test.y.Mul(test.x),
			"TestPolMul failed for %d: %v * %v: want %v, got %v",
			i, test.x, test.y, test.res, m)
	}
}

func TestPolMulOverflow(t *testing.T) {
	defer func() {
		// try to recover overflow error
		err := recover()

		if e, ok := err.(string); ok && e == "multiplication would overflow uint64" {
			return
		} else {
			t.Logf("invalid error raised: %v", err)
			// re-raise error if not overflow
			panic(err)
		}
	}()

	x := chunker.Pol(1 << 63)
	x.Mul(2)
	t.Fatal("overflow test did not panic")
}

var polDivTests = []struct {
	x, y chunker.Pol
	res  chunker.Pol
}{
	{10, 50, 0},
	{0, 1, 0},
	{
		parseBin("101101000"), // 0x168
		parseBin("1010"),      // 0xa
		parseBin("100100"),    // 0x24
	},
	{2, 2, 1},
	{
		0x8000000000000000,
		0x8000000000000000,
		1,
	},
	{
		parseBin("1100"),
		parseBin("100"),
		parseBin("11"),
	},
	{
		parseBin("1100001111"),
		parseBin("10011"),
		parseBin("110101"),
	},
}

func TestPolDiv(t *testing.T) {
	for i, test := range polDivTests {
		m := test.x.Div(test.y)
		assert(t, test.res == m,
			"TestPolDiv failed for test %d: %v * %v: want %v, got %v",
			i, test.x, test.y, test.res, m)
	}
}

var polModTests = []struct {
	x, y chunker.Pol
	res  chunker.Pol
}{
	{10, 50, 10},
	{0, 1, 0},
	{
		parseBin("101101001"),
		parseBin("1010"),
		parseBin("1"),
	},
	{2, 2, 0},
	{
		0x8000000000000000,
		0x8000000000000000,
		0,
	},
	{
		parseBin("1100"),
		parseBin("100"),
		parseBin("0"),
	},
	{
		parseBin("1100001111"),
		parseBin("10011"),
		parseBin("0"),
	},
}

func TestPolModt(t *testing.T) {
	for _, test := range polModTests {
		equals(t, test.res, test.x.Mod(test.y))
	}
}

func BenchmarkPolDivMod(t *testing.B) {
	f := chunker.Pol(0x2482734cacca49)
	g := chunker.Pol(0x3af4b284899)

	for i := 0; i < t.N; i++ {
		g.DivMod(f)
	}
}

func BenchmarkPolDeg(t *testing.B) {
	f := chunker.Pol(0x3af4b284899)
	d := f.Deg()
	if d != 41 {
		t.Fatalf("BenchmalPolDeg: Wrong degree %d returned, expected %d",
			d, 41)
	}

	for i := 0; i < t.N; i++ {<commit_msg>Add more benchmarks<commit_after>package chunker_test

import (
	"strconv"
	"testing"

	"github.com/restic/restic/chunker"
)

var polAddTests = []struct {
	x, y chunker.Pol
	sum  chunker.Pol
}{
	{23, 16, 23 ^ 16},
	{0x9a7e30d1e855e0a0, 0x670102a1f4bcd414, 0xfd7f32701ce934b4},
	{0x9a7e30d1e855e0a0, 0x9a7e30d1e855e0a0, 0},
}

func TestPolAdd(t *testing.T) {
	for _, test := range polAddTests {
		equals(t, test.sum, test.x.Add(test.y))
		equals(t, test.sum, test.y.Add(test.x))
	}
}

func parseBin(s string) chunker.Pol {
	i, err := strconv.ParseUint(s, 2, 64)
	if err != nil {
		panic(err)
	}

	return chunker.Pol(i)
}

var polMulTests = []struct {
	x, y chunker.Pol
	res  chunker.Pol
}{
	{1, 2, 2},
	{
		parseBin("1101"),
		parseBin("10"),
		parseBin("11010"),
	},
	{
		parseBin("1101"),
		parseBin("11"),
		parseBin("10111"),
	},
	{
		0x40000000,
		0x40000000,
		0x1000000000000000,
	},
	{
		parseBin("1010"),
		parseBin("100100"),
		parseBin("101101000"),
	},
	{
		parseBin("100"),
		parseBin("11"),
		parseBin("1100"),
	},
	{
		parseBin("11"),
		parseBin("110101"),
		parseBin("1011111"),
	},
	{
		parseBin("10011"),
		parseBin("110101"),
		parseBin("1100001111"),
	},
}

func TestPolMul(t *testing.T) {
	for i, test := range polMulTests {
		m := test.x.Mul(test.y)
		assert(t, test.res == m,
			"TestPolMul failed for test %d: %v * %v: want %v, got %v",
			i, test.x, test.y, test.res, m)
		m = test.y.Mul(test.x)
		assert(t, test.res == test.y.Mul(test.x),
			"TestPolMul failed for %d: %v * %v: want %v, got %v",
			i, test.x, test.y, test.res, m)
	}
}

func TestPolMulOverflow(t *testing.T) {
	defer func() {
		// try to recover overflow error
		err := recover()

		if e, ok := err.(string); ok && e == "multiplication would overflow uint64" {
			return
		} else {
			t.Logf("invalid error raised: %v", err)
			// re-raise error if not overflow
			panic(err)
		}
	}()

	x := chunker.Pol(1 << 63)
	x.Mul(2)
	t.Fatal("overflow test did not panic")
}

var polDivTests = []struct {
	x, y chunker.Pol
	res  chunker.Pol
}{
	{10, 50, 0},
	{0, 1, 0},
	{
		parseBin("101101000"), // 0x168
		parseBin("1010"),      // 0xa
		parseBin("100100"),    // 0x24
	},
	{2, 2, 1},
	{
		0x8000000000000000,
		0x8000000000000000,
		1,
	},
	{
		parseBin("1100"),
		parseBin("100"),
		parseBin("11"),
	},
	{
		parseBin("1100001111"),
		parseBin("10011"),
		parseBin("110101"),
	},
}

func TestPolDiv(t *testing.T) {
	for i, test := range polDivTests {
		m := test.x.Div(test.y)
		assert(t, test.res == m,
			"TestPolDiv failed for test %d: %v * %v: want %v, got %v",
			i, test.x, test.y, test.res, m)
	}
}

var polModTests = []struct {
	x, y chunker.Pol
	res  chunker.Pol
}{
	{10, 50, 10},
	{0, 1, 0},
	{
		parseBin("101101001"),
		parseBin("1010"),
		parseBin("1"),
	},
	{2, 2, 0},
	{
		0x8000000000000000,
		0x8000000000000000,
		0,
	},
	{
		parseBin("1100"),
		parseBin("100"),
		parseBin("0"),
	},
	{
		parseBin("1100001111"),
		parseBin("10011"),
		parseBin("0"),
	},
}

func TestPolModt(t *testing.T) {
	for _, test := range polModTests {
		equals(t, test.res, test.x.Mod(test.y))
	}
}

func BenchmarkPolDivMod(t *testing.B) {
	f := chunker.Pol(0x2482734cacca49)
	g := chunker.Pol(0x3af4b284899)

	for i := 0; i < t.N; i++ {
		g.DivMod(f)
	}
}

func BenchmarkPolDiv(t *testing.B) {
	f := chunker.Pol(0x2482734cacca49)
	g := chunker.Pol(0x3af4b284899)

	for i := 0; i < t.N; i++ {
		g.Div(f)
	}
}

func BenchmarkPolMod(t *testing.B) {
	f := chunker.Pol(0x2482734cacca49)
	g := chunker.Pol(0x3af4b284899)

	for i := 0; i < t.N; i++ {
		g.Mod(f)
	}
}

func BenchmarkPolDeg(t *testing.B) {
	f := chunker.Pol(0x3af4b284899)
	d := f.Deg()
	if d != 41 {
		t.Fatalf("BenchmalPolDeg: Wrong degree %d returned, expected %d",
			d, 41)
	}

	for i := 0; i < t.N; i++ {
{'fraction_non_alphanumeric': 0.196214862232118, 'fraction_numerical': 0.12246033954912329, 'mean_word_length': 3.3796465569774528, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 14, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3699374', 'n_tokens_mistral': 4036, 'n_tokens_neox': 3288, 'n_words': 662}