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