-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: lab1 | ||
|
||
on: | ||
push: | ||
paths: | ||
- 'lab1/**' | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: 'lab1/go.mod' | ||
cache: false | ||
|
||
- name: Run | ||
working-directory: 'lab1' | ||
run: go test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
fmt.Println("Welcome to Simple Calculator") | ||
|
||
var a, b int64 | ||
fmt.Print("Enter first number: ") | ||
fmt.Scan(&a) | ||
|
||
fmt.Print("Enter second number: ") | ||
fmt.Scan(&b) | ||
|
||
fmt.Println("Add:", Add(a, b)) | ||
fmt.Println("Subtract:", Sub(a, b)) | ||
fmt.Println("Multiply:", Mul(a, b)) | ||
fmt.Println("Divide:", Div(a, b)) | ||
} | ||
|
||
// TODO: Create `Add`, `Sub`, `Mul`, `Div` function here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var testCases = []struct { | ||
a, b, addExpected, subExpected, mulExpected, divExpected int64 | ||
}{ | ||
{1, 2, 3, -1, 2, 0}, | ||
{17, 3, 20, 14, 51, 5}, | ||
{2147, 107, 2254, 2040, 229729, 20}, | ||
{39957, 673, 40630, 39284, 26891061, 59}, | ||
{45571256, 1007, 45572263, 45570249, 45890254792, 45254}, | ||
} | ||
|
||
func TestAdd(t *testing.T) { | ||
for _, tc := range testCases { | ||
assert.Equal(t, tc.addExpected, Add(tc.a, tc.b)) | ||
} | ||
} | ||
|
||
func TestSub(t *testing.T) { | ||
for _, tc := range testCases { | ||
assert.Equal(t, tc.subExpected, Sub(tc.a, tc.b)) | ||
} | ||
} | ||
|
||
func TestMul(t *testing.T) { | ||
for _, tc := range testCases { | ||
assert.Equal(t, tc.mulExpected, Mul(tc.a, tc.b)) | ||
} | ||
} | ||
|
||
func TestDiv(t *testing.T) { | ||
for _, tc := range testCases { | ||
assert.Equal(t, tc.divExpected, Div(tc.a, tc.b)) | ||
} | ||
} | ||
|
||
func TestE2E(t *testing.T) { | ||
var err error | ||
|
||
r1, w1, _ := os.Pipe() | ||
r2, w2, _ := os.Pipe() | ||
|
||
stdin := os.Stdin | ||
stdout := os.Stdout | ||
|
||
defer func() { | ||
os.Stdin = stdin | ||
os.Stdout = stdout | ||
}() | ||
|
||
os.Stdin = r1 | ||
os.Stdout = w2 | ||
|
||
buf := make([]byte, 1024) | ||
var n int | ||
|
||
for _, tc := range testCases { | ||
input := fmt.Sprintf("%d\n%d\n", tc.a, tc.b) | ||
expected := fmt.Sprintf("Add: %d\nSubtract: %d\nMultiply: %d\nDivide: %d\n", tc.addExpected, tc.subExpected, tc.mulExpected, tc.divExpected) | ||
|
||
if _, err = w1.Write([]byte(input)); err != nil { | ||
t.Fatal(err) | ||
} | ||
main() | ||
|
||
if n, err = r2.Read(buf); err != nil { | ||
t.Fatal(err) | ||
} | ||
if n < 70 { | ||
t.Fatal("Error") | ||
} | ||
assert.Equal(t, expected, string(buf[70:n])) | ||
} | ||
} |