Skip to content

Commit

Permalink
update the lab01
Browse files Browse the repository at this point in the history
  • Loading branch information
KeithLin724 committed Sep 21, 2023
1 parent d8d5915 commit c5bba67
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/lab01.yml
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: lab01

on:
push:
paths:
- 'lab01/**'

jobs:

build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
go-version-file: 'lab01/go.mod'
cache: false

- name: Run
working-directory: 'lab01'
run: go test
33 changes: 33 additions & 0 deletions lab01/lab01.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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
func Add(a int64, b int64) int64 {
return a + b
}
func Sub(a int64, b int64) int64 {
return a - b
}
func Mul(a int64, b int64) int64 {
return a * b
}
func Div(a int64, b int64) int64 {
return a / b
}
82 changes: 82 additions & 0 deletions lab01/lab01_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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]))
}
}

0 comments on commit c5bba67

Please sign in to comment.