PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/test/index.go

http://github.com/tav/go
Go | 225 lines | 162 code | 24 blank | 39 comment | 27 complexity | 132fb05ae060688f6b2eddfafc82b418 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // $G $D/$F.go && $L $F.$A &&
  2. // ./$A.out -pass 0 >tmp.go && $G tmp.go && $L -o $A.out1 tmp.$A && ./$A.out1 &&
  3. // ./$A.out -pass 1 >tmp.go && errchk $G -e tmp.go &&
  4. // ./$A.out -pass 2 >tmp.go && errchk $G -e tmp.go
  5. // rm -f tmp.go $A.out1
  6. // Copyright 2010 The Go Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style
  8. // license that can be found in the LICENSE file.
  9. // Generate test of index and slice bounds checks.
  10. // The output is compiled and run.
  11. package main
  12. import (
  13. "bufio"
  14. "flag"
  15. "fmt"
  16. "os"
  17. )
  18. const prolog = `
  19. package main
  20. import (
  21. "runtime"
  22. )
  23. type quad struct { x, y, z, w int }
  24. const (
  25. cj = 11
  26. ci int = 12
  27. ci32 int32 = 13
  28. ci64 int64 = 14
  29. ci64big int64 = 1<<31
  30. ci64bigger int64 = 1<<32
  31. chuge = 1<<100
  32. cnj = -2
  33. cni int = -3
  34. cni32 int32 = -4
  35. cni64 int64 = -5
  36. cni64big int64 = -1<<31
  37. cni64bigger int64 = -1<<32
  38. cnhuge = -1<<100
  39. )
  40. var j int = 20
  41. var i int = 21
  42. var i32 int32 = 22
  43. var i64 int64 = 23
  44. var i64big int64 = 1<<31
  45. var i64bigger int64 = 1<<32
  46. var huge uint64 = 1<<64 - 1
  47. var nj int = -10
  48. var ni int = -11
  49. var ni32 int32 = -12
  50. var ni64 int64 = -13
  51. var ni64big int64 = -1<<31
  52. var ni64bigger int64 = -1<<32
  53. var nhuge int64 = -1<<63
  54. var si []int = make([]int, 10)
  55. var ai [10]int
  56. var pai *[10]int = &ai
  57. var sq []quad = make([]quad, 10)
  58. var aq [10]quad
  59. var paq *[10]quad = &aq
  60. type T struct {
  61. si []int
  62. ai [10]int
  63. pai *[10]int
  64. sq []quad
  65. aq [10]quad
  66. paq *[10]quad
  67. }
  68. var t = T{si, ai, pai, sq, aq, paq}
  69. var pt = &T{si, ai, pai, sq, aq, paq}
  70. // test that f panics
  71. func test(f func(), s string) {
  72. defer func() {
  73. if err := recover(); err == nil {
  74. _, file, line, _ := runtime.Caller(2)
  75. bug()
  76. print(file, ":", line, ": ", s, " did not panic\n")
  77. }
  78. }()
  79. f()
  80. }
  81. var X interface{}
  82. func use(y interface{}) {
  83. X = y
  84. }
  85. var didBug = false
  86. func bug() {
  87. if !didBug {
  88. didBug = true
  89. println("BUG")
  90. }
  91. }
  92. func main() {
  93. `
  94. // Passes:
  95. // 0 - dynamic checks
  96. // 1 - static checks of invalid constants (cannot assign to types)
  97. // 2 - static checks of array bounds
  98. var pass = flag.Int("pass", 0, "which test (0,1,2)")
  99. func testExpr(b *bufio.Writer, expr string) {
  100. if *pass == 0 {
  101. fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
  102. } else {
  103. fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow\"\n", expr)
  104. }
  105. }
  106. func main() {
  107. b := bufio.NewWriter(os.Stdout)
  108. flag.Parse()
  109. if *pass == 0 {
  110. fmt.Fprint(b, "// $G $D/$F.go && $L $F.$A && ./$A.out\n\n")
  111. } else {
  112. fmt.Fprint(b, "// errchk $G -e $D/$F.go\n\n")
  113. }
  114. fmt.Fprint(b, prolog)
  115. var choices = [][]string{
  116. // Direct value, fetch from struct, fetch from struct pointer.
  117. // The last two cases get us to oindex_const_sudo in gsubr.c.
  118. []string{"", "t.", "pt."},
  119. // Array, pointer to array, slice.
  120. []string{"a", "pa", "s"},
  121. // Element is int, element is quad (struct).
  122. // This controls whether we end up in gsubr.c (i) or cgen.c (q).
  123. []string{"i", "q"},
  124. // Variable or constant.
  125. []string{"", "c"},
  126. // Positive or negative.
  127. []string{"", "n"},
  128. // Size of index.
  129. []string{"j", "i", "i32", "i64", "i64big", "i64bigger", "huge"},
  130. }
  131. forall(choices, func(x []string) {
  132. p, a, e, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5]
  133. // Pass: dynamic=0, static=1, 2.
  134. // Which cases should be caught statically?
  135. // Only constants, obviously.
  136. // Beyond that, must be one of these:
  137. // indexing into array or pointer to array
  138. // negative constant
  139. // large constant
  140. thisPass := 0
  141. if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge") {
  142. if i == "huge" {
  143. // Due to a detail of 6g's internals,
  144. // the huge constant errors happen in an
  145. // earlier pass than the others and inhibits
  146. // the next pass from running.
  147. // So run it as a separate check.
  148. thisPass = 1
  149. } else {
  150. thisPass = 2
  151. }
  152. }
  153. // Only print the test case if it is appropriate for this pass.
  154. if thisPass == *pass {
  155. pae := p+a+e
  156. cni := c+n+i
  157. // Index operation
  158. testExpr(b, pae + "[" + cni + "]")
  159. // Slice operation.
  160. // Low index 0 is a special case in ggen.c
  161. // so test both 0 and 1.
  162. testExpr(b, pae + "[0:" + cni + "]")
  163. testExpr(b, pae + "[1:" + cni + "]")
  164. testExpr(b, pae + "[" + cni + ":]")
  165. testExpr(b, pae + "[" + cni + ":" + cni + "]")
  166. }
  167. })
  168. fmt.Fprintln(b, "}")
  169. b.Flush()
  170. }
  171. func forall(choices [][]string, f func([]string)) {
  172. x := make([]string, len(choices))
  173. var recurse func(d int)
  174. recurse = func(d int) {
  175. if d >= len(choices) {
  176. f(x)
  177. return
  178. }
  179. for _, x[d] = range choices[d] {
  180. recurse(d+1)
  181. }
  182. }
  183. recurse(0)
  184. }