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

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