Skip to content

Commit 854dc2c

Browse files
committed
feat: add lab7
1 parent d9f7424 commit 854dc2c

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

.github/workflows/lab7.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: lab7
5+
6+
on:
7+
push:
8+
paths:
9+
- 'lab7/**'
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: 'lab7/go.mod'
22+
cache: false
23+
24+
- name: Run
25+
working-directory: 'lab7'
26+
run: go test

lab7/lab7.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
"time"
7+
)
8+
9+
var doorStatus string
10+
var handStatus string
11+
12+
func hand() {
13+
handStatus = "in"
14+
time.Sleep(time.Millisecond * 200)
15+
handStatus = "out"
16+
wg.Done()
17+
}
18+
19+
func door() {
20+
doorStatus = "close"
21+
time.Sleep(time.Millisecond * 200)
22+
if handStatus == "in" {
23+
fmt.Println("夾到手了啦!")
24+
} else {
25+
fmt.Println("沒夾到喔!")
26+
}
27+
doorStatus = "open"
28+
wg.Done()
29+
}
30+
31+
var wg sync.WaitGroup
32+
33+
func main() {
34+
for i := 0; i < 50; i++ {
35+
wg.Add(2)
36+
go door()
37+
go hand()
38+
wg.Wait()
39+
time.Sleep(time.Millisecond * 200)
40+
}
41+
}

lab7/lab7_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestLab08(t *testing.T) {
11+
var err error
12+
r2, w2, _ := os.Pipe()
13+
stdout := os.Stdout
14+
15+
defer func() {
16+
os.Stdout = stdout
17+
}()
18+
os.Stdout = w2
19+
20+
buf := make([]byte, 10240)
21+
var n int
22+
23+
main()
24+
n, err = r2.Read(buf)
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
29+
expected := strings.Repeat("沒夾到喔!\n", 50)
30+
assert.Equal(t, expected, string(buf[:n]))
31+
}

0 commit comments

Comments
 (0)