main_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package main
  2. import "testing"
  3. func TestAdd(t *testing.T) {
  4. if got := add(2, 3); got != 5 {
  5. t.Fatalf("add(2, 3) = %d, want 5", got)
  6. }
  7. }
  8. func TestIncrease(t *testing.T) {
  9. value := 10
  10. if got := increase(value); got != 11 {
  11. t.Fatalf("increase(10) = %d, want 11", got)
  12. }
  13. if value != 10 {
  14. t.Fatalf("caller value = %d, want 10", value)
  15. }
  16. }
  17. func TestAverage(t *testing.T) {
  18. tests := []struct {
  19. name string
  20. scores []int
  21. want float64
  22. ok bool
  23. }{
  24. {name: "normal", scores: []int{80, 90, 85}, want: 85, ok: true},
  25. {name: "fraction", scores: []int{80, 81}, want: 80.5, ok: true},
  26. {name: "zero", scores: []int{0}, want: 0, ok: true},
  27. {name: "empty", scores: []int{}, want: 0, ok: false},
  28. {name: "nil", scores: nil, want: 0, ok: false},
  29. }
  30. for _, tt := range tests {
  31. t.Run(tt.name, func(t *testing.T) {
  32. got, ok := average(tt.scores)
  33. if got != tt.want || ok != tt.ok {
  34. t.Fatalf("average(%v) = (%v, %t), want (%v, %t)", tt.scores, got, ok, tt.want, tt.ok)
  35. }
  36. })
  37. }
  38. }
  39. func TestSum(t *testing.T) {
  40. if got := sum(1, 2, 3); got != 6 {
  41. t.Fatalf("sum(1, 2, 3) = %d, want 6", got)
  42. }
  43. if got := sum(); got != 0 {
  44. t.Fatalf("sum() = %d, want 0", got)
  45. }
  46. values := []int{10, 20, 30}
  47. if got := sum(values...); got != 60 {
  48. t.Fatalf("sum(%v...) = %d, want 60", values, got)
  49. }
  50. }