diff --git a/src/cmd/go2go/testdata/go2path/src/sets/sets.go2 b/src/cmd/go2go/testdata/go2path/src/sets/sets.go2 new file mode 100644 index 0000000000..55ce782477 --- /dev/null +++ b/src/cmd/go2go/testdata/go2path/src/sets/sets.go2 @@ -0,0 +1,111 @@ +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package sets implements sets of any type. +package sets + +// A Set is a set of elements of some type. +type Set(type Elem comparable) struct { + m map[Elem]struct{} +} + +// Make makes a new set. +func Make(type Elem comparable)() Set(Elem) { + return Set(Elem){m: make(map[Elem]struct{})} +} + +// Add adds an element to a set. +func (s Set(Elem)) Add(v Elem) { + s.m[v] = struct{}{} +} + +// Delete removes an element from a set. If the element is not present +// in the set, this does nothing. +func (s Set(Elem)) Delete(v Elem) { + delete(s.m, v) +} + +// Contains reports whether v is in the set. +func (s Set(Elem)) Contains(v Elem) bool { + _, ok := s.m[v] + return ok +} + +// Len returns the number of elements in the set. +func (s Set(Elem)) Len() int { + return len(s.m) +} + +// Values returns the values in the set. +// The values will be in an indeterminate order. +func (s Set(Elem)) Values() []Elem { + r := make([]Elem, 0, len(s.m)) + for v := range s.m { + r = append(r, v) + } + return r +} + +// Equal reports whether two sets contain the same elements. +func Equal(type Elem comparable)(s1, s2 Set(Elem)) bool { + if len(s1.m) != len(s2.m) { + return false + } + for v1 := range s1.m { + if !s2.Contains(v1) { + return false + } + } + return true +} + +// Copy returns a copy of s. +func (s Set(Elem)) Copy() Set(Elem) { + r := Set(Elem){m: make(map[Elem]struct{}, len(s.m))} + for v := range s.m { + r.m[v] = struct{}{} + } + return r +} + +// AddSet adds all the elements of s2 to s. +func (s Set(Elem)) AddSet(s2 Set(Elem)) { + for v := range s2.m { + s.m[v] = struct{}{} + } +} + +// SubSet removes all elements in s2 from s. +// Values in s2 that are not in s are ignored. +func (s Set(Elem)) SubSet(s2 Set(Elem)) { + for v := range s2.m { + delete(s.m, v) + } +} + +// Intersect removes all elements from s that are not present in s2. +// Values in s2 that are not in s are ignored. +func (s Set(Elem)) Intersect(s2 Set(Elem)) { + for v := range s.m { + if !s2.Contains(v) { + delete(s.m, v) + } + } +} + +// Iterate calls f on every element in the set. +func (s Set(Elem)) Iterate(f func(Elem)) { + for v := range s.m { + f(v) + } +} + +// Filter deletes any elements from s for which f returns false. +func (s Set(Elem)) Filter(f func(Elem) bool) { + for v := range s.m { + if !f(v) { + delete(s.m, v) + } + } +} diff --git a/src/cmd/go2go/testdata/go2path/src/sets/sets_test.go2 b/src/cmd/go2go/testdata/go2path/src/sets/sets_test.go2 new file mode 100644 index 0000000000..b06248f506 --- /dev/null +++ b/src/cmd/go2go/testdata/go2path/src/sets/sets_test.go2 @@ -0,0 +1,146 @@ +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package sets + +import ( + "sort" + "testing" + + "slices" +) + +func TestSet(t *testing.T) { + s1 := Make(int)() + if got := s1.Len(); got != 0 { + t.Errorf("Len of empty set = %d, want 0", got) + } + s1.Add(1) + s1.Add(1) + s1.Add(1) + if got := s1.Len(); got != 1 { + t.Errorf("(%v).Len() == %d, want 1", s1, got) + } + s1.Add(2) + s1.Add(3) + s1.Add(4) + if got := s1.Len(); got != 4 { + t.Errorf("(%v).Len() == %d, want 4", s1, got) + } + if !s1.Contains(1) { + t.Errorf("(%v).Contains(1) == false, want true", s1) + } + if s1.Contains(5) { + t.Errorf("(%v).Contains(5) == true, want false", s1) + } + vals := s1.Values() + sort.Ints(vals) + w1 := []int{1, 2, 3, 4} + if !slices.Equal(vals, w1) { + t.Errorf("(%v).Values() == %v, want %v", s1, vals, w1) + } +} + +func TestEqual(t *testing.T) { + s1 := Make(string)() + s2 := Make(string)() + if !Equal(s1, s2) { + t.Errorf("Equal(%v, %v) = false, want true", s1, s2) + } + s1.Add("hello") + s1.Add("world") + if got := s1.Len(); got != 2 { + t.Errorf("(%v).Len() == %d, want 2", s1, got) + } + if Equal(s1, s2) { + t.Errorf("Equal(%v, %v) = true, want false", s1, s2) + } +} + +func TestCopy(t *testing.T) { + s1 := Make(float64)() + s1.Add(0) + s2 := s1.Copy() + if !Equal(s1, s2) { + t.Errorf("Equal(%v, %v) = false, want true", s1, s2) + } + s1.Add(1) + if Equal(s1, s2) { + t.Errorf("Equal(%v, %v) = true, want false", s1, s2) + } +} + +func TestAddSet(t *testing.T) { + s1 := Make(int)() + s1.Add(1) + s1.Add(2) + s2 := Make(int)() + s2.Add(2) + s2.Add(3) + s1.AddSet(s2) + if got := s1.Len(); got != 3 { + t.Errorf("(%v).Len() == %d, want 3", s1, got) + } + s2.Add(1) + if !Equal(s1, s2) { + t.Errorf("Equal(%v, %v) = false, want true", s1, s2) + } +} + +func TestSubSet(t *testing.T) { + s1 := Make(int)() + s1.Add(1) + s1.Add(2) + s2 := Make(int)() + s2.Add(2) + s2.Add(3) + s1.SubSet(s2) + if got := s1.Len(); got != 1 { + t.Errorf("(%v).Len() == %d, want 1", s1, got) + } + if vals, want := s1.Values(), []int{1}; !slices.Equal(vals, want) { + t.Errorf("after SubSet got %v, want %v", vals, want) + } +} + +func TestIntersect(t *testing.T) { + s1 := Make(int)() + s1.Add(1) + s1.Add(2) + s2 := Make(int)() + s2.Add(2) + s2.Add(3) + s1.Intersect(s2) + if got := s1.Len(); got != 1 { + t.Errorf("(%v).Len() == %d, want 1", s1, got) + } + if vals, want := s1.Values(), []int{2}; !slices.Equal(vals, want) { + t.Errorf("after Intersect got %v, want %v", vals, want) + } +} + +func TestIterate(t *testing.T) { + s1 := Make(int)() + s1.Add(1) + s1.Add(2) + s1.Add(3) + s1.Add(4) + tot := 0 + s1.Iterate(func(i int) { tot += i }) + if tot != 10 { + t.Errorf("total of %v == %d, want 10", s1, tot) + } +} + +func TestFilter(t *testing.T) { + s1 := Make(int)() + s1.Add(1) + s1.Add(2) + s1.Add(3) + s1.Filter(func(v int) bool { return v%2 == 0 }) + if vals, want := s1.Values(), []int{2}; !slices.Equal(vals, want) { + t.Errorf("after Filter got %v, want %v", vals, want) + } + +}