You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: rate_estimator_test.go
    path of file: ./repos/restic/internal/ui/backup
    
    the code of the file until where you have to start completion: package backup

import (
	"fmt"
	"math"
	"testing"
	"time"

	rtest "github.com/restic/restic/internal/test"
)

const float64EqualityThreshold = 1e-6

func almostEqual(a, b float64) bool {
	if math.IsNaN(a) || math.IsNaN(b) {
		panic("almostEqual passed a NaN")
	}
	return math.Abs(a-b) <= float64EqualityThreshold
}

func TestEstimatorDefault(t *testing.T) {
	var start time.Time
	e := newRateEstimator(start)
	r := e.rate(start)
	rtest.Assert(t, r == 0, "e.Rate == %v, want zero", r)
	r = e.rate(start.Add(time.Hour))
	rtest.Assert(t, r == 0, "e.Rate == %v, want zero", r)
}

func TestEstimatorSimple(t *testing.T) {
	var start time.Time
	type testcase struct {
		bytes uint64
		when  time.Duration
		rate  float64
	}

	cases := []testcase{
		{0, 0, 0},
		{1, time.Secon
    
func TestBucketWidth(t *testing.T) {
	var when time.Time

	// Recording byte transfers within a bucket width's time window uses one
	// bucket.
	e := newRateEstimator(when)
	e.recordBytes(when, 1)
	e.recordBytes(when.Add(bucketWidth-time.Nanosecond), 1)
	rtest.Assert(t, e.buckets.Len() == 1, "e.buckets.Len() is %d, want 1", e.buckets.Len())

	b := e.buckets.Back().Value.(*rateBucket)
	rtest.Assert(t, b.totalBytes == 2, "b.totalBytes is %d, want 2", b.totalBytes)
	rtest.Assert(t, b.end == when.Add(bucketWidth), "b.end is %v, want %v", b.end, when.Add(bucketWidth))

	// Recording a byte outside the bucket width causes another bucket.
	e.recordBytes(when.Add(bucketWidth), 1)
	rtest.Assert(t, e.buckets.Len() == 2, "e.buckets.Len() is %d, want 2", e.buckets.Len())

	b = e.buckets.Back().Value.(*rateBucket)
	rtest.Assert(t, b.totalBytes == 1, "b.totalBytes is %d, want 1", b.totalBytes)
	rtest.Assert(t, b.end == when.Add(2*bucketWidth), "b.end is %v, want %v", b.end, when.Add(bucketWidth))

	// Recording a byte after a longer delay creates a sparse bucket list.
	e.recordBytes(when.Add(time.Hour+time.Millisecond), 7)
	rtest.Assert(t, e.buckets.Len() == 3, "e.buckets.Len() is %d, want 3", e.buckets.Len())

	b = e.buckets.Back().Value.(*rateBucket)
	rtest.Assert(t, b.totalBytes == 7, "b.totalBytes is %d, want 7", b.totalBytes)
	rtest.Equals(t, when.Add(time.Hour+time.Millisecond+time.Second), b.end)
}