| 13 | const epsilon = 0.001 |
| 14 | |
| 15 | func TestSquareRootCalculation(t *testing.T) { |
| 16 | tests := []struct { |
| 17 | name string |
| 18 | number float32 |
| 19 | want float64 |
| 20 | }{ |
| 21 | {"sqrt(1)", 1, 1}, |
| 22 | {"sqrt(9)", 9, 3}, |
| 23 | {"sqrt(25)", 25, 5}, |
| 24 | {"sqrt(121)", 121, 11}, |
| 25 | {"sqrt(10000)", 10000, 100}, |
| 26 | {"sqrt(169)", 169, 13}, |
| 27 | {"sqrt(0)", 0, 0}, |
| 28 | } |
| 29 | for _, test := range tests { |
| 30 | t.Run(test.name, func(t *testing.T) { |
| 31 | got := Sqrt(test.number) |
| 32 | delta := math.Abs(test.want - float64(got)) |
| 33 | if delta > epsilon { |
| 34 | t.Errorf("Sqrt() = %v, want %v delta %v", got, test.want, delta) |
| 35 | } |
| 36 | }) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func BenchmarkSquareRootCalculation(b *testing.B) { |
| 41 | for i := 0; i < b.N; i++ { |