version_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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("test", "test") != 0 {
  66. t.Error("should be 0")
  67. }
  68. if alphaCompare("test", "test123") > 0 {
  69. t.Error("should be less than 0")
  70. }
  71. if alphaCompare("test123", "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: 2,
  81. }
  82. older := []string{
  83. "0-3-4",
  84. "1-2-1",
  85. "1-1-1",
  86. }
  87. for _, o := range older {
  88. if a.Older(o) {
  89. t.Errorf("%s should be older than %s", o, a.String())
  90. }
  91. }
  92. newer := []string{
  93. "2-1-1",
  94. "1-3-1",
  95. "1-2-3",
  96. }
  97. for _, n := range newer {
  98. if a.Newer(n) {
  99. t.Errorf("%s should be newer than %s", n, a.String())
  100. }
  101. }
  102. }
  103. // Benchmark rpmvercmp
  104. func BenchmarkVersionCompare(b *testing.B) {
  105. for i := 0; i < b.N; i++ {
  106. rpmvercmp("1.0", "1.0.0")
  107. }
  108. }