func TestHas(t *testing.T) { set := populateSet(3, 0) has := set.Has("item2") if !has { t.Errorf("期望item2存在") } set.Delete("item2") has = set.Has("item2") if has { t.Errorf("期望item2被刪除") } set.Delete("item1") has = set.Has("item1") if has { t.Errorf("期望item1被刪除") } }
func TestItems(t *testing.T) { set := populateSet(3, 0) items := set.Items() if len(items) != 3 { t.Errorf("錯誤的計數,期望3獲得%d", len(items)) } set = populateSet(520, 0) items = set.Items() if len(items) != 520 { t.Errorf("錯誤的計數,期望520獲得%d", len(items)) } }
func TestSize(t *testing.T) { set := populateSet(3, 0) items := set.Items() if len(items) != set.Size() { t.Errorf("錯誤的計數,期望%d獲得%d", set.Size(), len(items)) } set = populateSet(0, 0) items = set.Items() if len(items) != set.Size() { t.Errorf("錯誤的計數,期望%d獲得%d", set.Size(), len(items)) } set = populateSet(10000, 0) items = set.Items() if len(items) != set.Size() { t.Errorf("錯誤的計數,期望%d獲得%d", set.Size(), len(items)) } }
// This file was automatically generated by genny. // Any changes will be lost if this file is regenerated. // see https://github.com/cheekybits/genny
// Package set creates a StringSet data structure for the string type package set
import "sync"
// StringSet the set of Strings type StringSet struct { items map[string]bool lock sync.RWMutex }
// Add adds a new element to the Set. Returns a pointer to the Set. func (s *StringSet) Add(t string) *StringSet { s.lock.Lock() defer s.lock.Unlock() if s.items == nil { s.items = make(map[string]bool) } _, ok := s.items[t] if !ok { s.items[t] = true } return s }
// Clear removes all elements from the Set func (s *StringSet) Clear() { s.lock.Lock() defer s.lock.Unlock() s.items = make(map[string]bool) }
// Delete removes the string from the Set and returns Has(string) func (s *StringSet) Delete(item string) bool { s.lock.Lock() defer s.lock.Unlock() _, ok := s.items[item] if ok { delete(s.items, item) } return ok }
// Has returns true if the Set contains the string func (s *StringSet) Has(item string) bool { s.lock.RLock() defer s.lock.RUnlock() _, ok := s.items[item] return ok }
// Strings returns the string(s) stored func (s *StringSet) Strings() []string { s.lock.RLock() defer s.lock.RUnlock() items := []string{} for i := range s.items { items = append(items, i) } return items }
// Size returns the size of the set func (s *StringSet) Size() int { s.lock.RLock() defer s.lock.RUnlock() return len(s.items) }
// Package set creates a IntSet data structure for the int type
// IntSet the set of Ints type IntSet struct { items map[int]bool lock sync.RWMutex }
// Add adds a new element to the Set. Returns a pointer to the Set. func (s *IntSet) Add(t int) *IntSet { s.lock.Lock() defer s.lock.Unlock() if s.items == nil { s.items = make(map[int]bool) } _, ok := s.items[t] if !ok { s.items[t] = true } return s }
// Clear removes all elements from the Set func (s *IntSet) Clear() { s.lock.Lock() defer s.lock.Unlock() s.items = make(map[int]bool) }
// Delete removes the int from the Set and returns Has(int) func (s *IntSet) Delete(item int) bool { s.lock.Lock() defer s.lock.Unlock() _, ok := s.items[item] if ok { delete(s.items, item) } return ok }
// Has returns true if the Set contains the int func (s *IntSet) Has(item int) bool { s.lock.RLock() defer s.lock.RUnlock() _, ok := s.items[item] return ok }
// Ints returns the int(s) stored func (s *IntSet) Ints() []int { s.lock.RLock() defer s.lock.RUnlock() items := []int{} for i := range s.items { items = append(items, i) } return items }
// Size returns the size of the set func (s *IntSet) Size() int { s.lock.RLock() defer s.lock.RUnlock() return len(s.items) }