Beispiel #1
0
func TestMarkerPQCallbackOnAdd(t *testing.T) {
	mpq1 := bufio.NewMarkerPQ(23)

	callback_ran := false
	callback_offset := -1
	mpq1.SetCallback(func(offset int) {
		callback_ran = true
		callback_offset = offset
	})

	mpq2 := bufio.NewMarkerPQ(42)
	mpq1.Add(mpq2)
	if callback_ran {
		t.Fatal("callback ran on adding a higher offset")
	}

	mpq3 := bufio.NewMarkerPQ(5)
	mpq1.Add(mpq3)
	if !callback_ran {
		t.Fatal("callback didn't run on adding a lower offset")
	}
	if callback_offset != 5 {
		t.Fatal("callback has been given wrong offset")
	}
}
Beispiel #2
0
func TestMarkerPQDoubleMemberPanic(t *testing.T) {
	mpq1 := bufio.NewMarkerPQ(5)
	mpq2 := bufio.NewMarkerPQ(23)
	mpq3 := bufio.NewMarkerPQ(42)

	mpq1.Add(mpq3)

	defer func() {
		if recover() == nil {
			t.Fatal("adding a MarkerPQ to 2 other didn't panic")
		}
	}()
	mpq2.Add(mpq3)
}
Beispiel #3
0
func TestMarkerPQDoubleRemovePanic(t *testing.T) {
	mpq1 := bufio.NewMarkerPQ(23)
	mpq2 := bufio.NewMarkerPQ(42)

	mpq1.Add(mpq2)
	mpq1.Remove(mpq2)

	defer func() {
		if recover() == nil {
			t.Fatal("removing a MarkerPQ twice didn't panic")
		}
	}()
	mpq1.Remove(mpq2)
}
Beispiel #4
0
func TestMarkerPQCreation(t *testing.T) {
	mpq := bufio.NewMarkerPQ(23)

	if mpq == nil {
		t.Fatal("MarkerPQ is nil")
	}

	if mpq.Len() != 1 {
		t.Fatal("MarkerPQ does not contain exactly 1 marker")
	}

	if mpq.Offset() != 23 {
		t.Fatal("MarkerPQ has offset != 23")
	}

	if mpq.Minimum() != 23 {
		t.Fatal("MarkerPQ has minimum != 23")
	}

	mpq.SetOffset(42)

	if mpq.Offset() != 42 {
		t.Fatal("MarkerPQ has offset != 42")
	}

	if mpq.Minimum() != 42 {
		t.Fatal("MarkerPQ has minimum != 42")
	}
}
Beispiel #5
0
func TestMarkerPQWrongRemovePanic(t *testing.T) {
	mpq1 := bufio.NewMarkerPQ(23)
	mpq2 := bufio.NewMarkerPQ(42)
	mpq1.Add(mpq2)

	mpq3 := bufio.NewMarkerPQ(23)
	mpq4 := bufio.NewMarkerPQ(42)
	mpq3.Add(mpq4)

	defer func() {
		if recover() == nil {
			t.Fatal("removing MarkerPQ from wrong one didn't panic")
		}
	}()
	mpq1.Remove(mpq4)
}
Beispiel #6
0
func TestMarkerPQDoubleCallbackPanic(t *testing.T) {
	mpq := bufio.NewMarkerPQ(0)

	mpq.SetCallback(func(offset int) {})

	defer func() {
		if recover() == nil {
			t.Fatal("setting 2 callbacks didn't panic")
		}
	}()
	mpq.SetCallback(func(offset int) {})
}
Beispiel #7
0
func TestMarkerPQCallbackOnRemove(t *testing.T) {
	mpq1 := bufio.NewMarkerPQ(23)

	callback_ran := false
	callback_offset := -1
	mpq1.SetCallback(func(offset int) {
		callback_ran = true
		callback_offset = offset
	})

	mpq2 := bufio.NewMarkerPQ(23)
	mpq1.Add(mpq2)
	if callback_ran {
		t.Fatal("callback ran on adding an unchanged offset")
	}

	mpq3 := bufio.NewMarkerPQ(23)
	mpq1.Add(mpq3)
	if callback_ran {
		t.Fatal("callback ran on adding an unchanged offset")
	}

	mpq1.SetOffset(42)
	if callback_ran {
		t.Fatal("callback ran while minimum should have remained unchanged")
	}

	mpq1.Remove(mpq3)
	if callback_ran {
		t.Fatal("callback ran while minimum should have remained unchanged")
	}

	mpq1.Remove(mpq2)
	if !callback_ran {
		t.Fatal("callback didn't run on supposedly changed minimum")
	}
	if callback_offset != 42 {
		t.Fatal("callback has been given wrong offset")
	}
}
Beispiel #8
0
func TestMarkerPQCallbackOnDirectSetOffset(t *testing.T) {
	mpq := bufio.NewMarkerPQ(23)

	callback_ran := false
	callback_offset := -1
	mpq.SetCallback(func(offset int) {
		callback_ran = true
		callback_offset = offset
	})

	mpq.SetOffset(23)
	if callback_ran {
		t.Fatal("callback ran on unchanged offset")
	}

	mpq.SetOffset(42)
	if !callback_ran {
		t.Fatal("callback didn't run on changed offset")
	}
	if callback_offset != 42 {
		t.Fatal("callback has been given wrong offset")
	}
}
Beispiel #9
0
func TestMarkerPQDeepSorting(t *testing.T) {
	r := rand.New(rand.NewSource(1))
	rParents := rand.New(rand.NewSource(2))

	offsets := []int{r.Intn(1000) + 499500}
	master := bufio.NewMarkerPQ(offsets[0])
	mpqs := []*bufio.MarkerPQ{master}
	parents := []*bufio.MarkerPQ{nil}

	callback_ran := false
	callback_offset := -1
	master.SetCallback(func(offset int) {
		callback_ran = true
		callback_offset = offset
	})

	remove := func(candidate *bufio.MarkerPQ, parent *bufio.MarkerPQ) {
		tmp := bufio.NewMarkerPQ(candidate.Minimum())
		parent.Add(tmp)
		parent.Remove(candidate)

		for i := 1; i < len(parents); i++ {
			if parents[i] == candidate {
				candidate.Remove(mpqs[i])
				parent.Add(mpqs[i])
				parents[i] = parent
			}
		}

		if callback_ran {
			t.Fatal("callback ran while temporary MarkerPQ was in place")
		}

		parent.Remove(tmp)
	}

	oldMinimum := offsets[0]
	check := func() {
		minimum := offsets[0]
		for _, offset := range offsets[1:] {
			if offset < minimum {
				minimum = offset
			}
		}

		if minimum != master.Minimum() {
			t.Fatal("wrong minimum")
		}

		if minimum == oldMinimum {
			if callback_ran {
				t.Fatal("callback ran on supposedly unchanged minimum")
			}
			return
		}
		oldMinimum = minimum

		if !callback_ran {
			t.Fatal("callback didn't run on supposedly changed minimum")
		}
		callback_ran = false

		if callback_offset != minimum {
			t.Fatal("wrong offset handed to callback")
		}
	}
	check()

	for i := 0; i < 2626; i++ {
		offset := r.Intn(1000000)
		offsets = append(offsets, offset)
		mpq := bufio.NewMarkerPQ(offset)
		mpqs = append(mpqs, mpq)
		parent := mpqs[rParents.Intn(len(parents))]
		parents = append(parents, parent)
		parent.Add(mpq)
		check()
	}

	offsets[0] = r.Intn(1000) + 499500
	mpqs[0].SetOffset(offsets[0])
	check()

	for i := 0; i < 2626; i++ {
		offset := r.Intn(1000000)
		offsets = append(offsets, offset)
		mpq := bufio.NewMarkerPQ(offset)
		mpqs = append(mpqs, mpq)
		parent := mpqs[rParents.Intn(len(parents))]
		parents = append(parents, parent)
		parent.Add(mpq)
		check()

		m := len(mpqs) - 1
		j := r.Intn(m) + 1
		offset = r.Intn(1000000)
		offsets[j] = offset
		mpqs[j].SetOffset(offset)
		check()
	}

	offsets[0] = r.Intn(1000) + 499500
	mpqs[0].SetOffset(offsets[0])
	check()

	for i := 0; i < 2626; i++ {
		offset := r.Intn(1000000)
		offsets = append(offsets, offset)
		mpq := bufio.NewMarkerPQ(offset)
		mpqs = append(mpqs, mpq)
		parent := mpqs[rParents.Intn(len(parents))]
		parents = append(parents, parent)
		parent.Add(mpq)
		check()

		m := len(mpqs) - 1
		j := r.Intn(m) + 1
		offset = r.Intn(1000000)
		offsets[j] = offset
		mpqs[j].SetOffset(offset)
		check()

		j = r.Intn(m) + 1
		offsets[j], offsets = offsets[m], offsets[:m]
		mpq = mpqs[j]
		mpqs[j], mpqs[m], mpqs = mpqs[m], nil, mpqs[:m]
		parent = parents[j]
		parents[j], parents[m], parents = parents[m], nil, parents[:m]
		remove(mpq, parent)
		check()
	}

	offsets[0] = r.Intn(1000) + 499500
	mpqs[0].SetOffset(offsets[0])
	check()

	for i := 0; i < 2626; i++ {
		m := len(mpqs) - 1
		j := r.Intn(m) + 1
		offset := r.Intn(1000000)
		offsets[j] = offset
		mpqs[j].SetOffset(offset)
		check()

		j = r.Intn(m) + 1
		offsets[j], offsets = offsets[m], offsets[:m]
		mpq := mpqs[j]
		mpqs[j], mpqs[m], mpqs = mpqs[m], nil, mpqs[:m]
		parent := parents[j]
		parents[j], parents[m], parents = parents[m], nil, parents[:m]
		remove(mpq, parent)
		check()
	}

	offsets[0] = r.Intn(1000) + 499500
	mpqs[0].SetOffset(offsets[0])
	check()

	for i := 0; i < 2626; i++ {
		m := len(mpqs) - 1
		j := r.Intn(m) + 1
		offsets[j], offsets = offsets[m], offsets[:m]
		mpq := mpqs[j]
		mpqs[j], mpqs[m], mpqs = mpqs[m], nil, mpqs[:m]
		parent := parents[j]
		parents[j], parents[m], parents = parents[m], nil, parents[:m]
		remove(mpq, parent)
		check()
	}

	offsets[0] = r.Intn(1000) + 499500
	mpqs[0].SetOffset(offsets[0])
	check()

	if len(mpqs) != 1 || len(offsets) != 1 {
		panic("something is wrong with the test")
	}
}
Beispiel #10
0
func TestMarkerPQFlatSorting(t *testing.T) {
	r := rand.New(rand.NewSource(1))

	offsets := []int{r.Intn(1000) + 499500}
	master := bufio.NewMarkerPQ(offsets[0])
	mpqs := []*bufio.MarkerPQ{master}

	callback_ran := false
	callback_offset := -1
	master.SetCallback(func(offset int) {
		callback_ran = true
		callback_offset = offset
	})

	oldMinimum := offsets[0]
	check := func() {
		minimum := offsets[0]
		for _, offset := range offsets[1:] {
			if offset < minimum {
				minimum = offset
			}
		}

		if minimum != master.Minimum() {
			t.Fatal("wrong minimum")
		}

		if minimum == oldMinimum {
			if callback_ran {
				t.Fatal("callback ran on supposedly unchanged minimum")
			}
			return
		}
		oldMinimum = minimum

		if !callback_ran {
			t.Fatal("callback didn't run on supposedly changed minimum")
		}
		callback_ran = false

		if callback_offset != minimum {
			t.Fatal("wrong offset handed to callback")
		}
	}
	check()

	for i := 0; i < 2626; i++ {
		offset := r.Intn(1000000)
		offsets = append(offsets, offset)
		mpq := bufio.NewMarkerPQ(offset)
		mpqs = append(mpqs, mpq)
		master.Add(mpq)
		check()
	}

	offsets[0] = r.Intn(1000) + 499500
	mpqs[0].SetOffset(offsets[0])
	check()

	for i := 0; i < 2626; i++ {
		offset := r.Intn(1000000)
		offsets = append(offsets, offset)
		mpq := bufio.NewMarkerPQ(offset)
		mpqs = append(mpqs, mpq)
		master.Add(mpq)
		check()

		m := len(mpqs) - 1
		j := r.Intn(m) + 1
		offset = r.Intn(1000000)
		offsets[j] = offset
		mpqs[j].SetOffset(offset)
		check()
	}

	offsets[0] = r.Intn(1000) + 499500
	mpqs[0].SetOffset(offsets[0])
	check()

	for i := 0; i < 2626; i++ {
		offset := r.Intn(1000000)
		offsets = append(offsets, offset)
		mpq := bufio.NewMarkerPQ(offset)
		mpqs = append(mpqs, mpq)
		master.Add(mpq)
		check()

		m := len(mpqs) - 1
		j := r.Intn(m) + 1
		offset = r.Intn(1000000)
		offsets[j] = offset
		mpqs[j].SetOffset(offset)
		check()

		j = r.Intn(m) + 1
		offsets[j], offsets = offsets[m], offsets[:m]
		mpq = mpqs[j]
		mpqs[j], mpqs[m], mpqs = mpqs[m], nil, mpqs[:m]
		master.Remove(mpq)
		check()
	}

	offsets[0] = r.Intn(1000) + 499500
	mpqs[0].SetOffset(offsets[0])
	check()

	for i := 0; i < 2626; i++ {
		m := len(mpqs) - 1
		j := r.Intn(m) + 1
		offset := r.Intn(1000000)
		offsets[j] = offset
		mpqs[j].SetOffset(offset)
		check()

		j = r.Intn(m) + 1
		offsets[j], offsets = offsets[m], offsets[:m]
		mpq := mpqs[j]
		mpqs[j], mpqs[m], mpqs = mpqs[m], nil, mpqs[:m]
		master.Remove(mpq)
		check()
	}

	offsets[0] = r.Intn(1000) + 499500
	mpqs[0].SetOffset(offsets[0])
	check()

	for i := 0; i < 2626; i++ {
		m := len(mpqs) - 1
		j := r.Intn(m) + 1
		offsets[j], offsets = offsets[m], offsets[:m]
		mpq := mpqs[j]
		mpqs[j], mpqs[m], mpqs = mpqs[m], nil, mpqs[:m]
		master.Remove(mpq)
		check()
	}

	offsets[0] = r.Intn(1000) + 499500
	mpqs[0].SetOffset(offsets[0])
	check()

	if len(mpqs) != 1 || len(offsets) != 1 {
		panic("something is wrong with the test")
	}
}