github.com/gogf/gf@v1.16.9/container/garray/garray_z_unit_normal_str_array_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go 8 9 package garray_test 10 11 import ( 12 "github.com/gogf/gf/frame/g" 13 "github.com/gogf/gf/internal/json" 14 "strings" 15 "testing" 16 "time" 17 18 "github.com/gogf/gf/container/garray" 19 "github.com/gogf/gf/test/gtest" 20 "github.com/gogf/gf/util/gconv" 21 ) 22 23 func Test_StrArray_Basic(t *testing.T) { 24 gtest.C(t, func(t *gtest.T) { 25 expect := []string{"0", "1", "2", "3"} 26 array := garray.NewStrArrayFrom(expect) 27 array2 := garray.NewStrArrayFrom(expect, true) 28 array3 := garray.NewStrArrayFrom([]string{}) 29 t.Assert(array.Slice(), expect) 30 t.Assert(array.Interfaces(), expect) 31 array.Set(0, "100") 32 33 v, ok := array.Get(0) 34 t.Assert(v, 100) 35 t.Assert(ok, true) 36 37 t.Assert(array.Search("100"), 0) 38 t.Assert(array.Contains("100"), true) 39 40 v, ok = array.Remove(0) 41 t.Assert(v, 100) 42 t.Assert(ok, true) 43 44 v, ok = array.Remove(-1) 45 t.Assert(v, "") 46 t.Assert(ok, false) 47 48 v, ok = array.Remove(100000) 49 t.Assert(v, "") 50 t.Assert(ok, false) 51 52 t.Assert(array.Contains("100"), false) 53 array.Append("4") 54 t.Assert(array.Len(), 4) 55 array.InsertBefore(0, "100") 56 array.InsertAfter(0, "200") 57 t.Assert(array.Slice(), []string{"100", "200", "1", "2", "3", "4"}) 58 array.InsertBefore(5, "300") 59 array.InsertAfter(6, "400") 60 t.Assert(array.Slice(), []string{"100", "200", "1", "2", "3", "300", "4", "400"}) 61 t.Assert(array.Clear().Len(), 0) 62 t.Assert(array2.Slice(), expect) 63 t.Assert(array3.Search("100"), -1) 64 }) 65 } 66 67 func TestStrArray_ContainsI(t *testing.T) { 68 gtest.C(t, func(t *gtest.T) { 69 s := garray.NewStrArray() 70 s.Append("a", "b", "C") 71 t.Assert(s.Contains("A"), false) 72 t.Assert(s.Contains("a"), true) 73 t.Assert(s.ContainsI("A"), true) 74 }) 75 } 76 77 func TestStrArray_Sort(t *testing.T) { 78 gtest.C(t, func(t *gtest.T) { 79 expect1 := []string{"0", "1", "2", "3"} 80 expect2 := []string{"3", "2", "1", "0"} 81 array := garray.NewStrArray() 82 for i := 3; i >= 0; i-- { 83 array.Append(gconv.String(i)) 84 } 85 array.Sort() 86 t.Assert(array.Slice(), expect1) 87 array.Sort(true) 88 t.Assert(array.Slice(), expect2) 89 }) 90 } 91 92 func TestStrArray_Unique(t *testing.T) { 93 gtest.C(t, func(t *gtest.T) { 94 expect := []string{"1", "1", "2", "2", "3", "3", "2", "2"} 95 array := garray.NewStrArrayFrom(expect) 96 t.Assert(array.Unique().Slice(), []string{"1", "2", "3"}) 97 }) 98 } 99 100 func TestStrArray_PushAndPop(t *testing.T) { 101 gtest.C(t, func(t *gtest.T) { 102 expect := []string{"0", "1", "2", "3"} 103 array := garray.NewStrArrayFrom(expect) 104 t.Assert(array.Slice(), expect) 105 106 v, ok := array.PopLeft() 107 t.Assert(v, "0") 108 t.Assert(ok, true) 109 110 v, ok = array.PopRight() 111 t.Assert(v, "3") 112 t.Assert(ok, true) 113 114 v, ok = array.PopRand() 115 t.AssertIN(v, []string{"1", "2"}) 116 t.Assert(ok, true) 117 118 v, ok = array.PopRand() 119 t.AssertIN(v, []string{"1", "2"}) 120 t.Assert(ok, true) 121 122 v, ok = array.PopRand() 123 t.Assert(v, "") 124 t.Assert(ok, false) 125 126 t.Assert(array.Len(), 0) 127 array.PushLeft("1").PushRight("2") 128 t.Assert(array.Slice(), []string{"1", "2"}) 129 }) 130 } 131 132 func TestStrArray_PopLeft(t *testing.T) { 133 gtest.C(t, func(t *gtest.T) { 134 array := garray.NewStrArrayFrom(g.SliceStr{"1", "2", "3"}) 135 v, ok := array.PopLeft() 136 t.Assert(v, 1) 137 t.Assert(ok, true) 138 t.Assert(array.Len(), 2) 139 v, ok = array.PopLeft() 140 t.Assert(v, 2) 141 t.Assert(ok, true) 142 t.Assert(array.Len(), 1) 143 v, ok = array.PopLeft() 144 t.Assert(v, 3) 145 t.Assert(ok, true) 146 t.Assert(array.Len(), 0) 147 }) 148 } 149 150 func TestStrArray_PopRight(t *testing.T) { 151 gtest.C(t, func(t *gtest.T) { 152 array := garray.NewStrArrayFrom(g.SliceStr{"1", "2", "3"}) 153 154 v, ok := array.PopRight() 155 t.Assert(v, 3) 156 t.Assert(ok, true) 157 t.Assert(array.Len(), 2) 158 159 v, ok = array.PopRight() 160 t.Assert(v, 2) 161 t.Assert(ok, true) 162 t.Assert(array.Len(), 1) 163 164 v, ok = array.PopRight() 165 t.Assert(v, 1) 166 t.Assert(ok, true) 167 t.Assert(array.Len(), 0) 168 }) 169 } 170 171 func TestStrArray_PopLefts(t *testing.T) { 172 gtest.C(t, func(t *gtest.T) { 173 array := garray.NewStrArrayFrom(g.SliceStr{"1", "2", "3"}) 174 t.Assert(array.PopLefts(2), g.Slice{"1", "2"}) 175 t.Assert(array.Len(), 1) 176 t.Assert(array.PopLefts(2), g.Slice{"3"}) 177 t.Assert(array.Len(), 0) 178 }) 179 } 180 181 func TestStrArray_PopRights(t *testing.T) { 182 gtest.C(t, func(t *gtest.T) { 183 array := garray.NewStrArrayFrom(g.SliceStr{"1", "2", "3"}) 184 t.Assert(array.PopRights(2), g.Slice{"2", "3"}) 185 t.Assert(array.Len(), 1) 186 t.Assert(array.PopLefts(2), g.Slice{"1"}) 187 t.Assert(array.Len(), 0) 188 }) 189 } 190 191 func TestStrArray_PopLeftsAndPopRights(t *testing.T) { 192 gtest.C(t, func(t *gtest.T) { 193 array := garray.NewStrArray() 194 v, ok := array.PopLeft() 195 t.Assert(v, "") 196 t.Assert(ok, false) 197 t.Assert(array.PopLefts(10), nil) 198 199 v, ok = array.PopRight() 200 t.Assert(v, "") 201 t.Assert(ok, false) 202 t.Assert(array.PopRights(10), nil) 203 204 v, ok = array.PopRand() 205 t.Assert(v, "") 206 t.Assert(ok, false) 207 t.Assert(array.PopRands(10), nil) 208 }) 209 210 gtest.C(t, func(t *gtest.T) { 211 value1 := []string{"0", "1", "2", "3", "4", "5", "6"} 212 value2 := []string{"0", "1", "2", "3", "4", "5", "6"} 213 array1 := garray.NewStrArrayFrom(value1) 214 array2 := garray.NewStrArrayFrom(value2) 215 t.Assert(array1.PopLefts(2), []interface{}{"0", "1"}) 216 t.Assert(array1.Slice(), []interface{}{"2", "3", "4", "5", "6"}) 217 t.Assert(array1.PopRights(2), []interface{}{"5", "6"}) 218 t.Assert(array1.Slice(), []interface{}{"2", "3", "4"}) 219 t.Assert(array1.PopRights(20), []interface{}{"2", "3", "4"}) 220 t.Assert(array1.Slice(), []interface{}{}) 221 t.Assert(array2.PopLefts(20), []interface{}{"0", "1", "2", "3", "4", "5", "6"}) 222 t.Assert(array2.Slice(), []interface{}{}) 223 }) 224 } 225 226 func TestString_Range(t *testing.T) { 227 gtest.C(t, func(t *gtest.T) { 228 value1 := []string{"0", "1", "2", "3", "4", "5", "6"} 229 array1 := garray.NewStrArrayFrom(value1) 230 array2 := garray.NewStrArrayFrom(value1, true) 231 t.Assert(array1.Range(0, 1), []interface{}{"0"}) 232 t.Assert(array1.Range(1, 2), []interface{}{"1"}) 233 t.Assert(array1.Range(0, 2), []interface{}{"0", "1"}) 234 t.Assert(array1.Range(-1, 10), value1) 235 t.Assert(array1.Range(10, 1), nil) 236 t.Assert(array2.Range(0, 1), []interface{}{"0"}) 237 }) 238 } 239 240 func TestStrArray_Merge(t *testing.T) { 241 gtest.C(t, func(t *gtest.T) { 242 a11 := []string{"0", "1", "2", "3"} 243 a21 := []string{"4", "5", "6", "7"} 244 array1 := garray.NewStrArrayFrom(a11) 245 array2 := garray.NewStrArrayFrom(a21) 246 t.Assert(array1.Merge(array2).Slice(), []string{"0", "1", "2", "3", "4", "5", "6", "7"}) 247 248 func1 := func(v1, v2 interface{}) int { 249 if gconv.Int(v1) < gconv.Int(v2) { 250 return 0 251 } 252 return 1 253 } 254 255 s1 := []string{"a", "b", "c", "d"} 256 s2 := []string{"e", "f"} 257 i1 := garray.NewIntArrayFrom([]int{1, 2, 3}) 258 i2 := garray.NewArrayFrom([]interface{}{3}) 259 s3 := garray.NewStrArrayFrom([]string{"g", "h"}) 260 s4 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1) 261 s5 := garray.NewSortedStrArrayFrom(s2) 262 s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3}) 263 a1 := garray.NewStrArrayFrom(s1) 264 265 t.Assert(a1.Merge(s2).Len(), 6) 266 t.Assert(a1.Merge(i1).Len(), 9) 267 t.Assert(a1.Merge(i2).Len(), 10) 268 t.Assert(a1.Merge(s3).Len(), 12) 269 t.Assert(a1.Merge(s4).Len(), 14) 270 t.Assert(a1.Merge(s5).Len(), 16) 271 t.Assert(a1.Merge(s6).Len(), 19) 272 }) 273 } 274 275 func TestStrArray_Fill(t *testing.T) { 276 gtest.C(t, func(t *gtest.T) { 277 a1 := []string{"0"} 278 a2 := []string{"0"} 279 array1 := garray.NewStrArrayFrom(a1) 280 array2 := garray.NewStrArrayFrom(a2) 281 t.Assert(array1.Fill(1, 2, "100"), nil) 282 t.Assert(array1.Slice(), []string{"0", "100", "100"}) 283 t.Assert(array2.Fill(0, 2, "100"), nil) 284 t.Assert(array2.Slice(), []string{"100", "100"}) 285 t.AssertNE(array2.Fill(-1, 2, "100"), nil) 286 t.Assert(array2.Len(), 2) 287 }) 288 } 289 290 func TestStrArray_Chunk(t *testing.T) { 291 gtest.C(t, func(t *gtest.T) { 292 a1 := []string{"1", "2", "3", "4", "5"} 293 array1 := garray.NewStrArrayFrom(a1) 294 chunks := array1.Chunk(2) 295 t.Assert(len(chunks), 3) 296 t.Assert(chunks[0], []string{"1", "2"}) 297 t.Assert(chunks[1], []string{"3", "4"}) 298 t.Assert(chunks[2], []string{"5"}) 299 t.Assert(len(array1.Chunk(0)), 0) 300 }) 301 gtest.C(t, func(t *gtest.T) { 302 a1 := []string{"1", "2", "3", "4", "5"} 303 array1 := garray.NewStrArrayFrom(a1) 304 chunks := array1.Chunk(3) 305 t.Assert(len(chunks), 2) 306 t.Assert(chunks[0], []string{"1", "2", "3"}) 307 t.Assert(chunks[1], []string{"4", "5"}) 308 t.Assert(array1.Chunk(0), nil) 309 }) 310 gtest.C(t, func(t *gtest.T) { 311 a1 := []string{"1", "2", "3", "4", "5", "6"} 312 array1 := garray.NewStrArrayFrom(a1) 313 chunks := array1.Chunk(2) 314 t.Assert(len(chunks), 3) 315 t.Assert(chunks[0], []string{"1", "2"}) 316 t.Assert(chunks[1], []string{"3", "4"}) 317 t.Assert(chunks[2], []string{"5", "6"}) 318 t.Assert(array1.Chunk(0), nil) 319 }) 320 gtest.C(t, func(t *gtest.T) { 321 a1 := []string{"1", "2", "3", "4", "5", "6"} 322 array1 := garray.NewStrArrayFrom(a1) 323 chunks := array1.Chunk(3) 324 t.Assert(len(chunks), 2) 325 t.Assert(chunks[0], []string{"1", "2", "3"}) 326 t.Assert(chunks[1], []string{"4", "5", "6"}) 327 t.Assert(array1.Chunk(0), nil) 328 }) 329 } 330 331 func TestStrArray_Pad(t *testing.T) { 332 gtest.C(t, func(t *gtest.T) { 333 a1 := []string{"0"} 334 array1 := garray.NewStrArrayFrom(a1) 335 t.Assert(array1.Pad(3, "1").Slice(), []string{"0", "1", "1"}) 336 t.Assert(array1.Pad(-4, "1").Slice(), []string{"1", "0", "1", "1"}) 337 t.Assert(array1.Pad(3, "1").Slice(), []string{"1", "0", "1", "1"}) 338 }) 339 } 340 341 func TestStrArray_SubSlice(t *testing.T) { 342 gtest.C(t, func(t *gtest.T) { 343 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 344 array1 := garray.NewStrArrayFrom(a1) 345 array2 := garray.NewStrArrayFrom(a1, true) 346 t.Assert(array1.SubSlice(0, 2), []string{"0", "1"}) 347 t.Assert(array1.SubSlice(2, 2), []string{"2", "3"}) 348 t.Assert(array1.SubSlice(5, 8), []string{"5", "6"}) 349 t.Assert(array1.SubSlice(8, 2), nil) 350 t.Assert(array1.SubSlice(1, -2), nil) 351 t.Assert(array1.SubSlice(-5, 2), []string{"2", "3"}) 352 t.Assert(array1.SubSlice(-10, 1), nil) 353 t.Assert(array2.SubSlice(0, 2), []string{"0", "1"}) 354 }) 355 } 356 357 func TestStrArray_Rand(t *testing.T) { 358 gtest.C(t, func(t *gtest.T) { 359 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 360 array1 := garray.NewStrArrayFrom(a1) 361 t.Assert(len(array1.Rands(2)), "2") 362 t.Assert(len(array1.Rands(10)), 10) 363 t.AssertIN(array1.Rands(1)[0], a1) 364 v, ok := array1.Rand() 365 t.Assert(ok, true) 366 t.AssertIN(v, a1) 367 }) 368 } 369 370 func TestStrArray_PopRands(t *testing.T) { 371 gtest.C(t, func(t *gtest.T) { 372 a1 := []string{"a", "b", "c", "d", "e", "f", "g"} 373 array1 := garray.NewStrArrayFrom(a1) 374 t.AssertIN(array1.PopRands(1), []string{"a", "b", "c", "d", "e", "f", "g"}) 375 t.AssertIN(array1.PopRands(1), []string{"a", "b", "c", "d", "e", "f", "g"}) 376 t.AssertNI(array1.PopRands(1), array1.Slice()) 377 t.AssertNI(array1.PopRands(1), array1.Slice()) 378 t.Assert(len(array1.PopRands(10)), 3) 379 }) 380 } 381 382 func TestStrArray_Shuffle(t *testing.T) { 383 gtest.C(t, func(t *gtest.T) { 384 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 385 array1 := garray.NewStrArrayFrom(a1) 386 t.Assert(array1.Shuffle().Len(), 7) 387 }) 388 } 389 390 func TestStrArray_Reverse(t *testing.T) { 391 gtest.C(t, func(t *gtest.T) { 392 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 393 array1 := garray.NewStrArrayFrom(a1) 394 t.Assert(array1.Reverse().Slice(), []string{"6", "5", "4", "3", "2", "1", "0"}) 395 }) 396 } 397 398 func TestStrArray_Join(t *testing.T) { 399 gtest.C(t, func(t *gtest.T) { 400 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 401 array1 := garray.NewStrArrayFrom(a1) 402 t.Assert(array1.Join("."), `0.1.2.3.4.5.6`) 403 }) 404 gtest.C(t, func(t *gtest.T) { 405 a1 := []string{"0", "1", `"a"`, `\a`} 406 array1 := garray.NewStrArrayFrom(a1) 407 t.Assert(array1.Join("."), `0.1."a".\a`) 408 }) 409 } 410 411 func TestStrArray_String(t *testing.T) { 412 gtest.C(t, func(t *gtest.T) { 413 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 414 array1 := garray.NewStrArrayFrom(a1) 415 t.Assert(array1.String(), `["0","1","2","3","4","5","6"]`) 416 }) 417 } 418 419 func TestNewStrArrayFromCopy(t *testing.T) { 420 gtest.C(t, func(t *gtest.T) { 421 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 422 a2 := garray.NewStrArrayFromCopy(a1) 423 a3 := garray.NewStrArrayFromCopy(a1, true) 424 t.Assert(a2.Contains("1"), true) 425 t.Assert(a2.Len(), 7) 426 t.Assert(a2, a3) 427 }) 428 } 429 430 func TestStrArray_SetArray(t *testing.T) { 431 gtest.C(t, func(t *gtest.T) { 432 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 433 a2 := []string{"a", "b", "c", "d"} 434 array1 := garray.NewStrArrayFrom(a1) 435 t.Assert(array1.Contains("2"), true) 436 t.Assert(array1.Len(), 7) 437 438 array1 = array1.SetArray(a2) 439 t.Assert(array1.Contains("2"), false) 440 t.Assert(array1.Contains("c"), true) 441 t.Assert(array1.Len(), 4) 442 }) 443 } 444 445 func TestStrArray_Replace(t *testing.T) { 446 gtest.C(t, func(t *gtest.T) { 447 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 448 a2 := []string{"a", "b", "c", "d"} 449 a3 := []string{"o", "p", "q", "x", "y", "z", "w", "r", "v"} 450 array1 := garray.NewStrArrayFrom(a1) 451 t.Assert(array1.Contains("2"), true) 452 t.Assert(array1.Len(), 7) 453 454 array1 = array1.Replace(a2) 455 t.Assert(array1.Contains("2"), false) 456 t.Assert(array1.Contains("c"), true) 457 t.Assert(array1.Contains("5"), true) 458 t.Assert(array1.Len(), 7) 459 460 array1 = array1.Replace(a3) 461 t.Assert(array1.Contains("2"), false) 462 t.Assert(array1.Contains("c"), false) 463 t.Assert(array1.Contains("5"), false) 464 t.Assert(array1.Contains("p"), true) 465 t.Assert(array1.Contains("r"), false) 466 t.Assert(array1.Len(), 7) 467 468 }) 469 } 470 471 func TestStrArray_Sum(t *testing.T) { 472 gtest.C(t, func(t *gtest.T) { 473 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 474 a2 := []string{"0", "a", "3", "4", "5", "6"} 475 array1 := garray.NewStrArrayFrom(a1) 476 array2 := garray.NewStrArrayFrom(a2) 477 t.Assert(array1.Sum(), 21) 478 t.Assert(array2.Sum(), 18) 479 }) 480 } 481 482 func TestStrArray_PopRand(t *testing.T) { 483 gtest.C(t, func(t *gtest.T) { 484 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 485 array1 := garray.NewStrArrayFrom(a1) 486 str1, ok := array1.PopRand() 487 t.Assert(strings.Contains("0,1,2,3,4,5,6", str1), true) 488 t.Assert(array1.Len(), 6) 489 t.Assert(ok, true) 490 }) 491 } 492 493 func TestStrArray_Clone(t *testing.T) { 494 gtest.C(t, func(t *gtest.T) { 495 a1 := []string{"0", "1", "2", "3", "4", "5", "6"} 496 array1 := garray.NewStrArrayFrom(a1) 497 array2 := array1.Clone() 498 t.Assert(array2, array1) 499 t.Assert(array2.Len(), 7) 500 }) 501 } 502 503 func TestStrArray_CountValues(t *testing.T) { 504 gtest.C(t, func(t *gtest.T) { 505 a1 := []string{"0", "1", "2", "3", "4", "4", "6"} 506 array1 := garray.NewStrArrayFrom(a1) 507 508 m1 := array1.CountValues() 509 t.Assert(len(m1), 6) 510 t.Assert(m1["2"], 1) 511 t.Assert(m1["4"], 2) 512 }) 513 } 514 515 func TestStrArray_Remove(t *testing.T) { 516 gtest.C(t, func(t *gtest.T) { 517 a1 := []string{"e", "a", "d", "a", "c"} 518 array1 := garray.NewStrArrayFrom(a1) 519 s1, ok := array1.Remove(1) 520 t.Assert(s1, "a") 521 t.Assert(ok, true) 522 t.Assert(array1.Len(), 4) 523 s1, ok = array1.Remove(3) 524 t.Assert(s1, "c") 525 t.Assert(ok, true) 526 t.Assert(array1.Len(), 3) 527 }) 528 } 529 530 func TestStrArray_RLockFunc(t *testing.T) { 531 gtest.C(t, func(t *gtest.T) { 532 s1 := []string{"a", "b", "c", "d"} 533 a1 := garray.NewStrArrayFrom(s1, true) 534 535 ch1 := make(chan int64, 3) 536 ch2 := make(chan int64, 1) 537 //go1 538 go a1.RLockFunc(func(n1 []string) { //读锁 539 time.Sleep(2 * time.Second) //暂停1秒 540 n1[2] = "g" 541 ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 542 }) 543 544 //go2 545 go func() { 546 time.Sleep(100 * time.Millisecond) //故意暂停0.01秒,等go1执行锁后,再开始执行. 547 ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 548 a1.Len() 549 ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 550 }() 551 552 t1 := <-ch1 553 t2 := <-ch1 554 <-ch2 //等待go1完成 555 556 // 防止ci抖动,以豪秒为单位 557 t.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。 558 t.Assert(a1.Contains("g"), true) 559 }) 560 } 561 562 func TestStrArray_SortFunc(t *testing.T) { 563 gtest.C(t, func(t *gtest.T) { 564 s1 := []string{"a", "d", "c", "b"} 565 a1 := garray.NewStrArrayFrom(s1) 566 func1 := func(v1, v2 string) bool { 567 return v1 < v2 568 } 569 a11 := a1.SortFunc(func1) 570 t.Assert(a11, []string{"a", "b", "c", "d"}) 571 }) 572 } 573 574 func TestStrArray_LockFunc(t *testing.T) { 575 gtest.C(t, func(t *gtest.T) { 576 s1 := []string{"a", "b", "c", "d"} 577 a1 := garray.NewStrArrayFrom(s1, true) 578 579 ch1 := make(chan int64, 3) 580 ch2 := make(chan int64, 3) 581 //go1 582 go a1.LockFunc(func(n1 []string) { //读写锁 583 time.Sleep(2 * time.Second) //暂停2秒 584 n1[2] = "g" 585 ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 586 }) 587 588 //go2 589 go func() { 590 time.Sleep(100 * time.Millisecond) //故意暂停0.01秒,等go1执行锁后,再开始执行. 591 ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 592 a1.Len() 593 ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000) 594 }() 595 596 t1 := <-ch1 597 t2 := <-ch1 598 <-ch2 //等待go1完成 599 600 // 防止ci抖动,以豪秒为单位 601 t.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。 602 t.Assert(a1.Contains("g"), true) 603 }) 604 } 605 606 func TestStrArray_Json(t *testing.T) { 607 // array pointer 608 gtest.C(t, func(t *gtest.T) { 609 s1 := []string{"a", "b", "d", "c"} 610 a1 := garray.NewStrArrayFrom(s1) 611 b1, err1 := json.Marshal(a1) 612 b2, err2 := json.Marshal(s1) 613 t.Assert(b1, b2) 614 t.Assert(err1, err2) 615 616 a2 := garray.NewStrArray() 617 err1 = json.UnmarshalUseNumber(b2, &a2) 618 t.Assert(a2.Slice(), s1) 619 620 var a3 garray.StrArray 621 err := json.UnmarshalUseNumber(b2, &a3) 622 t.Assert(err, nil) 623 t.Assert(a3.Slice(), s1) 624 }) 625 // array value 626 gtest.C(t, func(t *gtest.T) { 627 s1 := []string{"a", "b", "d", "c"} 628 a1 := *garray.NewStrArrayFrom(s1) 629 b1, err1 := json.Marshal(a1) 630 b2, err2 := json.Marshal(s1) 631 t.Assert(b1, b2) 632 t.Assert(err1, err2) 633 634 a2 := garray.NewStrArray() 635 err1 = json.UnmarshalUseNumber(b2, &a2) 636 t.Assert(a2.Slice(), s1) 637 638 var a3 garray.StrArray 639 err := json.UnmarshalUseNumber(b2, &a3) 640 t.Assert(err, nil) 641 t.Assert(a3.Slice(), s1) 642 }) 643 // array pointer 644 gtest.C(t, func(t *gtest.T) { 645 type User struct { 646 Name string 647 Scores *garray.StrArray 648 } 649 data := g.Map{ 650 "Name": "john", 651 "Scores": []string{"A+", "A", "A"}, 652 } 653 b, err := json.Marshal(data) 654 t.Assert(err, nil) 655 656 user := new(User) 657 err = json.UnmarshalUseNumber(b, user) 658 t.Assert(err, nil) 659 t.Assert(user.Name, data["Name"]) 660 t.Assert(user.Scores, data["Scores"]) 661 }) 662 // array value 663 gtest.C(t, func(t *gtest.T) { 664 type User struct { 665 Name string 666 Scores garray.StrArray 667 } 668 data := g.Map{ 669 "Name": "john", 670 "Scores": []string{"A+", "A", "A"}, 671 } 672 b, err := json.Marshal(data) 673 t.Assert(err, nil) 674 675 user := new(User) 676 err = json.UnmarshalUseNumber(b, user) 677 t.Assert(err, nil) 678 t.Assert(user.Name, data["Name"]) 679 t.Assert(user.Scores, data["Scores"]) 680 }) 681 } 682 683 func TestStrArray_Iterator(t *testing.T) { 684 slice := g.SliceStr{"a", "b", "d", "c"} 685 array := garray.NewStrArrayFrom(slice) 686 gtest.C(t, func(t *gtest.T) { 687 array.Iterator(func(k int, v string) bool { 688 t.Assert(v, slice[k]) 689 return true 690 }) 691 }) 692 gtest.C(t, func(t *gtest.T) { 693 array.IteratorAsc(func(k int, v string) bool { 694 t.Assert(v, slice[k]) 695 return true 696 }) 697 }) 698 gtest.C(t, func(t *gtest.T) { 699 array.IteratorDesc(func(k int, v string) bool { 700 t.Assert(v, slice[k]) 701 return true 702 }) 703 }) 704 gtest.C(t, func(t *gtest.T) { 705 index := 0 706 array.Iterator(func(k int, v string) bool { 707 index++ 708 return false 709 }) 710 t.Assert(index, 1) 711 }) 712 gtest.C(t, func(t *gtest.T) { 713 index := 0 714 array.IteratorAsc(func(k int, v string) bool { 715 index++ 716 return false 717 }) 718 t.Assert(index, 1) 719 }) 720 gtest.C(t, func(t *gtest.T) { 721 index := 0 722 array.IteratorDesc(func(k int, v string) bool { 723 index++ 724 return false 725 }) 726 t.Assert(index, 1) 727 }) 728 } 729 730 func TestStrArray_RemoveValue(t *testing.T) { 731 slice := g.SliceStr{"a", "b", "d", "c"} 732 array := garray.NewStrArrayFrom(slice) 733 gtest.C(t, func(t *gtest.T) { 734 t.Assert(array.RemoveValue("e"), false) 735 t.Assert(array.RemoveValue("b"), true) 736 t.Assert(array.RemoveValue("a"), true) 737 t.Assert(array.RemoveValue("c"), true) 738 t.Assert(array.RemoveValue("f"), false) 739 }) 740 } 741 742 func TestStrArray_UnmarshalValue(t *testing.T) { 743 type V struct { 744 Name string 745 Array *garray.StrArray 746 } 747 // JSON 748 gtest.C(t, func(t *gtest.T) { 749 var v *V 750 err := gconv.Struct(g.Map{ 751 "name": "john", 752 "array": []byte(`["1","2","3"]`), 753 }, &v) 754 t.Assert(err, nil) 755 t.Assert(v.Name, "john") 756 t.Assert(v.Array.Slice(), g.SliceStr{"1", "2", "3"}) 757 }) 758 // Map 759 gtest.C(t, func(t *gtest.T) { 760 var v *V 761 err := gconv.Struct(g.Map{ 762 "name": "john", 763 "array": g.SliceStr{"1", "2", "3"}, 764 }, &v) 765 t.Assert(err, nil) 766 t.Assert(v.Name, "john") 767 t.Assert(v.Array.Slice(), g.SliceStr{"1", "2", "3"}) 768 }) 769 } 770 771 func TestStrArray_FilterEmpty(t *testing.T) { 772 gtest.C(t, func(t *gtest.T) { 773 array := garray.NewStrArrayFrom(g.SliceStr{"", "1", "2", "0"}) 774 t.Assert(array.FilterEmpty(), g.SliceStr{"1", "2", "0"}) 775 }) 776 gtest.C(t, func(t *gtest.T) { 777 array := garray.NewStrArrayFrom(g.SliceStr{"1", "2"}) 778 t.Assert(array.FilterEmpty(), g.SliceStr{"1", "2"}) 779 }) 780 } 781 782 func TestStrArray_Walk(t *testing.T) { 783 gtest.C(t, func(t *gtest.T) { 784 array := garray.NewStrArrayFrom(g.SliceStr{"1", "2"}) 785 t.Assert(array.Walk(func(value string) string { 786 return "key-" + value 787 }), g.Slice{"key-1", "key-2"}) 788 }) 789 }