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