github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/test/ddd.go (about) 1 // run 2 3 // Copyright 2010 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Test variadic functions and calls (dot-dot-dot). 8 9 package main 10 11 func sum(args ...int) int { 12 s := 0 13 for _, v := range args { 14 s += v 15 } 16 return s 17 } 18 19 func sumC(args ...int) int { return func() int { return sum(args...) }() } 20 21 var sumD = func(args ...int) int { return sum(args...) } 22 23 var sumE = func() func(...int) int { return func(args ...int) int { return sum(args...) } }() 24 25 var sumF = func(args ...int) func() int { return func() int { return sum(args...) } } 26 27 func sumA(args []int) int { 28 s := 0 29 for _, v := range args { 30 s += v 31 } 32 return s 33 } 34 35 func sumB(args []int) int { return sum(args...) } 36 37 func sum2(args ...int) int { return 2 * sum(args...) } 38 39 func sum3(args ...int) int { return 3 * sumA(args) } 40 41 func sum4(args ...int) int { return 4 * sumB(args) } 42 43 func intersum(args ...interface{}) int { 44 s := 0 45 for _, v := range args { 46 s += v.(int) 47 } 48 return s 49 } 50 51 type T []T 52 53 func ln(args ...T) int { return len(args) } 54 55 func ln2(args ...T) int { return 2 * ln(args...) } 56 57 func (*T) Sum(args ...int) int { return sum(args...) } 58 59 type U struct { 60 *T 61 } 62 63 type I interface { 64 Sum(...int) int 65 } 66 67 func main() { 68 if x := sum(1, 2, 3); x != 6 { 69 println("sum 6", x) 70 panic("fail") 71 } 72 if x := sum(); x != 0 { 73 println("sum 0", x) 74 panic("fail") 75 } 76 if x := sum(10); x != 10 { 77 println("sum 10", x) 78 panic("fail") 79 } 80 if x := sum(1, 8); x != 9 { 81 println("sum 9", x) 82 panic("fail") 83 } 84 if x := sumC(4, 5, 6); x != 15 { 85 println("sumC 15", x) 86 panic("fail") 87 } 88 if x := sumD(4, 5, 7); x != 16 { 89 println("sumD 16", x) 90 panic("fail") 91 } 92 if x := sumE(4, 5, 8); x != 17 { 93 println("sumE 17", x) 94 panic("fail") 95 } 96 if x := sumF(4, 5, 9)(); x != 18 { 97 println("sumF 18", x) 98 panic("fail") 99 } 100 if x := sum2(1, 2, 3); x != 2*6 { 101 println("sum 6", x) 102 panic("fail") 103 } 104 if x := sum2(); x != 2*0 { 105 println("sum 0", x) 106 panic("fail") 107 } 108 if x := sum2(10); x != 2*10 { 109 println("sum 10", x) 110 panic("fail") 111 } 112 if x := sum2(1, 8); x != 2*9 { 113 println("sum 9", x) 114 panic("fail") 115 } 116 if x := sum3(1, 2, 3); x != 3*6 { 117 println("sum 6", x) 118 panic("fail") 119 } 120 if x := sum3(); x != 3*0 { 121 println("sum 0", x) 122 panic("fail") 123 } 124 if x := sum3(10); x != 3*10 { 125 println("sum 10", x) 126 panic("fail") 127 } 128 if x := sum3(1, 8); x != 3*9 { 129 println("sum 9", x) 130 panic("fail") 131 } 132 if x := sum4(1, 2, 3); x != 4*6 { 133 println("sum 6", x) 134 panic("fail") 135 } 136 if x := sum4(); x != 4*0 { 137 println("sum 0", x) 138 panic("fail") 139 } 140 if x := sum4(10); x != 4*10 { 141 println("sum 10", x) 142 panic("fail") 143 } 144 if x := sum4(1, 8); x != 4*9 { 145 println("sum 9", x) 146 panic("fail") 147 } 148 if x := intersum(1, 2, 3); x != 6 { 149 println("intersum 6", x) 150 panic("fail") 151 } 152 if x := intersum(); x != 0 { 153 println("intersum 0", x) 154 panic("fail") 155 } 156 if x := intersum(10); x != 10 { 157 println("intersum 10", x) 158 panic("fail") 159 } 160 if x := intersum(1, 8); x != 9 { 161 println("intersum 9", x) 162 panic("fail") 163 } 164 165 if x := ln(nil, nil, nil); x != 3 { 166 println("ln 3", x) 167 panic("fail") 168 } 169 if x := ln([]T{}); x != 1 { 170 println("ln 1", x) 171 panic("fail") 172 } 173 if x := ln2(nil, nil, nil); x != 2*3 { 174 println("ln2 3", x) 175 panic("fail") 176 } 177 if x := ln2([]T{}); x != 2*1 { 178 println("ln2 1", x) 179 panic("fail") 180 } 181 if x := ((*T)(nil)).Sum(1, 3, 5, 7); x != 16 { 182 println("(*T)(nil).Sum", x) 183 panic("fail") 184 } 185 if x := (*T).Sum(nil, 1, 3, 5, 6); x != 15 { 186 println("(*T).Sum", x) 187 panic("fail") 188 } 189 if x := (&U{}).Sum(1, 3, 5, 5); x != 14 { 190 println("(&U{}).Sum", x) 191 panic("fail") 192 } 193 var u U 194 if x := u.Sum(1, 3, 5, 4); x != 13 { 195 println("u.Sum", x) 196 panic("fail") 197 } 198 if x := (&u).Sum(1, 3, 5, 3); x != 12 { 199 println("(&u).Sum", x) 200 panic("fail") 201 } 202 var i interface { 203 Sum(...int) int 204 } = &u 205 if x := i.Sum(2, 3, 5, 7); x != 17 { 206 println("i(=&u).Sum", x) 207 panic("fail") 208 } 209 i = u 210 if x := i.Sum(2, 3, 5, 6); x != 16 { 211 println("i(=u).Sum", x) 212 panic("fail") 213 } 214 var s struct { 215 I 216 } 217 s.I = &u 218 if x := s.Sum(2, 3, 5, 8); x != 18 { 219 println("s{&u}.Sum", x) 220 panic("fail") 221 } 222 if x := (*U).Sum(&U{}, 1, 3, 5, 2); x != 11 { 223 println("(*U).Sum", x) 224 panic("fail") 225 } 226 if x := U.Sum(U{}, 1, 3, 5, 1); x != 10 { 227 println("U.Sum", x) 228 panic("fail") 229 } 230 }