123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- package fp_test
- import (
- "testing"
- fp "github.com/cloudflare/mitmengine/fputil"
- "github.com/cloudflare/mitmengine/testutil"
- )
- // Test IntList
- func TestIntListParse(t *testing.T) {
- var tests = []struct {
- in string
- out fp.IntList
- }{
- {"0", fp.IntList{0}},
- {"1,2,3", fp.IntList{1, 2, 3}},
- }
- for _, test := range tests {
- var actual fp.IntList
- err := actual.Parse(test.in)
- testutil.Ok(t, err)
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestIntListString(t *testing.T) {
- var tests = []struct {
- in fp.IntList
- out string
- }{
- {fp.IntList{0}, "0"},
- {fp.IntList{1, 2, 3}, "1,2,3"},
- }
- for _, test := range tests {
- actual := test.in.String()
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestIntListContains(t *testing.T) {
- var tests = []struct {
- a fp.IntList
- b fp.IntList
- out bool
- }{
- {fp.IntList{0}, fp.IntList{0}, true},
- {fp.IntList{0}, fp.IntList{}, true},
- {fp.IntList{}, fp.IntList{}, true},
- {fp.IntList{0}, fp.IntList{1}, false},
- {fp.IntList{}, fp.IntList{1}, false},
- {fp.IntList{1, 2, 3}, fp.IntList{1, 3}, true},
- {fp.IntList{1, 2, 3}, fp.IntList{3, 1}, false},
- {fp.IntList{1, 2, 3}, fp.IntList{2, 3}, true},
- {fp.IntList{1, 2, 3}, fp.IntList{3, 2}, false},
- {fp.IntList{1, 2, 3}, fp.IntList{1, 2, 3}, true},
- {fp.IntList{1, 2, 3}, fp.IntList{1, 2, 3, 4}, false},
- {fp.IntList{1, 2, 3}, fp.IntList{1, 3, 2}, false},
- }
- for _, test := range tests {
- actual := test.a.Contains(test.b)
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestIntListEquals(t *testing.T) {
- var tests = []struct {
- a fp.IntList
- b fp.IntList
- out bool
- }{
- {fp.IntList{0}, fp.IntList{0}, true},
- {fp.IntList{0}, fp.IntList{}, false},
- {fp.IntList{}, fp.IntList{}, true},
- {fp.IntList{0}, fp.IntList{1}, false},
- {fp.IntList{}, fp.IntList{1}, false},
- {fp.IntList{1, 2, 3}, fp.IntList{1, 3}, false},
- {fp.IntList{1, 2, 3}, fp.IntList{3, 1}, false},
- {fp.IntList{1, 2, 3}, fp.IntList{2, 3}, false},
- {fp.IntList{1, 2, 3}, fp.IntList{3, 2}, false},
- {fp.IntList{1, 2, 3}, fp.IntList{1, 2, 3}, true},
- {fp.IntList{1, 2, 3}, fp.IntList{1, 2, 3, 4}, false},
- {fp.IntList{1, 2, 3}, fp.IntList{1, 3, 2}, false},
- }
- for _, test := range tests {
- actual := test.a.Equals(test.b)
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestIntListSet(t *testing.T) {
- var tests = []struct {
- in fp.IntList
- out *fp.IntSet
- }{
- {fp.IntList{0}, fp.IntList{0}.Set()},
- {fp.IntList{1, 2, 3}, fp.IntList{1, 2, 3}.Set()},
- }
- for _, test := range tests {
- actual := test.in.Set()
- testutil.Equals(t, test.out, actual)
- }
- }
- // Test IntSet
- func TestIntSetList(t *testing.T) {
- var tests = []struct {
- in *fp.IntSet
- out fp.IntList
- }{
- {fp.IntList{}.Set(),fp.IntList{}},
- {fp.IntList{0}.Set(), fp.IntList{0}},
- {fp.IntList{1, 2, 3}.Set(), fp.IntList{1, 2, 3}},
- }
- for _, test := range tests {
- actual := test.in.List()
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestIntSetInter(t *testing.T) {
- var tests = []struct {
- a *fp.IntSet
- b *fp.IntSet
- out *fp.IntSet
- }{
- {fp.IntList{}.Set(), fp.IntList{}.Set(), fp.IntList{}.Set()},
- {fp.IntList{0}.Set(), fp.IntList{0}.Set(), fp.IntList{0}.Set()},
- {fp.IntList{1, 2, 3}.Set(), fp.IntList{2, 3, 4}.Set(), fp.IntList{2, 3}.Set()},
- }
- for _, test := range tests {
- actual := test.a.Inter(test.b)
- // Use Equal function for sets, defined for intset.Sparse; deep equals (as defined in package testutil)
- // does not handle intset.Sparse correctly
- test.out.Equals(&actual.Sparse)
- }
- }
- func TestIntSetDiff(t *testing.T) {
- var tests = []struct {
- a *fp.IntSet
- b *fp.IntSet
- out *fp.IntSet
- }{
- {fp.IntList{}.Set(), fp.IntList{}.Set(), fp.IntList{}.Set()},
- {fp.IntList{0}.Set(), fp.IntList{0}.Set(), fp.IntList{}.Set()},
- {fp.IntList{1, 2, 3}.Set(), fp.IntList{2, 3, 4}.Set(), fp.IntList{1}.Set()},
- }
- for _, test := range tests {
- actual := test.a.Diff(test.b)
- // Use Equal function for sets, defined for intset.Sparse; deep equals (as defined in package testutil)
- // does not handle intset.Sparse correctly
- test.out.Equals(&actual.Sparse)
- }
- }
- func TestIntSetUnion(t *testing.T) {
- var tests = []struct {
- a *fp.IntSet
- b *fp.IntSet
- out *fp.IntSet
- }{
- {fp.IntList{}.Set(), fp.IntList{}.Set(), fp.IntList{}.Set()},
- {fp.IntList{0}.Set(), fp.IntList{0}.Set(), fp.IntList{0}.Set()},
- {fp.IntList{1, 2, 3}.Set(), fp.IntList{2, 3, 4}.Set(), fp.IntList{1, 2, 3, 4}.Set()},
- }
- for _, test := range tests {
- actual := test.a.Union(test.b)
- // Use Equal function for sets, defined for intset.Sparse; deep equals (as defined in package testutil)
- // does not handle intset.Sparse correctly
- test.out.Equals(&actual.Sparse)
- }
- }
- // Test StringList
- func TestStringListParse(t *testing.T) {
- var tests = []struct {
- in string
- out fp.StringList
- }{
- {"0", fp.StringList{"0"}},
- {"1,2,3", fp.StringList{"1", "2", "3"}},
- }
- for _, test := range tests {
- var actual fp.StringList
- err := actual.Parse(test.in)
- testutil.Ok(t, err)
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestStringListString(t *testing.T) {
- var tests = []struct {
- in fp.StringList
- out string
- }{
- {fp.StringList{"0"}, "0"},
- {fp.StringList{"1", "2", "3"}, "1,2,3"},
- }
- for _, test := range tests {
- actual := test.in.String()
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestStringListContains(t *testing.T) {
- var tests = []struct {
- a fp.StringList
- b fp.StringList
- out bool
- }{
- {fp.StringList{"0"}, fp.StringList{"0"}, true},
- {fp.StringList{"0"}, fp.StringList{}, true},
- {fp.StringList{}, fp.StringList{}, true},
- {fp.StringList{"0"}, fp.StringList{"1"}, false},
- {fp.StringList{}, fp.StringList{"1"}, false},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "3"}, true},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"3", "1"}, false},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"2", "3"}, true},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"3", "2"}, false},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "2", "3"}, true},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "2", "3", "4"}, false},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "3", "2"}, false},
- }
- for _, test := range tests {
- actual := test.a.Contains(test.b)
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestStringListEquals(t *testing.T) {
- var tests = []struct {
- a fp.StringList
- b fp.StringList
- out bool
- }{
- {fp.StringList{"0"}, fp.StringList{"0"}, true},
- {fp.StringList{"0"}, fp.StringList{}, false},
- {fp.StringList{}, fp.StringList{}, true},
- {fp.StringList{"0"}, fp.StringList{"1"}, false},
- {fp.StringList{}, fp.StringList{"1"}, false},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "3"}, false},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"3", "1"}, false},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"2", "3"}, false},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"3", "2"}, false},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "2", "3"}, true},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "2", "3", "4"}, false},
- {fp.StringList{"1", "2", "3"}, fp.StringList{"1", "3", "2"}, false},
- }
- for _, test := range tests {
- actual := test.a.Equals(test.b)
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestStringListSet(t *testing.T) {
- var tests = []struct {
- in fp.StringList
- out fp.StringSet
- }{
- {fp.StringList{"0"}, fp.StringSet{"0": true}},
- {fp.StringList{"1", "2", "3"}, fp.StringSet{"1": true, "2": true, "3": true}},
- }
- for _, test := range tests {
- actual := test.in.Set()
- testutil.Equals(t, test.out, actual)
- }
- }
- // Test StringSet
- func TestStringSetList(t *testing.T) {
- var tests = []struct {
- in fp.StringSet
- out fp.StringList
- }{
- {fp.StringSet{"0": true}, fp.StringList{"0"}},
- {fp.StringSet{"1": true, "2": true, "3": true}, fp.StringList{"1", "2", "3"}},
- }
- for _, test := range tests {
- actual := test.in.List()
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestStringSetInter(t *testing.T) {
- var tests = []struct {
- a fp.StringSet
- b fp.StringSet
- out fp.StringSet
- }{
- {fp.StringSet{"0": true}, fp.StringSet{"0": true}, fp.StringSet{"0": true}},
- {fp.StringSet{"1": true, "2": true, "3": true}, fp.StringSet{"2": true, "3": true, "4": true}, fp.StringSet{"2": true, "3": true}},
- }
- for _, test := range tests {
- actual := test.a.Inter(test.b)
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestStringSetDiff(t *testing.T) {
- var tests = []struct {
- a fp.StringSet
- b fp.StringSet
- out fp.StringSet
- }{
- {fp.StringSet{"0": true}, fp.StringSet{"0": true}, fp.StringSet{}},
- {fp.StringSet{"1": true, "2": true, "3": true}, fp.StringSet{"2": true, "3": true, "4": true}, fp.StringSet{"1": true}},
- }
- for _, test := range tests {
- actual := test.a.Diff(test.b)
- testutil.Equals(t, test.out, actual)
- }
- }
- func TestStringSetUnion(t *testing.T) {
- var tests = []struct {
- a fp.StringSet
- b fp.StringSet
- out fp.StringSet
- }{
- {fp.StringSet{"0": true}, fp.StringSet{"0": true}, fp.StringSet{"0": true}},
- {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}},
- }
- for _, test := range tests {
- actual := test.a.Union(test.b)
- testutil.Equals(t, test.out, actual)
- }
- }
|