Best Got code snippet using got.Fatal
all_test.go
Source:all_test.go
...88 return89 }90 advise = func(off int64, len int, write bool) {91 if err = fileutil.Fadvise(file, off, off+int64(len), fileutil.POSIX_FADV_DONTNEED); err != nil {92 log.Fatal("advisor advise err", err)93 }94 }95 }96 var prob *storage.Probe97 if *probeFlag {98 prob = storage.NewProbe(store, nil)99 store = prob100 }101 if *cachedFlag {102 if store, err = storage.NewCache(store, *cacheTotalFlag, advise); err != nil {103 return104 }105 if *probeFlag {106 store = storage.NewProbe(store, prob)107 }108 }109 f, err = Open(newBalancedAcid(store))110 return111}112func fcreate(fn string) (f *File, err error) {113 var store storage.Accessor114 if store, err = storage.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666); err != nil {115 return116 }117 var advise func(int64, int, bool)118 if *fadviseFlag {119 file := store.(*storage.FileAccessor).File120 if err = fileutil.Fadvise(file, 0, 0, fileutil.POSIX_FADV_RANDOM); err != nil {121 return122 }123 advise = func(off int64, len int, write bool) {124 if err = fileutil.Fadvise(file, off, off+int64(len), fileutil.POSIX_FADV_DONTNEED); err != nil {125 log.Fatal("advisor advise err", err)126 }127 }128 }129 var prob *storage.Probe130 if *probeFlag {131 prob = storage.NewProbe(store, nil)132 store = prob133 }134 if *cachedFlag {135 if store, err = storage.NewCache(store, *cacheTotalFlag, advise); err != nil {136 return137 }138 if *probeFlag {139 store = storage.NewProbe(store, prob)140 }141 }142 f, err = New(newBalancedAcid(store))143 return144}145func probed(t *testing.T, f *File) {146 if f == nil {147 return148 }149 dump := func(p *storage.Probe) {150 t.Logf("OpsRd %d OpsWr %d BytesRd %d(avg %.1f) BytesWr %d(avg %.1f) SectorsRd %d(%d, +%d, x%.2f) SectorsWr %d(%d, +%d, x%.2f)",151 p.OpsRd, p.OpsWr,152 p.BytesRd, float64(p.BytesRd)/float64(p.OpsRd),153 p.BytesWr, float64(p.BytesWr)/float64(p.OpsWr),154 p.SectorsRd,155 p.SectorsRd<<9,156 p.SectorsRd<<9-p.BytesRd,157 float64(p.SectorsRd<<9)/float64(p.BytesRd),158 p.SectorsWr,159 p.SectorsWr<<9,160 p.SectorsWr<<9-p.BytesWr,161 float64(p.SectorsWr<<9)/float64(p.BytesWr),162 )163 }164 if ph, ok := f.Accessor().(*storage.Probe); ok {165 dump(ph)166 if c, ok := ph.Accessor.(*storage.Cache); ok {167 if pl, ok := c.Accessor().(*storage.Probe); ok {168 dump(pl)169 }170 }171 }172}173func (f *File) audit() (usedblocks, totalblocks int64, err error) {174 defer func() {175 if e := recover(); e != nil {176 err = e.(error)177 }178 }()179 fi, err := f.f.Stat()180 if err != nil {181 panic(err)182 }183 freemap := map[int64]int64{}184 fp := int64(0)185 buf := make([]byte, 22)186 freeblocks := int64(0)187 // linear scan188 for fp < fi.Size() {189 totalblocks++190 typ, size := f.getInfo(fp >> 4)191 f.read(buf[:1], fp+size<<4-1)192 last := buf[0]193 switch {194 default:195 panic("internal error")196 case typ == 0:197 if last != 0 {198 panic(fmt.Errorf("@%#x used empty, last @%#x: %#x != 0", fp, fp+size<<4-1, last))199 }200 case typ >= 0x1 && typ <= 0xed:201 if last >= 0xfe {202 panic(fmt.Errorf("@%#x used short, last @%#x: %#x > 0xfe", fp, fp+size<<4-1, last))203 }204 case typ >= 0xee && typ <= 0xfb:205 if last > 1 {206 panic(fmt.Errorf("@%#x used esc short, last @%#x: %#x > 1", fp, fp+size<<4-1, last))207 }208 case typ == 0xfc:209 f.read(buf[:2], fp+1)210 switch n := int(buf[0])<<8 + int(buf[1]); {211 default:212 panic(fmt.Errorf("@%#x used long, illegal content length %#x < 0xee(238)", fp, n))213 case n >= 0xee && n <= 0xf0f0:214 if last >= 0xfe {215 panic(fmt.Errorf("@%#x used long, last @%#x: %#x > 0xfe", fp, fp+size<<4-1, last))216 }217 case n >= 0xf0f1 && n <= 0xffff:218 if last > 1 {219 panic(fmt.Errorf("@%#x used esc long, last @%#x: %#x > 1", fp, fp+size<<4-1, last))220 }221 }222 case typ == 0xfd:223 if last != 0 {224 panic(fmt.Errorf("@%#x reloc, last @%#x: %#x != 0", fp, fp+size<<4-1, last))225 }226 var target int64227 f.read(buf[:7], fp+1)228 (*Handle)(&target).Get(buf)229 if target >= f.atoms {230 panic(fmt.Errorf("@%#x illegal reloc, target %#x > f.atoms(%#x)", fp, target, f.atoms))231 }232 ttyp, _ := f.getInfo(target)233 if ttyp >= 0xfe {234 panic(fmt.Errorf("@%#x reloc, points to unused @%#x", fp, target))235 }236 if ttyp == 0xfd {237 panic(fmt.Errorf("@%#x reloc, points to reloc @%#x", fp, target))238 }239 case typ == 0xfe:240 if size < 2 {241 panic(fmt.Errorf("@%#x illegal free block, atoms %d < 2", fp, size))242 }243 if fp>>4 < f.canfree {244 panic(fmt.Errorf("@%#x illegal free block @ < f.canfree", fp))245 }246 f.read(buf[:22], fp)247 var prev, next, sz int64248 (*Handle)(&prev).Get(buf[1:])249 (*Handle)(&next).Get(buf[8:])250 f.checkPrevNext(fp, prev, next)251 f.read(buf[:7], fp+size<<4-8)252 (*Handle)(&sz).Get(buf)253 if sz != size {254 panic(fmt.Errorf("@%#x mismatch size, %d != %d", fp, sz, size))255 }256 if last != 0xfe {257 panic(fmt.Errorf("@%#x free atom, last @%#x: %#x != 0xff", fp, fp+size<<4-1, last))258 }259 freemap[fp>>4] = size260 freeblocks++261 case typ == 0xff:262 f.read(buf[:14], fp+1)263 var prev, next int64264 (*Handle)(&prev).Get(buf)265 (*Handle)(&next).Get(buf[7:])266 f.checkPrevNext(fp, prev, next)267 if last != 0xff {268 panic(fmt.Errorf("@%#x free atom, last @%#x: %#x != 0xff", fp, fp+size<<4-1, last))269 }270 freemap[fp>>4] = size271 freeblocks++272 }273 fp += size << 4274 }275 usedblocks = totalblocks - freeblocks276 // check free table277 for size := len(f.freetab) - 1; size > 0; size-- {278 var prev, next, fprev int64279 this := f.freetab[size]280 for this != 0 {281 sz, ok := freemap[this]282 if !ok {283 panic(fmt.Errorf("bad freetab[%d] item @%#x", size, this))284 }285 delete(freemap, this)286 if sz < int64(size) {287 panic(fmt.Errorf("bad freetab[%d] item size @%#x %d", size, this, sz))288 }289 if sz == 1 {290 f.read(buf[:15], this<<4)291 (*Handle)(&fprev).Get(buf[1:])292 if fprev != prev {293 panic(fmt.Errorf("bad fprev %#x, exp %#x", fprev, prev))294 }295 (*Handle)(&next).Get(buf[8:])296 } else {297 f.read(buf, this<<4)298 (*Handle)(&fprev).Get(buf[1:])299 if fprev != prev {300 panic(fmt.Errorf("bad fprev %#x, exp %#x", fprev, prev))301 }302 var fsz int64303 (*Handle)(&fsz).Get(buf[15:])304 if fsz != sz {305 panic(fmt.Errorf("bad fsz %d @%#x, exp %#x", fsz, this<<4, sz))306 }307 (*Handle)(&next).Get(buf[8:])308 }309 prev, this = this, next310 }311 }312 if n := len(freemap); n != 0 {313 for h, s := range freemap {314 panic(fmt.Errorf("%d lost free blocks in freemap, e.g. %d free atoms @%#x", n, s, h))315 }316 }317 return318}319func (f *File) checkPrevNext(fp, prev, next int64) {320 if prev != 0 && prev < f.canfree {321 panic(fmt.Errorf("@%#x illegal free atom, prev %#x < f.canfree(%#x)", fp, prev, f.canfree))322 }323 if prev >= f.atoms {324 panic(fmt.Errorf("@%#x illegal free atom, prev %#x > f.atoms", fp, prev))325 }326 if next != 0 && next < f.canfree {327 panic(fmt.Errorf("@%#x illegal free atom, next %#x < f.canfree(%#x)", fp, next, f.canfree))328 }329 if next >= f.atoms {330 panic(fmt.Errorf("@%#x illegal free atom, next %#x > f.atoms", fp, next))331 }332}333func reaudit(t *testing.T, f *File, fn string) (of *File) {334 var err error335 if _, _, err := f.audit(); err != nil {336 t.Fatal(err)337 }338 if err := f.Close(); err != nil {339 t.Fatal(err)340 }341 f = nil342 runtime.GC()343 if of, err = fopen(fn); err != nil {344 t.Fatal(err)345 }346 if _, _, err := of.audit(); err != nil {347 t.Fatal(err)348 }349 return350}351func TestCreate(t *testing.T) {352 dir, name := temp()353 defer os.RemoveAll(dir)354 f, err := fcreate(name)355 if err != nil {356 t.Fatal(err)357 }358 defer func() {359 err := os.Remove(name)360 if err != nil {361 t.Fatal(err)362 }363 }()364 f.Accessor().Sync()365 probed(t, f)366 if err = f.Close(); err != nil {367 t.Log(f.f.(*balancedAcid).nesting)368 t.Fatal(err)369 }370 b, err := ioutil.ReadFile(name)371 if err != nil {372 t.Fatal(err)373 }374 x := b[:16]375 if !bytes.Equal(x, hdr) {376 t.Fatalf("\n% x\n% x", x, hdr)377 }378 x = b[16:32]379 if !bytes.Equal(x, empty) {380 t.Fatalf("\n% x\n% x", x, hdr)381 }382}383func TestOpen(t *testing.T) {384 dir, name := temp()385 defer os.RemoveAll(dir)386 f, err := fcreate(name)387 if err != nil {388 t.Fatal(err)389 }390 defer func() {391 probed(t, f)392 ec := f.Close()393 er := os.Remove(name)394 if ec != nil {395 t.Fatal(ec)396 }397 if er != nil {398 t.Fatal(er)399 }400 }()401 if err := f.Close(); err != nil {402 t.Fatal(err)403 }404 if f, err = fopen(name); err != nil {405 t.Fatal(err)406 }407 for i, p := range f.freetab {408 if p != 0 {409 t.Fatal(i+1, p)410 }411 }412}413func alloc(f *File, b []byte) (y int64) {414 if h, err := f.Alloc(b); err != nil {415 panic(err)416 } else {417 y = int64(h)418 }419 return420}421func realloc(f *File, atom int64, b []byte, keepHandle bool) (y int64) {422 if h, err := f.Realloc(Handle(atom), b, keepHandle); err != nil {423 panic(err)424 } else {425 y = int64(h)426 }427 return428}429func testContentEncodingDecoding(t *testing.T, min, max int) {430 dir, name := temp()431 defer os.RemoveAll(dir)432 f, err := fcreate(name)433 if err != nil {434 t.Fatal(err)435 }436 defer func() {437 ec := f.Close()438 er := os.Remove(name)439 if ec != nil {440 t.Fatal(ec)441 }442 if er != nil {443 t.Fatal(er)444 }445 }()446 b := make([]byte, max)447 r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true)448 if err != nil {449 t.Fatal(err)450 }451 blocks := int64(3)452 a := make([]int64, 0, 4*(max-min+1))453 for cl := min; cl <= max; cl++ {454 src := b[:cl]455 for i := range src {456 b[i] = byte(r.Next())457 }458 a = append(a, alloc(f, src))459 blocks++460 if cl == 0 {461 continue462 }463 for i := range src {464 b[i] = byte(r.Next())465 }466 src[cl-1] = 0xfd467 a = append(a, alloc(f, src))468 blocks++469 for i := range src {470 b[i] = byte(r.Next())471 }472 src[cl-1] = 0xfe473 a = append(a, alloc(f, src))474 blocks++475 for i := range src {476 b[i] = byte(r.Next())477 }478 src[cl-1] = 0xff479 a = append(a, alloc(f, src))480 blocks++481 }482 f.Accessor().Sync()483 probed(t, f)484 if err := f.Close(); err != nil {485 t.Fatal(err)486 }487 f = nil488 runtime.GC()489 if f, err = fopen(name); err != nil {490 t.Fatal(err)491 }492 r.Seek(0)493 ai := 0494 for cl := min; cl <= max; cl++ {495 h := a[ai]496 ai++497 src := b[:cl]498 for i := range src {499 b[i] = byte(r.Next())500 }501 got, _ := f.readUsed(h)502 if !bytes.Equal(src, got) {503 t.Fatalf("cl %d atom %#x\nexp % x\ngot % x", cl, h, src, got)504 }505 if cl == 0 {506 continue507 }508 for i := range src {509 b[i] = byte(r.Next())510 }511 src[cl-1] = 0xfd512 h = a[ai]513 ai++514 got, _ = f.readUsed(h)515 if !bytes.Equal(src, got) {516 t.Fatalf("cl %d atom %#x\nexp % x\ngot % x", cl, h, src, got)517 }518 for i := range src {519 b[i] = byte(r.Next())520 }521 src[cl-1] = 0xfe522 h = a[ai]523 ai++524 got, _ = f.readUsed(h)525 if !bytes.Equal(src, got) {526 t.Fatalf("cl %d atom %#x\nexp % x\ngot % x", cl, h, src, got)527 }528 for i := range src {529 b[i] = byte(r.Next())530 }531 src[cl-1] = 0xff532 h = a[ai]533 ai++534 got, _ = f.readUsed(h)535 if !bytes.Equal(src, got) {536 t.Fatalf("cl %d atom %#x\nexp % x\ngot % x", cl, h, src, got)537 }538 }539 auditblocks, _, err := f.audit()540 if err != nil {541 t.Fatal(err)542 }543 if auditblocks != blocks {544 t.Fatal(auditblocks, blocks)545 }546 if f = reaudit(t, f, name); err != nil {547 t.Fatal(err)548 }549}550func TestContentEncodingDecoding(t *testing.T) {551 testContentEncodingDecoding(t, 0, 1024)552 testContentEncodingDecoding(t, 61680-17, 61680)553}554type freeItem struct {555 size int64556 head int64557}558func (f *File) reportFree() (report []freeItem) {559 for size, head := range f.freetab {560 if size != 0 && head != 0 {561 report = append(report, freeItem{int64(size), head})562 }563 }564 return565}566func free(f *File, h int64) {567 if err := f.Free(Handle(h)); err != nil {568 panic(err)569 }570}571func testFreeTail(t *testing.T, b []byte) {572 dir, name := temp()573 defer os.RemoveAll(dir)574 f, err := fcreate(name)575 if err != nil {576 t.Fatal(err)577 }578 defer func() {579 ec := f.Close()580 er := os.Remove(name)581 if ec != nil {582 t.Fatal(ec)583 }584 if er != nil {585 t.Fatal(er)586 }587 }()588 fs0 := f.atoms589 used0, total0, err := f.audit()590 if err != nil {591 panic(err)592 }593 if used0 != total0 {594 t.Fatal(used0, total0)595 }596 handle := alloc(f, b)597 free(f, handle)598 if fs1 := f.atoms; fs1 != fs0 {599 t.Fatal(fs1, fs0)600 }601 if rep := f.reportFree(); len(rep) != 0 {602 t.Fatal(rep)603 }604 if err := f.Close(); err != nil {605 t.Fatal(err)606 }607 f = nil608 runtime.GC()609 if f, err = fopen(name); err != nil {610 t.Fatal(err)611 }612 used, total, err := f.audit()613 if err != nil {614 panic(err)615 }616 if used != used0 {617 t.Fatal(used, used0)618 }619 if total != total0 {620 t.Fatal(total, total0)621 }622}623func TestFreeTail(t *testing.T) {624 b := make([]byte, 61680)625 for n := 0; n <= 253+16; n++ {626 data := b[:n]627 testFreeTail(t, data)628 if n == 0 {629 continue630 }631 data[n-1] = 0xff632 testFreeTail(t, data)633 data[n-1] = 0634 }635 for n := 61680 - 16; n <= 61680; n++ {636 data := b[:n]637 testFreeTail(t, data)638 data[n-1] = 0xff639 testFreeTail(t, data)640 data[n-1] = 0641 }642}643func testFreeTail2(t *testing.T, b []byte) {644 dir, name := temp()645 defer os.RemoveAll(dir)646 f, err := fcreate(name)647 if err != nil {648 t.Fatal(err)649 }650 defer func() {651 ec := f.Close()652 er := os.Remove(name)653 if ec != nil {654 t.Fatal(ec)655 }656 if er != nil {657 t.Fatal(er)658 }659 }()660 fs0 := f.atoms661 used0, total0, err := f.audit()662 if err != nil {663 panic(err)664 }665 if used0 != total0 {666 t.Fatal(used0, total0)667 }668 handle := alloc(f, b)669 handle2 := alloc(f, b)670 free(f, handle)671 free(f, handle2)672 if fs1 := f.atoms; fs1 != fs0 {673 t.Fatal(fs1, fs0)674 }675 if rep := f.reportFree(); len(rep) != 0 {676 t.Fatal(rep)677 }678 if err := f.Close(); err != nil {679 t.Fatal(err)680 }681 f = nil682 runtime.GC()683 if f, err = fopen(name); err != nil {684 t.Fatal(err)685 }686 used, total, err := f.audit()687 if err != nil {688 panic(err)689 }690 if used != used0 {691 t.Fatal(used, used0)692 }693 if total != total0 {694 t.Fatal(total, total0)695 }696}697func TestFreeTail2(t *testing.T) {698 b := make([]byte, 61680)699 for n := 0; n <= 253+16; n++ {700 data := b[:n]701 testFreeTail2(t, data)702 if n == 0 {703 continue704 }705 data[n-1] = 0xff706 testFreeTail2(t, data)707 data[n-1] = 0708 }709 for n := 61680 - 16; n <= 61680; n++ {710 data := b[:n]711 testFreeTail2(t, data)712 data[n-1] = 0xff713 testFreeTail2(t, data)714 data[n-1] = 0715 }716}717func testFreeIsolated(t *testing.T, b []byte) {718 dir, name := temp()719 defer os.RemoveAll(dir)720 f, err := fcreate(name)721 if err != nil {722 t.Fatal(err)723 }724 defer func() {725 ec := f.Close()726 er := os.Remove(name)727 if ec != nil {728 t.Fatal(ec)729 }730 if er != nil {731 t.Fatal(er)732 }733 }()734 rqAtoms := rq2Atoms(len(b))735 left := alloc(f, nil)736 handle := alloc(f, b)737 right := alloc(f, nil)738 fs0 := f.atoms739 used0, total0, err := f.audit()740 if err != nil {741 panic(err)742 }743 if used0 != total0 {744 t.Fatal(used0, total0)745 }746 free(f, handle)747 if fs1 := f.atoms; fs1 != fs0 {748 t.Fatal(fs1, fs0)749 }750 rep := f.reportFree()751 if len(rep) != 1 {752 t.Fatal(rep)753 }754 if x := rep[0]; x.size != rqAtoms || x.head != handle {755 t.Fatal(x)756 }757 used, total, err := f.audit()758 if err != nil {759 panic(err)760 }761 if n, free := f.getSize(left); n != 1 || free {762 t.Fatal(n, free)763 }764 if n, free := f.getSize(right); n != 1 || free {765 t.Fatal(n, free)766 }767 if used != used0-1 {768 t.Fatal(used, used0)769 }770 if total != total0 {771 t.Fatal(total, total0)772 }773 if free := total - used; free != 1 {774 t.Fatal(free)775 }776 // verify persisted file correct777 if err := f.Close(); err != nil {778 t.Fatal(err)779 }780 f = nil781 runtime.GC()782 if f, err = fopen(name); err != nil {783 t.Fatal(err)784 }785 if fs1 := f.atoms; fs1 != fs0 {786 t.Fatal(fs1, fs0)787 }788 rep = f.reportFree()789 if len(rep) != 1 {790 t.Fatal(rep)791 }792 if x := rep[0]; x.size != rqAtoms || x.head != handle {793 t.Fatal(x)794 }795 used, total, err = f.audit()796 if err != nil {797 panic(err)798 }799 if n, free := f.getSize(left); n != 1 || free {800 t.Fatal(n, free)801 }802 if n, free := f.getSize(right); n != 1 || free {803 t.Fatal(n, free)804 }805 if used != used0-1 {806 t.Fatal(used, used0)807 }808 if total != total0 {809 t.Fatal(total, total0)810 }811 if free := total - used; free != 1 {812 t.Fatal(free)813 }814}815func TestFreeIsolated(t *testing.T) {816 b := make([]byte, 61680)817 for n := 0; n <= 253+16; n++ {818 data := b[:n]819 testFreeIsolated(t, data)820 }821 for n := 61680 - 16; n <= 61680; n++ {822 data := b[:n]823 testFreeIsolated(t, data)824 }825}826func testFreeBlockList(t *testing.T, a, b int) {827 var h [2]int64828 t.Log(a, b)829 dir, name := temp()830 defer os.RemoveAll(dir)831 f, err := fcreate(name)832 if err != nil {833 t.Fatal(err)834 }835 defer func() {836 if f != nil {837 if err := f.Close(); err != nil {838 t.Fatal(err)839 }840 }841 f = nil842 runtime.GC()843 os.Remove(name)844 }()845 used0, total0, err := f.audit()846 if err != nil {847 t.Fatal(err)848 }849 alloc(f, nil)850 h[0] = alloc(f, nil)851 alloc(f, nil)852 h[1] = alloc(f, nil)853 alloc(f, nil)854 if err := f.Close(); err != nil {855 t.Fatal(err)856 }857 f = nil858 runtime.GC()859 if f, err = fopen(name); err != nil {860 t.Fatal(err)861 }862 used, total, err := f.audit()863 if err != nil {864 t.Fatal(err)865 }866 if used-used0 != 5 || total-total0 != 5 || used != total {867 t.Fatal(used0, total0, used, total)868 }869 free(f, h[a])870 free(f, h[b])871 used, total, err = f.audit()872 if err != nil {873 t.Fatal(err)874 }875 if used-used0 != 3 || total-total0 != 5 || total-used != 2 {876 t.Fatal(used0, total0, used, total)877 }878 if err := f.Close(); err != nil {879 t.Fatal(err)880 }881 f = nil882 runtime.GC()883 if f, err = fopen(name); err != nil {884 t.Fatal(err)885 }886 used, total, err = f.audit()887 if err != nil {888 t.Fatal(err)889 }890 if used-used0 != 3 || total-total0 != 5 || total-used != 2 {891 t.Fatal(used0, total0, used, total)892 }893}894func TestFreeBlockList(t *testing.T) {895 testFreeBlockList(t, 0, 1)896 testFreeBlockList(t, 1, 0)897}898func testFreeBlockList2(t *testing.T, a, b, c int) {899 var h [3]int64900 dir, name := temp()901 defer os.RemoveAll(dir)902 f, err := fcreate(name)903 if err != nil {904 t.Fatal(err)905 }906 defer func() {907 if f != nil {908 if err := f.Close(); err != nil {909 t.Fatal(err)910 }911 }912 f = nil913 runtime.GC()914 os.Remove(name)915 }()916 used0, total0, err := f.audit()917 if err != nil {918 t.Fatal(err)919 }920 alloc(f, nil)921 h[0] = alloc(f, nil)922 alloc(f, nil)923 h[1] = alloc(f, nil)924 alloc(f, nil)925 h[2] = alloc(f, nil)926 alloc(f, nil)927 if err := f.Close(); err != nil {928 t.Fatal(err)929 }930 f = nil931 runtime.GC()932 if f, err = fopen(name); err != nil {933 t.Fatal(err)934 }935 used, total, err := f.audit()936 if err != nil {937 t.Fatal(err)938 }939 if used-used0 != 7 || total-total0 != 7 || used != total {940 t.Fatal(used0, total0, used, total)941 }942 free(f, h[a])943 free(f, h[b])944 free(f, h[c])945 used, total, err = f.audit()946 if err != nil {947 t.Fatal(err)948 }949 if used-used0 != 4 || total-total0 != 7 || total-used != 3 {950 t.Fatal(used0, total0, used, total)951 }952 if err := f.Close(); err != nil {953 t.Fatal(err)954 }955 f = nil956 runtime.GC()957 if f, err = fopen(name); err != nil {958 t.Fatal(err)959 }960 used, total, err = f.audit()961 if err != nil {962 t.Fatal(err)963 }964 if used-used0 != 4 || total-total0 != 7 || total-used != 3 {965 t.Fatal(used0, total0, used, total)966 }967}968func TestFreeBlockList2(t *testing.T) {969 testFreeBlockList2(t, 0, 1, 2)970 testFreeBlockList2(t, 0, 2, 1)971 testFreeBlockList2(t, 1, 0, 2)972 testFreeBlockList2(t, 1, 2, 0)973 testFreeBlockList2(t, 2, 0, 1)974 testFreeBlockList2(t, 2, 1, 0)975}976var crng *mathutil.FC32977func init() {978 var err error979 if crng, err = mathutil.NewFC32(0, math.MaxInt32, true); err != nil {980 panic(err)981 }982}983func content(b []byte, h int64) (c []byte) {984 crng.Seed(h)985 crng.Seek(0)986 c = b[:crng.Next()%61681]987 for i := range c {988 c[i] = byte(crng.Next())989 }990 return991}992func testFreeBlockList3(t *testing.T, n, mod int) {993 rng, err := mathutil.NewFC32(0, n-1, true)994 if err != nil {995 t.Fatal(err)996 }997 dir, name := temp()998 defer os.RemoveAll(dir)999 f, err := fcreate(name)1000 if err != nil {1001 t.Fatal(err)1002 }1003 defer func() {1004 if f != nil {1005 if err := f.Close(); err != nil {1006 t.Fatal(err)1007 }1008 }1009 f = nil1010 runtime.GC()1011 os.Remove(name)1012 }()1013 ha := make([]int64, n)1014 b := make([]byte, 61680)1015 for i := range ha {1016 h := f.atoms1017 ha[i] = h1018 c := content(b, h)1019 if alloc(f, c) != h {1020 t.Fatal(h)1021 }1022 }1023 f = reaudit(t, f, name)1024 del := map[int64]bool{}1025 for _ = range ha {1026 i := rng.Next()1027 if i%mod != 0 {1028 h := ha[i]1029 free(f, h)1030 del[h] = true1031 }1032 }1033 f = reaudit(t, f, name)1034 for _, h := range ha {1035 if !del[h] {1036 exp := content(b, h)1037 got, _ := f.readUsed(h)1038 if !bytes.Equal(exp, got) {1039 t.Fatal(len(got), len(exp))1040 }1041 }1042 }1043}1044func TestFreeBlockList3(t *testing.T) {1045 testFreeBlockList3(t, 111, 1)1046 testFreeBlockList3(t, 151, 2)1047 testFreeBlockList3(t, 170, 3)1048 testFreeBlockList3(t, 170, 4)1049}1050func TestRealloc1(t *testing.T) {1051 dir, name := temp()1052 defer os.RemoveAll(dir)1053 f, err := fcreate(name)1054 if err != nil {1055 t.Fatal(err)1056 }1057 defer func() {1058 if f != nil {1059 if err := f.Close(); err != nil {1060 t.Fatal(err)1061 }1062 }1063 f = nil1064 runtime.GC()1065 os.Remove(name)1066 }()1067 b := make([]byte, 61680)1068 c := content(b, 10)1069 h10 := alloc(f, nil)1070 h20 := alloc(f, nil)1071 used0, total0, err := f.audit()1072 if err != nil {1073 t.Fatal(err)1074 }1075 exp := c[:15]1076 if handle := realloc(f, h10, exp, false); handle != h10 {1077 t.Fatal(handle, h10)1078 }1079 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1080 t.Fatal(len(got), len(exp))1081 }1082 if got, _ := f.readUsed(h20); len(got) != 0 {1083 t.Fatal(len(got), 0)1084 }1085 f = reaudit(t, f, name)1086 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1087 t.Fatal(len(got), len(exp))1088 }1089 if got, _ := f.readUsed(h20); len(got) != 0 {1090 t.Fatal(len(got), 0)1091 }1092 used, total, err := f.audit()1093 if err != nil {1094 t.Fatal(err)1095 }1096 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 0 || free != 0 {1097 t.Fatal(difused, diftotal, free)1098 }1099}1100func TestRealloc1Keep(t *testing.T) {1101 dir, name := temp()1102 defer os.RemoveAll(dir)1103 f, err := fcreate(name)1104 if err != nil {1105 t.Fatal(err)1106 }1107 defer func() {1108 if f != nil {1109 if err := f.Close(); err != nil {1110 t.Fatal(err)1111 }1112 }1113 f = nil1114 runtime.GC()1115 os.Remove(name)1116 }()1117 b := make([]byte, 61680)1118 c := content(b, 10)1119 h10 := alloc(f, nil)1120 h20 := alloc(f, nil)1121 used0, total0, err := f.audit()1122 if err != nil {1123 t.Fatal(err)1124 }1125 exp := c[:15]1126 if handle := realloc(f, h10, exp, true); handle != h10 {1127 t.Fatal(handle, h10)1128 }1129 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1130 t.Fatal(len(got), len(exp))1131 }1132 if got, _ := f.readUsed(h20); len(got) != 0 {1133 t.Fatal(len(got), 0)1134 }1135 f = reaudit(t, f, name)1136 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1137 t.Fatal(len(got), len(exp))1138 }1139 if got, _ := f.readUsed(h20); len(got) != 0 {1140 t.Fatal(len(got), 0)1141 }1142 used, total, err := f.audit()1143 if err != nil {1144 t.Fatal(err)1145 }1146 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 0 || free != 0 {1147 t.Fatal(difused, diftotal, free)1148 }1149}1150func TestRealloc2(t *testing.T) {1151 dir, name := temp()1152 defer os.RemoveAll(dir)1153 f, err := fcreate(name)1154 if err != nil {1155 t.Fatal(err)1156 }1157 defer func() {1158 if f != nil {1159 if err := f.Close(); err != nil {1160 t.Fatal(err)1161 }1162 }1163 f = nil1164 runtime.GC()1165 os.Remove(name)1166 }()1167 b := make([]byte, 61680)1168 c := content(b, 10)1169 h10 := alloc(f, c[:31])1170 h20 := alloc(f, nil)1171 used0, total0, err := f.audit()1172 if err != nil {1173 t.Fatal(err)1174 }1175 exp := c[:15]1176 if handle := realloc(f, h10, exp, false); handle != h10 {1177 t.Fatal(handle, h10)1178 }1179 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1180 t.Fatal(len(got), len(exp))1181 }1182 if got, _ := f.readUsed(h20); len(got) != 0 {1183 t.Fatal(len(got), 0)1184 }1185 f = reaudit(t, f, name)1186 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1187 t.Fatal(len(got), len(exp))1188 }1189 if got, _ := f.readUsed(h20); len(got) != 0 {1190 t.Fatal(len(got), 0)1191 }1192 used, total, err := f.audit()1193 if err != nil {1194 t.Fatal(err)1195 }1196 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 1 || free != 1 {1197 t.Fatal(difused, diftotal, free)1198 }1199}1200func TestRealloc2Keep(t *testing.T) {1201 dir, name := temp()1202 defer os.RemoveAll(dir)1203 f, err := fcreate(name)1204 if err != nil {1205 t.Fatal(err)1206 }1207 defer func() {1208 if f != nil {1209 if err := f.Close(); err != nil {1210 t.Fatal(err)1211 }1212 }1213 f = nil1214 runtime.GC()1215 os.Remove(name)1216 }()1217 b := make([]byte, 61680)1218 c := content(b, 10)1219 h10 := alloc(f, c[:31])1220 h20 := alloc(f, nil)1221 used0, total0, err := f.audit()1222 if err != nil {1223 t.Fatal(err)1224 }1225 exp := c[:15]1226 if handle := realloc(f, h10, exp, true); handle != h10 {1227 t.Fatal(handle, h10)1228 }1229 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1230 t.Fatal(len(got), len(exp))1231 }1232 if got, _ := f.readUsed(h20); len(got) != 0 {1233 t.Fatal(len(got), 0)1234 }1235 f = reaudit(t, f, name)1236 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1237 t.Fatal(len(got), len(exp))1238 }1239 if got, _ := f.readUsed(h20); len(got) != 0 {1240 t.Fatal(len(got), 0)1241 }1242 used, total, err := f.audit()1243 if err != nil {1244 t.Fatal(err)1245 }1246 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 1 || free != 1 {1247 t.Fatal(difused, diftotal, free)1248 }1249}1250func TestRealloc3(t *testing.T) {1251 dir, name := temp()1252 defer os.RemoveAll(dir)1253 f, err := fcreate(name)1254 if err != nil {1255 t.Fatal(err)1256 }1257 defer func() {1258 if f != nil {1259 if err := f.Close(); err != nil {1260 t.Fatal(err)1261 }1262 }1263 f = nil1264 runtime.GC()1265 os.Remove(name)1266 }()1267 b := make([]byte, 61680)1268 c := content(b, 10)1269 h10 := alloc(f, nil)1270 h20 := alloc(f, nil)1271 used0, total0, err := f.audit()1272 if err != nil {1273 t.Fatal(err)1274 }1275 exp := c[:31]1276 var handle int641277 if handle = realloc(f, h10, exp, false); handle == h10 {1278 t.Fatal(handle, h10)1279 }1280 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1281 t.Fatal(len(got), len(exp))1282 }1283 if got, _ := f.readUsed(h20); len(got) != 0 {1284 t.Fatal(len(got), 0)1285 }1286 f = reaudit(t, f, name)1287 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1288 t.Fatal(len(got), len(exp))1289 }1290 if got, _ := f.readUsed(h20); len(got) != 0 {1291 t.Fatal(len(got), 0)1292 }1293 used, total, err := f.audit()1294 if err != nil {1295 t.Fatal(err)1296 }1297 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 1 || free != 1 {1298 t.Fatal(difused, diftotal, free)1299 }1300}1301func TestRealloc3Keep(t *testing.T) {1302 dir, name := temp()1303 defer os.RemoveAll(dir)1304 f, err := fcreate(name)1305 if err != nil {1306 t.Fatal(err)1307 }1308 defer func() {1309 if f != nil {1310 if err := f.Close(); err != nil {1311 t.Fatal(err)1312 }1313 }1314 f = nil1315 runtime.GC()1316 os.Remove(name)1317 }()1318 b := make([]byte, 61680)1319 c := content(b, 10)1320 h10 := alloc(f, nil)1321 h20 := alloc(f, nil)1322 used0, total0, err := f.audit()1323 if err != nil {1324 t.Fatal(err)1325 }1326 exp := c[:31]1327 var handle int641328 if handle = realloc(f, h10, exp, true); handle != h10 {1329 t.Fatal(handle, h10)1330 }1331 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1332 t.Fatal(len(got), len(exp))1333 }1334 if got, _ := f.readUsed(h20); len(got) != 0 {1335 t.Fatal(len(got), 0)1336 }1337 f = reaudit(t, f, name)1338 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1339 t.Fatal(len(got), len(exp))1340 }1341 if got, _ := f.readUsed(h20); len(got) != 0 {1342 t.Fatal(len(got), 0)1343 }1344 used, total, err := f.audit()1345 if err != nil {1346 t.Fatal(err)1347 }1348 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 1 || diftotal != 1 || free != 0 {1349 t.Fatal(difused, diftotal, free)1350 }1351}1352func TestRealloc4Keep(t *testing.T) {1353 dir, name := temp()1354 defer os.RemoveAll(dir)1355 f, err := fcreate(name)1356 if err != nil {1357 t.Fatal(err)1358 }1359 defer func() {1360 if f != nil {1361 if err := f.Close(); err != nil {1362 t.Fatal(err)1363 }1364 }1365 f = nil1366 runtime.GC()1367 os.Remove(name)1368 }()1369 b := make([]byte, 61680)1370 c := content(b, 10)1371 h10 := alloc(f, c[:31])1372 h20 := alloc(f, nil)1373 used0, total0, err := f.audit()1374 if err != nil {1375 t.Fatal(err)1376 }1377 exp := c[:47]1378 var handle int641379 if handle = realloc(f, h10, exp, true); handle != h10 {1380 t.Fatal(handle, h10)1381 }1382 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1383 t.Fatal(len(got), len(exp))1384 }1385 if got, _ := f.readUsed(h20); len(got) != 0 {1386 t.Fatal(len(got), 0)1387 }1388 f = reaudit(t, f, name)1389 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1390 t.Fatal(len(got), len(exp))1391 }1392 if got, _ := f.readUsed(h20); len(got) != 0 {1393 t.Fatal(len(got), 0)1394 }1395 used, total, err := f.audit()1396 if err != nil {1397 t.Fatal(err)1398 }1399 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 1 || diftotal != 2 || free != 1 {1400 t.Fatal(difused, diftotal, free)1401 }1402}1403func TestRealloc5(t *testing.T) {1404 dir, name := temp()1405 defer os.RemoveAll(dir)1406 f, err := fcreate(name)1407 if err != nil {1408 t.Fatal(err)1409 }1410 defer func() {1411 if f != nil {1412 if err := f.Close(); err != nil {1413 t.Fatal(err)1414 }1415 }1416 f = nil1417 runtime.GC()1418 os.Remove(name)1419 }()1420 b := make([]byte, 61680)1421 c := content(b, 10)1422 h10 := alloc(f, nil)1423 h15 := alloc(f, nil)1424 h20 := alloc(f, nil)1425 used0, total0, err := f.audit()1426 if err != nil {1427 t.Fatal(err)1428 }1429 free(f, h15)1430 exp := c[:31]1431 var handle int641432 if handle = realloc(f, h10, exp, false); handle != h10 {1433 t.Fatal(handle, h10)1434 }1435 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1436 t.Fatal(len(got), len(exp))1437 }1438 if got, _ := f.readUsed(h20); len(got) != 0 {1439 t.Fatal(len(got), 0)1440 }1441 f = reaudit(t, f, name)1442 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1443 t.Fatal(len(got), len(exp))1444 }1445 if got, _ := f.readUsed(h20); len(got) != 0 {1446 t.Fatal(len(got), 0)1447 }1448 used, total, err := f.audit()1449 if err != nil {1450 t.Fatal(err)1451 }1452 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -1 || free != 0 {1453 t.Fatal(difused, diftotal, free)1454 }1455}1456func TestRealloc5Keep(t *testing.T) {1457 dir, name := temp()1458 defer os.RemoveAll(dir)1459 f, err := fcreate(name)1460 if err != nil {1461 t.Fatal(err)1462 }1463 defer func() {1464 if f != nil {1465 if err := f.Close(); err != nil {1466 t.Fatal(err)1467 }1468 }1469 f = nil1470 runtime.GC()1471 os.Remove(name)1472 }()1473 b := make([]byte, 61680)1474 c := content(b, 10)1475 h10 := alloc(f, nil)1476 h15 := alloc(f, nil)1477 h20 := alloc(f, nil)1478 used0, total0, err := f.audit()1479 if err != nil {1480 t.Fatal(err)1481 }1482 free(f, h15)1483 exp := c[:31]1484 var handle int641485 if handle = realloc(f, h10, exp, true); handle != h10 {1486 t.Fatal(handle, h10)1487 }1488 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1489 t.Fatal(len(got), len(exp))1490 }1491 if got, _ := f.readUsed(h20); len(got) != 0 {1492 t.Fatal(len(got), 0)1493 }1494 f = reaudit(t, f, name)1495 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1496 t.Fatal(len(got), len(exp))1497 }1498 if got, _ := f.readUsed(h20); len(got) != 0 {1499 t.Fatal(len(got), 0)1500 }1501 used, total, err := f.audit()1502 if err != nil {1503 t.Fatal(err)1504 }1505 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -1 || free != 0 {1506 t.Fatal(difused, diftotal, free)1507 }1508}1509func TestRealloc6(t *testing.T) {1510 dir, name := temp()1511 defer os.RemoveAll(dir)1512 f, err := fcreate(name)1513 if err != nil {1514 t.Fatal(err)1515 }1516 defer func() {1517 if f != nil {1518 if err := f.Close(); err != nil {1519 t.Fatal(err)1520 }1521 }1522 f = nil1523 runtime.GC()1524 os.Remove(name)1525 }()1526 b := make([]byte, 61680)1527 c := content(b, 10)1528 h10 := alloc(f, nil)1529 h15 := alloc(f, c[:31])1530 h20 := alloc(f, nil)1531 used0, total0, err := f.audit()1532 if err != nil {1533 t.Fatal(err)1534 }1535 free(f, h15)1536 exp := c[:31]1537 var handle int641538 if handle = realloc(f, h10, exp, false); handle != h10 {1539 t.Fatal(handle, h10)1540 }1541 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1542 t.Fatal(len(got), len(exp))1543 }1544 if got, _ := f.readUsed(h20); len(got) != 0 {1545 t.Fatal(len(got), 0)1546 }1547 f = reaudit(t, f, name)1548 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1549 t.Fatal(len(got), len(exp))1550 }1551 if got, _ := f.readUsed(h20); len(got) != 0 {1552 t.Fatal(len(got), 0)1553 }1554 used, total, err := f.audit()1555 if err != nil {1556 t.Fatal(err)1557 }1558 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != 0 || free != 1 {1559 t.Fatal(difused, diftotal, free)1560 }1561}1562func TestRealloc6Keep(t *testing.T) {1563 dir, name := temp()1564 defer os.RemoveAll(dir)1565 f, err := fcreate(name)1566 if err != nil {1567 t.Fatal(err)1568 }1569 defer func() {1570 if f != nil {1571 if err := f.Close(); err != nil {1572 t.Fatal(err)1573 }1574 }1575 f = nil1576 runtime.GC()1577 os.Remove(name)1578 }()1579 b := make([]byte, 61680)1580 c := content(b, 10)1581 h10 := alloc(f, nil)1582 h15 := alloc(f, c[:31])1583 h20 := alloc(f, nil)1584 used0, total0, err := f.audit()1585 if err != nil {1586 t.Fatal(err)1587 }1588 free(f, h15)1589 exp := c[:31]1590 var handle int641591 if handle = realloc(f, h10, exp, true); handle != h10 {1592 t.Fatal(handle, h10)1593 }1594 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1595 t.Fatal(len(got), len(exp))1596 }1597 if got, _ := f.readUsed(h20); len(got) != 0 {1598 t.Fatal(len(got), 0)1599 }1600 f = reaudit(t, f, name)1601 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {1602 t.Fatal(len(got), len(exp))1603 }1604 if got, _ := f.readUsed(h20); len(got) != 0 {1605 t.Fatal(len(got), 0)1606 }1607 used, total, err := f.audit()1608 if err != nil {1609 t.Fatal(err)1610 }1611 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != 0 || free != 1 {1612 t.Fatal(difused, diftotal, free)1613 }1614}1615func TestRelocRealloc1(t *testing.T) {1616 dir, name := temp()1617 defer os.RemoveAll(dir)1618 f, err := fcreate(name)1619 if err != nil {1620 t.Fatal(err)1621 }1622 defer func() {1623 if f != nil {1624 if err := f.Close(); err != nil {1625 t.Fatal(err)1626 }1627 }1628 f = nil1629 runtime.GC()1630 os.Remove(name)1631 }()1632 b := make([]byte, 61680)1633 h10 := alloc(f, nil)1634 h20 := alloc(f, nil)1635 var handle int641636 if handle = realloc(f, h10, b[:31], true); handle != h10 {1637 t.Fatal(handle, h10)1638 }1639 used0, total0, err := f.audit() // c+3, c+31640 if err != nil {1641 t.Fatal(err)1642 }1643 c := content(b, 10)1644 exp := c[:15]1645 if handle = realloc(f, h10, exp, false); handle != h10 {1646 t.Fatal(handle, h10)1647 }1648 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1649 t.Fatal(len(got), len(exp))1650 }1651 if got, _ := f.readUsed(h20); len(got) != 0 {1652 t.Fatal(len(got), 0)1653 }1654 f = reaudit(t, f, name)1655 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1656 t.Fatal(len(got), len(exp))1657 }1658 if got, _ := f.readUsed(h20); len(got) != 0 {1659 t.Fatal(len(got), 0)1660 }1661 used, total, err := f.audit() // c+2, c+21662 if err != nil {1663 t.Fatal(err)1664 }1665 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -1 || free != 0 {1666 t.Fatal(difused, diftotal, free)1667 }1668}1669func TestRelocRealloc1Keep(t *testing.T) {1670 dir, name := temp()1671 defer os.RemoveAll(dir)1672 f, err := fcreate(name)1673 if err != nil {1674 t.Fatal(err)1675 }1676 defer func() {1677 if f != nil {1678 if err := f.Close(); err != nil {1679 t.Fatal(err)1680 }1681 }1682 f = nil1683 runtime.GC()1684 os.Remove(name)1685 }()1686 b := make([]byte, 61680)1687 h10 := alloc(f, nil)1688 h20 := alloc(f, nil)1689 var handle int641690 if handle = realloc(f, h10, b[:31], true); handle != h10 {1691 t.Fatal(handle, h10)1692 }1693 used0, total0, err := f.audit() // c+3, c+31694 if err != nil {1695 t.Fatal(err)1696 }1697 c := content(b, 10)1698 exp := c[:15]1699 if handle = realloc(f, h10, exp, true); handle != h10 {1700 t.Fatal(handle, h10)1701 }1702 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1703 t.Fatal(len(got), len(exp))1704 }1705 if got, _ := f.readUsed(h20); len(got) != 0 {1706 t.Fatal(len(got), 0)1707 }1708 f = reaudit(t, f, name)1709 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1710 t.Fatal(len(got), len(exp))1711 }1712 if got, _ := f.readUsed(h20); len(got) != 0 {1713 t.Fatal(len(got), 0)1714 }1715 used, total, err := f.audit() // c+2, c+21716 if err != nil {1717 t.Fatal(err)1718 }1719 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -1 || free != 0 {1720 t.Fatal(difused, diftotal, free)1721 }1722}1723func TestRelocRealloc2(t *testing.T) {1724 dir, name := temp()1725 defer os.RemoveAll(dir)1726 f, err := fcreate(name)1727 if err != nil {1728 t.Fatal(err)1729 }1730 defer func() {1731 if f != nil {1732 if err := f.Close(); err != nil {1733 t.Fatal(err)1734 }1735 }1736 f = nil1737 runtime.GC()1738 os.Remove(name)1739 }()1740 b := make([]byte, 61680)1741 h10 := alloc(f, nil)1742 h20 := alloc(f, nil)1743 var handle int641744 if handle = realloc(f, h10, b[:31], true); handle != h10 {1745 t.Fatal(handle, h10)1746 }1747 free(f, h20)1748 used0, total0, err := f.audit() // c+2, c+31749 if err != nil {1750 t.Fatal(err)1751 }1752 c := content(b, 10)1753 exp := c[:31]1754 if handle = realloc(f, h10, exp, false); handle != h10 {1755 t.Fatal(handle, h10)1756 }1757 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1758 t.Fatal(len(got), len(exp))1759 }1760 f = reaudit(t, f, name)1761 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1762 t.Fatal(len(got), len(exp))1763 }1764 used, total, err := f.audit() // c+1, c+11765 if err != nil {1766 t.Fatal(err)1767 }1768 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -2 || free != 0 {1769 t.Fatal(difused, diftotal, free)1770 }1771}1772func TestRelocRealloc2Keep(t *testing.T) {1773 dir, name := temp()1774 defer os.RemoveAll(dir)1775 f, err := fcreate(name)1776 if err != nil {1777 t.Fatal(err)1778 }1779 defer func() {1780 if f != nil {1781 if err := f.Close(); err != nil {1782 t.Fatal(err)1783 }1784 }1785 f = nil1786 runtime.GC()1787 os.Remove(name)1788 }()1789 b := make([]byte, 61680)1790 h10 := alloc(f, nil)1791 h20 := alloc(f, nil)1792 var handle int641793 if handle = realloc(f, h10, b[:31], true); handle != h10 {1794 t.Fatal(handle, h10)1795 }1796 free(f, h20)1797 used0, total0, err := f.audit() // c+2, c+31798 if err != nil {1799 t.Fatal(err)1800 }1801 c := content(b, 10)1802 exp := c[:31]1803 if handle = realloc(f, h10, exp, true); handle != h10 {1804 t.Fatal(handle, h10)1805 }1806 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1807 t.Fatal(len(got), len(exp))1808 }1809 f = reaudit(t, f, name)1810 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1811 t.Fatal(len(got), len(exp))1812 }1813 used, total, err := f.audit() // c+1, c+11814 if err != nil {1815 t.Fatal(err)1816 }1817 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -2 || free != 0 {1818 t.Fatal(difused, diftotal, free)1819 }1820}1821func TestRelocRealloc3(t *testing.T) {1822 dir, name := temp()1823 defer os.RemoveAll(dir)1824 f, err := fcreate(name)1825 if err != nil {1826 t.Fatal(err)1827 }1828 defer func() {1829 if f != nil {1830 if err := f.Close(); err != nil {1831 t.Fatal(err)1832 }1833 }1834 f = nil1835 runtime.GC()1836 os.Remove(name)1837 }()1838 b := make([]byte, 61680)1839 h10 := alloc(f, nil)1840 h20 := alloc(f, b[:31])1841 var handle int641842 if handle = realloc(f, h10, b[:31], true); handle != h10 {1843 t.Fatal(handle, h10)1844 }1845 free(f, h20)1846 used0, total0, err := f.audit() // c+2, c+31847 if err != nil {1848 t.Fatal(err)1849 }1850 c := content(b, 10)1851 exp := c[:31]1852 if handle = realloc(f, h10, exp, false); handle != h10 {1853 t.Fatal(handle, h10)1854 }1855 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1856 t.Fatal(len(got), len(exp))1857 }1858 f = reaudit(t, f, name)1859 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1860 t.Fatal(len(got), len(exp))1861 }1862 used, total, err := f.audit() // c+1, c+11863 if err != nil {1864 t.Fatal(err)1865 }1866 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -2 || free != 0 {1867 t.Fatal(difused, diftotal, free)1868 }1869}1870func TestRelocRealloc3Keep(t *testing.T) {1871 dir, name := temp()1872 defer os.RemoveAll(dir)1873 f, err := fcreate(name)1874 if err != nil {1875 t.Fatal(err)1876 }1877 defer func() {1878 if f != nil {1879 if err := f.Close(); err != nil {1880 t.Fatal(err)1881 }1882 }1883 f = nil1884 runtime.GC()1885 os.Remove(name)1886 }()1887 b := make([]byte, 61680)1888 h10 := alloc(f, nil)1889 h20 := alloc(f, b[:31])1890 var handle int641891 if handle = realloc(f, h10, b[:31], true); handle != h10 {1892 t.Fatal(handle, h10)1893 }1894 free(f, h20)1895 used0, total0, err := f.audit() // c+2, c+31896 if err != nil {1897 t.Fatal(err)1898 }1899 c := content(b, 10)1900 exp := c[:31]1901 if handle = realloc(f, h10, exp, true); handle != h10 {1902 t.Fatal(handle, h10)1903 }1904 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1905 t.Fatal(len(got), len(exp))1906 }1907 f = reaudit(t, f, name)1908 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1909 t.Fatal(len(got), len(exp))1910 }1911 used, total, err := f.audit() // c+1, c+11912 if err != nil {1913 t.Fatal(err)1914 }1915 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -2 || free != 0 {1916 t.Fatal(difused, diftotal, free)1917 }1918}1919func TestRelocRealloc4(t *testing.T) {1920 dir, name := temp()1921 defer os.RemoveAll(dir)1922 f, err := fcreate(name)1923 if err != nil {1924 t.Fatal(err)1925 }1926 defer func() {1927 if f != nil {1928 if err := f.Close(); err != nil {1929 t.Fatal(err)1930 }1931 }1932 f = nil1933 runtime.GC()1934 os.Remove(name)1935 }()1936 b := make([]byte, 61680)1937 h10 := alloc(f, nil)1938 _ = alloc(f, nil)1939 var handle int641940 if handle = realloc(f, h10, b[:47], true); handle != h10 {1941 t.Fatal(handle, h10)1942 }1943 _ = alloc(f, nil)1944 if handle = realloc(f, h10, b[:31], true); handle != h10 {1945 t.Fatal(handle, h10)1946 }1947 used0, total0, err := f.audit() // c+4, c+51948 if err != nil {1949 t.Fatal(err)1950 }1951 c := content(b, 10)1952 exp := c[:47]1953 if handle = realloc(f, h10, exp, false); handle != h10 {1954 t.Fatal(handle, h10)1955 }1956 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1957 t.Fatal(len(got), len(exp))1958 }1959 f = reaudit(t, f, name)1960 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {1961 t.Fatal(len(got), len(exp))1962 }1963 used, total, err := f.audit() // c+4, c+41964 if err != nil {1965 t.Fatal(err)1966 }1967 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != -1 || free != 0 {1968 t.Fatal(difused, diftotal, free)1969 }1970}1971func TestRelocRealloc4Keep(t *testing.T) {1972 dir, name := temp()1973 defer os.RemoveAll(dir)1974 f, err := fcreate(name)1975 if err != nil {1976 t.Fatal(err)1977 }1978 defer func() {1979 if f != nil {1980 if err := f.Close(); err != nil {1981 t.Fatal(err)1982 }1983 }1984 f = nil1985 runtime.GC()1986 os.Remove(name)1987 }()1988 b := make([]byte, 61680)1989 h10 := alloc(f, nil)1990 _ = alloc(f, nil)1991 var handle int641992 if handle = realloc(f, h10, b[:47], true); handle != h10 {1993 t.Fatal(handle, h10)1994 }1995 _ = alloc(f, nil)1996 if handle = realloc(f, h10, b[:31], true); handle != h10 {1997 t.Fatal(handle, h10)1998 }1999 used0, total0, err := f.audit() // c+4, c+52000 if err != nil {2001 t.Fatal(err)2002 }2003 c := content(b, 10)2004 exp := c[:47]2005 if handle = realloc(f, h10, exp, true); handle != h10 {2006 t.Fatal(handle, h10)2007 }2008 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {2009 t.Fatal(len(got), len(exp))2010 }2011 f = reaudit(t, f, name)2012 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {2013 t.Fatal(len(got), len(exp))2014 }2015 used, total, err := f.audit() // c+4, c+42016 if err != nil {2017 t.Fatal(err)2018 }2019 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != -1 || free != 0 {2020 t.Fatal(difused, diftotal, free)2021 }2022}2023func TestRelocRealloc5(t *testing.T) {2024 dir, name := temp()2025 defer os.RemoveAll(dir)2026 f, err := fcreate(name)2027 if err != nil {2028 t.Fatal(err)2029 }2030 defer func() {2031 if f != nil {2032 if err := f.Close(); err != nil {2033 t.Fatal(err)2034 }2035 }2036 f = nil2037 runtime.GC()2038 os.Remove(name)2039 }()2040 b := make([]byte, 61680)2041 h10 := alloc(f, nil)2042 _ = alloc(f, nil)2043 var handle int642044 if handle = realloc(f, h10, b[:31], true); handle != h10 {2045 t.Fatal(handle, h10)2046 }2047 _ = alloc(f, nil)2048 used0, total0, err := f.audit() // c+4, c+42049 if err != nil {2050 t.Fatal(err)2051 }2052 c := content(b, 10)2053 exp := c[:47]2054 if handle = realloc(f, h10, exp, false); handle != h10 {2055 t.Fatal(handle, h10)2056 }2057 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {2058 t.Fatal(len(got), len(exp))2059 }2060 f = reaudit(t, f, name)2061 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {2062 t.Fatal(len(got), len(exp))2063 }2064 used, total, err := f.audit() // c+4, c+52065 if err != nil {2066 t.Fatal(err)2067 }2068 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 1 || free != 1 {2069 t.Fatal(difused, diftotal, free)2070 }2071}2072func TestRelocRealloc5Keep(t *testing.T) {2073 dir, name := temp()2074 defer os.RemoveAll(dir)2075 f, err := fcreate(name)2076 if err != nil {2077 t.Fatal(err)2078 }2079 defer func() {2080 if f != nil {2081 if err := f.Close(); err != nil {2082 t.Fatal(err)2083 }2084 }2085 f = nil2086 runtime.GC()2087 os.Remove(name)2088 }()2089 b := make([]byte, 61680)2090 h10 := alloc(f, nil)2091 _ = alloc(f, nil)2092 var handle int642093 if handle = realloc(f, h10, b[:31], true); handle != h10 {2094 t.Fatal(handle, h10)2095 }2096 _ = alloc(f, nil)2097 used0, total0, err := f.audit() // c+4, c+42098 if err != nil {2099 t.Fatal(err)2100 }2101 c := content(b, 10)2102 exp := c[:47]2103 if handle = realloc(f, h10, exp, true); handle != h10 {2104 t.Fatal(handle, h10)2105 }2106 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {2107 t.Fatal(len(got), len(exp))2108 }2109 f = reaudit(t, f, name)2110 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {2111 t.Fatal(len(got), len(exp))2112 }2113 used, total, err := f.audit() // c+4, c+52114 if err != nil {2115 t.Fatal(err)2116 }2117 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 1 || free != 1 {2118 t.Fatal(difused, diftotal, free)2119 }2120}2121func TestRelocRealloc6(t *testing.T) {2122 dir, name := temp()2123 defer os.RemoveAll(dir)2124 f, err := fcreate(name)2125 if err != nil {2126 t.Fatal(err)2127 }2128 defer func() {2129 if f != nil {2130 if err := f.Close(); err != nil {2131 t.Fatal(err)2132 }2133 }2134 f = nil2135 runtime.GC()2136 os.Remove(name)2137 }()2138 b := make([]byte, 61680)2139 h10 := alloc(f, b[:31])2140 h20 := alloc(f, nil)2141 _ = alloc(f, nil)2142 free(f, h20)2143 used0, total0, err := f.audit() // c+2, c+32144 if err != nil {2145 t.Fatal(err)2146 }2147 c := content(b, 10)2148 exp := c[:15]2149 if handle := realloc(f, h10, exp, false); handle != h10 {2150 t.Fatal(handle, h10)2151 }2152 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {2153 t.Fatal(len(got), len(exp))2154 }2155 f = reaudit(t, f, name)2156 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {2157 t.Fatal(len(got), len(exp))2158 }2159 used, total, err := f.audit() // c+2, c+32160 if err != nil {2161 t.Fatal(err)2162 }2163 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 0 || free != 1 {2164 t.Fatal(difused, diftotal, free)2165 }2166}2167func TestRelocRealloc6Keep(t *testing.T) {2168 dir, name := temp()2169 defer os.RemoveAll(dir)2170 f, err := fcreate(name)2171 if err != nil {2172 t.Fatal(err)2173 }2174 defer func() {2175 if f != nil {2176 if err := f.Close(); err != nil {2177 t.Fatal(err)2178 }2179 }2180 f = nil2181 runtime.GC()2182 os.Remove(name)2183 }()2184 b := make([]byte, 61680)2185 h10 := alloc(f, b[:31])2186 h20 := alloc(f, nil)2187 _ = alloc(f, nil)2188 free(f, h20)2189 used0, total0, err := f.audit() // c+2, c+32190 if err != nil {2191 t.Fatal(err)2192 }2193 c := content(b, 10)2194 exp := c[:15]2195 if handle := realloc(f, h10, exp, true); handle != h10 {2196 t.Fatal(handle, h10)2197 }2198 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {2199 t.Fatal(len(got), len(exp))2200 }2201 f = reaudit(t, f, name)2202 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {2203 t.Fatal(len(got), len(exp))2204 }2205 used, total, err := f.audit() // c+2, c+32206 if err != nil {2207 t.Fatal(err)2208 }2209 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 0 || free != 1 {2210 t.Fatal(difused, diftotal, free)2211 }2212}2213func TestFreespaceReuse(t *testing.T) {2214 dir, name := temp()2215 defer os.RemoveAll(dir)2216 f, err := fcreate(name)2217 if err != nil {2218 t.Fatal(err)2219 }2220 defer func() {2221 if f != nil {2222 if err := f.Close(); err != nil {2223 t.Fatal(err)2224 }2225 }2226 f = nil2227 runtime.GC()2228 os.Remove(name)2229 }()2230 b := make([]byte, 61680)2231 c := content(b, 10)2232 c10 := c[0 : 0+15]2233 c20 := c[16:63]2234 c50 := c[64 : 64+15]2235 h10 := alloc(f, c10)2236 h201 := alloc(f, nil)2237 h202 := alloc(f, nil)2238 h203 := alloc(f, nil)2239 h50 := alloc(f, c50)2240 free(f, h201)2241 free(f, h202)2242 free(f, h203)2243 used0, total0, err := f.audit() // c+2, c+32244 if err != nil {2245 t.Fatal(err)2246 }2247 h20 := alloc(f, c20)2248 if got, _ := f.readUsed(h10); !bytes.Equal(got, c10) {2249 t.Fatal()2250 }2251 if got, _ := f.readUsed(h20); !bytes.Equal(got, c20) {2252 t.Fatal()2253 }2254 if got, _ := f.readUsed(h50); !bytes.Equal(got, c50) {2255 t.Fatal()2256 }2257 f = reaudit(t, f, name)2258 if got, _ := f.readUsed(h10); !bytes.Equal(got, c10) {2259 t.Fatal()2260 }2261 if got, _ := f.readUsed(h20); !bytes.Equal(got, c20) {2262 t.Fatal()2263 }2264 if got, _ := f.readUsed(h50); !bytes.Equal(got, c50) {2265 t.Fatal()2266 }2267 used, total, err := f.audit() // c+3, c+32268 if err != nil {2269 t.Fatal(err)2270 }2271 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 1 || diftotal != 0 || free != 0 {2272 t.Fatal(difused, diftotal, free)2273 }2274}2275func TestFreespaceReuse2(t *testing.T) {2276 dir, name := temp()2277 defer os.RemoveAll(dir)2278 f, err := fcreate(name)2279 if err != nil {2280 t.Fatal(err)2281 }2282 defer func() {2283 if f != nil {2284 if err := f.Close(); err != nil {2285 t.Fatal(err)2286 }2287 }2288 f = nil2289 runtime.GC()2290 os.Remove(name)2291 }()2292 b := make([]byte, 61680)2293 c := content(b, 10)2294 c10 := c[0 : 0+15]2295 c20 := c[16:47]2296 c50 := c[64 : 64+15]2297 h10 := alloc(f, c10)2298 h201 := alloc(f, nil)2299 h202 := alloc(f, nil)2300 h203 := alloc(f, nil)2301 h50 := alloc(f, c50)2302 free(f, h201)2303 free(f, h202)2304 free(f, h203)2305 used0, total0, err := f.audit() // c+2, c+32306 if err != nil {2307 t.Fatal(err)2308 }2309 h20 := alloc(f, c20)2310 if got, _ := f.readUsed(h10); !bytes.Equal(got, c10) {2311 t.Fatal()2312 }2313 if got, _ := f.readUsed(h20); !bytes.Equal(got, c20) {2314 t.Fatal()2315 }2316 if got, _ := f.readUsed(h50); !bytes.Equal(got, c50) {2317 t.Fatal()2318 }2319 f = reaudit(t, f, name)2320 if got, _ := f.readUsed(h10); !bytes.Equal(got, c10) {2321 t.Fatal()2322 }2323 if got, _ := f.readUsed(h20); !bytes.Equal(got, c20) {2324 t.Fatal()2325 }2326 if got, _ := f.readUsed(h50); !bytes.Equal(got, c50) {2327 t.Fatal()2328 }2329 used, total, err := f.audit() // c+3, c+42330 if err != nil {2331 t.Fatal(err)2332 }2333 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 1 || diftotal != 1 || free != 1 {2334 t.Fatal(difused, diftotal, free)2335 }2336}2337func testBug1(t *testing.T, swap bool) {2338 // Free lists table item for size 3856 points to list of free blocks2339 // NOT of size 3856 but at least 3856.2340 dir, name := temp()2341 defer os.RemoveAll(dir)2342 f, err := fcreate(name)2343 if err != nil {2344 t.Fatal(err)2345 }2346 defer func() {2347 if f != nil {2348 if err := f.Close(); err != nil {2349 t.Fatal(err)2350 }2351 }2352 f = nil2353 runtime.GC()2354 os.Remove(name)2355 }()2356 _ = alloc(f, nil)2357 b := make([]byte, 61680)2358 f1 := alloc(f, b)2359 f2 := alloc(f, b)2360 _ = alloc(f, nil)2361 used0, total0, err := f.audit() // c+4, c+42362 if err != nil {2363 t.Fatal(err)2364 }2365 if swap {2366 f1, f2 = f2, f12367 }2368 free(f, f1)2369 free(f, f2)2370 _ = alloc(f, nil)2371 f = reaudit(t, f, name)2372 used, total, err := f.audit() // c+3, c+42373 if err != nil {2374 t.Fatal(err)2375 }2376 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != 0 || free != 1 {2377 t.Fatal(difused, diftotal, free)2378 }2379}2380func TestBug1(t *testing.T) {2381 testBug1(t, false)2382 testBug1(t, true)2383}2384func TestMix(t *testing.T) {2385 if testing.Short() {2386 t.Log("skipped")2387 return2388 }2389 const (2390 n = 1 << 102391 )2392 if testing.Short() {2393 t.Log("skipped")2394 return2395 }2396 t.Log(n)2397 dir, name := temp()2398 defer os.RemoveAll(dir)2399 f, err := fcreate(name)2400 if err != nil {2401 t.Fatal(err)2402 }2403 defer func() {2404 if f != nil {2405 if err := f.Close(); err != nil {2406 t.Fatal(err)2407 }2408 }2409 f = nil2410 runtime.GC()2411 os.Remove(name)2412 }()2413 b := make([]byte, 61680)2414 rng, err := mathutil.NewFC32(0, n-1, true)2415 if err != nil {2416 t.Fatal(err)2417 }2418 ha := make([]int64, n)2419 payload := 02420 t0 := time.Now()2421 // Alloc n block with upper half of content2422 for _ = range ha {2423 r := rng.Next()2424 c := content(b, int64(r))2425 c = c[len(c)/2:]2426 ha[r] = alloc(f, c)2427 payload += len(c)2428 }2429 dt := float64(time.Now().Sub(t0)) / 1e92430 t.Logf("write time A %.3g", dt)2431 // verify2432 f = reaudit(t, f, name)2433 t.Logf("size A %d for %d bytes (fill factor %3.1f%%)", f.atoms<<4, payload, 100*float64(payload)/float64(f.atoms<<4))2434 t0 = time.Now()2435 for _ = range ha {2436 r := rng.Next()2437 c := content(b, int64(r))2438 c = c[len(c)/2:]2439 if got, _ := f.readUsed(ha[r]); !bytes.Equal(got, c) {2440 t.Fatal()2441 }2442 }2443 dt = float64(time.Now().Sub(t0)) / 1e92444 t.Logf("read time A %.3g", dt)2445 // free half of the blocks2446 t0 = time.Now()2447 for i := 0; i < n/2; i++ {2448 free(f, ha[i])2449 ha[i] = 02450 }2451 dt = float64(time.Now().Sub(t0)) / 1e92452 t.Logf("free time A %.3g", dt)2453 // verify2454 f = reaudit(t, f, name)2455 t.Logf("size B %d (freeing half of the blocks)", f.atoms<<4)2456 t0 = time.Now()2457 for _ = range ha {2458 r := rng.Next()2459 h := ha[r]2460 if h == 0 {2461 continue2462 }2463 c := content(b, int64(r))2464 c = c[len(c)/2:]2465 if got, _ := f.readUsed(h); !bytes.Equal(got, c) {2466 t.Fatal()2467 }2468 }2469 dt = float64(time.Now().Sub(t0)) / 1e92470 t.Logf("read time B %.3g", dt)2471 // reloc extend2472 t0 = time.Now()2473 for _ = range ha {2474 r := rng.Next()2475 h := ha[r]2476 if h == 0 {2477 continue2478 }2479 c := content(b, int64(r))2480 //f = reaudit(t, f, name)2481 if h2 := realloc(f, h, c, true); h2 != h {2482 t.Fatal()2483 }2484 }2485 dt = float64(time.Now().Sub(t0)) / 1e92486 t.Logf("realoc time B %.3g", dt)2487 // verify2488 f = reaudit(t, f, name)2489 t.Logf("size C %d for %d bytes (reallocated all used blocks to double size, fill factor %3.1f%%", f.atoms<<4, payload, 100*float64(payload)/float64(f.atoms<<4))2490 t0 = time.Now()2491 for _ = range ha {2492 r := rng.Next()2493 h := ha[r]2494 if h == 0 {2495 continue2496 }2497 c := content(b, int64(r))2498 if got, _ := f.readUsed(ha[r]); !bytes.Equal(got, c) {2499 t.Fatal()2500 }2501 }2502 dt = float64(time.Now().Sub(t0)) / 1e92503 t.Logf("read time C %.3g", dt)2504}...
search_aggs_pipeline_test.go
Source:search_aggs_pipeline_test.go
...11 client := setupTestClientAndCreateIndexAndAddDocs(t)1213 esversion, err := client.ElasticsearchVersion(DefaultURL)14 if err != nil {15 t.Fatal(err)16 }1718 if esversion < "2.0" {19 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)20 return21 }2223 // Match all should return all documents24 builder := client.Search().25 Index(testIndexName).26 Type("order").27 Query(NewMatchAllQuery()).28 Pretty(true)29 h := NewDateHistogramAggregation().Field("time").Interval("month")30 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))31 builder = builder.Aggregation("sales_per_month", h)32 builder = builder.Aggregation("avg_monthly_sales", NewAvgBucketAggregation().BucketsPath("sales_per_month>sales"))3334 res, err := builder.Do()35 if err != nil {36 t.Fatal(err)37 }38 if res.Hits == nil {39 t.Errorf("expected Hits != nil; got: nil")40 }4142 aggs := res.Aggregations43 if aggs == nil {44 t.Fatal("expected aggregations != nil; got: nil")45 }4647 agg, found := aggs.AvgBucket("avg_monthly_sales")48 if !found {49 t.Fatal("expected avg_monthly_sales aggregation")50 }51 if agg == nil {52 t.Fatal("expected avg_monthly_sales aggregation")53 }54 if agg.Value == nil {55 t.Fatal("expected avg_monthly_sales.value != nil")56 }57 if got, want := *agg.Value, float64(939.2); got != want {58 t.Fatalf("expected avg_monthly_sales.value=%v; got: %v", want, got)59 }60}6162func TestAggsIntegrationDerivative(t *testing.T) {63 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))64 client := setupTestClientAndCreateIndexAndAddDocs(t)6566 esversion, err := client.ElasticsearchVersion(DefaultURL)67 if err != nil {68 t.Fatal(err)69 }7071 if esversion < "2.0" {72 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)73 return74 }7576 // Match all should return all documents77 builder := client.Search().78 Index(testIndexName).79 Type("order").80 Query(NewMatchAllQuery()).81 Pretty(true)82 h := NewDateHistogramAggregation().Field("time").Interval("month")83 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))84 h = h.SubAggregation("sales_deriv", NewDerivativeAggregation().BucketsPath("sales"))85 builder = builder.Aggregation("sales_per_month", h)8687 res, err := builder.Do()88 if err != nil {89 t.Fatal(err)90 }91 if res.Hits == nil {92 t.Errorf("expected Hits != nil; got: nil")93 }9495 aggs := res.Aggregations96 if aggs == nil {97 t.Fatal("expected aggregations != nil; got: nil")98 }99100 agg, found := aggs.DateHistogram("sales_per_month")101 if !found {102 t.Fatal("expected sales_per_month aggregation")103 }104 if agg == nil {105 t.Fatal("expected sales_per_month aggregation")106 }107 if got, want := len(agg.Buckets), 6; got != want {108 t.Fatalf("expected %d buckets; got: %d", want, got)109 }110111 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {112 t.Fatalf("expected DocCount=%d; got: %d", want, got)113 }114 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {115 t.Fatalf("expected DocCount=%d; got: %d", want, got)116 }117 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {118 t.Fatalf("expected DocCount=%d; got: %d", want, got)119 }120 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {121 t.Fatalf("expected DocCount=%d; got: %d", want, got)122 }123 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {124 t.Fatalf("expected DocCount=%d; got: %d", want, got)125 }126 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {127 t.Fatalf("expected DocCount=%d; got: %d", want, got)128 }129130 d, found := agg.Buckets[0].Derivative("sales_deriv")131 if found {132 t.Fatal("expected no sales_deriv aggregation")133 }134 if d != nil {135 t.Fatal("expected no sales_deriv aggregation")136 }137138 d, found = agg.Buckets[1].Derivative("sales_deriv")139 if !found {140 t.Fatal("expected sales_deriv aggregation")141 }142 if d == nil {143 t.Fatal("expected sales_deriv aggregation")144 }145 if d.Value != nil {146 t.Fatal("expected sales_deriv value == nil")147 }148149 d, found = agg.Buckets[2].Derivative("sales_deriv")150 if !found {151 t.Fatal("expected sales_deriv aggregation")152 }153 if d == nil {154 t.Fatal("expected sales_deriv aggregation")155 }156 if d.Value != nil {157 t.Fatal("expected sales_deriv value == nil")158 }159160 d, found = agg.Buckets[3].Derivative("sales_deriv")161 if !found {162 t.Fatal("expected sales_deriv aggregation")163 }164 if d == nil {165 t.Fatal("expected sales_deriv aggregation")166 }167 if d.Value == nil {168 t.Fatal("expected sales_deriv value != nil")169 }170 if got, want := *d.Value, float64(2348.0); got != want {171 t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)172 }173174 d, found = agg.Buckets[4].Derivative("sales_deriv")175 if !found {176 t.Fatal("expected sales_deriv aggregation")177 }178 if d == nil {179 t.Fatal("expected sales_deriv aggregation")180 }181 if d.Value == nil {182 t.Fatal("expected sales_deriv value != nil")183 }184 if got, want := *d.Value, float64(-1658.0); got != want {185 t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)186 }187188 d, found = agg.Buckets[5].Derivative("sales_deriv")189 if !found {190 t.Fatal("expected sales_deriv aggregation")191 }192 if d == nil {193 t.Fatal("expected sales_deriv aggregation")194 }195 if d.Value == nil {196 t.Fatal("expected sales_deriv value != nil")197 }198 if got, want := *d.Value, float64(-722.0); got != want {199 t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)200 }201}202203func TestAggsIntegrationMaxBucket(t *testing.T) {204 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))205 client := setupTestClientAndCreateIndexAndAddDocs(t)206207 esversion, err := client.ElasticsearchVersion(DefaultURL)208 if err != nil {209 t.Fatal(err)210 }211212 if esversion < "2.0" {213 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)214 return215 }216217 // Match all should return all documents218 builder := client.Search().219 Index(testIndexName).220 Type("order").221 Query(NewMatchAllQuery()).222 Pretty(true)223 h := NewDateHistogramAggregation().Field("time").Interval("month")224 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))225 builder = builder.Aggregation("sales_per_month", h)226 builder = builder.Aggregation("max_monthly_sales", NewMaxBucketAggregation().BucketsPath("sales_per_month>sales"))227228 res, err := builder.Do()229 if err != nil {230 t.Fatal(err)231 }232 if res.Hits == nil {233 t.Errorf("expected Hits != nil; got: nil")234 }235236 aggs := res.Aggregations237 if aggs == nil {238 t.Fatal("expected aggregations != nil; got: nil")239 }240241 agg, found := aggs.MaxBucket("max_monthly_sales")242 if !found {243 t.Fatal("expected max_monthly_sales aggregation")244 }245 if agg == nil {246 t.Fatal("expected max_monthly_sales aggregation")247 }248 if got, want := len(agg.Keys), 1; got != want {249 t.Fatalf("expected len(max_monthly_sales.keys)=%d; got: %d", want, got)250 }251 if got, want := agg.Keys[0], "2015-04-01"; got != want {252 t.Fatalf("expected max_monthly_sales.keys[0]=%v; got: %v", want, got)253 }254 if agg.Value == nil {255 t.Fatal("expected max_monthly_sales.value != nil")256 }257 if got, want := *agg.Value, float64(2448); got != want {258 t.Fatalf("expected max_monthly_sales.value=%v; got: %v", want, got)259 }260}261262func TestAggsIntegrationMinBucket(t *testing.T) {263 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))264 client := setupTestClientAndCreateIndexAndAddDocs(t)265266 esversion, err := client.ElasticsearchVersion(DefaultURL)267 if err != nil {268 t.Fatal(err)269 }270271 if esversion < "2.0" {272 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)273 return274 }275276 // Match all should return all documents277 builder := client.Search().278 Index(testIndexName).279 Type("order").280 Query(NewMatchAllQuery()).281 Pretty(true)282 h := NewDateHistogramAggregation().Field("time").Interval("month")283 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))284 builder = builder.Aggregation("sales_per_month", h)285 builder = builder.Aggregation("min_monthly_sales", NewMinBucketAggregation().BucketsPath("sales_per_month>sales"))286287 res, err := builder.Do()288 if err != nil {289 t.Fatal(err)290 }291 if res.Hits == nil {292 t.Errorf("expected Hits != nil; got: nil")293 }294295 aggs := res.Aggregations296 if aggs == nil {297 t.Fatal("expected aggregations != nil; got: nil")298 }299300 agg, found := aggs.MinBucket("min_monthly_sales")301 if !found {302 t.Fatal("expected min_monthly_sales aggregation")303 }304 if agg == nil {305 t.Fatal("expected min_monthly_sales aggregation")306 }307 if got, want := len(agg.Keys), 1; got != want {308 t.Fatalf("expected len(min_monthly_sales.keys)=%d; got: %d", want, got)309 }310 if got, want := agg.Keys[0], "2015-06-01"; got != want {311 t.Fatalf("expected min_monthly_sales.keys[0]=%v; got: %v", want, got)312 }313 if agg.Value == nil {314 t.Fatal("expected min_monthly_sales.value != nil")315 }316 if got, want := *agg.Value, float64(68); got != want {317 t.Fatalf("expected min_monthly_sales.value=%v; got: %v", want, got)318 }319}320321func TestAggsIntegrationSumBucket(t *testing.T) {322 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))323 client := setupTestClientAndCreateIndexAndAddDocs(t)324325 esversion, err := client.ElasticsearchVersion(DefaultURL)326 if err != nil {327 t.Fatal(err)328 }329330 if esversion < "2.0" {331 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)332 return333 }334335 // Match all should return all documents336 builder := client.Search().337 Index(testIndexName).338 Type("order").339 Query(NewMatchAllQuery()).340 Pretty(true)341 h := NewDateHistogramAggregation().Field("time").Interval("month")342 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))343 builder = builder.Aggregation("sales_per_month", h)344 builder = builder.Aggregation("sum_monthly_sales", NewSumBucketAggregation().BucketsPath("sales_per_month>sales"))345346 res, err := builder.Do()347 if err != nil {348 t.Fatal(err)349 }350 if res.Hits == nil {351 t.Errorf("expected Hits != nil; got: nil")352 }353354 aggs := res.Aggregations355 if aggs == nil {356 t.Fatal("expected aggregations != nil; got: nil")357 }358359 agg, found := aggs.SumBucket("sum_monthly_sales")360 if !found {361 t.Fatal("expected sum_monthly_sales aggregation")362 }363 if agg == nil {364 t.Fatal("expected sum_monthly_sales aggregation")365 }366 if agg.Value == nil {367 t.Fatal("expected sum_monthly_sales.value != nil")368 }369 if got, want := *agg.Value, float64(4696.0); got != want {370 t.Fatalf("expected sum_monthly_sales.value=%v; got: %v", want, got)371 }372}373374func TestAggsIntegrationMovAvg(t *testing.T) {375 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))376 client := setupTestClientAndCreateIndexAndAddDocs(t)377378 esversion, err := client.ElasticsearchVersion(DefaultURL)379 if err != nil {380 t.Fatal(err)381 }382383 if esversion < "2.0" {384 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)385 return386 }387388 // Match all should return all documents389 builder := client.Search().390 Index(testIndexName).391 Type("order").392 Query(NewMatchAllQuery()).393 Pretty(true)394 h := NewDateHistogramAggregation().Field("time").Interval("month")395 h = h.SubAggregation("the_sum", NewSumAggregation().Field("price"))396 h = h.SubAggregation("the_movavg", NewMovAvgAggregation().BucketsPath("the_sum"))397 builder = builder.Aggregation("my_date_histo", h)398399 res, err := builder.Do()400 if err != nil {401 t.Fatal(err)402 }403 if res.Hits == nil {404 t.Errorf("expected Hits != nil; got: nil")405 }406407 aggs := res.Aggregations408 if aggs == nil {409 t.Fatal("expected aggregations != nil; got: nil")410 }411412 agg, found := aggs.DateHistogram("my_date_histo")413 if !found {414 t.Fatal("expected sum_monthly_sales aggregation")415 }416 if agg == nil {417 t.Fatal("expected sum_monthly_sales aggregation")418 }419 if got, want := len(agg.Buckets), 6; got != want {420 t.Fatalf("expected %d buckets; got: %d", want, got)421 }422423 d, found := agg.Buckets[0].MovAvg("the_movavg")424 if found {425 t.Fatal("expected no the_movavg aggregation")426 }427 if d != nil {428 t.Fatal("expected no the_movavg aggregation")429 }430431 d, found = agg.Buckets[1].MovAvg("the_movavg")432 if found {433 t.Fatal("expected no the_movavg aggregation")434 }435 if d != nil {436 t.Fatal("expected no the_movavg aggregation")437 }438439 d, found = agg.Buckets[2].MovAvg("the_movavg")440 if !found {441 t.Fatal("expected the_movavg aggregation")442 }443 if d == nil {444 t.Fatal("expected the_movavg aggregation")445 }446 if d.Value == nil {447 t.Fatal("expected the_movavg value")448 }449 if got, want := *d.Value, float64(1290.0); got != want {450 t.Fatalf("expected %v buckets; got: %v", want, got)451 }452453 d, found = agg.Buckets[3].MovAvg("the_movavg")454 if !found {455 t.Fatal("expected the_movavg aggregation")456 }457 if d == nil {458 t.Fatal("expected the_movavg aggregation")459 }460 if d.Value == nil {461 t.Fatal("expected the_movavg value")462 }463 if got, want := *d.Value, float64(695.0); got != want {464 t.Fatalf("expected %v buckets; got: %v", want, got)465 }466467 d, found = agg.Buckets[4].MovAvg("the_movavg")468 if !found {469 t.Fatal("expected the_movavg aggregation")470 }471 if d == nil {472 t.Fatal("expected the_movavg aggregation")473 }474 if d.Value == nil {475 t.Fatal("expected the_movavg value")476 }477 if got, want := *d.Value, float64(1279.3333333333333); got != want {478 t.Fatalf("expected %v buckets; got: %v", want, got)479 }480481 d, found = agg.Buckets[5].MovAvg("the_movavg")482 if !found {483 t.Fatal("expected the_movavg aggregation")484 }485 if d == nil {486 t.Fatal("expected the_movavg aggregation")487 }488 if d.Value == nil {489 t.Fatal("expected the_movavg value")490 }491 if got, want := *d.Value, float64(1157.0); got != want {492 t.Fatalf("expected %v buckets; got: %v", want, got)493 }494}495496func TestAggsIntegrationCumulativeSum(t *testing.T) {497 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))498 client := setupTestClientAndCreateIndexAndAddDocs(t)499500 esversion, err := client.ElasticsearchVersion(DefaultURL)501 if err != nil {502 t.Fatal(err)503 }504505 if esversion < "2.0" {506 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)507 return508 }509510 // Match all should return all documents511 builder := client.Search().512 Index(testIndexName).513 Type("order").514 Query(NewMatchAllQuery()).515 Pretty(true)516 h := NewDateHistogramAggregation().Field("time").Interval("month")517 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))518 h = h.SubAggregation("cumulative_sales", NewCumulativeSumAggregation().BucketsPath("sales"))519 builder = builder.Aggregation("sales_per_month", h)520521 res, err := builder.Do()522 if err != nil {523 t.Fatal(err)524 }525 if res.Hits == nil {526 t.Errorf("expected Hits != nil; got: nil")527 }528529 aggs := res.Aggregations530 if aggs == nil {531 t.Fatal("expected aggregations != nil; got: nil")532 }533534 agg, found := aggs.DateHistogram("sales_per_month")535 if !found {536 t.Fatal("expected sales_per_month aggregation")537 }538 if agg == nil {539 t.Fatal("expected sales_per_month aggregation")540 }541 if got, want := len(agg.Buckets), 6; got != want {542 t.Fatalf("expected %d buckets; got: %d", want, got)543 }544545 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {546 t.Fatalf("expected DocCount=%d; got: %d", want, got)547 }548 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {549 t.Fatalf("expected DocCount=%d; got: %d", want, got)550 }551 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {552 t.Fatalf("expected DocCount=%d; got: %d", want, got)553 }554 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {555 t.Fatalf("expected DocCount=%d; got: %d", want, got)556 }557 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {558 t.Fatalf("expected DocCount=%d; got: %d", want, got)559 }560 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {561 t.Fatalf("expected DocCount=%d; got: %d", want, got)562 }563564 d, found := agg.Buckets[0].CumulativeSum("cumulative_sales")565 if !found {566 t.Fatal("expected cumulative_sales aggregation")567 }568 if d == nil {569 t.Fatal("expected cumulative_sales aggregation")570 }571 if d.Value == nil {572 t.Fatal("expected cumulative_sales value != nil")573 }574 if got, want := *d.Value, float64(1290.0); got != want {575 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)576 }577578 d, found = agg.Buckets[1].CumulativeSum("cumulative_sales")579 if !found {580 t.Fatal("expected cumulative_sales aggregation")581 }582 if d == nil {583 t.Fatal("expected cumulative_sales aggregation")584 }585 if d.Value == nil {586 t.Fatal("expected cumulative_sales value != nil")587 }588 if got, want := *d.Value, float64(1290.0); got != want {589 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)590 }591592 d, found = agg.Buckets[2].CumulativeSum("cumulative_sales")593 if !found {594 t.Fatal("expected cumulative_sales aggregation")595 }596 if d == nil {597 t.Fatal("expected cumulative_sales aggregation")598 }599 if d.Value == nil {600 t.Fatal("expected cumulative_sales value != nil")601 }602 if got, want := *d.Value, float64(1390.0); got != want {603 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)604 }605606 d, found = agg.Buckets[3].CumulativeSum("cumulative_sales")607 if !found {608 t.Fatal("expected cumulative_sales aggregation")609 }610 if d == nil {611 t.Fatal("expected cumulative_sales aggregation")612 }613 if d.Value == nil {614 t.Fatal("expected cumulative_sales value != nil")615 }616 if got, want := *d.Value, float64(3838.0); got != want {617 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)618 }619620 d, found = agg.Buckets[4].CumulativeSum("cumulative_sales")621 if !found {622 t.Fatal("expected cumulative_sales aggregation")623 }624 if d == nil {625 t.Fatal("expected cumulative_sales aggregation")626 }627 if d.Value == nil {628 t.Fatal("expected cumulative_sales value != nil")629 }630 if got, want := *d.Value, float64(4628.0); got != want {631 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)632 }633634 d, found = agg.Buckets[5].CumulativeSum("cumulative_sales")635 if !found {636 t.Fatal("expected cumulative_sales aggregation")637 }638 if d == nil {639 t.Fatal("expected cumulative_sales aggregation")640 }641 if d.Value == nil {642 t.Fatal("expected cumulative_sales value != nil")643 }644 if got, want := *d.Value, float64(4696.0); got != want {645 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)646 }647}648649func TestAggsIntegrationBucketScript(t *testing.T) {650 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))651 client := setupTestClientAndCreateIndexAndAddDocs(t)652653 esversion, err := client.ElasticsearchVersion(DefaultURL)654 if err != nil {655 t.Fatal(err)656 }657658 if esversion < "2.0" {659 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)660 return661 }662663 // Match all should return all documents664 builder := client.Search().665 Index(testIndexName).666 Type("order").667 Query(NewMatchAllQuery()).668 Pretty(true)669 h := NewDateHistogramAggregation().Field("time").Interval("month")670 h = h.SubAggregation("total_sales", NewSumAggregation().Field("price"))671 appleFilter := NewFilterAggregation().Filter(NewTermQuery("manufacturer", "Apple"))672 appleFilter = appleFilter.SubAggregation("sales", NewSumAggregation().Field("price"))673 h = h.SubAggregation("apple_sales", appleFilter)674 h = h.SubAggregation("apple_percentage",675 NewBucketScriptAggregation().676 GapPolicy("insert_zeros").677 AddBucketsPath("appleSales", "apple_sales>sales").678 AddBucketsPath("totalSales", "total_sales").679 Script(NewScript("appleSales / totalSales * 100")))680 builder = builder.Aggregation("sales_per_month", h)681682 res, err := builder.Do()683 if err != nil {684 t.Fatalf("%v (maybe scripting is disabled?)", err)685 }686 if res.Hits == nil {687 t.Errorf("expected Hits != nil; got: nil")688 }689690 aggs := res.Aggregations691 if aggs == nil {692 t.Fatal("expected aggregations != nil; got: nil")693 }694695 agg, found := aggs.DateHistogram("sales_per_month")696 if !found {697 t.Fatal("expected sales_per_month aggregation")698 }699 if agg == nil {700 t.Fatal("expected sales_per_month aggregation")701 }702 if got, want := len(agg.Buckets), 6; got != want {703 t.Fatalf("expected %d buckets; got: %d", want, got)704 }705706 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {707 t.Fatalf("expected DocCount=%d; got: %d", want, got)708 }709 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {710 t.Fatalf("expected DocCount=%d; got: %d", want, got)711 }712 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {713 t.Fatalf("expected DocCount=%d; got: %d", want, got)714 }715 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {716 t.Fatalf("expected DocCount=%d; got: %d", want, got)717 }718 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {719 t.Fatalf("expected DocCount=%d; got: %d", want, got)720 }721 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {722 t.Fatalf("expected DocCount=%d; got: %d", want, got)723 }724725 d, found := agg.Buckets[0].BucketScript("apple_percentage")726 if !found {727 t.Fatal("expected apple_percentage aggregation")728 }729 if d == nil {730 t.Fatal("expected apple_percentage aggregation")731 }732 if d.Value == nil {733 t.Fatal("expected apple_percentage value != nil")734 }735 if got, want := *d.Value, float64(100.0); got != want {736 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)737 }738739 d, found = agg.Buckets[1].BucketScript("apple_percentage")740 if !found {741 t.Fatal("expected apple_percentage aggregation")742 }743 if d == nil {744 t.Fatal("expected apple_percentage aggregation")745 }746 if d.Value != nil {747 t.Fatal("expected apple_percentage value == nil")748 }749750 d, found = agg.Buckets[2].BucketScript("apple_percentage")751 if !found {752 t.Fatal("expected apple_percentage aggregation")753 }754 if d == nil {755 t.Fatal("expected apple_percentage aggregation")756 }757 if d.Value == nil {758 t.Fatal("expected apple_percentage value != nil")759 }760 if got, want := *d.Value, float64(0.0); got != want {761 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)762 }763764 d, found = agg.Buckets[3].BucketScript("apple_percentage")765 if !found {766 t.Fatal("expected apple_percentage aggregation")767 }768 if d == nil {769 t.Fatal("expected apple_percentage aggregation")770 }771 if d.Value == nil {772 t.Fatal("expected apple_percentage value != nil")773 }774 if got, want := *d.Value, float64(34.64052287581699); got != want {775 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)776 }777778 d, found = agg.Buckets[4].BucketScript("apple_percentage")779 if !found {780 t.Fatal("expected apple_percentage aggregation")781 }782 if d == nil {783 t.Fatal("expected apple_percentage aggregation")784 }785 if d.Value == nil {786 t.Fatal("expected apple_percentage value != nil")787 }788 if got, want := *d.Value, float64(0.0); got != want {789 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)790 }791792 d, found = agg.Buckets[5].BucketScript("apple_percentage")793 if !found {794 t.Fatal("expected apple_percentage aggregation")795 }796 if d == nil {797 t.Fatal("expected apple_percentage aggregation")798 }799 if d.Value == nil {800 t.Fatal("expected apple_percentage value != nil")801 }802 if got, want := *d.Value, float64(0.0); got != want {803 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)804 }805}806807func TestAggsIntegrationBucketSelector(t *testing.T) {808 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))809 client := setupTestClientAndCreateIndexAndAddDocs(t)810811 esversion, err := client.ElasticsearchVersion(DefaultURL)812 if err != nil {813 t.Fatal(err)814 }815816 if esversion < "2.0" {817 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)818 return819 }820821 // Match all should return all documents822 builder := client.Search().823 Index(testIndexName).824 Type("order").825 Query(NewMatchAllQuery()).826 Pretty(true)827 h := NewDateHistogramAggregation().Field("time").Interval("month")828 h = h.SubAggregation("total_sales", NewSumAggregation().Field("price"))829 h = h.SubAggregation("sales_bucket_filter",830 NewBucketSelectorAggregation().831 AddBucketsPath("totalSales", "total_sales").832 Script(NewScript("totalSales <= 100")))833 builder = builder.Aggregation("sales_per_month", h)834835 res, err := builder.Do()836 if err != nil {837 t.Fatalf("%v (maybe scripting is disabled?)", err)838 }839 if res.Hits == nil {840 t.Errorf("expected Hits != nil; got: nil")841 }842843 aggs := res.Aggregations844 if aggs == nil {845 t.Fatal("expected aggregations != nil; got: nil")846 }847848 agg, found := aggs.DateHistogram("sales_per_month")849 if !found {850 t.Fatal("expected sales_per_month aggregation")851 }852 if agg == nil {853 t.Fatal("expected sales_per_month aggregation")854 }855 if got, want := len(agg.Buckets), 2; got != want {856 t.Fatalf("expected %d buckets; got: %d", want, got)857 }858859 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {860 t.Fatalf("expected DocCount=%d; got: %d", want, got)861 }862 if got, want := agg.Buckets[1].DocCount, int64(2); got != want {863 t.Fatalf("expected DocCount=%d; got: %d", want, got)864 }865}866867func TestAggsIntegrationSerialDiff(t *testing.T) {868 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))869 client := setupTestClientAndCreateIndexAndAddDocs(t)870871 esversion, err := client.ElasticsearchVersion(DefaultURL)872 if err != nil {873 t.Fatal(err)874 }875876 if esversion < "2.0" {877 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)878 return879 }880881 // Match all should return all documents882 builder := client.Search().883 Index(testIndexName).884 Type("order").885 Query(NewMatchAllQuery()).886 Pretty(true)887 h := NewDateHistogramAggregation().Field("time").Interval("month")888 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))889 h = h.SubAggregation("the_diff", NewSerialDiffAggregation().BucketsPath("sales").Lag(1))890 builder = builder.Aggregation("sales_per_month", h)891892 res, err := builder.Do()893 if err != nil {894 t.Fatal(err)895 }896 if res.Hits == nil {897 t.Errorf("expected Hits != nil; got: nil")898 }899900 aggs := res.Aggregations901 if aggs == nil {902 t.Fatal("expected aggregations != nil; got: nil")903 }904905 agg, found := aggs.DateHistogram("sales_per_month")906 if !found {907 t.Fatal("expected sales_per_month aggregation")908 }909 if agg == nil {910 t.Fatal("expected sales_per_month aggregation")911 }912 if got, want := len(agg.Buckets), 6; got != want {913 t.Fatalf("expected %d buckets; got: %d", want, got)914 }915916 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {917 t.Fatalf("expected DocCount=%d; got: %d", want, got)918 }919 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {920 t.Fatalf("expected DocCount=%d; got: %d", want, got)921 }922 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {923 t.Fatalf("expected DocCount=%d; got: %d", want, got)924 }925 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {926 t.Fatalf("expected DocCount=%d; got: %d", want, got)927 }928 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {929 t.Fatalf("expected DocCount=%d; got: %d", want, got)930 }931 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {932 t.Fatalf("expected DocCount=%d; got: %d", want, got)933 }934935 d, found := agg.Buckets[0].SerialDiff("the_diff")936 if found {937 t.Fatal("expected no the_diff aggregation")938 }939 if d != nil {940 t.Fatal("expected no the_diff aggregation")941 }942943 d, found = agg.Buckets[1].SerialDiff("the_diff")944 if found {945 t.Fatal("expected no the_diff aggregation")946 }947 if d != nil {948 t.Fatal("expected no the_diff aggregation")949 }950951 d, found = agg.Buckets[2].SerialDiff("the_diff")952 if found {953 t.Fatal("expected no the_diff aggregation")954 }955 if d != nil {956 t.Fatal("expected no the_diff aggregation")957 }958959 d, found = agg.Buckets[3].SerialDiff("the_diff")960 if !found {961 t.Fatal("expected the_diff aggregation")962 }963 if d == nil {964 t.Fatal("expected the_diff aggregation")965 }966 if d.Value == nil {967 t.Fatal("expected the_diff value != nil")968 }969 if got, want := *d.Value, float64(2348.0); got != want {970 t.Fatalf("expected the_diff.value=%v; got: %v", want, got)971 }972973 d, found = agg.Buckets[4].SerialDiff("the_diff")974 if !found {975 t.Fatal("expected the_diff aggregation")976 }977 if d == nil {978 t.Fatal("expected the_diff aggregation")979 }980 if d.Value == nil {981 t.Fatal("expected the_diff value != nil")982 }983 if got, want := *d.Value, float64(-1658.0); got != want {984 t.Fatalf("expected the_diff.value=%v; got: %v", want, got)985 }986987 d, found = agg.Buckets[5].SerialDiff("the_diff")988 if !found {989 t.Fatal("expected the_diff aggregation")990 }991 if d == nil {992 t.Fatal("expected the_diff aggregation")993 }994 if d.Value == nil {995 t.Fatal("expected the_diff value != nil")996 }997 if got, want := *d.Value, float64(-722.0); got != want {998 t.Fatalf("expected the_diff.value=%v; got: %v", want, got)999 }1000}
...
flow_test.go
Source:flow_test.go
...25}26func TestFlowAdd(t *testing.T) {27 var f flow28 if !f.add(1) {29 t.Fatal("failed to add 1")30 }31 if !f.add(-1) {32 t.Fatal("failed to add -1")33 }34 if got, want := f.available(), int32(0); got != want {35 t.Fatalf("size = %d; want %d", got, want)36 }37 if !f.add(1<<31 - 1) {38 t.Fatal("failed to add 2^31-1")39 }40 if got, want := f.available(), int32(1<<31-1); got != want {41 t.Fatalf("size = %d; want %d", got, want)42 }43 if f.add(1) {44 t.Fatal("adding 1 to max shouldn't be allowed")45 }46}47func TestFlowAddOverflow(t *testing.T) {48 var f flow49 if !f.add(0) {50 t.Fatal("failed to add 0")51 }52 if !f.add(-1) {53 t.Fatal("failed to add -1")54 }55 if !f.add(0) {56 t.Fatal("failed to add 0")57 }58 if !f.add(1) {59 t.Fatal("failed to add 1")60 }61 if !f.add(1) {62 t.Fatal("failed to add 1")63 }64 if !f.add(0) {65 t.Fatal("failed to add 0")66 }67 if !f.add(-3) {68 t.Fatal("failed to add -3")69 }70 if got, want := f.available(), int32(-2); got != want {71 t.Fatalf("size = %d; want %d", got, want)72 }73 if !f.add(1<<31 - 1) {74 t.Fatal("failed to add 2^31-1")75 }76 if got, want := f.available(), int32(1+-3+(1<<31-1)); got != want {77 t.Fatalf("size = %d; want %d", got, want)78 }79}...
Fatal
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, playground")4}5import "fmt"6func main() {7 fmt.Println("Hello, playground")8}9import "fmt"10func main() {11 fmt.Println("Hello, playground")12}13import "fmt"14func main() {15 fmt.Println("Hello, playground")16}17import "fmt"18func main() {19 fmt.Println("Hello, playground")20}21import "fmt"22func main() {23 fmt.Println("Hello, playground")24}25import "fmt"26func main() {27 fmt.Println("Hello, playground")28}29import "fmt"30func main() {31 fmt.Println("Hello, playground")32}33import "fmt"34func main() {35 fmt.Println("Hello, playground")36}37import "fmt"38func main() {39 fmt.Println("Hello, playground")40}41import "fmt"42func main() {43 fmt.Println("Hello, playground")44}45import "fmt"46func main() {47 fmt.Println("Hello, playground")48}49import "fmt"50func main() {51 fmt.Println("Hello, playground")52}53import "fmt"
Fatal
Using AI Code Generation
1import (2func main() {3 fmt.Println("Starting the program")4 log.Fatal("Fatal error")5 fmt.Println("Ending the program")6}7import (8func main() {9 fmt.Println("Starting the program")10 log.Panic("Panic error")11 fmt.Println("Ending the program")12}13import (14func main() {15 fmt.Println("Starting the program")16 log.Panicln("Panic error")17 fmt.Println("Ending the program")18}19import (20func main() {21 fmt.Println("Starting the program")22 log.Print("Print error")23 fmt.Println("Ending the program")24}25import (26func main() {27 fmt.Println("Starting the program")28 log.Println("Println error")29 fmt.Println("Ending the program")30}31import (32func main() {33 fmt.Println("Starting the
Fatal
Using AI Code Generation
1import (2func main() {3 log.Fatal("Fatal error message")4 fmt.Println("This message will not print")5}6import (7func main() {8 log.Panic("Panic error message")9 fmt.Println("This message will not print")10}11import (12func main() {13 log.Panicf("Panic error message")14 fmt.Println("This message will not print")15}16import (17func main() {18 log.Fatalf("Fatal error message")19 fmt.Println("This message will not print")20}21import (22func main() {23 log.Panicln("Panic error message")24 fmt.Println("This message will not print")25}26import (27func main() {28 log.Fatalln("Fatal error message")29 fmt.Println("This message will not print")30}31import (32func main() {33 log.Print("Print error message")34 fmt.Println("This message will print")35}36import (37func main() {38 log.Printf("Print error message")39 fmt.Println("This message will print")40}41import (42func main() {43 log.Println("Print error message")44 fmt.Println("This message will print")45}46import (47func main() {48 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
Fatal
Using AI Code Generation
1import "log"2func main() {3 log.Fatal("This is a fatal error")4}5import "log"6func main() {7 log.Panic("This is a panic error")8}9import "log"10func main() {11 log.Print("This is a print error")12}13import "log"14func main() {15 log.Printf("This is a %s error", "printf")16}17import "log"18func main() {19 log.Println("This is a println error")20}21import "log"22func main() {23 log.Fatal("This is a fatal error")24}
Fatal
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, world.")4}5import "fmt"6func main() {7 fmt.Println("Hello, world.")8}9main.main()10import "fmt"11func main() {12 fmt.Println("Hello, world.")13}14import "fmt"15func main() {16 defer fmt.Println("world")17 fmt.Println("Hello,")18}
Fatal
Using AI Code Generation
1import "log"2func main() {3log.Fatal("Fatal message")4}5import "log"6func main() {7log.Fatalf("%s","Fatal message")8}9import "log"10func main() {11log.Fatalln("Fatal message")12}13import "log"14func main() {15log.Print("Print message")16}17import "log"18func main() {19log.Printf("%s","Print message")20}21import "log"22func main() {23log.Println("Print message")24}25import "log"26func main() {27log.Panic("Panic message")28}29log.Panic(0xc42004ff78, 0x1, 0x1)30main.main()31runtime.goexit()
Fatal
Using AI Code Generation
1import (2func main() {3log.Fatal("Fatal error")4}5import (6func main() {7log.Panic("Panic error")8}9log.Panic(0xc0000a5f00, 0x1, 0x1)10main.main()11import (12func main() {13log.Print("Print error")14}15import (16func main() {17log.Printf("Printf error")18}19import (20func main() {21log.Println("Println error")22}23import (24func main() {25log.Fatalf("Fatalf error")26}27import (28func main() {29log.Panicf("Panicf error")30}31log.Panicf(0x4a3c0f, 0xc, 0x0, 0x0, 0x0)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!