github.com/mymmsc/gox@v1.3.33/util/lambda/array_test.go (about) 1 package lambda 2 3 import ( 4 "fmt" 5 "math/rand" 6 "strconv" 7 "testing" 8 "time" 9 ) 10 11 type user struct { 12 name string 13 age int 14 } 15 16 type account struct { 17 name string 18 age int 19 } 20 21 const count = 10000 22 23 func makeIntArray() []int { 24 want := make([]int, count) 25 for i := 0; i < count; i++ { 26 want[i] = i + 1 27 } 28 return want 29 } 30 31 func makeUserArray() []user { 32 want := make([]user, count) 33 for i := 0; i < count; i++ { 34 want[i] = user{"un:" + strconv.Itoa(i+1), i + 1} 35 } 36 return want 37 } 38 39 func report(t *testing.T, start time.Time) { 40 end := time.Now() 41 ms := float32(end.Nanosecond()-start.Nanosecond()) / float32(1e6) 42 t.Log(fmt.Sprintf("run time %.2f ms", ms)) 43 } 44 45 func isTrue(tv interface{}, v bool) { 46 t := tv.(*testing.T) 47 if !v { 48 t.Fail() 49 panic(v) 50 } 51 } 52 53 func isFalse(tv interface{}, v bool) { 54 t := tv.(*testing.T) 55 if v { 56 t.Fail() 57 panic(v) 58 } 59 } 60 61 func Test__array_Join(t *testing.T) { 62 defer report(t, time.Now()) 63 result := LambdaArray(makeIntArray()).Join(JoinOptions{ 64 express: func(e int) string { return strconv.Itoa(e) }, 65 }) 66 t.Log("string length", len(result)) 67 68 arr := []int{1, 2, 3, 4, 5} 69 str1 := LambdaArray(arr).Join(JoinOptions{ 70 express: func(e int) string { return strconv.Itoa(e) }, 71 }) 72 fmt.Println(str1) 73 74 str2 := LambdaArray(arr).Join(JoinOptions{ 75 express: func(e int) string { return strconv.Itoa(e) }, 76 Symbol: "|", 77 }) 78 fmt.Println(str2) 79 } 80 81 func Test__array_Filter(t *testing.T) { 82 defer report(t, time.Now()) 83 want := makeIntArray() 84 ret := LambdaArray(want).Filter( 85 func(ele int) bool { return ele%3 == 0 }).Pointer().([]int) 86 isTrue(t, len(ret) == count/3) 87 88 arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 89 larr := LambdaArray(arr) 90 ret1 := larr.Filter(func(ele int) bool { return ele > 5 }).Pointer().([]int) 91 fmt.Println(ret1) 92 93 ret2 := larr.Filter(func(ele int) bool { return ele%2 == 0 }).Pointer().([]int) 94 fmt.Println(ret2) 95 96 users := []user{ 97 {"Abraham", 20}, 98 {"Edith", 25}, 99 {"Charles", 40}, 100 {"Anthony", 26}, 101 {"Abel", 33}, 102 } 103 ret3 := LambdaArray(users).Filter(func(u user) bool { return u.age < 30 }).Pointer().([]user) 104 fmt.Println(ret3) 105 } 106 107 func Test__array_Sort_Quick(t *testing.T) { 108 defer report(t, time.Now()) 109 want := make([]int, count) 110 rand.Seed(time.Now().UnixNano()) 111 for i := 0; i < count; i++ { 112 want[i] = rand.Intn(count * 10) 113 } 114 t.Log(want[:10], "...", want[count-10:], " count=", len(want)) 115 ret := LambdaArray(want).Sort(func(e1, e2 int) bool { 116 return e1 > e2 117 }).Pointer().([]int) 118 t.Log(ret[:10], "...", ret[count-10:], " count=", len(ret)) 119 120 arr := []int{1, 3, 8, 6, 12, 5, 9} 121 // order by asc 122 ret1 := LambdaArray(arr).Sort(func(a, b int) bool { return a < b }).Pointer().([]int) 123 // order by desc 124 ret2 := LambdaArray(arr).Sort(func(a, b int) bool { return a > b }).Pointer().([]int) 125 126 fmt.Println(ret1) 127 fmt.Println(ret2) 128 129 users := []user{ 130 {"Abraham", 20}, 131 {"Edith", 25}, 132 {"Charles", 40}, 133 {"Anthony", 26}, 134 {"Abel", 33}, 135 } 136 ret3 := LambdaArray(users).Sort(func(a, b user) bool { return a.age < b.age }).Pointer().([]user) 137 fmt.Println(ret3) 138 } 139 140 func Test__array_Sort_QuickMT(t *testing.T) { 141 defer report(t, time.Now()) 142 want := make([]int, count) 143 rand.Seed(time.Now().UnixNano()) 144 for i := 0; i < count; i++ { 145 want[i] = rand.Intn(count * 10) 146 } 147 t.Log(want[:10], "...", want[count-10:], " count=", len(want)) 148 ret := LambdaArray(want).SortMT(func(e1, e2 int) bool { 149 return e1 > e2 150 }).Pointer().([]int) 151 t.Log(ret[:10], "...", ret[count-10:], " count=", len(ret)) 152 } 153 154 func Test__array_Map(t *testing.T) { 155 defer report(t, time.Now()) 156 157 result := LambdaArray(makeIntArray()).Map(func(e int) int { 158 return e + 1 159 }).Pointer().([]int) 160 161 isTrue(t, len(result) == count) 162 163 arr := LambdaArray([]int{1, 2, 3, 4, 5}) 164 users := arr.Map(func(i int) user { 165 return user{name: "un:" + strconv.Itoa(i), age: i} 166 }).Pointer().([]user) 167 fmt.Println(users) 168 } 169 170 func Test__array_Append(t *testing.T) { 171 defer report(t, time.Now()) 172 want := LambdaArray(makeIntArray()) 173 want.Append(count + 1) 174 isTrue(t, count+1 == want.Count(nil)) 175 176 arr := LambdaArray([]int{1, 2, 3}) 177 arr.Append(4) 178 fmt.Println(arr.Pointer().([]int)) 179 arr.Append(5, 6) 180 fmt.Println(arr.Pointer().([]int)) 181 } 182 183 func (p account) CompareTo(a interface{}) int { 184 return p.age - a.(account).age 185 } 186 187 func Test__array_Max(t *testing.T) { 188 defer report(t, time.Now()) 189 want := []int{1, 5, 6, 3, 8, 9, 3, 12, 56, 186, 4, 9, 14} 190 191 var iArr = LambdaArray(want) 192 193 ret := iArr.Max(nil).(int) 194 t.Log(ret) 195 ret = iArr.Max(func(ele int) int { return ele }).(int) 196 t.Log(ret) 197 198 wantUsers := iArr.Map(func(ele int) account { 199 s := fmt.Sprintf("%d", ele) 200 return account{"zzz" + s, ele} 201 }) 202 203 ret2 := wantUsers.Max(func(u account) int { return u.age }) 204 t.Log(ret2) 205 206 ret3 := wantUsers.Max(nil) 207 t.Log(ret3) 208 209 users := []user{ 210 {"Abraham", 20}, 211 {"Edith", 25}, 212 {"Charles", 40}, 213 {"Anthony", 26}, 214 {"Abel", 33}, 215 } 216 eldest := LambdaArray(users).Max(func(u user) int { return u.age }).(user) 217 fmt.Println(eldest.name + " is the eldest") 218 } 219 220 func Test__array_Sort_Min(t *testing.T) { 221 222 defer report(t, time.Now()) 223 224 want := []int{1, 5, 6, 3, 8, 9, 3, 12, 56, 186, 4, 9, 14} 225 226 var iArr = LambdaArray(want) 227 228 ret := iArr.Min(nil).(int) 229 t.Log(ret) 230 ret = iArr.Min(func(ele int) int { return ele }).(int) 231 t.Log(ret) 232 233 wantUsers := iArr.Map(func(ele int) account { 234 s := fmt.Sprintf("%d", ele) 235 return account{"zzz" + s, ele} 236 }) 237 238 ret2 := wantUsers.Min(func(u account) int { return u.age }) 239 t.Log(ret2) 240 241 ret3 := wantUsers.Min(nil) 242 t.Log(ret3) 243 244 users := []user{ 245 {"Abraham", 20}, 246 {"Edith", 25}, 247 {"Charles", 40}, 248 {"Anthony", 26}, 249 {"Abel", 33}, 250 } 251 eldest := LambdaArray(users).Min(func(u user) int { return u.age }).(user) 252 fmt.Println(eldest.name + " is the Charles") 253 } 254 255 func Test__array_Any(t *testing.T) { 256 defer report(t, time.Now()) 257 ints := LambdaArray(makeIntArray()) 258 users := LambdaArray(makeUserArray()) 259 ret := []bool{ 260 ints.Any(nil), 261 ints.Any(func(ele int) bool { return ele > 99999999 }), 262 users.Any(func(u user) bool { return u.name == "un:1997" }), 263 } 264 isTrue(t, ret[0]) 265 isFalse(t, ret[1]) 266 isTrue(t, ret[2]) 267 268 us := []user{ 269 {"Abraham", 20}, 270 {"Edith", 25}, 271 {"Charles", 40}, 272 {"Anthony", 26}, 273 {"Abel", 33}, 274 } 275 ret1 := LambdaArray(us).Any(func(u user) bool { return u.age > 30 }) 276 fmt.Println(ret1) 277 ret2 := LambdaArray(us).Any(func(u user) bool { return u.age < 0 }) 278 fmt.Println(ret2) 279 } 280 281 func Test__array_All(t *testing.T) { 282 defer report(t, time.Now()) 283 ints := LambdaArray(makeIntArray()) 284 users := LambdaArray(makeUserArray()) 285 ret := []bool{ 286 ints.All(nil), 287 ints.All(func(ele int) bool { return ele > 0 }), 288 users.All(func(u user) bool { return u.name == "un:1997" }), 289 } 290 isTrue(t, ret[0]) 291 isTrue(t, ret[1]) 292 isFalse(t, ret[2]) 293 294 us := []user{ 295 {"Abraham", 20}, 296 {"Edith", 25}, 297 {"Charles", 40}, 298 {"Anthony", 26}, 299 {"Abel", 33}, 300 } 301 ret1 := LambdaArray(us).All(func(u user) bool { return u.age > 30 }) 302 fmt.Println(ret1) 303 ret2 := LambdaArray(us).All(func(u user) bool { return u.age > 10 }) 304 fmt.Println(ret2) 305 } 306 307 func Test__array_Count(t *testing.T) { 308 defer report(t, time.Now()) 309 ints := LambdaArray(makeIntArray()) 310 ret := []int{ 311 ints.Count(nil), 312 ints.Count(func(ele int) bool { return ele%2 == 0 }), 313 } 314 isTrue(t, ret[0] == count) 315 isTrue(t, ret[1]*2 == count) 316 317 us := []user{ 318 {"Abraham", 20}, 319 {"Edith", 25}, 320 {"Charles", 40}, 321 {"Anthony", 26}, 322 {"Abel", 33}, 323 } 324 ret1 := LambdaArray(us).Count(func(u user) bool { return u.age > 30 }) 325 fmt.Println(ret1) 326 ret2 := LambdaArray(us).Count(func(u user) bool { return u.age > 20 }) 327 fmt.Println(ret2) 328 } 329 330 func Test__array_First(t *testing.T) { 331 defer report(t, time.Now()) 332 want := []int{1, 5, 6, 3, 8, 9, 3, 12, 56, 186, 4, 9, 14} 333 if c, err := LambdaArray(want).First(func(e int) bool { return e > 30 }); err == nil { 334 t.Log(c) 335 isTrue(t, c == 56) 336 } else { 337 t.Fail() 338 } 339 340 us := []user{ 341 {"Abraham", 20}, 342 {"Edith", 25}, 343 {"Charles", 40}, 344 {"Anthony", 26}, 345 {"Abel", 33}, 346 } 347 arr := LambdaArray(us) 348 if u, err := arr.First(func(u user) bool { return u.name == "Charles" }); err == nil { 349 fmt.Println(u, " found") 350 } else { 351 fmt.Println("not found") 352 } 353 354 if u, err := arr.First(func(u user) bool { return u.name == "jack" }); err == nil { 355 fmt.Println(u, " found") 356 } else { 357 fmt.Println("not found") 358 } 359 } 360 361 func Test__array_Index(t *testing.T) { 362 defer report(t, time.Now()) 363 ints := LambdaArray(makeIntArray()) 364 ret := []int{ 365 ints.Count(nil), 366 ints.Count(func(ele int) bool { return ele%2 == 0 }), 367 } 368 isTrue(t, ret[0] == count) 369 isTrue(t, ret[1]*2 == count) 370 371 if element, err := LambdaArray([]int{1, 2, 3, 4, 5}).Index(3); err == nil { 372 fmt.Println(element) 373 } else { 374 fmt.Println(err) 375 } 376 if element, err := LambdaArray([]int{1, 2, 3, 4, 5}).Index(10); err == nil { 377 fmt.Println(element) 378 } else { 379 fmt.Println(err) 380 } 381 } 382 383 func Test__array_Last(t *testing.T) { 384 defer report(t, time.Now()) 385 want := []int{1, 5, 6, 3, 8, 9, 3, 12, 56, 186, 4, 9, 14} 386 if c, err := LambdaArray(want).Last(func(e int) bool { return e > 30 }); err == nil { 387 t.Log(c) 388 isTrue(t, c == 186) 389 } else { 390 t.Fail() 391 } 392 393 us := []user{ 394 {"Abraham", 20}, 395 {"Edith", 25}, 396 {"Charles", 40}, 397 {"Anthony", 26}, 398 {"Abel", 33}, 399 } 400 arr := LambdaArray(us) 401 if u, err := arr.Last(func(u user) bool { return u.name == "Anthony" }); err == nil { 402 fmt.Println(u, " found") 403 } else { 404 fmt.Println("not found") 405 } 406 407 if u, err := arr.Last(func(u user) bool { return u.age > 35 }); err == nil { 408 fmt.Println(u, " found") 409 } else { 410 fmt.Println("not found") 411 } 412 } 413 414 func Test__array_Take(t *testing.T) { 415 defer report(t, time.Now()) 416 ints := LambdaArray(makeIntArray()) 417 ret := ints.Take(200, 10).Pointer().([]int) 418 isTrue(t, ret[0] == 201) 419 isTrue(t, ret[9] == 210) 420 421 ret1 := LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).Take(4, 10).Pointer().([]int) 422 fmt.Println(ret1) 423 ret2 := LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).Take(10, 10).Pointer().([]int) 424 fmt.Println(ret2) 425 } 426 427 func Test__array_Sum(t *testing.T) { 428 defer report(t, time.Now()) 429 ret := LambdaArray(makeIntArray()).Sum(nil).(int) 430 ret2 := LambdaArray(makeUserArray()).Sum(func(u user) int { return u.age }) 431 t.Log(ret, ret2) 432 433 us := []user{ 434 {"Abraham", 20}, 435 {"Edith", 25}, 436 {"Charles", 40}, 437 {"Anthony", 26}, 438 {"Abel", 33}, 439 } 440 arr := LambdaArray(us) 441 fmt.Println("total user age is", arr.Sum(func(u user) int { return u.age })) 442 } 443 444 func Test__array_Avg(t *testing.T) { 445 defer report(t, time.Now()) 446 ints := LambdaArray(makeIntArray()) 447 ret := ints.Average(nil) 448 t.Log(ret) 449 450 us := []user{ 451 {"Abraham", 20}, 452 {"Edith", 25}, 453 {"Charles", 40}, 454 {"Anthony", 26}, 455 {"Abel", 33}, 456 } 457 arr := LambdaArray(us) 458 fmt.Println("all user average age is", arr.Average(func(u user) int { return u.age })) 459 } 460 461 func Test__array_Contain(t *testing.T) { 462 463 defer report(t, time.Now()) 464 465 want := makeIntArray() 466 arr := LambdaArray(want) 467 ret := []bool{arr.Contains(7777), arr.Contains(count + 1)} 468 isTrue(t, ret[0]) 469 isFalse(t, ret[1]) 470 471 users := LambdaArray(makeUserArray()) 472 ret = []bool{ 473 users.Contains(user{"un:18", 18}), 474 users.Contains(user{"zzz", 18}), 475 } 476 isTrue(t, ret[0]) 477 isFalse(t, ret[1]) 478 479 ret = []bool{ 480 users.Contains(func(u user) bool { return u.age > 5000 }), 481 users.Contains(func(u user) bool { return u.age > count+1 }), 482 } 483 isTrue(t, ret[0]) 484 isFalse(t, ret[1]) 485 486 us := []user{ 487 {"Abraham", 20}, 488 {"Edith", 25}, 489 {"Charles", 40}, 490 {"Anthony", 26}, 491 {"Abel", 33}, 492 } 493 arr2 := LambdaArray(us) 494 fmt.Println(arr2.Contains(func(u user) bool { return u.age > 25 })) 495 496 fmt.Println(LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}).Contains(9)) 497 fmt.Println(LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}).Contains(0)) 498 } 499 500 func (u user) Equals(obj interface{}) bool { 501 if c, ok := obj.(user); ok { 502 return u.name == c.name && u.age == c.age 503 } 504 return false 505 }