func TestDiff(t *testing.T) { existing := mapset.NewSetFromSlice([]interface{}{"1.1.1.1", "2.2.2.2"}) addedIP := "3.3.3.3" updated := existing.Clone() updated.Add(addedIP) added, removed := diff(existing, updated) if len(added) != 1 { t.Fatalf("Diff failed for an Add update. Expecting 1 element, but got %d elements", len(added)) } if added[0].String() != addedIP { t.Fatalf("Expecting : %v, Got : %v", addedIP, added[0]) } if len(removed) > 0 { t.Fatalf("Diff failed for remove use-case. Expecting 0 element, but got %d elements", len(removed)) } updated = mapset.NewSetFromSlice([]interface{}{addedIP}) added, removed = diff(existing, updated) if len(removed) != 2 { t.Fatalf("Diff failed for a remove update. Expecting 2 element, but got %d elements", len(removed)) } if len(added) != 1 { t.Fatalf("Diff failed for add use-case. Expecting 1 element, but got %d elements", len(added)) } }
func FindNames(db_collections []string, provided_collections []string) ([]string, error) { if len(provided_collections) < 1 { return db_collections, nil } else { db_collections_i := make([]interface{}, len(db_collections)) provided_collections_i := make([]interface{}, len(provided_collections)) for i, v := range db_collections { db_collections_i[i] = v } for i, v := range provided_collections { provided_collections_i[i] = v } db_set := set.NewSetFromSlice(db_collections_i) provided_set := set.NewSetFromSlice(provided_collections_i) result_set := db_set.Intersect(provided_set) result_collection := make([]string, len(result_set.ToSlice())) for i, v := range result_set.ToSlice() { result_collection[i] = v.(string) } return result_collection, nil } }
func main() { //note that the set accepts []interface{} X := []interface{}{10, 12, 12, 12, 13} Y := []interface{}{12, 14, 15} Sx := set.NewSetFromSlice(X) Sy := set.NewSetFromSlice(Y) result1 := Sx.Difference(Sy) result2 := Sy.Difference(Sx) fmt.Println(result1) fmt.Println(result2) }
func ToSet(strings []string) set.Set { // First convert to array of []interface{} .. yikes! generics := make([]interface{}, len(strings)) for i, v := range strings { generics[i] = v } return set.NewSetFromSlice(generics) }
func (suite *StatsTestSuite) Test_Regex() { // We'll make a new client with actual regexps. pageViewRegexes := make([]map[string]string, 0) httpsRequestRegexes := []map[string]string{make(map[string]string), make(map[string]string)} httpsRequestRegexes[0]["regex"] = `^[a-z0-9\.]*\.(example\.com)$` httpsRequestRegexes[0]["replace"] = "$1" httpsRequestRegexes[1]["regex"] = `^.*example\.org$` httpsRequestRegexes[1]["replace"] = "replacement" regexps, _ := MakeRegexps(pageViewRegexes, httpsRequestRegexes) suite.httpClient = &http.Client{ Transport: &http.Transport{ Dial: makeStatsDialer(_SERVER_ID, regexps), }, } // Using both HTTP and HTTPS will help us to exercise both methods of hostname parsing for _, scheme := range []string{"http", "https"} { // No subdomain, so won't match regex url := fmt.Sprintf("%s://example.com/index.html", scheme) resp, err := suite.httpClient.Get(url) suite.Nil(err) resp.Body.Close() // Will match the first regex url = fmt.Sprintf("%s://www.example.com/index.html", scheme) resp, err = suite.httpClient.Get(url) suite.Nil(err) resp.Body.Close() // Will match the second regex url = fmt.Sprintf("%s://example.org/index.html", scheme) resp, err = suite.httpClient.Get(url) suite.Nil(err) resp.Body.Close() payload := GetForServer(_SERVER_ID) suite.NotNil(payload, "should get stats because we made HTTP reqs; %s", scheme) expectedHostnames := mapset.NewSet() expectedHostnames.Add("(OTHER)") expectedHostnames.Add("example.com") expectedHostnames.Add("replacement") hostnames := make([]interface{}, 0) for hostname := range payload.hostnameToStats { hostnames = append(hostnames, hostname) } actualHostnames := mapset.NewSetFromSlice(hostnames) suite.Equal(expectedHostnames, actualHostnames, "post-regex hostnames should be processed as expecteds; %s", scheme) } }
func TestNoCallback(t *testing.T) { hd := hostDiscovery{} hd.nodes = mapset.NewSetFromSlice([]interface{}{"1.1.1.1", "2.2.2.2"}) update := []*discovery.Entry{{Host: "1.1.1.1", Port: "0"}, {Host: "2.2.2.2", Port: "0"}} added := false removed := false hd.processCallback(update, func() {}, func(hosts []net.IP) { added = true }, func(hosts []net.IP) { removed = true }) if added || removed { t.Fatalf("Not expecting any callback notification. But received a callback") } }
func TestRemovedCallback(t *testing.T) { hd := hostDiscovery{} hd.nodes = mapset.NewSetFromSlice([]interface{}{"1.1.1.1", "2.2.2.2"}) update := []*discovery.Entry{&discovery.Entry{Host: "1.1.1.1", Port: "0"}} added := false removed := false hd.processCallback(update, func(hosts []net.IP) { added = true }, func(hosts []net.IP) { removed = true }) if !removed { t.Fatalf("Expecting a Removed callback notification. But none received") } }