proceed to learn go!

This commit is contained in:
a.pivkin
2025-06-30 06:58:29 +03:00
parent 498d83835a
commit 6f809ab005
24 changed files with 567 additions and 15 deletions
+14 -3
View File
@@ -1,14 +1,25 @@
// Package calculator does simple calculations.
package calculator // Add takes two numbers and returns the result of adding them together.
func Add(a, b float64) float64 {
import (
"errors"
)
func Add(a, b int) int {
return a + b
}
func Subtract(a, b float64) float64 {
func Subtract(a, b int) int {
return a - b
}
func Multiply(a, b float64) float64 {
func Multiply(a, b int) int {
return a * b
}
func Divide(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("division by zero not allowed")
}
return a / b, nil
}
+65 -12
View File
@@ -7,27 +7,80 @@ import (
func TestAdd(t *testing.T) {
t.Parallel()
var want float64 = 4
got := calculator.Add(2, 2)
if want != got {
t.Errorf("want %f, got %f", want, got)
type TestCase struct {
a, b int
want int
}
testCases := []TestCase{
{a: 2, b: 2, want: 4},
{a: 5, b: 0, want: 5},
{a: 1, b: 1, want: 2},
}
for _, tc := range testCases {
got := calculator.Add(tc.a, tc.b)
if tc.want != got {
t.Errorf("Add(%d, %d): want %d, got %d", tc.a, tc.b, tc.want, got)
}
}
}
func TestSubtract(t *testing.T) {
t.Parallel()
var want float64 = 2
got := calculator.Subtract(4, 2)
if want != got {
t.Errorf("want %f, got %f", want, got)
type testCase struct {
a,b int
want int
}
testCases := []testCase {
{a: 5,b: 3,want: 2},
{a: 4,b: 3,want: 1},
{a: 10,b: 3,want: 7},
}
for _,tc := range testCases {
got := calculator.Subtract(tc.a, tc.b)
if tc.want != got {
t.Errorf("Subtract(%d, %d): want %d, got %d", tc.a,tc.b,tc.want, got)
}
}
}
func TestMultiply(t *testing.T) {
t.Parallel()
var want float64 = 6
got := calculator.Multiply(2, 3)
if want != got {
t.Errorf("want %f, got %f", want, got)
type testCase struct {
a,b int
want int
}
testCases := []testCase {
{a: 5,b: 3,want: 15},
{a: 4,b: 3,want: 12},
{a: 10,b: 3,want: 30},
}
for _,tc := range testCases {
got := calculator.Multiply(tc.a,tc.b)
if tc.want != got {
t.Errorf("Multiply(%d, %d): want %d, got %d", tc.a, tc.b, tc.want, got)
}
}
}
func TestDivide(t *testing.T) {
t.Parallel()
type testCase struct {
a,b int
want int
}
testCases := []testCase {
{a: 6,b: 3,want: 2},
{a: 12,b: 3,want: 4},
{a: 10,b: 2,want: 5},
}
for _,tc := range testCases {
got,err := calculator.Divide(tc.a,tc.b)
if err != nil {
t.Errorf("WTF r u doing?")
}
if tc.want != got {
t.Errorf("Multiply(%d, %d): want %d, got %d", tc.a, tc.b, tc.want, got)
}
}
}
+11
View File
@@ -0,0 +1,11 @@
package main
import (
"calculator"
"fmt"
)
func main() {
result := calculator.Add(2, 2)
fmt.Println(result)
}
Executable
BIN
View File
Binary file not shown.