github.com/yandex/pandora@v0.5.32/lib/math/gcd_lcm_test.go (about)

     1  package math
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func Test_GCD(t *testing.T) {
    10  	tests := []struct {
    11  		name string
    12  		a    int64
    13  		b    int64
    14  		want int64
    15  	}{
    16  		{
    17  			name: "",
    18  			a:    40,
    19  			b:    60,
    20  			want: 20,
    21  		},
    22  		{
    23  			name: "",
    24  			a:    2,
    25  			b:    3,
    26  			want: 1,
    27  		},
    28  	}
    29  	for _, tt := range tests {
    30  		t.Run(tt.name, func(t *testing.T) {
    31  			assert.Equalf(t, tt.want, GCD(tt.a, tt.b), "GCD(%v, %v)", tt.a, tt.b)
    32  		})
    33  	}
    34  }
    35  
    36  func Test_GCDM(t *testing.T) {
    37  	tests := []struct {
    38  		name    string
    39  		weights []int64
    40  		want    int64
    41  	}{
    42  		{
    43  			name:    "",
    44  			weights: []int64{20, 30, 60},
    45  			want:    10,
    46  		},
    47  		{
    48  			name:    "",
    49  			weights: []int64{6, 6, 6},
    50  			want:    6,
    51  		},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			assert.Equalf(t, tt.want, GCDM(tt.weights...), "GCDM(%v)", tt.weights)
    56  		})
    57  	}
    58  }
    59  func Test_LCM(t *testing.T) {
    60  	tests := []struct {
    61  		name string
    62  		a    int64
    63  		b    int64
    64  		want int64
    65  	}{
    66  		{
    67  			name: "",
    68  			a:    40,
    69  			b:    60,
    70  			want: 120,
    71  		},
    72  	}
    73  	for _, tt := range tests {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			assert.Equalf(t, tt.want, LCM(tt.a, tt.b), "LCM(%v, %v)", tt.a, tt.b)
    76  		})
    77  	}
    78  }
    79  
    80  func Test_lcmm(t *testing.T) {
    81  	tests := []struct {
    82  		name string
    83  		a    []int64
    84  		want int64
    85  	}{
    86  		{
    87  			name: "",
    88  			a:    []int64{3, 4, 6},
    89  			want: 12,
    90  		},
    91  		{
    92  			name: "",
    93  			a:    []int64{3, 4, 5, 6, 7}, // 140,105,84,70,60
    94  			want: 420,
    95  		},
    96  		{
    97  			name: "",
    98  			a:    []int64{2, 4, 5, 10},
    99  			want: 20,
   100  		},
   101  		{
   102  			name: "",
   103  			a:    []int64{20, 20, 20, 20},
   104  			want: 20,
   105  		},
   106  		{
   107  			name: "",
   108  			a:    []int64{40, 50, 70},
   109  			want: 1400,
   110  		},
   111  	}
   112  	for _, tt := range tests {
   113  		t.Run(tt.name, func(t *testing.T) {
   114  			assert.Equalf(t, tt.want, LCMM(tt.a...), "LCMM(%v)", tt.a)
   115  		})
   116  	}
   117  }