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.

compress_test.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package main
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. )
  7. func Test_replace(t *testing.T) {
  8. type args struct {
  9. parts []string
  10. function []string
  11. name string
  12. }
  13. tests := []struct {
  14. name string
  15. args args
  16. want []string
  17. }{
  18. {"replace entire set", args{[]string{"R", "1", "L", "2"}, []string{"R", "1", "L", "2"}, "A"}, []string{"A"}},
  19. {"replace prefix", args{[]string{"R", "1", "L", "2"}, []string{"R", "1", "L"}, "A"}, []string{"A", "2"}},
  20. {"replace middle", args{[]string{"R", "1", "L", "2"}, []string{"1", "L"}, "A"}, []string{"R", "A", "2"}},
  21. {"replace suffix", args{[]string{"R", "1", "L", "2"}, []string{"L", "2"}, "A"}, []string{"R", "1", "A"}},
  22. {"replace multi", args{[]string{"R", "1", "L", "R", "1", "2", "R", "1"}, []string{"R", "1"}, "A"}, []string{"A", "L", "A", "2", "A"}},
  23. {"replace adjacent", args{[]string{"R", "1", "R", "1"}, []string{"R", "1"}, "A"}, []string{"A", "A"}},
  24. }
  25. for _, tt := range tests {
  26. t.Run(tt.name, func(t *testing.T) {
  27. if got := replace(tt.args.parts, tt.args.function, tt.args.name); !reflect.DeepEqual(got, tt.want) {
  28. t.Errorf("replace() = %v, want %v", got, tt.want)
  29. }
  30. })
  31. }
  32. }
  33. func Test_compress(t *testing.T) {
  34. tests := []struct {
  35. name string
  36. args string
  37. wantMain string
  38. wantA string
  39. wantB string
  40. wantC string
  41. }{
  42. {
  43. name: "simple",
  44. args: "R,L,1",
  45. wantMain: "A,B,C",
  46. wantA: "R",
  47. wantB: "L",
  48. wantC: "1",
  49. },
  50. {
  51. name: "reddit1",
  52. args: "L,12,R,4,R,4,L,6,L,12,R,4,R,4,R,12,L,12,R,4,R,4,L,6,L,10,L,6,R,4,L,12,R,4,R,4,L,6,L,12,R,4,R,4,R,12,L,10,L,6,R,4,L,12,R,4,R,4,R,12,L,10,L,6,R,4,L,12,R,4,R,4,L,6",
  53. wantMain: "A,B,A,C,A,B,C,B,C,A",
  54. wantA: "L,12,R,4,R,4,L,6",
  55. wantB: "L,12,R,4,R,4,R,12",
  56. wantC: "L,10,L,6,R,4",
  57. },
  58. {
  59. name: "reddit2",
  60. args: "L,6,R,12,L,4,L,6,R,6,L,6,R,12,R,6,L,6,R,12,L,6,L,10,L,10,R,6,L,6,R,12,L,4,L,6,R,6,L,6,R,12,L,6,L,10,L,10,R,6,L,6,R,12,L,4,L,6,R,6,L,6,R,12,L,6,L,10,L,10,R,6",
  61. wantMain: "A,B,B,C,A,B,C,A,B,C",
  62. wantA: "L,6,R,12,L,4,L,6",
  63. wantB: "R,6,L,6,R,12",
  64. wantC: "L,6,L,10,L,10,R,6",
  65. },
  66. {
  67. name: "reddit3",
  68. args: "L,12,L,12,R,4,R,10,R,6,R,4,R,4,L,12,L,12,R,4,R,6,L,12,L,12,R,10,R,6,R,4,R,4,L,12,L,12,R,4,R,10,R,6,R,4,R,4,R,6,L,12,L,12,R,6,L,12,L,12,R,10,R,6,R,4,R,4",
  69. wantMain: "A,B,A,C,B,A,B,C,C,B",
  70. wantA: "L,12,L,12,R,4",
  71. wantB: "R,10,R,6,R,4,R,4",
  72. wantC: "R,6,L,12,L,12",
  73. },
  74. }
  75. for _, tt := range tests {
  76. t.Run(tt.name, func(t *testing.T) {
  77. gotMain, gotA, gotB, gotC := compress(strings.Split(tt.args, ","))
  78. if gotMain != tt.wantMain {
  79. t.Errorf("compress() gotMain = %v, want %v", gotMain, tt.wantMain)
  80. }
  81. if gotA != tt.wantA {
  82. t.Errorf("compress() gotA = %v, want %v", gotA, tt.wantA)
  83. }
  84. if gotB != tt.wantB {
  85. t.Errorf("compress() gotB = %v, want %v", gotB, tt.wantB)
  86. }
  87. if gotC != tt.wantC {
  88. t.Errorf("compress() gotC = %v, want %v", gotC, tt.wantC)
  89. }
  90. })
  91. }
  92. }
  93. func Test_prefixes(t *testing.T) {
  94. tests := []struct {
  95. name string
  96. args []string
  97. want [][]string
  98. }{
  99. {"simple", []string{"1", "2", "3"}, [][]string{{"1", "2", "3"}, {"1", "2"}, {"1"}}},
  100. }
  101. for _, tt := range tests {
  102. t.Run(tt.name, func(t *testing.T) {
  103. if got := prefixes(tt.args); !reflect.DeepEqual(got, tt.want) {
  104. t.Errorf("prefixes() = %v, want %v", got, tt.want)
  105. }
  106. })
  107. }
  108. }