You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

graph_test.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package gitgraph
  5. import (
  6. "bytes"
  7. "fmt"
  8. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/modules/git"
  11. )
  12. func BenchmarkGetCommitGraph(b *testing.B) {
  13. currentRepo, err := git.OpenRepository(".")
  14. if err != nil || currentRepo == nil {
  15. b.Error("Could not open repository")
  16. }
  17. defer currentRepo.Close()
  18. for i := 0; i < b.N; i++ {
  19. graph, err := GetCommitGraph(currentRepo, 1, 0, false, nil, nil)
  20. if err != nil {
  21. b.Error("Could get commit graph")
  22. }
  23. if len(graph.Commits) < 100 {
  24. b.Error("Should get 100 log lines.")
  25. }
  26. }
  27. }
  28. func BenchmarkParseCommitString(b *testing.B) {
  29. testString := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|4e61bac|Add route for graph"
  30. parser := &Parser{}
  31. parser.Reset()
  32. for i := 0; i < b.N; i++ {
  33. parser.Reset()
  34. graph := NewGraph()
  35. if err := parser.AddLineToGraph(graph, 0, []byte(testString)); err != nil {
  36. b.Error("could not parse teststring")
  37. }
  38. if graph.Flows[1].Commits[0].Rev != "4e61bacab44e9b4730e44a6615d04098dd3a8eaf" {
  39. b.Error("Did not get expected data")
  40. }
  41. }
  42. }
  43. func BenchmarkParseGlyphs(b *testing.B) {
  44. parser := &Parser{}
  45. parser.Reset()
  46. tgBytes := []byte(testglyphs)
  47. tg := tgBytes
  48. idx := bytes.Index(tg, []byte("\n"))
  49. for i := 0; i < b.N; i++ {
  50. parser.Reset()
  51. tg = tgBytes
  52. idx = bytes.Index(tg, []byte("\n"))
  53. for idx > 0 {
  54. parser.ParseGlyphs(tg[:idx])
  55. tg = tg[idx+1:]
  56. idx = bytes.Index(tg, []byte("\n"))
  57. }
  58. }
  59. }
  60. func TestReleaseUnusedColors(t *testing.T) {
  61. testcases := []struct {
  62. availableColors []int
  63. oldColors []int
  64. firstInUse int // these values have to be either be correct or suggest less is
  65. firstAvailable int // available than possibly is - i.e. you cannot say 10 is available when it
  66. }{
  67. {
  68. availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  69. oldColors: []int{1, 1, 1, 1, 1},
  70. firstAvailable: -1,
  71. firstInUse: 1,
  72. },
  73. {
  74. availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  75. oldColors: []int{1, 2, 3, 4},
  76. firstAvailable: 6,
  77. firstInUse: 0,
  78. },
  79. {
  80. availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  81. oldColors: []int{6, 0, 3, 5, 3, 4, 0, 0},
  82. firstAvailable: 6,
  83. firstInUse: 0,
  84. },
  85. {
  86. availableColors: []int{1, 2, 3, 4, 5, 6, 7},
  87. oldColors: []int{6, 1, 3, 5, 3, 4, 2, 7},
  88. firstAvailable: -1,
  89. firstInUse: 0,
  90. },
  91. {
  92. availableColors: []int{1, 2, 3, 4, 5, 6, 7},
  93. oldColors: []int{6, 0, 3, 5, 3, 4, 2, 7},
  94. firstAvailable: -1,
  95. firstInUse: 0,
  96. },
  97. }
  98. for _, testcase := range testcases {
  99. parser := &Parser{}
  100. parser.Reset()
  101. parser.availableColors = append([]int{}, testcase.availableColors...)
  102. parser.oldColors = append(parser.oldColors, testcase.oldColors...)
  103. parser.firstAvailable = testcase.firstAvailable
  104. parser.firstInUse = testcase.firstInUse
  105. parser.releaseUnusedColors()
  106. if parser.firstAvailable == -1 {
  107. // All in use
  108. for _, color := range parser.availableColors {
  109. found := false
  110. for _, oldColor := range parser.oldColors {
  111. if oldColor == color {
  112. found = true
  113. break
  114. }
  115. }
  116. if !found {
  117. 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",
  118. testcase.availableColors,
  119. testcase.oldColors,
  120. testcase.firstAvailable,
  121. testcase.firstInUse,
  122. parser.availableColors,
  123. parser.oldColors,
  124. parser.firstAvailable,
  125. parser.firstInUse,
  126. color)
  127. }
  128. }
  129. } else if parser.firstInUse != -1 {
  130. // Some in use
  131. for i := parser.firstInUse; i != parser.firstAvailable; i = (i + 1) % len(parser.availableColors) {
  132. color := parser.availableColors[i]
  133. found := false
  134. for _, oldColor := range parser.oldColors {
  135. if oldColor == color {
  136. found = true
  137. break
  138. }
  139. }
  140. if !found {
  141. 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",
  142. testcase.availableColors,
  143. testcase.oldColors,
  144. testcase.firstAvailable,
  145. testcase.firstInUse,
  146. parser.availableColors,
  147. parser.oldColors,
  148. parser.firstAvailable,
  149. parser.firstInUse,
  150. color)
  151. }
  152. }
  153. for i := parser.firstAvailable; i != parser.firstInUse; i = (i + 1) % len(parser.availableColors) {
  154. color := parser.availableColors[i]
  155. found := false
  156. for _, oldColor := range parser.oldColors {
  157. if oldColor == color {
  158. found = true
  159. break
  160. }
  161. }
  162. if found {
  163. 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",
  164. testcase.availableColors,
  165. testcase.oldColors,
  166. testcase.firstAvailable,
  167. testcase.firstInUse,
  168. parser.availableColors,
  169. parser.oldColors,
  170. parser.firstAvailable,
  171. parser.firstInUse,
  172. color)
  173. }
  174. }
  175. } else {
  176. // None in use
  177. for _, color := range parser.oldColors {
  178. if color != 0 {
  179. 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",
  180. testcase.availableColors,
  181. testcase.oldColors,
  182. testcase.firstAvailable,
  183. testcase.firstInUse,
  184. parser.availableColors,
  185. parser.oldColors,
  186. parser.firstAvailable,
  187. parser.firstInUse,
  188. color)
  189. }
  190. }
  191. }
  192. }
  193. }
  194. func TestParseGlyphs(t *testing.T) {
  195. parser := &Parser{}
  196. parser.Reset()
  197. tgBytes := []byte(testglyphs)
  198. tg := tgBytes
  199. idx := bytes.Index(tg, []byte("\n"))
  200. row := 0
  201. for idx > 0 {
  202. parser.ParseGlyphs(tg[:idx])
  203. tg = tg[idx+1:]
  204. idx = bytes.Index(tg, []byte("\n"))
  205. if parser.flows[0] != 1 {
  206. t.Errorf("First column flow should be 1 but was %d", parser.flows[0])
  207. }
  208. colorToFlow := map[int]int64{}
  209. flowToColor := map[int64]int{}
  210. for i, flow := range parser.flows {
  211. if flow == 0 {
  212. continue
  213. }
  214. color := parser.colors[i]
  215. if fColor, in := flowToColor[flow]; in && fColor != color {
  216. t.Errorf("Row %d column %d flow %d has color %d but should be %d", row, i, flow, color, fColor)
  217. }
  218. flowToColor[flow] = color
  219. if cFlow, in := colorToFlow[color]; in && cFlow != flow {
  220. t.Errorf("Row %d column %d flow %d has color %d but conflicts with flow %d", row, i, flow, color, cFlow)
  221. }
  222. colorToFlow[color] = flow
  223. }
  224. row++
  225. }
  226. if len(parser.availableColors) != 9 {
  227. t.Errorf("Expected 9 colors but have %d", len(parser.availableColors))
  228. }
  229. }
  230. func TestCommitStringParsing(t *testing.T) {
  231. dataFirstPart := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|4e61bac|"
  232. tests := []struct {
  233. shouldPass bool
  234. testName string
  235. commitMessage string
  236. }{
  237. {true, "normal", "not a fancy message"},
  238. {true, "extra pipe", "An extra pipe: |"},
  239. {true, "extra 'Data:'", "DATA: might be trouble"},
  240. }
  241. for _, test := range tests {
  242. t.Run(test.testName, func(t *testing.T) {
  243. testString := fmt.Sprintf("%s%s", dataFirstPart, test.commitMessage)
  244. idx := strings.Index(testString, "DATA:")
  245. commit, err := NewCommit(0, 0, []byte(testString[idx+5:]))
  246. if err != nil && test.shouldPass {
  247. t.Errorf("Could not parse %s", testString)
  248. return
  249. }
  250. if test.commitMessage != commit.Subject {
  251. t.Errorf("%s does not match %s", test.commitMessage, commit.Subject)
  252. }
  253. })
  254. }
  255. }
  256. var testglyphs = `*
  257. *
  258. *
  259. *
  260. *
  261. *
  262. *
  263. *
  264. |\
  265. * |
  266. * |
  267. * |
  268. * |
  269. * |
  270. | *
  271. * |
  272. | *
  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. `