File: rebalance_test.go

package info (click to toggle)
golang-vbom-util 0.0~git20180919.efcd4e0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: makefile: 3
file content (60 lines) | stat: -rw-r--r-- 1,738 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package rope

import (
	"testing"

	"github.com/bruth/assert"
)

func disableCoalesce() func() {
	// Temporarily disable auto-coalescing.
	thr := concatThreshold
	concatThreshold = 0
	return func() { concatThreshold = thr }
}

var rebalanceTestRopes []Rope

func init() {
	defer disableCoalesce()()

	rebalanceTestRopes = []Rope{
		New("a").Append(New("bc").Append(New("d").AppendString("ef"))), //.AppendString("g"),
		New("a").Append(New("bc").Append(New("d").AppendString("ef"))).AppendString("g"),
		New("a").AppendString("bcd").AppendString("efghijkl").AppendString("mno"),
		New("abc").AppendString("def").AppendString("def").AppendString("def").AppendString("def").AppendString("def").AppendString("ghiklmnopqrstuvwxyzklmnopqrstuvwxyz").AppendString("j").AppendString("j").AppendString("j").AppendString("j").AppendString("j").AppendString("klmnopqrstuvwxyz"),
	}
	//~ for i, r := range largeRopes {
	//~ for j := 0; j < 8; j++ {
	//~ r = r.Append(r)
	//~ }
	//~ largeRopes[i] = r
	//~ }
	//~ n := New("a")

	var r Rope
	for i := 0; i < 100; i++ {
		r = r.AppendString(string(' ' + i))
	}
	rebalanceTestRopes = append(rebalanceTestRopes, r)

	rebalanceTestRopes = append(rebalanceTestRopes, emptyRope, Rope{})
}

func TestRebalance(t *testing.T) {
	defer disableCoalesce()()

	for _, orig := range rebalanceTestRopes {
		origStr := orig.String()
		rebalanced := orig.Rebalance()
		rebalancedStr := rebalanced.String()

		//~ pretty.Println(orig, "(", orig.isBalanced(), ") ==> (", rebalanced.isBalanced(), ")", rebalanced)

		assert.Equal(t, origStr, rebalancedStr)
		if rebalanced.node != nil && orig.node != nil {
			assert.True(t, rebalanced.node.depth() <= orig.node.depth())
		}
		assert.True(t, rebalanced.isBalanced())
	}
}