gen_test.go 1.6 KB

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