Skip to content

Commit c00e19c

Browse files
committed
feat: add lab1
1 parent 38f466e commit c00e19c

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

.github/workflows/lab1.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: lab1
5+
6+
on:
7+
push:
8+
paths:
9+
- 'lab1/**'
10+
11+
jobs:
12+
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- uses: actions/setup-go@v4
20+
with:
21+
go-version-file: 'lab1/go.mod'
22+
cache: false
23+
24+
- name: Run
25+
working-directory: 'lab1'
26+
run: go test

lab1/lab1.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("Welcome to Simple Calculator")
7+
8+
var a, b int64
9+
fmt.Print("Enter first number: ")
10+
fmt.Scan(&a)
11+
12+
fmt.Print("Enter second number: ")
13+
fmt.Scan(&b)
14+
15+
fmt.Println("Add:", Add(a, b))
16+
fmt.Println("Subtract:", Sub(a, b))
17+
fmt.Println("Multiply:", Mul(a, b))
18+
fmt.Println("Divide:", Div(a, b))
19+
}
20+
21+
// TODO: Create `Add`, `Sub`, `Mul`, `Div` function here

lab1/lab1_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
package main
3+
4+
import (
5+
"fmt"
6+
"os"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
var testCases = []struct {
13+
a, b, addExpected, subExpected, mulExpected, divExpected int64
14+
}{
15+
{1, 2, 3, -1, 2, 0},
16+
{17, 3, 20, 14, 51, 5},
17+
{2147, 107, 2254, 2040, 229729, 20},
18+
{39957, 673, 40630, 39284, 26891061, 59},
19+
{45571256, 1007, 45572263, 45570249, 45890254792, 45254},
20+
}
21+
22+
func TestAdd(t *testing.T) {
23+
for _, tc := range testCases {
24+
assert.Equal(t, tc.addExpected, Add(tc.a, tc.b))
25+
}
26+
}
27+
28+
func TestSub(t *testing.T) {
29+
for _, tc := range testCases {
30+
assert.Equal(t, tc.subExpected, Sub(tc.a, tc.b))
31+
}
32+
}
33+
34+
func TestMul(t *testing.T) {
35+
for _, tc := range testCases {
36+
assert.Equal(t, tc.mulExpected, Mul(tc.a, tc.b))
37+
}
38+
}
39+
40+
func TestDiv(t *testing.T) {
41+
for _, tc := range testCases {
42+
assert.Equal(t, tc.divExpected, Div(tc.a, tc.b))
43+
}
44+
}
45+
46+
func TestE2E(t *testing.T) {
47+
var err error
48+
49+
r1, w1, _ := os.Pipe()
50+
r2, w2, _ := os.Pipe()
51+
52+
stdin := os.Stdin
53+
stdout := os.Stdout
54+
55+
defer func() {
56+
os.Stdin = stdin
57+
os.Stdout = stdout
58+
}()
59+
60+
os.Stdin = r1
61+
os.Stdout = w2
62+
63+
buf := make([]byte, 1024)
64+
var n int
65+
66+
for _, tc := range testCases {
67+
input := fmt.Sprintf("%d\n%d\n", tc.a, tc.b)
68+
expected := fmt.Sprintf("Add: %d\nSubtract: %d\nMultiply: %d\nDivide: %d\n", tc.addExpected, tc.subExpected, tc.mulExpected, tc.divExpected)
69+
70+
if _, err = w1.Write([]byte(input)); err != nil {
71+
t.Fatal(err)
72+
}
73+
main()
74+
75+
if n, err = r2.Read(buf); err != nil {
76+
t.Fatal(err)
77+
}
78+
if n < 70 {
79+
t.Fatal("Error")
80+
}
81+
assert.Equal(t, expected, string(buf[70:n]))
82+
}
83+
}

0 commit comments

Comments
 (0)