github.com/packtpublishing/learning-functional-programming-in-go@v0.0.0-20230130084745-8b849f6d58c4/Chapter01/03_sum/02_recursive_test.go (about)

     1  package fib
     2  
     3  import "testing"
     4  
     5  func TestSumRecursive(t *testing.T) {
     6  	for _, st := range sumTests {
     7  		if v := SumRecursive(st.a); v != st.expected {
     8  			t.Errorf("SumRecursive(%d) returned %d, expected %d", st.a, v, st.expected)
     9  		}
    10  	}
    11  }
    12  
    13  func BenchmarkSumRecursive(b *testing.B) {
    14  	fn := SumRecursive
    15  	for i := 0; i < b.N; i++ {
    16  		_ = fn([]int{1, 2, 3})
    17  	}
    18  }
    19  
    20  func benchmarkSumRecursive(s []int, b *testing.B) {
    21  	for n := 0; n < b.N; n++ {
    22  		SumRecursive(s)
    23  	}
    24  }
    25  
    26  func BenchmarkSumRecursive1(b *testing.B)  { benchmarkSumRecursive([]int{1}, b) }
    27  func BenchmarkSumRecursive2(b *testing.B)  { benchmarkSumRecursive([]int{1, 2}, b) }
    28  func BenchmarkSumRecursive3(b *testing.B)  { benchmarkSumRecursive([]int{1, 2, 3}, b) }
    29  func BenchmarkSumRecursive10(b *testing.B) { benchmarkSumRecursive([]int{1, 2, 3, 4}, b) }
    30  func BenchmarkSumRecursive20(b *testing.B) { benchmarkSumRecursive([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, b) }
    31  func BenchmarkSumRecursive40(b *testing.B) { benchmarkSumRecursive([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, b) }