github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bslice/calculable_test.go (about)

     1  package bslice
     2  
     3  import (
     4  	"github.com/songzhibin97/go-baseutils/base/bternaryexpr"
     5  	"github.com/stretchr/testify/assert"
     6  	"math"
     7  	"math/rand"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestUnsafeCalculableBSlice_Sum(t *testing.T) {
    13  	type fields struct {
    14  		es []int
    15  	}
    16  	tests := []struct {
    17  		name   string
    18  		fields fields
    19  		want   int
    20  	}{}
    21  	rand.Seed(time.Now().Unix())
    22  	for i := 0; i < 1000; i++ {
    23  		ln := rand.Intn(100)
    24  		list := make([]int, ln)
    25  		want := 0
    26  		for j := 0; j < ln; j++ {
    27  			v := rand.Int()
    28  			list = append(list, v)
    29  			want += v
    30  		}
    31  		tests = append(tests, struct {
    32  			name   string
    33  			fields fields
    34  			want   int
    35  		}{name: "", fields: fields{es: list}, want: want})
    36  	}
    37  	for _, tt := range tests {
    38  		t.Run(tt.name, func(t *testing.T) {
    39  			x := NewUnsafeCalculableBSliceBySlice(tt.fields.es)
    40  			assert.Equalf(t, tt.want, x.Sum(), "Sum()")
    41  		})
    42  	}
    43  }
    44  
    45  func TestUnsafeCalculableBSlice_Avg(t *testing.T) {
    46  	type fields struct {
    47  		es []int
    48  	}
    49  	tests := []struct {
    50  		name   string
    51  		fields fields
    52  		want   int
    53  	}{}
    54  	rand.Seed(time.Now().Unix())
    55  	for i := 0; i < 1000; i++ {
    56  		ln := rand.Intn(100)
    57  		list := make([]int, 0, ln)
    58  		want := 0
    59  		for j := 0; j < ln; j++ {
    60  			v := rand.Int()
    61  			list = append(list, v)
    62  			want += v
    63  		}
    64  		want /= bternaryexpr.TernaryExpr(ln == 0, 1, ln)
    65  		tests = append(tests, struct {
    66  			name   string
    67  			fields fields
    68  			want   int
    69  		}{name: "", fields: fields{es: list}, want: want})
    70  	}
    71  	for _, tt := range tests {
    72  		t.Run(tt.name, func(t *testing.T) {
    73  			x := NewUnsafeCalculableBSliceBySlice(tt.fields.es)
    74  			assert.Equalf(t, tt.want, x.Avg(), "Avg(%v)", tt.fields.es)
    75  		})
    76  	}
    77  }
    78  
    79  func TestUnsafeCalculableBSlice_Max(t *testing.T) {
    80  	type fields struct {
    81  		es []int
    82  	}
    83  	tests := []struct {
    84  		name   string
    85  		fields fields
    86  		want   int
    87  	}{}
    88  	rand.Seed(time.Now().Unix())
    89  	for i := 0; i < 1000; i++ {
    90  		ln := rand.Intn(100) + 1
    91  		list := make([]int, 0, ln)
    92  		want := math.MinInt
    93  		for j := 0; j < ln; j++ {
    94  			v := rand.Int()
    95  			list = append(list, v)
    96  			if want < v {
    97  				want = v
    98  			}
    99  		}
   100  		tests = append(tests, struct {
   101  			name   string
   102  			fields fields
   103  			want   int
   104  		}{name: "", fields: fields{es: list}, want: want})
   105  	}
   106  	for _, tt := range tests {
   107  		t.Run(tt.name, func(t *testing.T) {
   108  			x := NewUnsafeCalculableBSliceBySlice(tt.fields.es)
   109  			assert.Equalf(t, tt.want, x.Max(), "Max(%v)", tt.fields.es)
   110  		})
   111  	}
   112  }
   113  
   114  func TestUnsafeCalculableBSlice_Min(t *testing.T) {
   115  	type fields struct {
   116  		es []int
   117  	}
   118  	tests := []struct {
   119  		name   string
   120  		fields fields
   121  		want   int
   122  	}{}
   123  	rand.Seed(time.Now().Unix())
   124  	for i := 0; i < 1000; i++ {
   125  		ln := rand.Intn(100) + 1
   126  		list := make([]int, 0, ln)
   127  		want := math.MaxInt
   128  		for j := 0; j < ln; j++ {
   129  			v := rand.Int()
   130  			list = append(list, v)
   131  			if want > v {
   132  				want = v
   133  			}
   134  		}
   135  		tests = append(tests, struct {
   136  			name   string
   137  			fields fields
   138  			want   int
   139  		}{name: "", fields: fields{es: list}, want: want})
   140  	}
   141  	for _, tt := range tests {
   142  		t.Run(tt.name, func(t *testing.T) {
   143  			x := NewUnsafeCalculableBSliceBySlice(tt.fields.es)
   144  			assert.Equalf(t, tt.want, x.Min(), "Min(%v)", tt.fields.es)
   145  		})
   146  	}
   147  }
   148  
   149  func TestSafeCalculableBSlice_Sum(t *testing.T) {
   150  	type fields struct {
   151  		es []int
   152  	}
   153  	tests := []struct {
   154  		name   string
   155  		fields fields
   156  		want   int
   157  	}{}
   158  	rand.Seed(time.Now().Unix())
   159  	for i := 0; i < 1000; i++ {
   160  		ln := rand.Intn(100)
   161  		list := make([]int, ln)
   162  		want := 0
   163  		for j := 0; j < ln; j++ {
   164  			v := rand.Int()
   165  			list = append(list, v)
   166  			want += v
   167  		}
   168  		tests = append(tests, struct {
   169  			name   string
   170  			fields fields
   171  			want   int
   172  		}{name: "", fields: fields{es: list}, want: want})
   173  	}
   174  	for _, tt := range tests {
   175  		t.Run(tt.name, func(t *testing.T) {
   176  			x := NewSafeCalculableBSliceBySlice(tt.fields.es)
   177  			assert.Equalf(t, tt.want, x.Sum(), "Sum()")
   178  		})
   179  	}
   180  }
   181  
   182  func TestSafeCalculableBSlice_Avg(t *testing.T) {
   183  	type fields struct {
   184  		es []int
   185  	}
   186  	tests := []struct {
   187  		name   string
   188  		fields fields
   189  		want   int
   190  	}{}
   191  	rand.Seed(time.Now().Unix())
   192  	for i := 0; i < 1000; i++ {
   193  		ln := rand.Intn(100)
   194  		list := make([]int, 0, ln)
   195  		want := 0
   196  		for j := 0; j < ln; j++ {
   197  			v := rand.Int()
   198  			list = append(list, v)
   199  			want += v
   200  		}
   201  		want /= bternaryexpr.TernaryExpr(ln == 0, 1, ln)
   202  		tests = append(tests, struct {
   203  			name   string
   204  			fields fields
   205  			want   int
   206  		}{name: "", fields: fields{es: list}, want: want})
   207  	}
   208  	for _, tt := range tests {
   209  		t.Run(tt.name, func(t *testing.T) {
   210  			x := NewSafeCalculableBSliceBySlice(tt.fields.es)
   211  			assert.Equalf(t, tt.want, x.Avg(), "Avg(%v)", tt.fields.es)
   212  		})
   213  	}
   214  }
   215  
   216  func TestSafeCalculableBSlice_Max(t *testing.T) {
   217  	type fields struct {
   218  		es []int
   219  	}
   220  	tests := []struct {
   221  		name   string
   222  		fields fields
   223  		want   int
   224  	}{}
   225  	rand.Seed(time.Now().Unix())
   226  	for i := 0; i < 1000; i++ {
   227  		ln := rand.Intn(100) + 1
   228  		list := make([]int, 0, ln)
   229  		want := math.MinInt
   230  		for j := 0; j < ln; j++ {
   231  			v := rand.Int()
   232  			list = append(list, v)
   233  			if want < v {
   234  				want = v
   235  			}
   236  		}
   237  		tests = append(tests, struct {
   238  			name   string
   239  			fields fields
   240  			want   int
   241  		}{name: "", fields: fields{es: list}, want: want})
   242  	}
   243  	for _, tt := range tests {
   244  		t.Run(tt.name, func(t *testing.T) {
   245  			x := NewSafeCalculableBSliceBySlice(tt.fields.es)
   246  			assert.Equalf(t, tt.want, x.Max(), "Max(%v)", tt.fields.es)
   247  		})
   248  	}
   249  }
   250  
   251  func TestSafeCalculableBSlice_Min(t *testing.T) {
   252  	type fields struct {
   253  		es []int
   254  	}
   255  	tests := []struct {
   256  		name   string
   257  		fields fields
   258  		want   int
   259  	}{}
   260  	rand.Seed(time.Now().Unix())
   261  	for i := 0; i < 1000; i++ {
   262  		ln := rand.Intn(100) + 1
   263  		list := make([]int, 0, ln)
   264  		want := math.MaxInt
   265  		for j := 0; j < ln; j++ {
   266  			v := rand.Int()
   267  			list = append(list, v)
   268  			if want > v {
   269  				want = v
   270  			}
   271  		}
   272  		tests = append(tests, struct {
   273  			name   string
   274  			fields fields
   275  			want   int
   276  		}{name: "", fields: fields{es: list}, want: want})
   277  	}
   278  	for _, tt := range tests {
   279  		t.Run(tt.name, func(t *testing.T) {
   280  			x := NewSafeCalculableBSliceBySlice(tt.fields.es)
   281  			assert.Equalf(t, tt.want, x.Min(), "Min(%v)", tt.fields.es)
   282  		})
   283  	}
   284  }