github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/testdata/courses/qf104-2022/tests/lab2/sequence/triangular_ag_test.go (about)

     1  package sequence
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  )
     8  
     9  func init() {
    10  	// Reduce max score by 1 since the first test-case ({0, 0}) always passes, which gave free points.
    11  	max, weight := len(triangularTestsAG)-1, 5
    12  	scores.Add(TestTriangularAG, max, weight)
    13  	scores.Add(TestTriangularRecurrenceAG, max, weight)
    14  	scores.Add(TestTriangularFormulaAG, max, weight)
    15  	// Here is alternative strategy using subtests
    16  	for name := range funcs {
    17  		scores.AddSub(TestTriangularSubTestAG, name, max, weight)
    18  	}
    19  }
    20  
    21  var funcs = map[string]func(uint) uint{
    22  	"triangular":           triangular,
    23  	"triangularRecurrence": triangularRecurrence,
    24  	"triangularFormula":    triangularFormula,
    25  }
    26  
    27  var triangularTestsAG = []struct {
    28  	in, want uint
    29  }{
    30  	{0, 0},
    31  	{1, 1},
    32  	{2, 3},
    33  	{3, 6},
    34  	{4, 10},
    35  	{5, 15},
    36  	{6, 21},
    37  	{7, 28},
    38  	{8, 36},
    39  	{9, 45},
    40  	{10, 55},
    41  	{20, 210},
    42  	{42, 903},
    43  	{56, 1596},
    44  	{62, 1953},
    45  	{75, 2850},
    46  	{90, 4095},
    47  }
    48  
    49  func TestTriangularAG(t *testing.T) {
    50  	sc := scores.Max()
    51  	defer sc.Print(t)
    52  
    53  	for _, test := range triangularTestsAG {
    54  		if diff := cmp.Diff(test.want, triangular(test.in)); diff != "" {
    55  			sc.Dec()
    56  			t.Errorf("triangular(%d): (-want +got):\n%s", test.in, diff)
    57  		}
    58  	}
    59  }
    60  
    61  func TestTriangularRecurrenceAG(t *testing.T) {
    62  	sc := scores.Max()
    63  	defer sc.Print(t)
    64  
    65  	for _, test := range triangularTestsAG {
    66  		if diff := cmp.Diff(test.want, triangularRecurrence(test.in)); diff != "" {
    67  			sc.Dec()
    68  			t.Errorf("triangularRecurrence(%d): (-want +got):\n%s", test.in, diff)
    69  		}
    70  	}
    71  }
    72  
    73  func TestTriangularFormulaAG(t *testing.T) {
    74  	sc := scores.Max()
    75  	defer sc.Print(t)
    76  
    77  	for _, test := range triangularTestsAG {
    78  		if diff := cmp.Diff(test.want, triangularFormula(test.in)); diff != "" {
    79  			sc.Dec()
    80  			t.Errorf("triangularFormula(%d): (-want +got):\n%s", test.in, diff)
    81  		}
    82  	}
    83  }
    84  
    85  func TestTriangularSubTestAG(t *testing.T) {
    86  	for name, fn := range funcs {
    87  		t.Run(name, func(t *testing.T) {
    88  			sc := scores.MaxByName(t.Name())
    89  			defer sc.Print(t)
    90  			for _, test := range triangularTestsAG {
    91  				if diff := cmp.Diff(test.want, fn(test.in)); diff != "" {
    92  					sc.Dec()
    93  					t.Errorf("%s(%d): (-want +got):\n%s", name, test.in, diff)
    94  				}
    95  			}
    96  		})
    97  	}
    98  }