2012-02-17 04:48:57 +00:00
|
|
|
// run
|
2010-02-01 08:25:59 +00:00
|
|
|
|
2016-04-10 21:32:26 +00:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2010-02-01 08:25:59 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-19 03:28:53 +00:00
|
|
|
// Test variadic functions and calls (dot-dot-dot).
|
|
|
|
|
2010-02-01 08:25:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
func sum(args ...int) int {
|
|
|
|
s := 0
|
|
|
|
for _, v := range args {
|
|
|
|
s += v
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2010-09-24 15:55:48 +00:00
|
|
|
func sumC(args ...int) int { return func() int { return sum(args...) }() }
|
2010-02-01 18:49:24 +00:00
|
|
|
|
2010-09-24 15:55:48 +00:00
|
|
|
var sumD = func(args ...int) int { return sum(args...) }
|
2010-02-01 18:49:24 +00:00
|
|
|
|
2010-09-24 15:55:48 +00:00
|
|
|
var sumE = func() func(...int) int { return func(args ...int) int { return sum(args...) } }()
|
2010-02-01 18:49:24 +00:00
|
|
|
|
2010-09-24 15:55:48 +00:00
|
|
|
var sumF = func(args ...int) func() int { return func() int { return sum(args...) } }
|
2010-02-01 18:49:24 +00:00
|
|
|
|
2010-02-01 08:25:59 +00:00
|
|
|
func sumA(args []int) int {
|
|
|
|
s := 0
|
|
|
|
for _, v := range args {
|
|
|
|
s += v
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2010-10-06 15:12:30 +00:00
|
|
|
func sumB(args []int) int { return sum(args...) }
|
|
|
|
|
2010-09-24 15:55:48 +00:00
|
|
|
func sum2(args ...int) int { return 2 * sum(args...) }
|
2010-02-01 08:25:59 +00:00
|
|
|
|
|
|
|
func sum3(args ...int) int { return 3 * sumA(args) }
|
|
|
|
|
2010-10-06 15:12:30 +00:00
|
|
|
func sum4(args ...int) int { return 4 * sumB(args) }
|
|
|
|
|
2010-02-01 08:25:59 +00:00
|
|
|
func intersum(args ...interface{}) int {
|
|
|
|
s := 0
|
|
|
|
for _, v := range args {
|
|
|
|
s += v.(int)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type T []T
|
|
|
|
|
|
|
|
func ln(args ...T) int { return len(args) }
|
|
|
|
|
2010-09-24 15:55:48 +00:00
|
|
|
func ln2(args ...T) int { return 2 * ln(args...) }
|
2010-02-01 08:25:59 +00:00
|
|
|
|
2010-09-24 15:55:48 +00:00
|
|
|
func (*T) Sum(args ...int) int { return sum(args...) }
|
2010-02-01 18:49:24 +00:00
|
|
|
|
|
|
|
type U struct {
|
|
|
|
*T
|
|
|
|
}
|
|
|
|
|
2012-03-07 16:24:10 +00:00
|
|
|
type I interface {
|
|
|
|
Sum(...int) int
|
|
|
|
}
|
|
|
|
|
2010-02-01 08:25:59 +00:00
|
|
|
func main() {
|
|
|
|
if x := sum(1, 2, 3); x != 6 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 6", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := sum(); x != 0 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 0", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := sum(10); x != 10 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 10", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := sum(1, 8); x != 9 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 9", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
2010-02-01 18:49:24 +00:00
|
|
|
if x := sumC(4, 5, 6); x != 15 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sumC 15", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
|
|
|
if x := sumD(4, 5, 7); x != 16 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sumD 16", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
|
|
|
if x := sumE(4, 5, 8); x != 17 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sumE 17", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
|
|
|
if x := sumF(4, 5, 9)(); x != 18 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sumF 18", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
2010-02-01 08:25:59 +00:00
|
|
|
if x := sum2(1, 2, 3); x != 2*6 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 6", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := sum2(); x != 2*0 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 0", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := sum2(10); x != 2*10 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 10", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := sum2(1, 8); x != 2*9 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 9", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := sum3(1, 2, 3); x != 3*6 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 6", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := sum3(); x != 3*0 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 0", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := sum3(10); x != 3*10 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 10", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := sum3(1, 8); x != 3*9 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("sum 9", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
2010-10-06 15:12:30 +00:00
|
|
|
if x := sum4(1, 2, 3); x != 4*6 {
|
|
|
|
println("sum 6", x)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if x := sum4(); x != 4*0 {
|
|
|
|
println("sum 0", x)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if x := sum4(10); x != 4*10 {
|
|
|
|
println("sum 10", x)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if x := sum4(1, 8); x != 4*9 {
|
|
|
|
println("sum 9", x)
|
|
|
|
panic("fail")
|
|
|
|
}
|
2010-02-01 08:25:59 +00:00
|
|
|
if x := intersum(1, 2, 3); x != 6 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("intersum 6", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := intersum(); x != 0 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("intersum 0", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := intersum(10); x != 10 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("intersum 10", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := intersum(1, 8); x != 9 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("intersum 9", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if x := ln(nil, nil, nil); x != 3 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("ln 3", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := ln([]T{}); x != 1 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("ln 1", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := ln2(nil, nil, nil); x != 2*3 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("ln2 3", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
|
|
|
if x := ln2([]T{}); x != 2*1 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("ln2 1", x)
|
|
|
|
panic("fail")
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|
2010-03-24 23:46:53 +00:00
|
|
|
if x := ((*T)(nil)).Sum(1, 3, 5, 7); x != 16 {
|
|
|
|
println("(*T)(nil).Sum", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
|
|
|
if x := (*T).Sum(nil, 1, 3, 5, 6); x != 15 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("(*T).Sum", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
2010-03-24 23:46:53 +00:00
|
|
|
if x := (&U{}).Sum(1, 3, 5, 5); x != 14 {
|
|
|
|
println("(&U{}).Sum", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
|
|
|
var u U
|
2010-03-24 23:46:53 +00:00
|
|
|
if x := u.Sum(1, 3, 5, 4); x != 13 {
|
|
|
|
println("u.Sum", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
2010-03-24 23:46:53 +00:00
|
|
|
if x := (&u).Sum(1, 3, 5, 3); x != 12 {
|
|
|
|
println("(&u).Sum", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
2010-03-24 23:46:53 +00:00
|
|
|
var i interface {
|
|
|
|
Sum(...int) int
|
|
|
|
} = &u
|
|
|
|
if x := i.Sum(2, 3, 5, 7); x != 17 {
|
|
|
|
println("i(=&u).Sum", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
|
|
|
i = u
|
2010-03-24 23:46:53 +00:00
|
|
|
if x := i.Sum(2, 3, 5, 6); x != 16 {
|
|
|
|
println("i(=u).Sum", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
2012-03-07 16:24:10 +00:00
|
|
|
var s struct {
|
|
|
|
I
|
|
|
|
}
|
|
|
|
s.I = &u
|
|
|
|
if x := s.Sum(2, 3, 5, 8); x != 18 {
|
|
|
|
println("s{&u}.Sum", x)
|
|
|
|
panic("fail")
|
|
|
|
}
|
2010-02-01 18:49:24 +00:00
|
|
|
if x := (*U).Sum(&U{}, 1, 3, 5, 2); x != 11 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("(*U).Sum", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
|
|
|
if x := U.Sum(U{}, 1, 3, 5, 1); x != 10 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("U.Sum", x)
|
|
|
|
panic("fail")
|
2010-02-01 18:49:24 +00:00
|
|
|
}
|
2010-02-01 08:25:59 +00:00
|
|
|
}
|