code.gitea.io/gitea@v1.19.3/modules/gitgraph/graph_test.go (about) 1 // Copyright 2016 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package gitgraph 5 6 import ( 7 "bytes" 8 "fmt" 9 "strings" 10 "testing" 11 12 "code.gitea.io/gitea/modules/git" 13 ) 14 15 func BenchmarkGetCommitGraph(b *testing.B) { 16 currentRepo, err := git.OpenRepository(git.DefaultContext, ".") 17 if err != nil || currentRepo == nil { 18 b.Error("Could not open repository") 19 } 20 defer currentRepo.Close() 21 22 for i := 0; i < b.N; i++ { 23 graph, err := GetCommitGraph(currentRepo, 1, 0, false, nil, nil) 24 if err != nil { 25 b.Error("Could get commit graph") 26 } 27 28 if len(graph.Commits) < 100 { 29 b.Error("Should get 100 log lines.") 30 } 31 } 32 } 33 34 func BenchmarkParseCommitString(b *testing.B) { 35 testString := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|4e61bac|Add route for graph" 36 37 parser := &Parser{} 38 parser.Reset() 39 for i := 0; i < b.N; i++ { 40 parser.Reset() 41 graph := NewGraph() 42 if err := parser.AddLineToGraph(graph, 0, []byte(testString)); err != nil { 43 b.Error("could not parse teststring") 44 } 45 if graph.Flows[1].Commits[0].Rev != "4e61bacab44e9b4730e44a6615d04098dd3a8eaf" { 46 b.Error("Did not get expected data") 47 } 48 } 49 } 50 51 func BenchmarkParseGlyphs(b *testing.B) { 52 parser := &Parser{} 53 parser.Reset() 54 tgBytes := []byte(testglyphs) 55 var tg []byte 56 for i := 0; i < b.N; i++ { 57 parser.Reset() 58 tg = tgBytes 59 idx := bytes.Index(tg, []byte("\n")) 60 for idx > 0 { 61 parser.ParseGlyphs(tg[:idx]) 62 tg = tg[idx+1:] 63 idx = bytes.Index(tg, []byte("\n")) 64 } 65 } 66 } 67 68 func TestReleaseUnusedColors(t *testing.T) { 69 testcases := []struct { 70 availableColors []int 71 oldColors []int 72 firstInUse int // these values have to be either be correct or suggest less is 73 firstAvailable int // available than possibly is - i.e. you cannot say 10 is available when it 74 }{ 75 { 76 availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 77 oldColors: []int{1, 1, 1, 1, 1}, 78 firstAvailable: -1, 79 firstInUse: 1, 80 }, 81 { 82 availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 83 oldColors: []int{1, 2, 3, 4}, 84 firstAvailable: 6, 85 firstInUse: 0, 86 }, 87 { 88 availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 89 oldColors: []int{6, 0, 3, 5, 3, 4, 0, 0}, 90 firstAvailable: 6, 91 firstInUse: 0, 92 }, 93 { 94 availableColors: []int{1, 2, 3, 4, 5, 6, 7}, 95 oldColors: []int{6, 1, 3, 5, 3, 4, 2, 7}, 96 firstAvailable: -1, 97 firstInUse: 0, 98 }, 99 { 100 availableColors: []int{1, 2, 3, 4, 5, 6, 7}, 101 oldColors: []int{6, 0, 3, 5, 3, 4, 2, 7}, 102 firstAvailable: -1, 103 firstInUse: 0, 104 }, 105 } 106 for _, testcase := range testcases { 107 parser := &Parser{} 108 parser.Reset() 109 parser.availableColors = append([]int{}, testcase.availableColors...) 110 parser.oldColors = append(parser.oldColors, testcase.oldColors...) 111 parser.firstAvailable = testcase.firstAvailable 112 parser.firstInUse = testcase.firstInUse 113 parser.releaseUnusedColors() 114 115 if parser.firstAvailable == -1 { 116 // All in use 117 for _, color := range parser.availableColors { 118 found := false 119 for _, oldColor := range parser.oldColors { 120 if oldColor == color { 121 found = true 122 break 123 } 124 } 125 if !found { 126 t.Errorf("In testcase:\n%d\t%d\t%d %d =>\n%d\t%d\t%d %d: %d should be available but is not", 127 testcase.availableColors, 128 testcase.oldColors, 129 testcase.firstAvailable, 130 testcase.firstInUse, 131 parser.availableColors, 132 parser.oldColors, 133 parser.firstAvailable, 134 parser.firstInUse, 135 color) 136 } 137 } 138 } else if parser.firstInUse != -1 { 139 // Some in use 140 for i := parser.firstInUse; i != parser.firstAvailable; i = (i + 1) % len(parser.availableColors) { 141 color := parser.availableColors[i] 142 found := false 143 for _, oldColor := range parser.oldColors { 144 if oldColor == color { 145 found = true 146 break 147 } 148 } 149 if !found { 150 t.Errorf("In testcase:\n%d\t%d\t%d %d =>\n%d\t%d\t%d %d: %d should be available but is not", 151 testcase.availableColors, 152 testcase.oldColors, 153 testcase.firstAvailable, 154 testcase.firstInUse, 155 parser.availableColors, 156 parser.oldColors, 157 parser.firstAvailable, 158 parser.firstInUse, 159 color) 160 } 161 } 162 for i := parser.firstAvailable; i != parser.firstInUse; i = (i + 1) % len(parser.availableColors) { 163 color := parser.availableColors[i] 164 found := false 165 for _, oldColor := range parser.oldColors { 166 if oldColor == color { 167 found = true 168 break 169 } 170 } 171 if found { 172 t.Errorf("In testcase:\n%d\t%d\t%d %d =>\n%d\t%d\t%d %d: %d should not be available but is", 173 testcase.availableColors, 174 testcase.oldColors, 175 testcase.firstAvailable, 176 testcase.firstInUse, 177 parser.availableColors, 178 parser.oldColors, 179 parser.firstAvailable, 180 parser.firstInUse, 181 color) 182 } 183 } 184 } else { 185 // None in use 186 for _, color := range parser.oldColors { 187 if color != 0 { 188 t.Errorf("In testcase:\n%d\t%d\t%d %d =>\n%d\t%d\t%d %d: %d should not be available but is", 189 testcase.availableColors, 190 testcase.oldColors, 191 testcase.firstAvailable, 192 testcase.firstInUse, 193 parser.availableColors, 194 parser.oldColors, 195 parser.firstAvailable, 196 parser.firstInUse, 197 color) 198 } 199 } 200 } 201 } 202 } 203 204 func TestParseGlyphs(t *testing.T) { 205 parser := &Parser{} 206 parser.Reset() 207 tgBytes := []byte(testglyphs) 208 tg := tgBytes 209 idx := bytes.Index(tg, []byte("\n")) 210 row := 0 211 for idx > 0 { 212 parser.ParseGlyphs(tg[:idx]) 213 tg = tg[idx+1:] 214 idx = bytes.Index(tg, []byte("\n")) 215 if parser.flows[0] != 1 { 216 t.Errorf("First column flow should be 1 but was %d", parser.flows[0]) 217 } 218 colorToFlow := map[int]int64{} 219 flowToColor := map[int64]int{} 220 221 for i, flow := range parser.flows { 222 if flow == 0 { 223 continue 224 } 225 color := parser.colors[i] 226 227 if fColor, in := flowToColor[flow]; in && fColor != color { 228 t.Errorf("Row %d column %d flow %d has color %d but should be %d", row, i, flow, color, fColor) 229 } 230 flowToColor[flow] = color 231 if cFlow, in := colorToFlow[color]; in && cFlow != flow { 232 t.Errorf("Row %d column %d flow %d has color %d but conflicts with flow %d", row, i, flow, color, cFlow) 233 } 234 colorToFlow[color] = flow 235 } 236 row++ 237 } 238 if len(parser.availableColors) != 9 { 239 t.Errorf("Expected 9 colors but have %d", len(parser.availableColors)) 240 } 241 } 242 243 func TestCommitStringParsing(t *testing.T) { 244 dataFirstPart := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|4e61bac|" 245 tests := []struct { 246 shouldPass bool 247 testName string 248 commitMessage string 249 }{ 250 {true, "normal", "not a fancy message"}, 251 {true, "extra pipe", "An extra pipe: |"}, 252 {true, "extra 'Data:'", "DATA: might be trouble"}, 253 } 254 255 for _, test := range tests { 256 t.Run(test.testName, func(t *testing.T) { 257 testString := fmt.Sprintf("%s%s", dataFirstPart, test.commitMessage) 258 idx := strings.Index(testString, "DATA:") 259 commit, err := NewCommit(0, 0, []byte(testString[idx+5:])) 260 if err != nil && test.shouldPass { 261 t.Errorf("Could not parse %s", testString) 262 return 263 } 264 265 if test.commitMessage != commit.Subject { 266 t.Errorf("%s does not match %s", test.commitMessage, commit.Subject) 267 } 268 }) 269 } 270 } 271 272 var testglyphs = `* 273 * 274 * 275 * 276 * 277 * 278 * 279 * 280 |\ 281 * | 282 * | 283 * | 284 * | 285 * | 286 | * 287 * | 288 | * 289 | |\ 290 * | | 291 | | * 292 | | |\ 293 * | | \ 294 |\ \ \ \ 295 | * | | | 296 | |\| | | 297 * | | | | 298 |/ / / / 299 | | | * 300 | * | | 301 | * | | 302 | * | | 303 * | | | 304 * | | | 305 * | | | 306 * | | | 307 * | | | 308 |\ \ \ \ 309 | | * | | 310 | | |\| | 311 | | | * | 312 | | | | * 313 * | | | | 314 * | | | | 315 * | | | | 316 * | | | | 317 * | | | | 318 |\ \ \ \ \ 319 | * | | | | 320 |/| | | | | 321 | | |/ / / 322 | |/| | | 323 | | | | * 324 | * | | | 325 |/| | | | 326 | * | | | 327 |/| | | | 328 | | |/ / 329 | |/| | 330 | * | | 331 | * | | 332 | |\ \ \ 333 | | * | | 334 | |/| | | 335 | | | |/ 336 | | |/| 337 | * | | 338 | * | | 339 | * | | 340 | | * | 341 | | |\ \ 342 | | | * | 343 | | |/| | 344 | | | * | 345 | | | |\ \ 346 | | | | * | 347 | | | |/| | 348 | | * | | | 349 | | * | | | 350 | | |\ \ \ \ 351 | | | * | | | 352 | | |/| | | | 353 | | | | | * | 354 | | | | |/ / 355 * | | | / / 356 |/ / / / / 357 * | | | | 358 |\ \ \ \ \ 359 | * | | | | 360 |/| | | | | 361 | * | | | | 362 | * | | | | 363 | |\ \ \ \ \ 364 | | | * \ \ \ 365 | | | |\ \ \ \ 366 | | | | * | | | 367 | | | |/| | | | 368 | | | | | |/ / 369 | | | | |/| | 370 * | | | | | | 371 * | | | | | | 372 * | | | | | | 373 | | | | * | | 374 * | | | | | | 375 | | * | | | | 376 | |/| | | | | 377 * | | | | | | 378 | |/ / / / / 379 |/| | | | | 380 | | | | * | 381 | | | |/ / 382 | | |/| | 383 | * | | | 384 | | | | * 385 | | * | | 386 | | |\ \ \ 387 | | | * | | 388 | | |/| | | 389 | | | |/ / 390 | | | * | 391 | | * | | 392 | | |\ \ \ 393 | | | * | | 394 | | |/| | | 395 | | | |/ / 396 | | | * | 397 * | | | | 398 |\ \ \ \ \ 399 | * \ \ \ \ 400 | |\ \ \ \ \ 401 | | | |/ / / 402 | | |/| | | 403 | | | | * | 404 | | | | * | 405 * | | | | | 406 * | | | | | 407 |/ / / / / 408 | | | * | 409 * | | | | 410 * | | | | 411 * | | | | 412 * | | | | 413 |\ \ \ \ \ 414 | * | | | | 415 |/| | | | | 416 | | * | | | 417 | | |\ \ \ \ 418 | | | * | | | 419 | | |/| | | | 420 | |/| | |/ / 421 | | | |/| | 422 | | | | | * 423 | |_|_|_|/ 424 |/| | | | 425 | | * | | 426 | |/ / / 427 * | | | 428 * | | | 429 | | * | 430 * | | | 431 * | | | 432 | * | | 433 | | * | 434 | * | | 435 * | | | 436 |\ \ \ \ 437 | * | | | 438 |/| | | | 439 | |/ / / 440 | * | | 441 | |\ \ \ 442 | | * | | 443 | |/| | | 444 | | |/ / 445 | | * | 446 | | |\ \ 447 | | | * | 448 | | |/| | 449 * | | | | 450 * | | | | 451 |\ \ \ \ \ 452 | * | | | | 453 |/| | | | | 454 | | * | | | 455 | | * | | | 456 | | * | | | 457 | |/ / / / 458 | * | | | 459 | |\ \ \ \ 460 | | * | | | 461 | |/| | | | 462 * | | | | | 463 * | | | | | 464 * | | | | | 465 * | | | | | 466 * | | | | | 467 | | | | * | 468 * | | | | | 469 |\ \ \ \ \ \ 470 | * | | | | | 471 |/| | | | | | 472 | | | | | * | 473 | | | | |/ / 474 * | | | | | 475 |\ \ \ \ \ \ 476 * | | | | | | 477 * | | | | | | 478 | | | | * | | 479 * | | | | | | 480 * | | | | | | 481 |\ \ \ \ \ \ \ 482 | | |_|_|/ / / 483 | |/| | | | | 484 | | | | * | | 485 | | | | * | | 486 | | | | * | | 487 | | | | * | | 488 | | | | * | | 489 | | | | * | | 490 | | | |/ / / 491 | | | * | | 492 | | | * | | 493 | | | * | | 494 | | |/| | | 495 | | | * | | 496 | | |/| | | 497 | | | |/ / 498 | | * | | 499 | |/| | | 500 | | | * | 501 | | |/ / 502 | | * | 503 | * | | 504 | |\ \ \ 505 | * | | | 506 | | * | | 507 | |/| | | 508 | | |/ / 509 | | * | 510 | | |\ \ 511 | | * | | 512 * | | | | 513 |\| | | | 514 | * | | | 515 | * | | | 516 | * | | | 517 | | * | | 518 | * | | | 519 | |\| | | 520 | * | | | 521 | | * | | 522 | | * | | 523 | * | | | 524 | * | | | 525 | * | | | 526 | * | | | 527 | * | | | 528 | * | | | 529 | * | | | 530 | * | | | 531 | | * | | 532 | * | | | 533 | * | | | 534 | * | | | 535 | * | | | 536 | | * | | 537 * | | | | 538 |\| | | | 539 | | * | | 540 | * | | | 541 | |\| | | 542 | | * | | 543 | | * | | 544 | | * | | 545 | | | * | 546 * | | | | 547 |\| | | | 548 | | * | | 549 | | |/ / 550 | * | | 551 | * | | 552 | |\| | 553 * | | | 554 |\| | | 555 | | * | 556 | | * | 557 | | * | 558 | * | | 559 | | * | 560 | * | | 561 | | * | 562 | | * | 563 | | * | 564 | * | | 565 | * | | 566 | * | | 567 | * | | 568 | * | | 569 | * | | 570 | * | | 571 * | | | 572 |\| | | 573 | * | | 574 | |\| | 575 | | * | 576 | | |\ \ 577 * | | | | 578 |\| | | | 579 | * | | | 580 | |\| | | 581 | | * | | 582 | | | * | 583 | | |/ / 584 * | | | 585 * | | | 586 |\| | | 587 | * | | 588 | |\| | 589 | | * | 590 | | * | 591 | | * | 592 | | | * 593 * | | | 594 |\| | | 595 | * | | 596 | * | | 597 | | | * 598 | | | |\ 599 * | | | | 600 | |_|_|/ 601 |/| | | 602 | * | | 603 | |\| | 604 | | * | 605 | | * | 606 | | * | 607 | | * | 608 | | * | 609 | * | | 610 * | | | 611 |\| | | 612 | * | | 613 |/| | | 614 | |/ / 615 | * | 616 | |\ \ 617 | * | | 618 | * | | 619 * | | | 620 |\| | | 621 | | * | 622 | * | | 623 | * | | 624 | * | | 625 * | | | 626 |\| | | 627 | * | | 628 | * | | 629 | | * | 630 | | |\ \ 631 | | |/ / 632 | |/| | 633 | * | | 634 * | | | 635 |\| | | 636 | * | | 637 * | | | 638 |\| | | 639 | * | | 640 | |\ \ \ 641 | * | | | 642 | * | | | 643 | | | * | 644 | * | | | 645 | * | | | 646 | | |/ / 647 | |/| | 648 | | * | 649 * | | | 650 |\| | | 651 | * | | 652 | * | | 653 | * | | 654 | * | | 655 | * | | 656 | |\ \ \ 657 * | | | | 658 |\| | | | 659 | * | | | 660 | * | | | 661 * | | | | 662 * | | | | 663 |\| | | | 664 | | | | * 665 | | | | |\ 666 | |_|_|_|/ 667 |/| | | | 668 | * | | | 669 * | | | | 670 * | | | | 671 |\| | | | 672 | * | | | 673 | |\ \ \ \ 674 | | | |/ / 675 | | |/| | 676 | * | | | 677 | * | | | 678 | * | | | 679 | * | | | 680 | | * | | 681 | | | * | 682 | | |/ / 683 | |/| | 684 * | | | 685 |\| | | 686 | * | | 687 | * | | 688 | * | | 689 | * | | 690 | * | | 691 * | | | 692 |\| | | 693 | * | | 694 | * | | 695 * | | | 696 | * | | 697 | * | | 698 | * | | 699 * | | | 700 * | | | 701 * | | | 702 |\| | | 703 | * | | 704 * | | | 705 * | | | 706 * | | | 707 * | | | 708 | | | * 709 * | | | 710 |\| | | 711 | * | | 712 | * | | 713 | * | | 714 `