pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/sliceutil/sliceutil_test.go (about)

     1  package sliceutil
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"errors"
    12  	"sort"
    13  	"testing"
    14  
    15  	. "pkg.re/essentialkaos/check.v1"
    16  )
    17  
    18  // ////////////////////////////////////////////////////////////////////////////////// //
    19  
    20  func Test(t *testing.T) { TestingT(t) }
    21  
    22  type SliceSuite struct{}
    23  
    24  // ////////////////////////////////////////////////////////////////////////////////// //
    25  
    26  var _ = Suite(&SliceSuite{})
    27  
    28  // ////////////////////////////////////////////////////////////////////////////////// //
    29  
    30  func (s *SliceSuite) TestCopy(c *C) {
    31  	c.Assert(Copy(nil), IsNil)
    32  	c.Assert(CopyInts(nil), IsNil)
    33  	c.Assert(CopyFloats(nil), IsNil)
    34  
    35  	c.Assert(Copy([]string{"A"}), DeepEquals, []string{"A"})
    36  	c.Assert(CopyInts([]int{1}), DeepEquals, []int{1})
    37  	c.Assert(CopyFloats([]float64{1.0}), DeepEquals, []float64{1.0})
    38  }
    39  
    40  func (s *SliceSuite) TestStringToInterface(c *C) {
    41  	source := []string{"1", "2", "3"}
    42  
    43  	c.Assert(StringToInterface(nil), IsNil)
    44  	c.Assert(StringToInterface(source), DeepEquals, []interface{}{"1", "2", "3"})
    45  }
    46  
    47  func (s *SliceSuite) TestIntToInterface(c *C) {
    48  	source := []int{1, 2, 3}
    49  
    50  	c.Assert(IntToInterface(nil), IsNil)
    51  	c.Assert(IntToInterface(source), DeepEquals, []interface{}{1, 2, 3})
    52  }
    53  
    54  func (s *SliceSuite) TestStringToError(c *C) {
    55  	source := []string{"A", "B", "C"}
    56  
    57  	c.Assert(StringToError(nil), IsNil)
    58  	c.Assert(StringToError(source), DeepEquals,
    59  		[]error{
    60  			errors.New("A"),
    61  			errors.New("B"),
    62  			errors.New("C"),
    63  		})
    64  }
    65  
    66  func (s *SliceSuite) TestErrorToString(c *C) {
    67  	source := []error{
    68  		errors.New("A"),
    69  		errors.New("B"),
    70  		errors.New("C"),
    71  	}
    72  
    73  	c.Assert(ErrorToString(nil), IsNil)
    74  	c.Assert(ErrorToString(source), DeepEquals, []string{"A", "B", "C"})
    75  }
    76  
    77  func (s *SliceSuite) TestIndex(c *C) {
    78  	source := []string{"1", "2", "3"}
    79  
    80  	c.Assert(Index(source, "2"), Equals, 1)
    81  	c.Assert(Index(source, "4"), Equals, -1)
    82  	c.Assert(Index([]string{}, "1"), Equals, -1)
    83  }
    84  
    85  func (s *SliceSuite) TestContains(c *C) {
    86  	source := []string{"1", "2", "3"}
    87  
    88  	c.Assert(Contains(source, "1"), Equals, true)
    89  	c.Assert(Contains(source, "4"), Equals, false)
    90  	c.Assert(Contains([]string{}, "1"), Equals, false)
    91  }
    92  
    93  func (s *SliceSuite) TestExclude(c *C) {
    94  	source := []string{"1", "2", "3", "4", "5", "6"}
    95  
    96  	c.Assert(Exclude(source), DeepEquals, []string{"1", "2", "3", "4", "5", "6"})
    97  	c.Assert(Exclude(source, "1"), DeepEquals, []string{"2", "3", "4", "5", "6"})
    98  	c.Assert(Exclude(source, "1", "3", "6"), DeepEquals, []string{"2", "4", "5"})
    99  	c.Assert(Exclude(source, "1", "2", "3", "4", "5", "6"), DeepEquals, []string{})
   100  }
   101  
   102  func (s *SliceSuite) TestDeduplicate(c *C) {
   103  	source1 := []string{"1", "2", "2", "2", "3", "4", "5", "5", "6", "6"}
   104  	source2 := []string{"abc", "ABC", "A", "B", "C", "abc", "D", "E", "ABC"}
   105  
   106  	sort.Strings(source1)
   107  	sort.Strings(source2)
   108  
   109  	c.Assert(Deduplicate(source1), DeepEquals, []string{"1", "2", "3", "4", "5", "6"})
   110  	c.Assert(Deduplicate(source2), DeepEquals, []string{"A", "ABC", "B", "C", "D", "E", "abc"})
   111  	c.Assert(Deduplicate([]string{"1"}), DeepEquals, []string{"1"})
   112  }
   113  
   114  // ////////////////////////////////////////////////////////////////////////////////// //
   115  
   116  func (s *SliceSuite) BenchmarkStringToInterface(c *C) {
   117  	source := []string{"1", "2", "3"}
   118  
   119  	for i := 0; i < c.N; i++ {
   120  		StringToInterface(source)
   121  	}
   122  }
   123  
   124  func (s *SliceSuite) BenchmarkIntToInterface(c *C) {
   125  	source := []int{1, 2, 3}
   126  
   127  	for i := 0; i < c.N; i++ {
   128  		IntToInterface(source)
   129  	}
   130  }
   131  
   132  func (s *SliceSuite) BenchmarkStringToError(c *C) {
   133  	source := []string{"A", "B", "C"}
   134  
   135  	for i := 0; i < c.N; i++ {
   136  		StringToError(source)
   137  	}
   138  }
   139  
   140  func (s *SliceSuite) BenchmarkErrorToString(c *C) {
   141  	source := []error{
   142  		errors.New("A"),
   143  		errors.New("B"),
   144  		errors.New("C"),
   145  	}
   146  
   147  	for i := 0; i < c.N; i++ {
   148  		ErrorToString(source)
   149  	}
   150  }
   151  
   152  func (s *SliceSuite) BenchmarkDeduplicate(c *C) {
   153  	source := []string{"1", "2", "2", "2", "3", "4", "5", "5", "6", "6"}
   154  
   155  	for i := 0; i < c.N; i++ {
   156  		Deduplicate(source)
   157  	}
   158  }
   159  
   160  func (s *SliceSuite) BenchmarkExclude(c *C) {
   161  	source := []string{"1", "2", "3", "4", "5", "6"}
   162  
   163  	for i := 0; i < c.N; i++ {
   164  		Exclude(source, "1", "3", "6")
   165  	}
   166  }