// https://code.google.com/p/camlistore/issues/detail?id=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) } } }
func rng() *mathutil.FC32 { x, err := mathutil.NewFC32(math.MinInt32/4, math.MaxInt32/4, false) if err != nil { panic(err) } return x }
func r32() *mathutil.FC32 { r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true) if err != nil { panic(err) } return r }
func fc() *mathutil.FC32 { r, err := mathutil.NewFC32(0, math.MaxInt32, false) if err != nil { panic(err) } return r }
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())) } }
func newRng() *mathutil.FC32 { r, err := mathutil.NewFC32(0, math.MaxInt32, true) if err != nil { panic(err) } r.Seed(42) return r }
func MakeUniverse(universeid uint) *Universe { bodyIdGen, _ := mathutil.NewFC32(0, 999999, true) return &Universe{ Id: universeid, Space: &Space{ Dimension: space.Vector{}, Division: nil, Sub: nil, Entities: make([]Entity, 0), }, Entities: make(map[uint]*Entity), bodyIdGen: bodyIdGen, } }
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(p) } 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, big.NewInt(math.MaxInt64), true) for i := 0; i < N; i++ { f(r64.Next().Int64(), uint32(r32.Next())) } }
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))) } }
// $ go run example2.go # view rand.png and rnd.png by your favorite pic viewer // // see http://www.boallen.com/random-numbers.html func main() { sqr := image.Rect(0, 0, 511, 511) r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true) if err != nil { log.Fatal("NewFC32", err) } img := image.NewGray(sqr) for y := 0; y < 512; y++ { for x := 0; x < 512; x++ { if r.Next()&1 != 0 { img.Set(x, y, image.White) } } } buf := bytes.NewBuffer(nil) if err := png.Encode(buf, img); err != nil { log.Fatal("Encode rnd.png ", err) } if err := ioutil.WriteFile("rnd.png", buf.Bytes(), 0666); err != nil { log.Fatal("ioutil.WriteFile/rnd.png ", err) } r2 := rand.New(rand.NewSource(0)) img = image.NewGray(sqr) for y := 0; y < 512; y++ { for x := 0; x < 512; x++ { if r2.Int()&1 != 0 { img.Set(x, y, image.White) } } } buf = bytes.NewBuffer(nil) if err := png.Encode(buf, img); err != nil { log.Fatal("Encode rand.png ", err) } if err := ioutil.WriteFile("rand.png", buf.Bytes(), 0666); err != nil { log.Fatal("ioutil.WriteFile/rand.png ", err) } }
func TestBase32Ext(t *testing.T) { const max = 640 r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true) if err != nil { t.Fatal(err) } bin := []byte{} for i := 0; i < max; i++ { bin = append(bin, byte(r.Next())) cmp, err := Base32ExtDecode(Base32ExtEncode(bin)) if err != nil { t.Fatal(err) } if !bytes.Equal(bin, cmp) { t.Fatalf("a: % x\nb: % x", bin, cmp) } } }
/* $ # Usage e.g.: $ go run example.go -max 1024 > mathutil.dat # generate 1kB of "random" data */ func main() { r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true) if err != nil { log.Fatal(err) } var mflag uint64 flag.Uint64Var(&mflag, "max", 0, "limit output to max bytes") flag.Parse() stdout := bufio.NewWriter(os.Stdout) if mflag != 0 { for i := uint64(0); i < mflag; i++ { if err := stdout.WriteByte(byte(r.Next())); err != nil { log.Fatal(err) } } stdout.Flush() return } for stdout.WriteByte(byte(r.Next())) == nil { } }
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 }
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 }
// data["animations"] = p.Entity.Component(COMP_Animations).(Animations).Snapshot() snapshot, _ := json.Marshal(map[string]interface{}{ "event": "Update", "data": data, }) s.Send(string(snapshot)) } func (s *Session) Snapshot() map[string]interface{} { return nil } func (s *Session) WriteSessionId(w http.ResponseWriter) { w.Header().Set("Set-Cookie", "SessionId="+strconv.Itoa(int(s.SessionId))+"; Path=/;") } func (s *Session) Refresh() { s.Expire = time.Now().Add(time.Duration(option.Int("sessionlife")) * time.Second) } func (s *Session) Send(message string) { websocket.Message.Send(s.Websocket, message) } var Sessions = make(map[uint]*Session) var sessionIdGen, _ = mathutil.NewFC32(0, 999999, true)