Ejemplo n.º 1
0
// https://camlistore.org/issue/216
func TestBug216(t *testing.T) {
	const S = 2*kKV + 2 // 2*kKV+1 ok
	const N = 300000
	rng, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true)
	if err != nil {
		t.Fatal(err)
	}
	k := make([]byte, S/2)
	v := make([]byte, S-S/2)
	tr := NewBTree(nil)
	for i := 0; i < N; i++ {
		for i := range k {
			k[i] = byte(rng.Next())
		}
		for i := range v {
			v[i] = byte(rng.Next())
		}

		if err := tr.Set(h2b(k, int64(i)), h2b(v, int64(i))); err != nil {
			t.Fatal(i, err)
		}

		if (i+1)%10000 == 0 {
			//dbg("%v", i+1)
		}
	}
}
Ejemplo n.º 2
0
func fc() *mathutil.FC32 {
	r, err := mathutil.NewFC32(0, math.MaxInt32, false)
	if err != nil {
		panic(err)
	}

	return r
}
Ejemplo n.º 3
0
func r32() *mathutil.FC32 {
	r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true)
	if err != nil {
		panic(err)
	}

	return r
}
Ejemplo n.º 4
0
func TestModPow2(t *testing.T) {
	const N = 1e3
	data := []struct{ e, m uint32 }{
		// e == 0 -> x == 0
		{0, 2},
		{0, 3},
		{0, 4},

		{1, 2},
		{1, 3},
		{1, 4},
		{1, 5},

		{2, 2},
		{2, 3},
		{2, 4},
		{2, 5},

		{3, 2},
		{3, 3},
		{3, 4},
		{3, 5},
		{3, 6},
		{3, 7},
		{3, 8},
		{3, 9},

		{4, 2},
		{4, 3},
		{4, 4},
		{4, 5},
		{4, 6},
		{4, 7},
		{4, 8},
		{4, 9},
	}

	var got big.Int
	f := func(e, m uint32) {
		x := ModPow2(e, m)
		exp := ModPow(2, e, m)
		got.SetInt64(0)
		got.SetBit(&got, int(x), 1)
		if got.Cmp(exp) != 0 {
			t.Fatalf("\ne %d, m %d\ng: %s\ne: %s", e, m, &got, exp)
		}
	}

	for _, v := range data {
		f(v.e, v.m)
	}

	rg, _ := mathutil.NewFC32(2, 1<<10, true)
	for i := 0; i < N; i++ {
		f(uint32(rg.Next()), uint32(rg.Next()))
	}
}
Ejemplo n.º 5
0
func TestMod(t *testing.T) {
	const N = 1e4
	data := []struct {
		mod, n int64
		exp    uint32
	}{
		{0, 0x00, 3},
		{1, 0x01, 3},
		{3, 0x03, 3},
		{0, 0x07, 3},
		{1, 0x0f, 3},
		{3, 0x1f, 3},
		{0, 0x3f, 3},
		{1, 0x7f, 3},
		{3, 0xff, 3},
		{0, 0x1ff, 3},
	}

	var mod, n big.Int
	for _, v := range data {
		n.SetInt64(v.n)
		p := Mod(&mod, &n, v.exp)
		if p != &mod {
			t.Fatal()
		}

		if g, e := mod.Int64(), v.mod; g != e {
			t.Fatal(v.n, v.exp, g, e)
		}
	}

	f := func(in int64, exp uint32) {
		n.SetInt64(in)
		mod.Mod(&n, New(exp))
		e := mod.Int64()
		Mod(&mod, &n, exp)
		g := mod.Int64()
		if g != e {
			t.Fatal(in, exp, g, e)
		}
	}

	r32, _ := mathutil.NewFC32(1, 1e6, true)
	r64, _ := mathutil.NewFCBig(_0, mathutil.Uint64ToBigInt(math.MaxInt64), true)
	for i := 0; i < N; i++ {
		f(r64.Next().Int64(), uint32(r32.Next()))
	}
}
Ejemplo n.º 6
0
func TestModPow(t *testing.T) {
	const N = 2e2
	data := []struct{ b, e, m, r uint32 }{
		{0, 1, 1, 0},
		{0, 2, 1, 0},
		{0, 3, 1, 0},

		{1, 0, 1, 0},
		{1, 1, 1, 0},
		{1, 2, 1, 0},
		{1, 3, 1, 0},

		{2, 0, 1, 0},
		{2, 1, 1, 0},
		{2, 2, 1, 0},
		{2, 3, 1, 0},

		{2, 3, 4, 8},
		{2, 3, 5, 4},
		{2, 4, 3, 1},
		{3, 3, 3, 3},
		{3, 4, 5, 30},
	}

	f := func(b, e, m uint32, expect *big.Int) {
		got := ModPow(b, e, m)
		if got.Cmp(expect) != 0 {
			t.Fatal(b, e, m, got, expect)
		}
	}

	var r big.Int
	for _, v := range data {
		r.SetInt64(int64(v.r))
		f(v.b, v.e, v.m, &r)
	}

	rg, _ := mathutil.NewFC32(2, 1<<10, true)
	var bb big.Int
	for i := 0; i < N; i++ {
		b, e, m := uint32(rg.Next()), uint32(rg.Next()), uint32(rg.Next())
		bb.SetInt64(int64(b))
		f(b, e, m, mathutil.ModPowBigInt(&bb, New(e), New(m)))
	}
}
Ejemplo n.º 7
0
func testBTreePut2(t *testing.T, nf func() btreeStore, grow, n int) (tree btree) {
	if *noGrow {
		grow = 0
	}
	rng, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true)
	if err != nil {
		t.Fatal(err)
	}

	a := nf()
	if a == nil {
		t.Fatal(a)
	}

	tree, err = newBTree(a)
	if err != nil {
		t.Fatal(err)
	}

	var k, v [7]byte
	for i := 0; i < n; i++ {
		ik, iv := int64(rng.Next()), int64(rng.Next())
		h2b(k[:], ik)
		h2b(v[:], iv)
		kk := append(make([]byte, grow*i), k[:]...)
		vv := append(make([]byte, grow*i), v[:]...)
		prev, err := tree.put(nil, a, nil, kk, vv, true)
		if err != nil || len(prev) != 0 {
			t.Fatal(i, prev, err)
		}

		var buf []byte
		if buf, err = tree.get(a, nil, nil, kk); err != nil {
			t.Fatal(err)
		}

		if !bytes.Equal(buf, vv) {
			t.Fatalf("\n%s%s%s%s", hex.Dump(kk), hex.Dump(buf), hex.Dump(vv), tree.String(a))
		}
	}

	if err := verifyPageLinks(a, tree, n); err != nil {
		t.Fatalf("%s\n%s\n", err, tree.String(a))
	}

	rng.Seek(0)
	for i := 0; i < n; i++ {
		ik, iv := int64(rng.Next()), int64(rng.Next())
		h2b(k[:], ik)
		h2b(v[:], iv)
		kk := append(make([]byte, grow*i), k[:]...)
		vv := append(make([]byte, grow*i), v[:]...)
		var buf []byte
		buf, err := tree.extract(a, nil, nil, kk)
		if err != nil {
			t.Fatal(i, err)
		}

		if !bytes.Equal(buf, vv) {
			t.Fatalf("\n%s\n%s\n%s\n%s", hex.Dump(kk), hex.Dump(buf), hex.Dump(vv), tree.String(a))
		}

		if err := verifyPageLinks(a, tree, n-i-1); err != nil {
			t.Fatalf("%s\n%s", err, tree.String(a))
		}
	}

	return
}
Ejemplo n.º 8
0
func testBenchReadRnd(t *testing.T, n, block int) (dt int64) {
	println(252, n, block) //TODO-
	f, err := fcreate(*fnFlag)
	println(254) //TODO-
	if err != nil {
		println(254) //TODO-
		t.Fatal(err)
	}

	println(259) //TODO-
	defer func() {
		f.Accessor().Sync()
		probed(t, f)
		ec := f.Close()
		er := os.Remove(*fnFlag)
		if ec != nil {
			t.Fatal(ec)
		}

		if er != nil {
			t.Fatal(er)
		}
	}()

	h := make([]Handle, n)
	b := make([]byte, block)
	perc := 1
	if n >= 1000 {
		perc = n / 1000
	}
	println(281) //TODO-
	t0 := time.Now().UnixNano()
	for i := 0; i < n; i++ {
		if h[i], err = f.Alloc(b); err != nil {
			println(285) //TODO-
			t.Fatal(err)
		}
		if i != 0 && i%perc == 0 {
			dt := float64((time.Now().UnixNano() - t0) / 1e9)
			q := float64(i) / float64(n)
			rt := dt/q - dt
			print("w ", (1000*uint64(i))/uint64(n), " eta ", int(rt/60), ":", int(rt)%60, "              \r")
		}
	}
	rng, err := mathutil.NewFC32(0, n-1, true)
	if err != nil {
		println(297) //TODO-
		t.Fatal(err)
	}
	println("\nsync")
	f.Accessor().Sync()
	println("synced")
	t0 = time.Now().UnixNano()
	for i := 0; i < n; i++ {
		if _, err := f.Read(h[rng.Next()]); err != nil {
			println(306) //TODO-
			t.Fatal(err)
		}
		if i != 0 && i%perc == 0 {
			dt := float64((time.Now().UnixNano() - t0) / 1e9)
			q := float64(i) / float64(n)
			rt := dt/q - dt
			print("r ", (1000*uint64(i))/uint64(n), " eta ", int(rt/60), ":", int(rt)%60, "                   \r")
		}
	}
	println()
	println(314) //TODO-
	dt = time.Now().UnixNano() - t0
	if c, ok := f.Accessor().(*storage.Cache); ok {
		x, _ := c.Stat()
		t.Logf("Cache rq %10d, load %10d, purge %10d, top %10d, fs %10d", c.Rq, c.Load, c.Purge, c.Top, x.Size())
	}
	println(323) //TODO-
	return
}