Source file test/typeparam/setsimp.dir/main.go

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"./a"
     9  	"fmt"
    10  	"sort"
    11  )
    12  
    13  func TestSet() {
    14  	s1 := a.Make[int]()
    15  	if got := s1.Len(); got != 0 {
    16  		panic(fmt.Sprintf("Len of empty set = %d, want 0", got))
    17  	}
    18  	s1.Add(1)
    19  	s1.Add(1)
    20  	s1.Add(1)
    21  	if got := s1.Len(); got != 1 {
    22  		panic(fmt.Sprintf("(%v).Len() == %d, want 1", s1, got))
    23  	}
    24  	s1.Add(2)
    25  	s1.Add(3)
    26  	s1.Add(4)
    27  	if got := s1.Len(); got != 4 {
    28  		panic(fmt.Sprintf("(%v).Len() == %d, want 4", s1, got))
    29  	}
    30  	if !s1.Contains(1) {
    31  		panic(fmt.Sprintf("(%v).Contains(1) == false, want true", s1))
    32  	}
    33  	if s1.Contains(5) {
    34  		panic(fmt.Sprintf("(%v).Contains(5) == true, want false", s1))
    35  	}
    36  	vals := s1.Values()
    37  	sort.Ints(vals)
    38  	w1 := []int{1, 2, 3, 4}
    39  	if !a.SliceEqual(vals, w1) {
    40  		panic(fmt.Sprintf("(%v).Values() == %v, want %v", s1, vals, w1))
    41  	}
    42  }
    43  
    44  func TestEqual() {
    45  	s1 := a.Make[string]()
    46  	s2 := a.Make[string]()
    47  	if !a.Equal(s1, s2) {
    48  		panic(fmt.Sprintf("a.Equal(%v, %v) = false, want true", s1, s2))
    49  	}
    50  	s1.Add("hello")
    51  	s1.Add("world")
    52  	if got := s1.Len(); got != 2 {
    53  		panic(fmt.Sprintf("(%v).Len() == %d, want 2", s1, got))
    54  	}
    55  	if a.Equal(s1, s2) {
    56  		panic(fmt.Sprintf("a.Equal(%v, %v) = true, want false", s1, s2))
    57  	}
    58  }
    59  
    60  func TestCopy() {
    61  	s1 := a.Make[float64]()
    62  	s1.Add(0)
    63  	s2 := s1.Copy()
    64  	if !a.Equal(s1, s2) {
    65  		panic(fmt.Sprintf("a.Equal(%v, %v) = false, want true", s1, s2))
    66  	}
    67  	s1.Add(1)
    68  	if a.Equal(s1, s2) {
    69  		panic(fmt.Sprintf("a.Equal(%v, %v) = true, want false", s1, s2))
    70  	}
    71  }
    72  
    73  func TestAddSet() {
    74  	s1 := a.Make[int]()
    75  	s1.Add(1)
    76  	s1.Add(2)
    77  	s2 := a.Make[int]()
    78  	s2.Add(2)
    79  	s2.Add(3)
    80  	s1.AddSet(s2)
    81  	if got := s1.Len(); got != 3 {
    82  		panic(fmt.Sprintf("(%v).Len() == %d, want 3", s1, got))
    83  	}
    84  	s2.Add(1)
    85  	if !a.Equal(s1, s2) {
    86  		panic(fmt.Sprintf("a.Equal(%v, %v) = false, want true", s1, s2))
    87  	}
    88  }
    89  
    90  func TestSubSet() {
    91  	s1 := a.Make[int]()
    92  	s1.Add(1)
    93  	s1.Add(2)
    94  	s2 := a.Make[int]()
    95  	s2.Add(2)
    96  	s2.Add(3)
    97  	s1.SubSet(s2)
    98  	if got := s1.Len(); got != 1 {
    99  		panic(fmt.Sprintf("(%v).Len() == %d, want 1", s1, got))
   100  	}
   101  	if vals, want := s1.Values(), []int{1}; !a.SliceEqual(vals, want) {
   102  		panic(fmt.Sprintf("after SubSet got %v, want %v", vals, want))
   103  	}
   104  }
   105  
   106  func TestIntersect() {
   107  	s1 := a.Make[int]()
   108  	s1.Add(1)
   109  	s1.Add(2)
   110  	s2 := a.Make[int]()
   111  	s2.Add(2)
   112  	s2.Add(3)
   113  	s1.Intersect(s2)
   114  	if got := s1.Len(); got != 1 {
   115  		panic(fmt.Sprintf("(%v).Len() == %d, want 1", s1, got))
   116  	}
   117  	if vals, want := s1.Values(), []int{2}; !a.SliceEqual(vals, want) {
   118  		panic(fmt.Sprintf("after Intersect got %v, want %v", vals, want))
   119  	}
   120  }
   121  
   122  func TestIterate() {
   123  	s1 := a.Make[int]()
   124  	s1.Add(1)
   125  	s1.Add(2)
   126  	s1.Add(3)
   127  	s1.Add(4)
   128  	tot := 0
   129  	s1.Iterate(func(i int) { tot += i })
   130  	if tot != 10 {
   131  		panic(fmt.Sprintf("total of %v == %d, want 10", s1, tot))
   132  	}
   133  }
   134  
   135  func TestFilter() {
   136  	s1 := a.Make[int]()
   137  	s1.Add(1)
   138  	s1.Add(2)
   139  	s1.Add(3)
   140  	s1.Filter(func(v int) bool { return v%2 == 0 })
   141  	if vals, want := s1.Values(), []int{2}; !a.SliceEqual(vals, want) {
   142  		panic(fmt.Sprintf("after Filter got %v, want %v", vals, want))
   143  	}
   144  
   145  }
   146  
   147  func main() {
   148  	TestSet()
   149  	TestEqual()
   150  	TestCopy()
   151  	TestAddSet()
   152  	TestSubSet()
   153  	TestIntersect()
   154  	TestIterate()
   155  	TestFilter()
   156  }
   157  

View as plain text