// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package build

import (
	"bytes"
	"testing"
)

// We take the smallest, largest and an arbitrary value for each 
// of the UTF-8 sequence lengths.
var testRunes = []rune{
	0x01, 0x0C, 0x7F, // 1-byte sequences
	0x80, 0x100, 0x7FF, // 2-byte sequences
	0x800, 0x999, 0xFFFF, // 3-byte sequences
	0x10000, 0x10101, 0x10FFFF, // 4-byte sequences
	0x200, 0x201, 0x202, 0x210, 0x215, // five entries in one sparse block
}

func makeTestTrie(t *testing.T) trie {
	n := newNode()
	for i, r := range testRunes {
		n.insert(r, uint32(i))
	}
	tr, err := n.generate()
	if err != nil {
		t.Errorf(err.Error())
	}
	return *tr
}

func TestGenerateTrie(t *testing.T) {
	testdata := makeTestTrie(t)
	buf := &bytes.Buffer{}
	testdata.print(buf, "test")
	if output != buf.String() {
		t.Errorf("output differs")
	}
}

var output = `// testValues: 832 entries, 3328 bytes
// Block 2 is the null block.
var testValues = [832]uint32 {
	// Block 0x0, offset 0x0
	0x000c:0x00000001, 
	// Block 0x1, offset 0x40
	0x007f:0x00000002, 
	// Block 0x2, offset 0x80
	// Block 0x3, offset 0xc0
	0x00c0:0x00000003, 
	// Block 0x4, offset 0x100
	0x0100:0x00000004, 
	// Block 0x5, offset 0x140
	0x0140:0x0000000c, 0x0141:0x0000000d, 0x0142:0x0000000e, 
	0x0150:0x0000000f, 
	0x0155:0x00000010, 
	// Block 0x6, offset 0x180
	0x01bf:0x00000005, 
	// Block 0x7, offset 0x1c0
	0x01c0:0x00000006, 
	// Block 0x8, offset 0x200
	0x0219:0x00000007, 
	// Block 0x9, offset 0x240
	0x027f:0x00000008, 
	// Block 0xa, offset 0x280
	0x0280:0x00000009, 
	// Block 0xb, offset 0x2c0
	0x02c1:0x0000000a, 
	// Block 0xc, offset 0x300
	0x033f:0x0000000b, 
}

// testLookup: 640 entries, 1280 bytes
// Block 0 is the null block.
var testLookup = [640]uint16 {
	// Block 0x0, offset 0x0
	// Block 0x1, offset 0x40
	// Block 0x2, offset 0x80
	// Block 0x3, offset 0xc0
	0x0c2:0x03, 0x0c4:0x04, 
	0x0c8:0x05, 
	0x0df:0x06, 
	0x0e0:0x04, 
	0x0ef:0x05, 
	0x0f0:0x07, 0x0f4:0x09, 
	// Block 0x4, offset 0x100
	0x120:0x07, 0x126:0x08, 
	// Block 0x5, offset 0x140
	0x17f:0x09, 
	// Block 0x6, offset 0x180
	0x180:0x0a, 0x184:0x0b, 
	// Block 0x7, offset 0x1c0
	0x1d0:0x06, 
	// Block 0x8, offset 0x200
	0x23f:0x0c, 
	// Block 0x9, offset 0x240
	0x24f:0x08, 
}

var testTrie = trie{ testLookup[:], testValues[:]}
`
