| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package main
- import "testing"
- func TestAdd(t *testing.T) {
- if got := add(2, 3); got != 5 {
- t.Fatalf("add(2, 3) = %d, want 5", got)
- }
- }
- func TestIncrease(t *testing.T) {
- value := 10
- if got := increase(value); got != 11 {
- t.Fatalf("increase(10) = %d, want 11", got)
- }
- if value != 10 {
- t.Fatalf("caller value = %d, want 10", value)
- }
- }
- func TestAverage(t *testing.T) {
- tests := []struct {
- name string
- scores []int
- want float64
- ok bool
- }{
- {name: "normal", scores: []int{80, 90, 85}, want: 85, ok: true},
- {name: "fraction", scores: []int{80, 81}, want: 80.5, ok: true},
- {name: "zero", scores: []int{0}, want: 0, ok: true},
- {name: "empty", scores: []int{}, want: 0, ok: false},
- {name: "nil", scores: nil, want: 0, ok: false},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, ok := average(tt.scores)
- if got != tt.want || ok != tt.ok {
- t.Fatalf("average(%v) = (%v, %t), want (%v, %t)", tt.scores, got, ok, tt.want, tt.ok)
- }
- })
- }
- }
- func TestSum(t *testing.T) {
- if got := sum(1, 2, 3); got != 6 {
- t.Fatalf("sum(1, 2, 3) = %d, want 6", got)
- }
- if got := sum(); got != 0 {
- t.Fatalf("sum() = %d, want 0", got)
- }
- values := []int{10, 20, 30}
- if got := sum(values...); got != 60 {
- t.Fatalf("sum(%v...) = %d, want 60", values, got)
- }
- }
|