123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- // Copyright 2016 The Gitea Authors. All rights reserved.
- // Use of this source code is governed by a MIT-style
- // license that can be found in the LICENSE file.
-
- package gitgraph
-
- import (
- "bytes"
- "fmt"
- "strings"
- "testing"
-
- "code.gitea.io/gitea/modules/git"
- )
-
- func BenchmarkGetCommitGraph(b *testing.B) {
-
- currentRepo, err := git.OpenRepository(".")
- if err != nil || currentRepo == nil {
- b.Error("Could not open repository")
- }
- defer currentRepo.Close()
-
- for i := 0; i < b.N; i++ {
- graph, err := GetCommitGraph(currentRepo, 1, 0, false, nil, nil)
- if err != nil {
- b.Error("Could get commit graph")
- }
-
- if len(graph.Commits) < 100 {
- b.Error("Should get 100 log lines.")
- }
- }
- }
-
- func BenchmarkParseCommitString(b *testing.B) {
- testString := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|4e61bac|Add route for graph"
-
- parser := &Parser{}
- parser.Reset()
- for i := 0; i < b.N; i++ {
- parser.Reset()
- graph := NewGraph()
- if err := parser.AddLineToGraph(graph, 0, []byte(testString)); err != nil {
- b.Error("could not parse teststring")
- }
- if graph.Flows[1].Commits[0].Rev != "4e61bacab44e9b4730e44a6615d04098dd3a8eaf" {
- b.Error("Did not get expected data")
- }
- }
- }
-
- func BenchmarkParseGlyphs(b *testing.B) {
- parser := &Parser{}
- parser.Reset()
- tgBytes := []byte(testglyphs)
- tg := tgBytes
- for i := 0; i < b.N; i++ {
- parser.Reset()
- tg = tgBytes
- idx := bytes.Index(tg, []byte("\n"))
- for idx > 0 {
- parser.ParseGlyphs(tg[:idx])
- tg = tg[idx+1:]
- idx = bytes.Index(tg, []byte("\n"))
- }
- }
- }
-
- func TestReleaseUnusedColors(t *testing.T) {
- testcases := []struct {
- availableColors []int
- oldColors []int
- firstInUse int // these values have to be either be correct or suggest less is
- firstAvailable int // available than possibly is - i.e. you cannot say 10 is available when it
- }{
- {
- availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
- oldColors: []int{1, 1, 1, 1, 1},
- firstAvailable: -1,
- firstInUse: 1,
- },
- {
- availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
- oldColors: []int{1, 2, 3, 4},
- firstAvailable: 6,
- firstInUse: 0,
- },
- {
- availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
- oldColors: []int{6, 0, 3, 5, 3, 4, 0, 0},
- firstAvailable: 6,
- firstInUse: 0,
- },
- {
- availableColors: []int{1, 2, 3, 4, 5, 6, 7},
- oldColors: []int{6, 1, 3, 5, 3, 4, 2, 7},
- firstAvailable: -1,
- firstInUse: 0,
- },
- {
- availableColors: []int{1, 2, 3, 4, 5, 6, 7},
- oldColors: []int{6, 0, 3, 5, 3, 4, 2, 7},
- firstAvailable: -1,
- firstInUse: 0,
- },
- }
- for _, testcase := range testcases {
- parser := &Parser{}
- parser.Reset()
- parser.availableColors = append([]int{}, testcase.availableColors...)
- parser.oldColors = append(parser.oldColors, testcase.oldColors...)
- parser.firstAvailable = testcase.firstAvailable
- parser.firstInUse = testcase.firstInUse
- parser.releaseUnusedColors()
-
- if parser.firstAvailable == -1 {
- // All in use
- for _, color := range parser.availableColors {
- found := false
- for _, oldColor := range parser.oldColors {
- if oldColor == color {
- found = true
- break
- }
- }
- if !found {
- 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",
- testcase.availableColors,
- testcase.oldColors,
- testcase.firstAvailable,
- testcase.firstInUse,
- parser.availableColors,
- parser.oldColors,
- parser.firstAvailable,
- parser.firstInUse,
- color)
- }
- }
- } else if parser.firstInUse != -1 {
- // Some in use
- for i := parser.firstInUse; i != parser.firstAvailable; i = (i + 1) % len(parser.availableColors) {
- color := parser.availableColors[i]
- found := false
- for _, oldColor := range parser.oldColors {
- if oldColor == color {
- found = true
- break
- }
- }
- if !found {
- 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",
- testcase.availableColors,
- testcase.oldColors,
- testcase.firstAvailable,
- testcase.firstInUse,
- parser.availableColors,
- parser.oldColors,
- parser.firstAvailable,
- parser.firstInUse,
- color)
- }
- }
- for i := parser.firstAvailable; i != parser.firstInUse; i = (i + 1) % len(parser.availableColors) {
- color := parser.availableColors[i]
- found := false
- for _, oldColor := range parser.oldColors {
- if oldColor == color {
- found = true
- break
- }
- }
- if found {
- 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",
- testcase.availableColors,
- testcase.oldColors,
- testcase.firstAvailable,
- testcase.firstInUse,
- parser.availableColors,
- parser.oldColors,
- parser.firstAvailable,
- parser.firstInUse,
- color)
- }
- }
- } else {
- // None in use
- for _, color := range parser.oldColors {
- if color != 0 {
- 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",
- testcase.availableColors,
- testcase.oldColors,
- testcase.firstAvailable,
- testcase.firstInUse,
- parser.availableColors,
- parser.oldColors,
- parser.firstAvailable,
- parser.firstInUse,
- color)
- }
- }
- }
- }
- }
-
- func TestParseGlyphs(t *testing.T) {
- parser := &Parser{}
- parser.Reset()
- tgBytes := []byte(testglyphs)
- tg := tgBytes
- idx := bytes.Index(tg, []byte("\n"))
- row := 0
- for idx > 0 {
- parser.ParseGlyphs(tg[:idx])
- tg = tg[idx+1:]
- idx = bytes.Index(tg, []byte("\n"))
- if parser.flows[0] != 1 {
- t.Errorf("First column flow should be 1 but was %d", parser.flows[0])
- }
- colorToFlow := map[int]int64{}
- flowToColor := map[int64]int{}
-
- for i, flow := range parser.flows {
- if flow == 0 {
- continue
- }
- color := parser.colors[i]
-
- if fColor, in := flowToColor[flow]; in && fColor != color {
- t.Errorf("Row %d column %d flow %d has color %d but should be %d", row, i, flow, color, fColor)
- }
- flowToColor[flow] = color
- if cFlow, in := colorToFlow[color]; in && cFlow != flow {
- t.Errorf("Row %d column %d flow %d has color %d but conflicts with flow %d", row, i, flow, color, cFlow)
- }
- colorToFlow[color] = flow
- }
- row++
- }
- if len(parser.availableColors) != 9 {
- t.Errorf("Expected 9 colors but have %d", len(parser.availableColors))
- }
- }
-
- func TestCommitStringParsing(t *testing.T) {
- dataFirstPart := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|4e61bac|"
- tests := []struct {
- shouldPass bool
- testName string
- commitMessage string
- }{
- {true, "normal", "not a fancy message"},
- {true, "extra pipe", "An extra pipe: |"},
- {true, "extra 'Data:'", "DATA: might be trouble"},
- }
-
- for _, test := range tests {
-
- t.Run(test.testName, func(t *testing.T) {
- testString := fmt.Sprintf("%s%s", dataFirstPart, test.commitMessage)
- idx := strings.Index(testString, "DATA:")
- commit, err := NewCommit(0, 0, []byte(testString[idx+5:]))
- if err != nil && test.shouldPass {
- t.Errorf("Could not parse %s", testString)
- return
- }
-
- if test.commitMessage != commit.Subject {
- t.Errorf("%s does not match %s", test.commitMessage, commit.Subject)
- }
- })
- }
- }
-
- var testglyphs = `*
- *
- *
- *
- *
- *
- *
- *
- |\
- * |
- * |
- * |
- * |
- * |
- | *
- * |
- | *
- | |\
- * | |
- | | *
- | | |\
- * | | \
- |\ \ \ \
- | * | | |
- | |\| | |
- * | | | |
- |/ / / /
- | | | *
- | * | |
- | * | |
- | * | |
- * | | |
- * | | |
- * | | |
- * | | |
- * | | |
- |\ \ \ \
- | | * | |
- | | |\| |
- | | | * |
- | | | | *
- * | | | |
- * | | | |
- * | | | |
- * | | | |
- * | | | |
- |\ \ \ \ \
- | * | | | |
- |/| | | | |
- | | |/ / /
- | |/| | |
- | | | | *
- | * | | |
- |/| | | |
- | * | | |
- |/| | | |
- | | |/ /
- | |/| |
- | * | |
- | * | |
- | |\ \ \
- | | * | |
- | |/| | |
- | | | |/
- | | |/|
- | * | |
- | * | |
- | * | |
- | | * |
- | | |\ \
- | | | * |
- | | |/| |
- | | | * |
- | | | |\ \
- | | | | * |
- | | | |/| |
- | | * | | |
- | | * | | |
- | | |\ \ \ \
- | | | * | | |
- | | |/| | | |
- | | | | | * |
- | | | | |/ /
- * | | | / /
- |/ / / / /
- * | | | |
- |\ \ \ \ \
- | * | | | |
- |/| | | | |
- | * | | | |
- | * | | | |
- | |\ \ \ \ \
- | | | * \ \ \
- | | | |\ \ \ \
- | | | | * | | |
- | | | |/| | | |
- | | | | | |/ /
- | | | | |/| |
- * | | | | | |
- * | | | | | |
- * | | | | | |
- | | | | * | |
- * | | | | | |
- | | * | | | |
- | |/| | | | |
- * | | | | | |
- | |/ / / / /
- |/| | | | |
- | | | | * |
- | | | |/ /
- | | |/| |
- | * | | |
- | | | | *
- | | * | |
- | | |\ \ \
- | | | * | |
- | | |/| | |
- | | | |/ /
- | | | * |
- | | * | |
- | | |\ \ \
- | | | * | |
- | | |/| | |
- | | | |/ /
- | | | * |
- * | | | |
- |\ \ \ \ \
- | * \ \ \ \
- | |\ \ \ \ \
- | | | |/ / /
- | | |/| | |
- | | | | * |
- | | | | * |
- * | | | | |
- * | | | | |
- |/ / / / /
- | | | * |
- * | | | |
- * | | | |
- * | | | |
- * | | | |
- |\ \ \ \ \
- | * | | | |
- |/| | | | |
- | | * | | |
- | | |\ \ \ \
- | | | * | | |
- | | |/| | | |
- | |/| | |/ /
- | | | |/| |
- | | | | | *
- | |_|_|_|/
- |/| | | |
- | | * | |
- | |/ / /
- * | | |
- * | | |
- | | * |
- * | | |
- * | | |
- | * | |
- | | * |
- | * | |
- * | | |
- |\ \ \ \
- | * | | |
- |/| | | |
- | |/ / /
- | * | |
- | |\ \ \
- | | * | |
- | |/| | |
- | | |/ /
- | | * |
- | | |\ \
- | | | * |
- | | |/| |
- * | | | |
- * | | | |
- |\ \ \ \ \
- | * | | | |
- |/| | | | |
- | | * | | |
- | | * | | |
- | | * | | |
- | |/ / / /
- | * | | |
- | |\ \ \ \
- | | * | | |
- | |/| | | |
- * | | | | |
- * | | | | |
- * | | | | |
- * | | | | |
- * | | | | |
- | | | | * |
- * | | | | |
- |\ \ \ \ \ \
- | * | | | | |
- |/| | | | | |
- | | | | | * |
- | | | | |/ /
- * | | | | |
- |\ \ \ \ \ \
- * | | | | | |
- * | | | | | |
- | | | | * | |
- * | | | | | |
- * | | | | | |
- |\ \ \ \ \ \ \
- | | |_|_|/ / /
- | |/| | | | |
- | | | | * | |
- | | | | * | |
- | | | | * | |
- | | | | * | |
- | | | | * | |
- | | | | * | |
- | | | |/ / /
- | | | * | |
- | | | * | |
- | | | * | |
- | | |/| | |
- | | | * | |
- | | |/| | |
- | | | |/ /
- | | * | |
- | |/| | |
- | | | * |
- | | |/ /
- | | * |
- | * | |
- | |\ \ \
- | * | | |
- | | * | |
- | |/| | |
- | | |/ /
- | | * |
- | | |\ \
- | | * | |
- * | | | |
- |\| | | |
- | * | | |
- | * | | |
- | * | | |
- | | * | |
- | * | | |
- | |\| | |
- | * | | |
- | | * | |
- | | * | |
- | * | | |
- | * | | |
- | * | | |
- | * | | |
- | * | | |
- | * | | |
- | * | | |
- | * | | |
- | | * | |
- | * | | |
- | * | | |
- | * | | |
- | * | | |
- | | * | |
- * | | | |
- |\| | | |
- | | * | |
- | * | | |
- | |\| | |
- | | * | |
- | | * | |
- | | * | |
- | | | * |
- * | | | |
- |\| | | |
- | | * | |
- | | |/ /
- | * | |
- | * | |
- | |\| |
- * | | |
- |\| | |
- | | * |
- | | * |
- | | * |
- | * | |
- | | * |
- | * | |
- | | * |
- | | * |
- | | * |
- | * | |
- | * | |
- | * | |
- | * | |
- | * | |
- | * | |
- | * | |
- * | | |
- |\| | |
- | * | |
- | |\| |
- | | * |
- | | |\ \
- * | | | |
- |\| | | |
- | * | | |
- | |\| | |
- | | * | |
- | | | * |
- | | |/ /
- * | | |
- * | | |
- |\| | |
- | * | |
- | |\| |
- | | * |
- | | * |
- | | * |
- | | | *
- * | | |
- |\| | |
- | * | |
- | * | |
- | | | *
- | | | |\
- * | | | |
- | |_|_|/
- |/| | |
- | * | |
- | |\| |
- | | * |
- | | * |
- | | * |
- | | * |
- | | * |
- | * | |
- * | | |
- |\| | |
- | * | |
- |/| | |
- | |/ /
- | * |
- | |\ \
- | * | |
- | * | |
- * | | |
- |\| | |
- | | * |
- | * | |
- | * | |
- | * | |
- * | | |
- |\| | |
- | * | |
- | * | |
- | | * |
- | | |\ \
- | | |/ /
- | |/| |
- | * | |
- * | | |
- |\| | |
- | * | |
- * | | |
- |\| | |
- | * | |
- | |\ \ \
- | * | | |
- | * | | |
- | | | * |
- | * | | |
- | * | | |
- | | |/ /
- | |/| |
- | | * |
- * | | |
- |\| | |
- | * | |
- | * | |
- | * | |
- | * | |
- | * | |
- | |\ \ \
- * | | | |
- |\| | | |
- | * | | |
- | * | | |
- * | | | |
- * | | | |
- |\| | | |
- | | | | *
- | | | | |\
- | |_|_|_|/
- |/| | | |
- | * | | |
- * | | | |
- * | | | |
- |\| | | |
- | * | | |
- | |\ \ \ \
- | | | |/ /
- | | |/| |
- | * | | |
- | * | | |
- | * | | |
- | * | | |
- | | * | |
- | | | * |
- | | |/ /
- | |/| |
- * | | |
- |\| | |
- | * | |
- | * | |
- | * | |
- | * | |
- | * | |
- * | | |
- |\| | |
- | * | |
- | * | |
- * | | |
- | * | |
- | * | |
- | * | |
- * | | |
- * | | |
- * | | |
- |\| | |
- | * | |
- * | | |
- * | | |
- * | | |
- * | | |
- | | | *
- * | | |
- |\| | |
- | * | |
- | * | |
- | * | |
- `
|