radix_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package radix
  2. import (
  3. crand "crypto/rand"
  4. "fmt"
  5. "reflect"
  6. "sort"
  7. "testing"
  8. )
  9. func TestRadix(t *testing.T) {
  10. var min, max string
  11. inp := make(map[string]interface{})
  12. for i := 0; i < 1000; i++ {
  13. gen := generateUUID()
  14. inp[gen] = i
  15. if gen < min || i == 0 {
  16. min = gen
  17. }
  18. if gen > max || i == 0 {
  19. max = gen
  20. }
  21. }
  22. r := NewFromMap(inp)
  23. if r.Len() != len(inp) {
  24. t.Fatalf("bad length: %v %v", r.Len(), len(inp))
  25. }
  26. r.Walk(func(k string, v interface{}) bool {
  27. println(k)
  28. return false
  29. })
  30. for k, v := range inp {
  31. out, ok := r.Get(k)
  32. if !ok {
  33. t.Fatalf("missing key: %v", k)
  34. }
  35. if out != v {
  36. t.Fatalf("value mis-match: %v %v", out, v)
  37. }
  38. }
  39. // Check min and max
  40. outMin, _, _ := r.Minimum()
  41. if outMin != min {
  42. t.Fatalf("bad minimum: %v %v", outMin, min)
  43. }
  44. outMax, _, _ := r.Maximum()
  45. if outMax != max {
  46. t.Fatalf("bad maximum: %v %v", outMax, max)
  47. }
  48. for k, v := range inp {
  49. out, ok := r.Delete(k)
  50. if !ok {
  51. t.Fatalf("missing key: %v", k)
  52. }
  53. if out != v {
  54. t.Fatalf("value mis-match: %v %v", out, v)
  55. }
  56. }
  57. if r.Len() != 0 {
  58. t.Fatalf("bad length: %v", r.Len())
  59. }
  60. }
  61. func TestRoot(t *testing.T) {
  62. r := New()
  63. _, ok := r.Delete("")
  64. if ok {
  65. t.Fatalf("bad")
  66. }
  67. _, ok = r.Insert("", true)
  68. if ok {
  69. t.Fatalf("bad")
  70. }
  71. val, ok := r.Get("")
  72. if !ok || val != true {
  73. t.Fatalf("bad: %v", val)
  74. }
  75. val, ok = r.Delete("")
  76. if !ok || val != true {
  77. t.Fatalf("bad: %v", val)
  78. }
  79. }
  80. func TestDelete(t *testing.T) {
  81. r := New()
  82. s := []string{"", "A", "AB"}
  83. for _, ss := range s {
  84. r.Insert(ss, true)
  85. }
  86. for _, ss := range s {
  87. _, ok := r.Delete(ss)
  88. if !ok {
  89. t.Fatalf("bad %q", ss)
  90. }
  91. }
  92. }
  93. func TestDeletePrefix(t *testing.T) {
  94. type exp struct {
  95. inp []string
  96. prefix string
  97. out []string
  98. numDeleted int
  99. }
  100. cases := []exp{
  101. {[]string{"", "A", "AB", "ABC", "R", "S"}, "A", []string{"", "R", "S"}, 3},
  102. {[]string{"", "A", "AB", "ABC", "R", "S"}, "ABC", []string{"", "A", "AB", "R", "S"}, 1},
  103. {[]string{"", "A", "AB", "ABC", "R", "S"}, "", []string{}, 6},
  104. {[]string{"", "A", "AB", "ABC", "R", "S"}, "S", []string{"", "A", "AB", "ABC", "R"}, 1},
  105. {[]string{"", "A", "AB", "ABC", "R", "S"}, "SS", []string{"", "A", "AB", "ABC", "R", "S"}, 0},
  106. }
  107. for _, test := range cases {
  108. r := New()
  109. for _, ss := range test.inp {
  110. r.Insert(ss, true)
  111. }
  112. deleted := r.DeletePrefix(test.prefix)
  113. if deleted != test.numDeleted {
  114. t.Fatalf("Bad delete, expected %v to be deleted but got %v", test.numDeleted, deleted)
  115. }
  116. out := []string{}
  117. fn := func(s string, v interface{}) bool {
  118. out = append(out, s)
  119. return false
  120. }
  121. r.Walk(fn)
  122. if !reflect.DeepEqual(out, test.out) {
  123. t.Fatalf("mis-match: %v %v", out, test.out)
  124. }
  125. }
  126. }
  127. func TestLongestPrefix(t *testing.T) {
  128. r := New()
  129. keys := []string{
  130. "",
  131. "foo",
  132. "foobar",
  133. "foobarbaz",
  134. "foobarbazzip",
  135. "foozip",
  136. }
  137. for _, k := range keys {
  138. r.Insert(k, nil)
  139. }
  140. if r.Len() != len(keys) {
  141. t.Fatalf("bad len: %v %v", r.Len(), len(keys))
  142. }
  143. type exp struct {
  144. inp string
  145. out string
  146. }
  147. cases := []exp{
  148. {"a", ""},
  149. {"abc", ""},
  150. {"fo", ""},
  151. {"foo", "foo"},
  152. {"foob", "foo"},
  153. {"foobar", "foobar"},
  154. {"foobarba", "foobar"},
  155. {"foobarbaz", "foobarbaz"},
  156. {"foobarbazzi", "foobarbaz"},
  157. {"foobarbazzip", "foobarbazzip"},
  158. {"foozi", "foo"},
  159. {"foozip", "foozip"},
  160. {"foozipzap", "foozip"},
  161. }
  162. for _, test := range cases {
  163. m, _, ok := r.LongestPrefix(test.inp)
  164. if !ok {
  165. t.Fatalf("no match: %v", test)
  166. }
  167. if m != test.out {
  168. t.Fatalf("mis-match: %v %v", m, test)
  169. }
  170. }
  171. }
  172. func TestWalkPrefix(t *testing.T) {
  173. r := New()
  174. keys := []string{
  175. "foobar",
  176. "foo/bar/baz",
  177. "foo/baz/bar",
  178. "foo/zip/zap",
  179. "zipzap",
  180. }
  181. for _, k := range keys {
  182. r.Insert(k, nil)
  183. }
  184. if r.Len() != len(keys) {
  185. t.Fatalf("bad len: %v %v", r.Len(), len(keys))
  186. }
  187. type exp struct {
  188. inp string
  189. out []string
  190. }
  191. cases := []exp{
  192. {
  193. "f",
  194. []string{"foobar", "foo/bar/baz", "foo/baz/bar", "foo/zip/zap"},
  195. },
  196. {
  197. "foo",
  198. []string{"foobar", "foo/bar/baz", "foo/baz/bar", "foo/zip/zap"},
  199. },
  200. {
  201. "foob",
  202. []string{"foobar"},
  203. },
  204. {
  205. "foo/",
  206. []string{"foo/bar/baz", "foo/baz/bar", "foo/zip/zap"},
  207. },
  208. {
  209. "foo/b",
  210. []string{"foo/bar/baz", "foo/baz/bar"},
  211. },
  212. {
  213. "foo/ba",
  214. []string{"foo/bar/baz", "foo/baz/bar"},
  215. },
  216. {
  217. "foo/bar",
  218. []string{"foo/bar/baz"},
  219. },
  220. {
  221. "foo/bar/baz",
  222. []string{"foo/bar/baz"},
  223. },
  224. {
  225. "foo/bar/bazoo",
  226. []string{},
  227. },
  228. {
  229. "z",
  230. []string{"zipzap"},
  231. },
  232. }
  233. for _, test := range cases {
  234. out := []string{}
  235. fn := func(s string, v interface{}) bool {
  236. out = append(out, s)
  237. return false
  238. }
  239. r.WalkPrefix(test.inp, fn)
  240. sort.Strings(out)
  241. sort.Strings(test.out)
  242. if !reflect.DeepEqual(out, test.out) {
  243. t.Fatalf("mis-match: %v %v", out, test.out)
  244. }
  245. }
  246. }
  247. func TestWalkPath(t *testing.T) {
  248. r := New()
  249. keys := []string{
  250. "foo",
  251. "foo/bar",
  252. "foo/bar/baz",
  253. "foo/baz/bar",
  254. "foo/zip/zap",
  255. "zipzap",
  256. }
  257. for _, k := range keys {
  258. r.Insert(k, nil)
  259. }
  260. if r.Len() != len(keys) {
  261. t.Fatalf("bad len: %v %v", r.Len(), len(keys))
  262. }
  263. type exp struct {
  264. inp string
  265. out []string
  266. }
  267. cases := []exp{
  268. {
  269. "f",
  270. []string{},
  271. },
  272. {
  273. "foo",
  274. []string{"foo"},
  275. },
  276. {
  277. "foo/",
  278. []string{"foo"},
  279. },
  280. {
  281. "foo/ba",
  282. []string{"foo"},
  283. },
  284. {
  285. "foo/bar",
  286. []string{"foo", "foo/bar"},
  287. },
  288. {
  289. "foo/bar/baz",
  290. []string{"foo", "foo/bar", "foo/bar/baz"},
  291. },
  292. {
  293. "foo/bar/bazoo",
  294. []string{"foo", "foo/bar", "foo/bar/baz"},
  295. },
  296. {
  297. "z",
  298. []string{},
  299. },
  300. }
  301. for _, test := range cases {
  302. out := []string{}
  303. fn := func(s string, v interface{}) bool {
  304. out = append(out, s)
  305. return false
  306. }
  307. r.WalkPath(test.inp, fn)
  308. sort.Strings(out)
  309. sort.Strings(test.out)
  310. if !reflect.DeepEqual(out, test.out) {
  311. t.Fatalf("mis-match: %v %v", out, test.out)
  312. }
  313. }
  314. }
  315. // generateUUID is used to generate a random UUID
  316. func generateUUID() string {
  317. buf := make([]byte, 16)
  318. if _, err := crand.Read(buf); err != nil {
  319. panic(fmt.Errorf("failed to read random bytes: %v", err))
  320. }
  321. return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
  322. buf[0:4],
  323. buf[4:6],
  324. buf[6:8],
  325. buf[8:10],
  326. buf[10:16])
  327. }