collection_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package fp_test
  2. import (
  3. "testing"
  4. fp "github.com/cloudflare/mitmengine/fputil"
  5. "github.com/cloudflare/mitmengine/testutil"
  6. )
  7. // Test IntList
  8. func TestIntListParse(t *testing.T) {
  9. var tests = []struct {
  10. in string
  11. out fp.IntList
  12. }{
  13. {"0", fp.IntList{0}},
  14. {"1,2,3", fp.IntList{1, 2, 3}},
  15. }
  16. for _, test := range tests {
  17. var actual fp.IntList
  18. err := actual.Parse(test.in)
  19. testutil.Ok(t, err)
  20. testutil.Equals(t, test.out, actual)
  21. }
  22. }
  23. func TestIntListString(t *testing.T) {
  24. var tests = []struct {
  25. in fp.IntList
  26. out string
  27. }{
  28. {fp.IntList{0}, "0"},
  29. {fp.IntList{1, 2, 3}, "1,2,3"},
  30. }
  31. for _, test := range tests {
  32. actual := test.in.String()
  33. testutil.Equals(t, test.out, actual)
  34. }
  35. }
  36. func TestIntListContains(t *testing.T) {
  37. var tests = []struct {
  38. a fp.IntList
  39. b fp.IntList
  40. out bool
  41. }{
  42. {fp.IntList{0}, fp.IntList{0}, true},
  43. {fp.IntList{0}, fp.IntList{}, true},
  44. {fp.IntList{}, fp.IntList{}, true},
  45. {fp.IntList{0}, fp.IntList{1}, false},
  46. {fp.IntList{}, fp.IntList{1}, false},
  47. {fp.IntList{1, 2, 3}, fp.IntList{1, 3}, true},
  48. {fp.IntList{1, 2, 3}, fp.IntList{3, 1}, false},
  49. {fp.IntList{1, 2, 3}, fp.IntList{2, 3}, true},
  50. {fp.IntList{1, 2, 3}, fp.IntList{3, 2}, false},
  51. {fp.IntList{1, 2, 3}, fp.IntList{1, 2, 3}, true},
  52. {fp.IntList{1, 2, 3}, fp.IntList{1, 2, 3, 4}, false},
  53. {fp.IntList{1, 2, 3}, fp.IntList{1, 3, 2}, false},
  54. }
  55. for _, test := range tests {
  56. actual := test.a.Contains(test.b)
  57. testutil.Equals(t, test.out, actual)
  58. }
  59. }
  60. func TestIntListEquals(t *testing.T) {
  61. var tests = []struct {
  62. a fp.IntList
  63. b fp.IntList
  64. out bool
  65. }{
  66. {fp.IntList{0}, fp.IntList{0}, true},
  67. {fp.IntList{0}, fp.IntList{}, false},
  68. {fp.IntList{}, fp.IntList{}, true},
  69. {fp.IntList{0}, fp.IntList{1}, false},
  70. {fp.IntList{}, fp.IntList{1}, false},
  71. {fp.IntList{1, 2, 3}, fp.IntList{1, 3}, false},
  72. {fp.IntList{1, 2, 3}, fp.IntList{3, 1}, false},
  73. {fp.IntList{1, 2, 3}, fp.IntList{2, 3}, false},
  74. {fp.IntList{1, 2, 3}, fp.IntList{3, 2}, false},
  75. {fp.IntList{1, 2, 3}, fp.IntList{1, 2, 3}, true},
  76. {fp.IntList{1, 2, 3}, fp.IntList{1, 2, 3, 4}, false},
  77. {fp.IntList{1, 2, 3}, fp.IntList{1, 3, 2}, false},
  78. }
  79. for _, test := range tests {
  80. actual := test.a.Equals(test.b)
  81. testutil.Equals(t, test.out, actual)
  82. }
  83. }
  84. func TestIntListSet(t *testing.T) {
  85. var tests = []struct {
  86. in fp.IntList
  87. out *fp.IntSet
  88. }{
  89. {fp.IntList{0}, fp.IntList{0}.Set()},
  90. {fp.IntList{1, 2, 3}, fp.IntList{1, 2, 3}.Set()},
  91. }
  92. for _, test := range tests {
  93. actual := test.in.Set()
  94. testutil.Equals(t, test.out, actual)
  95. }
  96. }
  97. // Test IntSet
  98. func TestIntSetList(t *testing.T) {
  99. var tests = []struct {
  100. in *fp.IntSet
  101. out fp.IntList
  102. }{
  103. {fp.IntList{}.Set(),fp.IntList{}},
  104. {fp.IntList{0}.Set(), fp.IntList{0}},
  105. {fp.IntList{1, 2, 3}.Set(), fp.IntList{1, 2, 3}},
  106. }
  107. for _, test := range tests {
  108. actual := test.in.List()
  109. testutil.Equals(t, test.out, actual)
  110. }
  111. }
  112. func TestIntSetInter(t *testing.T) {
  113. var tests = []struct {
  114. a *fp.IntSet
  115. b *fp.IntSet
  116. out *fp.IntSet
  117. }{
  118. {fp.IntList{}.Set(), fp.IntList{}.Set(), fp.IntList{}.Set()},
  119. {fp.IntList{0}.Set(), fp.IntList{0}.Set(), fp.IntList{0}.Set()},
  120. {fp.IntList{1, 2, 3}.Set(), fp.IntList{2, 3, 4}.Set(), fp.IntList{2, 3}.Set()},
  121. }
  122. for _, test := range tests {
  123. actual := test.a.Inter(test.b)
  124. // Use Equal function for sets, defined for intset.Sparse; deep equals (as defined in package testutil)
  125. // does not handle intset.Sparse correctly
  126. test.out.Equals(&actual.Sparse)
  127. }
  128. }
  129. func TestIntSetDiff(t *testing.T) {
  130. var tests = []struct {
  131. a *fp.IntSet
  132. b *fp.IntSet
  133. out *fp.IntSet
  134. }{
  135. {fp.IntList{}.Set(), fp.IntList{}.Set(), fp.IntList{}.Set()},
  136. {fp.IntList{0}.Set(), fp.IntList{0}.Set(), fp.IntList{}.Set()},
  137. {fp.IntList{1, 2, 3}.Set(), fp.IntList{2, 3, 4}.Set(), fp.IntList{1}.Set()},
  138. }
  139. for _, test := range tests {
  140. actual := test.a.Diff(test.b)
  141. // Use Equal function for sets, defined for intset.Sparse; deep equals (as defined in package testutil)
  142. // does not handle intset.Sparse correctly
  143. test.out.Equals(&actual.Sparse)
  144. }
  145. }
  146. func TestIntSetUnion(t *testing.T) {
  147. var tests = []struct {
  148. a *fp.IntSet
  149. b *fp.IntSet
  150. out *fp.IntSet
  151. }{
  152. {fp.IntList{}.Set(), fp.IntList{}.Set(), fp.IntList{}.Set()},
  153. {fp.IntList{0}.Set(), fp.IntList{0}.Set(), fp.IntList{0}.Set()},
  154. {fp.IntList{1, 2, 3}.Set(), fp.IntList{2, 3, 4}.Set(), fp.IntList{1, 2, 3, 4}.Set()},
  155. }
  156. for _, test := range tests {
  157. actual := test.a.Union(test.b)
  158. // Use Equal function for sets, defined for intset.Sparse; deep equals (as defined in package testutil)
  159. // does not handle intset.Sparse correctly
  160. test.out.Equals(&actual.Sparse)
  161. }
  162. }
  163. // Test StringList
  164. func TestStringListParse(t *testing.T) {
  165. var tests = []struct {
  166. in string
  167. out fp.StringList
  168. }{
  169. {"0", fp.StringList{"0"}},
  170. {"1,2,3", fp.StringList{"1", "2", "3"}},
  171. }
  172. for _, test := range tests {
  173. var actual fp.StringList
  174. err := actual.Parse(test.in)
  175. testutil.Ok(t, err)
  176. testutil.Equals(t, test.out, actual)
  177. }
  178. }
  179. func TestStringListString(t *testing.T) {
  180. var tests = []struct {
  181. in fp.StringList
  182. out string
  183. }{
  184. {fp.StringList{"0"}, "0"},
  185. {fp.StringList{"1", "2", "3"}, "1,2,3"},
  186. }
  187. for _, test := range tests {
  188. actual := test.in.String()
  189. testutil.Equals(t, test.out, actual)
  190. }
  191. }
  192. func TestStringListContains(t *testing.T) {
  193. var tests = []struct {
  194. a fp.StringList
  195. b fp.StringList
  196. out bool
  197. }{
  198. {fp.StringList{"0"}, fp.StringList{"0"}, true},
  199. {fp.StringList{"0"}, fp.StringList{}, true},
  200. {fp.StringList{}, fp.StringList{}, true},
  201. {fp.StringList{"0"}, fp.StringList{"1"}, false},
  202. {fp.StringList{}, fp.StringList{"1"}, false},
  203. {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "3"}, true},
  204. {fp.StringList{"1", "2", "3"}, fp.StringList{"3", "1"}, false},
  205. {fp.StringList{"1", "2", "3"}, fp.StringList{"2", "3"}, true},
  206. {fp.StringList{"1", "2", "3"}, fp.StringList{"3", "2"}, false},
  207. {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "2", "3"}, true},
  208. {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "2", "3", "4"}, false},
  209. {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "3", "2"}, false},
  210. }
  211. for _, test := range tests {
  212. actual := test.a.Contains(test.b)
  213. testutil.Equals(t, test.out, actual)
  214. }
  215. }
  216. func TestStringListEquals(t *testing.T) {
  217. var tests = []struct {
  218. a fp.StringList
  219. b fp.StringList
  220. out bool
  221. }{
  222. {fp.StringList{"0"}, fp.StringList{"0"}, true},
  223. {fp.StringList{"0"}, fp.StringList{}, false},
  224. {fp.StringList{}, fp.StringList{}, true},
  225. {fp.StringList{"0"}, fp.StringList{"1"}, false},
  226. {fp.StringList{}, fp.StringList{"1"}, false},
  227. {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "3"}, false},
  228. {fp.StringList{"1", "2", "3"}, fp.StringList{"3", "1"}, false},
  229. {fp.StringList{"1", "2", "3"}, fp.StringList{"2", "3"}, false},
  230. {fp.StringList{"1", "2", "3"}, fp.StringList{"3", "2"}, false},
  231. {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "2", "3"}, true},
  232. {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "2", "3", "4"}, false},
  233. {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "3", "2"}, false},
  234. }
  235. for _, test := range tests {
  236. actual := test.a.Equals(test.b)
  237. testutil.Equals(t, test.out, actual)
  238. }
  239. }
  240. func TestStringListSet(t *testing.T) {
  241. var tests = []struct {
  242. in fp.StringList
  243. out fp.StringSet
  244. }{
  245. {fp.StringList{"0"}, fp.StringSet{"0": true}},
  246. {fp.StringList{"1", "2", "3"}, fp.StringSet{"1": true, "2": true, "3": true}},
  247. }
  248. for _, test := range tests {
  249. actual := test.in.Set()
  250. testutil.Equals(t, test.out, actual)
  251. }
  252. }
  253. // Test StringSet
  254. func TestStringSetList(t *testing.T) {
  255. var tests = []struct {
  256. in fp.StringSet
  257. out fp.StringList
  258. }{
  259. {fp.StringSet{"0": true}, fp.StringList{"0"}},
  260. {fp.StringSet{"1": true, "2": true, "3": true}, fp.StringList{"1", "2", "3"}},
  261. }
  262. for _, test := range tests {
  263. actual := test.in.List()
  264. testutil.Equals(t, test.out, actual)
  265. }
  266. }
  267. func TestStringSetInter(t *testing.T) {
  268. var tests = []struct {
  269. a fp.StringSet
  270. b fp.StringSet
  271. out fp.StringSet
  272. }{
  273. {fp.StringSet{"0": true}, fp.StringSet{"0": true}, fp.StringSet{"0": true}},
  274. {fp.StringSet{"1": true, "2": true, "3": true}, fp.StringSet{"2": true, "3": true, "4": true}, fp.StringSet{"2": true, "3": true}},
  275. }
  276. for _, test := range tests {
  277. actual := test.a.Inter(test.b)
  278. testutil.Equals(t, test.out, actual)
  279. }
  280. }
  281. func TestStringSetDiff(t *testing.T) {
  282. var tests = []struct {
  283. a fp.StringSet
  284. b fp.StringSet
  285. out fp.StringSet
  286. }{
  287. {fp.StringSet{"0": true}, fp.StringSet{"0": true}, fp.StringSet{}},
  288. {fp.StringSet{"1": true, "2": true, "3": true}, fp.StringSet{"2": true, "3": true, "4": true}, fp.StringSet{"1": true}},
  289. }
  290. for _, test := range tests {
  291. actual := test.a.Diff(test.b)
  292. testutil.Equals(t, test.out, actual)
  293. }
  294. }
  295. func TestStringSetUnion(t *testing.T) {
  296. var tests = []struct {
  297. a fp.StringSet
  298. b fp.StringSet
  299. out fp.StringSet
  300. }{
  301. {fp.StringSet{"0": true}, fp.StringSet{"0": true}, fp.StringSet{"0": true}},
  302. {fp.StringSet{"1": true, "2": true, "3": true}, fp.StringSet{"2": true, "3": true, "4": true}, fp.StringSet{"1": true, "2": true, "3": true, "4": true}},
  303. }
  304. for _, test := range tests {
  305. actual := test.a.Union(test.b)
  306. testutil.Equals(t, test.out, actual)
  307. }
  308. }