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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. for i := 0; i < b.N; i++ {
  49. parser.Reset()
  50. tg = tgBytes
  51. idx := bytes.Index(tg, []byte("\n"))
  52. for idx > 0 {
  53. parser.ParseGlyphs(tg[:idx])
  54. tg = tg[idx+1:]
  55. idx = bytes.Index(tg, []byte("\n"))
  56. }
  57. }
  58. }
  59. func TestReleaseUnusedColors(t *testing.T) {
  60. testcases := []struct {
  61. availableColors []int
  62. oldColors []int
  63. firstInUse int // these values have to be either be correct or suggest less is
  64. firstAvailable int // available than possibly is - i.e. you cannot say 10 is available when it
  65. }{
  66. {
  67. availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  68. oldColors: []int{1, 1, 1, 1, 1},
  69. firstAvailable: -1,
  70. firstInUse: 1,
  71. },
  72. {
  73. availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  74. oldColors: []int{1, 2, 3, 4},
  75. firstAvailable: 6,
  76. firstInUse: 0,
  77. },
  78. {
  79. availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  80. oldColors: []int{6, 0, 3, 5, 3, 4, 0, 0},
  81. firstAvailable: 6,
  82. firstInUse: 0,
  83. },
  84. {
  85. availableColors: []int{1, 2, 3, 4, 5, 6, 7},
  86. oldColors: []int{6, 1, 3, 5, 3, 4, 2, 7},
  87. firstAvailable: -1,
  88. firstInUse: 0,
  89. },
  90. {
  91. availableColors: []int{1, 2, 3, 4, 5, 6, 7},
  92. oldColors: []int{6, 0, 3, 5, 3, 4, 2, 7},
  93. firstAvailable: -1,
  94. firstInUse: 0,
  95. },
  96. }
  97. for _, testcase := range testcases {
  98. parser := &Parser{}
  99. parser.Reset()
  100. parser.availableColors = append([]int{}, testcase.availableColors...)
  101. parser.oldColors = append(parser.oldColors, testcase.oldColors...)
  102. parser.firstAvailable = testcase.firstAvailable
  103. parser.firstInUse = testcase.firstInUse
  104. parser.releaseUnusedColors()
  105. if parser.firstAvailable == -1 {
  106. // All in use
  107. for _, color := range parser.availableColors {
  108. found := false
  109. for _, oldColor := range parser.oldColors {
  110. if oldColor == color {
  111. found = true
  112. break
  113. }
  114. }
  115. if !found {
  116. 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",
  117. testcase.availableColors,
  118. testcase.oldColors,
  119. testcase.firstAvailable,
  120. testcase.firstInUse,
  121. parser.availableColors,
  122. parser.oldColors,
  123. parser.firstAvailable,
  124. parser.firstInUse,
  125. color)
  126. }
  127. }
  128. } else if parser.firstInUse != -1 {
  129. // Some in use
  130. for i := parser.firstInUse; i != parser.firstAvailable; i = (i + 1) % len(parser.availableColors) {
  131. color := parser.availableColors[i]
  132. found := false
  133. for _, oldColor := range parser.oldColors {
  134. if oldColor == color {
  135. found = true
  136. break
  137. }
  138. }
  139. if !found {
  140. 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",
  141. testcase.availableColors,
  142. testcase.oldColors,
  143. testcase.firstAvailable,
  144. testcase.firstInUse,
  145. parser.availableColors,
  146. parser.oldColors,
  147. parser.firstAvailable,
  148. parser.firstInUse,
  149. color)
  150. }
  151. }
  152. for i := parser.firstAvailable; i != parser.firstInUse; i = (i + 1) % len(parser.availableColors) {
  153. color := parser.availableColors[i]
  154. found := false
  155. for _, oldColor := range parser.oldColors {
  156. if oldColor == color {
  157. found = true
  158. break
  159. }
  160. }
  161. if found {
  162. 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",
  163. testcase.availableColors,
  164. testcase.oldColors,
  165. testcase.firstAvailable,
  166. testcase.firstInUse,
  167. parser.availableColors,
  168. parser.oldColors,
  169. parser.firstAvailable,
  170. parser.firstInUse,
  171. color)
  172. }
  173. }
  174. } else {
  175. // None in use
  176. for _, color := range parser.oldColors {
  177. if color != 0 {
  178. 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",
  179. testcase.availableColors,
  180. testcase.oldColors,
  181. testcase.firstAvailable,
  182. testcase.firstInUse,
  183. parser.availableColors,
  184. parser.oldColors,
  185. parser.firstAvailable,
  186. parser.firstInUse,
  187. color)
  188. }
  189. }
  190. }
  191. }
  192. }
  193. func TestParseGlyphs(t *testing.T) {
  194. parser := &Parser{}
  195. parser.Reset()
  196. tgBytes := []byte(testglyphs)
  197. tg := tgBytes
  198. idx := bytes.Index(tg, []byte("\n"))
  199. row := 0
  200. for idx > 0 {
  201. parser.ParseGlyphs(tg[:idx])
  202. tg = tg[idx+1:]
  203. idx = bytes.Index(tg, []byte("\n"))
  204. if parser.flows[0] != 1 {
  205. t.Errorf("First column flow should be 1 but was %d", parser.flows[0])
  206. }
  207. colorToFlow := map[int]int64{}
  208. flowToColor := map[int64]int{}
  209. for i, flow := range parser.flows {
  210. if flow == 0 {
  211. continue
  212. }
  213. color := parser.colors[i]
  214. if fColor, in := flowToColor[flow]; in && fColor != color {
  215. t.Errorf("Row %d column %d flow %d has color %d but should be %d", row, i, flow, color, fColor)
  216. }
  217. flowToColor[flow] = color
  218. if cFlow, in := colorToFlow[color]; in && cFlow != flow {
  219. t.Errorf("Row %d column %d flow %d has color %d but conflicts with flow %d", row, i, flow, color, cFlow)
  220. }
  221. colorToFlow[color] = flow
  222. }
  223. row++
  224. }
  225. if len(parser.availableColors) != 9 {
  226. t.Errorf("Expected 9 colors but have %d", len(parser.availableColors))
  227. }
  228. }
  229. func TestCommitStringParsing(t *testing.T) {
  230. dataFirstPart := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|4e61bac|"
  231. tests := []struct {
  232. shouldPass bool
  233. testName string
  234. commitMessage string
  235. }{
  236. {true, "normal", "not a fancy message"},
  237. {true, "extra pipe", "An extra pipe: |"},
  238. {true, "extra 'Data:'", "DATA: might be trouble"},
  239. }
  240. for _, test := range tests {
  241. t.Run(test.testName, func(t *testing.T) {
  242. testString := fmt.Sprintf("%s%s", dataFirstPart, test.commitMessage)
  243. idx := strings.Index(testString, "DATA:")
  244. commit, err := NewCommit(0, 0, []byte(testString[idx+5:]))
  245. if err != nil && test.shouldPass {
  246. t.Errorf("Could not parse %s", testString)
  247. return
  248. }
  249. if test.commitMessage != commit.Subject {
  250. t.Errorf("%s does not match %s", test.commitMessage, commit.Subject)
  251. }
  252. })
  253. }
  254. }
  255. var testglyphs = `*
  256. *
  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. `