Skip to content

Commit 6a7ef36

Browse files
griesemergopherbot
authored andcommitted
test: run range-over-integer tests without need for -goexperiment
Move the range-over-function tests into range4.go. Change-Id: Idccf30a0c7d7e8d2a17fb1c5561cf21e00506135 Reviewed-on: https://go-review.googlesource.com/c/go/+/539095 Reviewed-by: Cherry Mui <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]>
1 parent 11677d9 commit 6a7ef36

File tree

2 files changed

+329
-321
lines changed

2 files changed

+329
-321
lines changed

test/range3.go

Lines changed: 3 additions & 321 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
// run -goexperiment range
1+
// run
22

3-
// Copyright 2009 The Go Authors. All rights reserved.
3+
// Copyright 2023 The Go Authors. All rights reserved.
44
// Use of this source code is governed by a BSD-style
55
// license that can be found in the LICENSE file.
66

7-
// Test the 'for range' construct.
7+
// Test the 'for range' construct ranging over integers.
88

99
package main
1010

11-
// test range over integers
12-
1311
func testint1() {
1412
bad := false
1513
j := 0
@@ -76,325 +74,9 @@ func testint4() {
7674
}
7775
}
7876

79-
// test range over functions
80-
81-
var gj int
82-
83-
func yield4x(yield func() bool) {
84-
_ = yield() && yield() && yield() && yield()
85-
}
86-
87-
func yield4(yield func(int) bool) {
88-
_ = yield(1) && yield(2) && yield(3) && yield(4)
89-
}
90-
91-
func yield3(yield func(int) bool) {
92-
_ = yield(1) && yield(2) && yield(3)
93-
}
94-
95-
func yield2(yield func(int) bool) {
96-
_ = yield(1) && yield(2)
97-
}
98-
99-
func testfunc0() {
100-
j := 0
101-
for range yield4x {
102-
j++
103-
}
104-
if j != 4 {
105-
println("wrong count ranging over yield4x:", j)
106-
panic("testfunc0")
107-
}
108-
109-
j = 0
110-
for _ = range yield4 {
111-
j++
112-
}
113-
if j != 4 {
114-
println("wrong count ranging over yield4:", j)
115-
panic("testfunc0")
116-
}
117-
}
118-
119-
func testfunc1() {
120-
bad := false
121-
j := 1
122-
for i := range yield4 {
123-
if i != j {
124-
println("range var", i, "want", j)
125-
bad = true
126-
}
127-
j++
128-
}
129-
if j != 5 {
130-
println("wrong count ranging over f:", j)
131-
bad = true
132-
}
133-
if bad {
134-
panic("testfunc1")
135-
}
136-
}
137-
138-
func testfunc2() {
139-
bad := false
140-
j := 1
141-
var i int
142-
for i = range yield4 {
143-
if i != j {
144-
println("range var", i, "want", j)
145-
bad = true
146-
}
147-
j++
148-
}
149-
if j != 5 {
150-
println("wrong count ranging over f:", j)
151-
bad = true
152-
}
153-
if i != 4 {
154-
println("wrong final i ranging over f:", i)
155-
bad = true
156-
}
157-
if bad {
158-
panic("testfunc2")
159-
}
160-
}
161-
162-
func testfunc3() {
163-
bad := false
164-
j := 1
165-
var i int
166-
for i = range yield4 {
167-
if i != j {
168-
println("range var", i, "want", j)
169-
bad = true
170-
}
171-
j++
172-
if i == 2 {
173-
break
174-
}
175-
continue
176-
}
177-
if j != 3 {
178-
println("wrong count ranging over f:", j)
179-
bad = true
180-
}
181-
if i != 2 {
182-
println("wrong final i ranging over f:", i)
183-
bad = true
184-
}
185-
if bad {
186-
panic("testfunc3")
187-
}
188-
}
189-
190-
func testfunc4() {
191-
bad := false
192-
j := 1
193-
var i int
194-
func() {
195-
for i = range yield4 {
196-
if i != j {
197-
println("range var", i, "want", j)
198-
bad = true
199-
}
200-
j++
201-
if i == 2 {
202-
return
203-
}
204-
}
205-
}()
206-
if j != 3 {
207-
println("wrong count ranging over f:", j)
208-
bad = true
209-
}
210-
if i != 2 {
211-
println("wrong final i ranging over f:", i)
212-
bad = true
213-
}
214-
if bad {
215-
panic("testfunc3")
216-
}
217-
}
218-
219-
func func5() (int, int) {
220-
for i := range yield4 {
221-
return 10, i
222-
}
223-
panic("still here")
224-
}
225-
226-
func testfunc5() {
227-
x, y := func5()
228-
if x != 10 || y != 1 {
229-
println("wrong results", x, y, "want", 10, 1)
230-
panic("testfunc5")
231-
}
232-
}
233-
234-
func func6() (z, w int) {
235-
for i := range yield4 {
236-
z = 10
237-
w = i
238-
return
239-
}
240-
panic("still here")
241-
}
242-
243-
func testfunc6() {
244-
x, y := func6()
245-
if x != 10 || y != 1 {
246-
println("wrong results", x, y, "want", 10, 1)
247-
panic("testfunc6")
248-
}
249-
}
250-
251-
var saved []int
252-
253-
func save(x int) {
254-
saved = append(saved, x)
255-
}
256-
257-
func printslice(s []int) {
258-
print("[")
259-
for i, x := range s {
260-
if i > 0 {
261-
print(", ")
262-
}
263-
print(x)
264-
}
265-
print("]")
266-
}
267-
268-
func eqslice(s, t []int) bool {
269-
if len(s) != len(t) {
270-
return false
271-
}
272-
for i, x := range s {
273-
if x != t[i] {
274-
return false
275-
}
276-
}
277-
return true
278-
}
279-
280-
func func7() {
281-
defer save(-1)
282-
for i := range yield4 {
283-
defer save(i)
284-
}
285-
defer save(5)
286-
}
287-
288-
func checkslice(name string, saved, want []int) {
289-
if !eqslice(saved, want) {
290-
print("wrong results ")
291-
printslice(saved)
292-
print(" want ")
293-
printslice(want)
294-
print("\n")
295-
panic(name)
296-
}
297-
}
298-
299-
func testfunc7() {
300-
saved = nil
301-
func7()
302-
want := []int{5, 4, 3, 2, 1, -1}
303-
checkslice("testfunc7", saved, want)
304-
}
305-
306-
func func8() {
307-
defer save(-1)
308-
for i := range yield2 {
309-
for j := range yield3 {
310-
defer save(i*10 + j)
311-
}
312-
defer save(i)
313-
}
314-
defer save(-2)
315-
for i := range yield4 {
316-
defer save(i)
317-
}
318-
defer save(-3)
319-
}
320-
321-
func testfunc8() {
322-
saved = nil
323-
func8()
324-
want := []int{-3, 4, 3, 2, 1, -2, 2, 23, 22, 21, 1, 13, 12, 11, -1}
325-
checkslice("testfunc8", saved, want)
326-
}
327-
328-
func func9() {
329-
n := 0
330-
for _ = range yield2 {
331-
for _ = range yield3 {
332-
n++
333-
defer save(n)
334-
}
335-
}
336-
}
337-
338-
func testfunc9() {
339-
saved = nil
340-
func9()
341-
want := []int{6, 5, 4, 3, 2, 1}
342-
checkslice("testfunc9", saved, want)
343-
}
344-
345-
// test that range evaluates the index and value expressions
346-
// exactly once per iteration.
347-
348-
var ncalls = 0
349-
350-
func getvar(p *int) *int {
351-
ncalls++
352-
return p
353-
}
354-
355-
func iter2(list ...int) func(func(int, int) bool) {
356-
return func(yield func(int, int) bool) {
357-
for i, x := range list {
358-
if !yield(i, x) {
359-
return
360-
}
361-
}
362-
}
363-
}
364-
365-
func testcalls() {
366-
var i, v int
367-
ncalls = 0
368-
si := 0
369-
sv := 0
370-
for *getvar(&i), *getvar(&v) = range iter2(1, 2) {
371-
si += i
372-
sv += v
373-
}
374-
if ncalls != 4 {
375-
println("wrong number of calls:", ncalls, "!= 4")
376-
panic("fail")
377-
}
378-
if si != 1 || sv != 3 {
379-
println("wrong sum in testcalls", si, sv)
380-
panic("fail")
381-
}
382-
}
383-
38477
func main() {
38578
testint1()
38679
testint2()
38780
testint3()
38881
testint4()
389-
testfunc0()
390-
testfunc1()
391-
testfunc2()
392-
testfunc3()
393-
testfunc4()
394-
testfunc5()
395-
testfunc6()
396-
testfunc7()
397-
testfunc8()
398-
testfunc9()
399-
testcalls()
40082
}

0 commit comments

Comments
 (0)