fileinfosums_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package tarsum
  2. import "testing"
  3. func newFileInfoSums() FileInfoSums {
  4. return FileInfoSums{
  5. fileInfoSum{name: "file3", sum: "2abcdef1234567890", pos: 2},
  6. fileInfoSum{name: "dup1", sum: "deadbeef1", pos: 5},
  7. fileInfoSum{name: "file1", sum: "0abcdef1234567890", pos: 0},
  8. fileInfoSum{name: "file4", sum: "3abcdef1234567890", pos: 3},
  9. fileInfoSum{name: "dup1", sum: "deadbeef0", pos: 4},
  10. fileInfoSum{name: "file2", sum: "1abcdef1234567890", pos: 1},
  11. }
  12. }
  13. func TestSortFileInfoSums(t *testing.T) {
  14. dups := newFileInfoSums().GetAllFile("dup1")
  15. if len(dups) != 2 {
  16. t.Errorf("expected length 2, got %d", len(dups))
  17. }
  18. dups.SortByNames()
  19. if dups[0].Pos() != 4 {
  20. t.Errorf("sorted dups should be ordered by position. Expected 4, got %d", dups[0].Pos())
  21. }
  22. fis := newFileInfoSums()
  23. expected := "0abcdef1234567890"
  24. fis.SortBySums()
  25. got := fis[0].Sum()
  26. if got != expected {
  27. t.Errorf("Expected %q, got %q", expected, got)
  28. }
  29. fis = newFileInfoSums()
  30. expected = "dup1"
  31. fis.SortByNames()
  32. gotFis := fis[0]
  33. if gotFis.Name() != expected {
  34. t.Errorf("Expected %q, got %q", expected, gotFis.Name())
  35. }
  36. // since a duplicate is first, ensure it is ordered first by position too
  37. if gotFis.Pos() != 4 {
  38. t.Errorf("Expected %d, got %d", 4, gotFis.Pos())
  39. }
  40. fis = newFileInfoSums()
  41. fis.SortByPos()
  42. if fis[0].Pos() != 0 {
  43. t.Error("sorted fileInfoSums by Pos should order them by position.")
  44. }
  45. fis = newFileInfoSums()
  46. expected = "deadbeef1"
  47. gotFileInfoSum := fis.GetFile("dup1")
  48. if gotFileInfoSum.Sum() != expected {
  49. t.Errorf("Expected %q, got %q", expected, gotFileInfoSum)
  50. }
  51. if fis.GetFile("noPresent") != nil {
  52. t.Error("Should have return nil if name not found.")
  53. }
  54. }