| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build generate |
| 6 |
|
| 7 |
package triegen_test |
| 8 |
|
| 9 |
// The code in this file generates captures and writes the tries generated in |
| 10 |
// the examples to data_test.go. To invoke it, run: |
| 11 |
// go test -tags=generate |
| 12 |
// |
| 13 |
// Making the generation code a "test" allows us to link in the necessary test |
| 14 |
// code. |
| 15 |
|
| 16 |
import ( |
| 17 |
"log" |
| 18 |
"os" |
| 19 |
"os/exec" |
| 20 |
) |
| 21 |
|
| 22 |
func init() { |
| 23 |
const tmpfile = "tmpout" |
| 24 |
const dstfile = "data_test.go" |
| 25 |
|
| 26 |
f, err := os.Create(tmpfile) |
| 27 |
if err != nil { |
| 28 |
log.Fatalf("Could not create output file: %v", err) |
| 29 |
} |
| 30 |
defer os.Remove(tmpfile) |
| 31 |
defer f.Close() |
| 32 |
|
| 33 |
// We exit before this function returns, regardless of success or failure, |
| 34 |
// so there's no need to save (and later restore) the existing genWriter |
| 35 |
// value. |
| 36 |
genWriter = f |
| 37 |
|
| 38 |
f.Write([]byte(header)) |
| 39 |
|
| 40 |
Example_build() |
| 41 |
ExampleGen_build() |
| 42 |
|
| 43 |
if err := exec.Command("gofmt", "-w", tmpfile).Run(); err != nil { |
| 44 |
log.Fatal(err) |
| 45 |
} |
| 46 |
os.Remove(dstfile) |
| 47 |
os.Rename(tmpfile, dstfile) |
| 48 |
|
| 49 |
os.Exit(0) |
| 50 |
} |
| 51 |
|
| 52 |
const header = `// This file is generated with "go test -tags generate". DO NOT EDIT! |
| 53 |
//go:build !generate |
| 54 |
|
| 55 |
package triegen_test |
| 56 |
` |
| 57 |
|
| 58 |
// Stubs for generated tries. These are needed as we exclude data_test.go if |
| 59 |
// the generate flag is set. This will clearly make the tests fail, but that |
| 60 |
// is okay. It allows us to bootstrap. |
| 61 |
|
| 62 |
type trie struct{} |
| 63 |
|
| 64 |
func (t *trie) lookupString(string) (uint8, int) { return 0, 1 } |
| 65 |
func (t *trie) lookupStringUnsafe(string) uint64 { return 0 } |
| 66 |
|
| 67 |
func newRandTrie(i int) *trie { return &trie{} } |
| 68 |
func newMultiTrie(i int) *trie { return &trie{} } |