version_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package pkgbuild
  2. import "testing"
  3. // Test version comparison
  4. func TestVersionComparison(t *testing.T) {
  5. alphaNumeric := []Version{
  6. "1.0.1",
  7. "1.0.a",
  8. "1.0",
  9. "1.0rc",
  10. "1.0pre",
  11. "1.0p",
  12. "1.0beta",
  13. "1.0b",
  14. "1.0a",
  15. }
  16. numeric := []Version{
  17. "20141130",
  18. "012",
  19. "11",
  20. "3.0.0",
  21. "2.011",
  22. "2.03",
  23. "2.0",
  24. "1.2",
  25. "1.1.1",
  26. "1.1",
  27. "1.0.1",
  28. "1.0.0.0.0.0",
  29. "1.0",
  30. "1",
  31. }
  32. git := []Version{
  33. "r1000.b481c3c",
  34. "r37.e481c3c",
  35. "r36.f481c3c",
  36. }
  37. bigger := func(list []Version) {
  38. for i, v := range list {
  39. for _, v2 := range list[i:] {
  40. if v != v2 && !v.bigger(v2) {
  41. t.Errorf("%s should be bigger than %s", v, v2)
  42. }
  43. }
  44. }
  45. }
  46. smaller := func(list []Version) {
  47. for i := len(list) - 1; i >= 0; i-- {
  48. v := list[i]
  49. for _, v2 := range list[:i] {
  50. if v != v2 && v.bigger(v2) {
  51. t.Errorf("%s should be smaller than %s", v, v2)
  52. }
  53. }
  54. }
  55. }
  56. bigger(alphaNumeric)
  57. smaller(alphaNumeric)
  58. bigger(numeric)
  59. smaller(numeric)
  60. bigger(git)
  61. smaller(git)
  62. }
  63. // Test alphaCompare function
  64. func TestAlphaCompare(t *testing.T) {
  65. if alphaCompare([]rune("test"), []rune("test")) != 0 {
  66. t.Error("should be 0")
  67. }
  68. if alphaCompare([]rune("test"), []rune("test123")) > 0 {
  69. t.Error("should be less than 0")
  70. }
  71. if alphaCompare([]rune("test123"), []rune("test")) < 0 {
  72. t.Error("should be greater than 0")
  73. }
  74. }
  75. // Test CompleteVersion comparisons
  76. func TestCompleteVersionComparison(t *testing.T) {
  77. a := &CompleteVersion{
  78. Version: "2",
  79. Epoch: 1,
  80. Pkgrel: Version("2"),
  81. }
  82. older := []string{
  83. "0:3-4",
  84. "1:2-1",
  85. "1:2-1.5",
  86. "1:1-1",
  87. }
  88. for _, o := range older {
  89. if _, err := NewCompleteVersion(o); err != nil {
  90. t.Errorf("%s fails to parse %v", o, err)
  91. }
  92. if a.Older(o) || !a.Newer(o) {
  93. t.Errorf("%s should be older than %s", o, a.String())
  94. }
  95. }
  96. newer := []string{
  97. "2:1-1",
  98. "1:3-1",
  99. "1:2-3",
  100. "1:2-2.1",
  101. }
  102. for _, n := range newer {
  103. if _, err := NewCompleteVersion(n); err != nil {
  104. t.Errorf("%s fails to parse %v", n, err)
  105. }
  106. if a.Newer(n) || !a.Older(n) {
  107. t.Errorf("%s should be newer than %s", n, a.String())
  108. }
  109. }
  110. }
  111. func TestCompleteVersionString(t *testing.T) {
  112. str := "42:3.14-1"
  113. version, _ := NewCompleteVersion(str)
  114. if version.String() != str {
  115. t.Errorf("%v should equal %s", version, str)
  116. }
  117. }
  118. // Benchmark rpmvercmp
  119. func BenchmarkVersionCompare(b *testing.B) {
  120. for i := 0; i < b.N; i++ {
  121. rpmvercmp("1.0", "1.0.0")
  122. }
  123. }
  124. func TestUnicode(t *testing.T) {
  125. str := "13:2.0.0.α.r29.g18fc492-1"
  126. expected := CompleteVersion{
  127. Epoch: 13,
  128. Version: "2.0.0.α.r29.g18fc492",
  129. Pkgrel: "1",
  130. }
  131. version, err := NewCompleteVersion(str)
  132. if err != nil {
  133. t.Error(err)
  134. } else if *version != expected {
  135. t.Errorf("%v should be %v", version, expected)
  136. }
  137. }