github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/common/graph/choose_test.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package graph 8 9 import ( 10 "fmt" 11 "testing" 12 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestCombinationsExceed(t *testing.T) { 17 // 20 choose 5 is 15504. 18 require.False(t, CombinationsExceed(20, 5, 15504)) 19 require.False(t, CombinationsExceed(20, 5, 15505)) 20 require.True(t, CombinationsExceed(20, 5, 15503)) 21 22 // A huge number of combinations doesn't overflow. 23 require.True(t, CombinationsExceed(10000, 500, 9000)) 24 25 // N < K returns false 26 require.False(t, CombinationsExceed(20, 30, 0)) 27 } 28 29 func TestChooseKoutOfN(t *testing.T) { 30 expectedSets := indiceSets{ 31 &indiceSet{[]int{0, 1, 2, 3}}, 32 &indiceSet{[]int{0, 1, 2, 4}}, 33 &indiceSet{[]int{0, 1, 2, 5}}, 34 &indiceSet{[]int{0, 1, 3, 4}}, 35 &indiceSet{[]int{0, 1, 3, 5}}, 36 &indiceSet{[]int{0, 1, 4, 5}}, 37 &indiceSet{[]int{0, 2, 3, 4}}, 38 &indiceSet{[]int{0, 2, 3, 5}}, 39 &indiceSet{[]int{0, 2, 4, 5}}, 40 &indiceSet{[]int{0, 3, 4, 5}}, 41 &indiceSet{[]int{1, 2, 3, 4}}, 42 &indiceSet{[]int{1, 2, 3, 5}}, 43 &indiceSet{[]int{1, 2, 4, 5}}, 44 &indiceSet{[]int{1, 3, 4, 5}}, 45 &indiceSet{[]int{2, 3, 4, 5}}, 46 } 47 require.Equal(t, indiceSetsToStrings(expectedSets), indiceSetsToStrings(chooseKoutOfN(6, 4))) 48 } 49 50 func indiceSetsToStrings(sets indiceSets) []string { 51 var res []string 52 for _, set := range sets { 53 res = append(res, fmt.Sprintf("%v", set.indices)) 54 } 55 return res 56 }