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.

73 lines
1.6 KiB

  1. package filer
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. )
  7. func TestDirectory(t *testing.T) {
  8. {
  9. dm, _ := NewDirectoryManagerInMap("/tmp/dir.log")
  10. dm.MakeDirectory("/a/b/c")
  11. dm.MakeDirectory("/a/b/d")
  12. dm.MakeDirectory("/a/b/e")
  13. dm.MakeDirectory("/a/b/e/f")
  14. dm.MakeDirectory("/a/b/e/f/g")
  15. dm.MoveUnderDirectory("/a/b/e/f/g", "/a/b")
  16. dm.MakeDirectory("/a/b/g/h/i")
  17. dm.DeleteDirectory("/a/b/e/f")
  18. dm.DeleteDirectory("/a/b/e")
  19. dirNames, _ := dm.ListDirectories("/a/b/e")
  20. for _, v := range dirNames {
  21. println("sub1 dir:", v.Name, "id", v.Id)
  22. }
  23. dm.logFile.Close()
  24. var path []string
  25. printTree(dm.Root, path)
  26. dm2, e := NewDirectoryManagerInMap("/tmp/dir.log")
  27. if e != nil {
  28. println("load error", e.Error())
  29. }
  30. if !compare(dm.Root, dm2.Root) {
  31. t.Fatal("restored dir not the same!")
  32. }
  33. printTree(dm2.Root, path)
  34. }
  35. if true {
  36. os.Remove("/tmp/dir.log")
  37. }
  38. }
  39. func printTree(node *DirectoryEntryInMap, path []string) {
  40. println(strings.Join(path, "/") + "/" + node.Name)
  41. path = append(path, node.Name)
  42. for _, v := range node.SubDirectories {
  43. printTree(v, path)
  44. }
  45. }
  46. func compare(root1 *DirectoryEntryInMap, root2 *DirectoryEntryInMap) bool {
  47. if len(root1.SubDirectories) != len(root2.SubDirectories) {
  48. return false
  49. }
  50. if root1.Name != root2.Name {
  51. return false
  52. }
  53. if root1.Id != root2.Id {
  54. return false
  55. }
  56. if !(root1.Parent == nil && root2.Parent == nil) {
  57. if root1.Parent.Id != root2.Parent.Id {
  58. return false
  59. }
  60. }
  61. for k, v := range root1.SubDirectories {
  62. if !compare(v, root2.SubDirectories[k]) {
  63. return false
  64. }
  65. }
  66. return true
  67. }