Skip to content

Commit

Permalink
supporting macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
tsureko-chinese committed Dec 29, 2019
1 parent 380a609 commit f8d50a8
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 447 deletions.
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

## 序列检查工具

如果您想查看您的程序的输出结果与正确结果不同的地方,可以使用`result_check_windows.exe``result_check_linux`序列检查工具,它可以将您的输出与答案对比,高亮出不同的地方:
如果您想查看您的程序的输出结果与正确结果不同的地方,可以使用`result_check_windows.exe``result_check_linux``result_check_darwin`序列检查工具,它可以将您的输出与答案对比,高亮出不同的地方:

* 序列检查工具会帮助您自动运行运行所有示例
* 序列检查工具目前支持运行`可执行程序``.class`文件(java)和`.py`文件
Expand All @@ -65,7 +65,13 @@
* Linux

```powershell
linux@someone-linux~$./os-test-shell/result_check_linux [exec-filename]
someone@someone-linux~$./os-test-shell/result_check_linux [exec-filename]
```

* macOS

```powershell
someone@someone-mbp~$./os-test-shell/result_check_darwin [exec-filename]
```

### Python
Expand All @@ -85,10 +91,16 @@
* Linux

```powershell
linux@someone-pc~$./os-test-shell/result_check_linux [filename].py [python3|python2]
someone@someone-linux~$./os-test-shell/result_check_linux [filename].py [python3|python2]
```

### Java(仅限Windows)
* macOS

```powershell
someone@someone-mbp~$./os-test-shell/result_check_darwin [filename].py [python3|python2]
```

### Java

使用方式如下:

Expand All @@ -101,7 +113,19 @@
* Windows

```powershell
D:>\os_test_shell\result_check_windows.exe [.class filename] java
D:>\os_test_shell\result_check_windows.exe [.class filename(without suffix)] java
```

* Linux

```powershell
someone@someone-linux~$./os-test-shell/result_check_linux [.class filename(without suffix)] java
```

* macOS

```powershell
someone@someone-mbp~$./os-test-shell/result_check_darwin [.class filename(without suffix)] java
```

### Example
Expand All @@ -118,7 +142,6 @@

![full](https://raw.githubusercontent.com/joexu01/joexu01.github.io/master/full.png)

* 请注意,序号下方第一行是您的程序的输出,第二行是预期的正确输出,所有超过正确输出长度的输出都会被忽略掉

## 具体用例

Expand Down
Binary file added result_check_darwin
Binary file not shown.
Binary file modified result_check_linux
Binary file not shown.
Binary file modified result_check_windows.exe
Binary file not shown.
5 changes: 4 additions & 1 deletion source_code/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
如果你想自己编译,可以把这个文件夹放在`$GOPATH/src`中然后`cd`到文件夹直接`go build`
如果你想自己编译,可以把这个文件夹放在`$GOPATH/src`中然后`cd`到文件夹

* 执行`go get github.com/issue9/term`
* `go build check.go`

208 changes: 208 additions & 0 deletions source_code/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package main

import (
"bufio"
"errors"
"fmt"
"github.com/issue9/term/colors"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strings"
)

type checkBox struct {
outputStr string
supposedStr string
correct bool
}

var (
dirFileNames []string
thirdArg = false
)

func main() {
// 获取当前目录下的文件,并将文件名都存入dirFileNames
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatalln("cannot read files from current directory")
}
for _, eachFcb := range files {
dirFileNames = append(dirFileNames, eachFcb.Name())
}

osInfo := runtime.GOOS
fmt.Printf("Environment: %s\n\n", osInfo)

if len(os.Args) <= 1 {
fmt.Println("please pass your file name as the first parameter")
os.Exit(1)
} else if len(os.Args) == 3 {
thirdArg = true
err = repeatCheck()
} else {
err = repeatCheck()
}

if err != nil {
log.Fatalln(err.Error())
}
os.Exit(0)
}

// checkIfFileExists 检查txt文件是否在当前工作目录
func checkIfFileExists(fileName string) bool {
for _, eachName := range dirFileNames {
if eachName == fileName[2:] {
return true
}
}
return false
}

// readCorrectResults 函数返回测试序列结果文件内容
func readCorrectResults() ([]string, error) {
resultFile, err := os.Open("./result.txt")
if err != nil {
return nil, err
}
defer resultFile.Close()

bufReader := bufio.NewReader(resultFile)
var lines []string
for {
line, _, err := bufReader.ReadLine() // 按行读
if err != nil {
if err == io.EOF {
// 到文件尾就跳出
break
}
} else {
l := string(line)
lines = append(lines, l)
}
}
if len(lines) == 0 {
return nil, errors.New("empty result file")
}
return lines, nil
}

// checkResult 执行一次学生的实验程序,将输出结果和
// 从参数接收到的字符串序列做比较,高亮显示不同的部分
func checkResult(testFileName, supposedSeq string) error {
var cmd *exec.Cmd

// 如果有第三个参数,就将第三个参数作为命令
// 常见的参数有java、python2、python3
if !thirdArg {
cmd = exec.Command(os.Args[1], testFileName)
} else {
cmd = exec.Command(os.Args[2], os.Args[1], testFileName)
}

resultBuf, err := cmd.Output()
if err != nil {
return err
}

// 去掉换行符和不必要的前后空格
result := string(resultBuf)
result = strings.Replace(result, "\r", "", -1)
result = strings.Replace(result, "\n", "", -1)
result = strings.TrimSpace(result)

// 把输出字符串和结果字符串劈成List逐项比较
resultList := strings.Split(result, " ")
supposedList := strings.Split(supposedSeq, " ")

var boxes []checkBox
var incorrect = false
for index, supposed := range supposedList {
// 长度大于期望串长度的输出会被忽略
if index < len(resultList) && supposed == resultList[index] {
boxes = append(boxes, checkBox{
outputStr: supposed,
supposedStr: supposed,
correct: true,
})
} else if index < len(resultList) && supposed != resultList[index] {
boxes = append(boxes, checkBox{
outputStr: resultList[index],
supposedStr: supposed,
correct: false,
})
incorrect = true
} else {
boxes = append(boxes, checkBox{
outputStr: "",
supposedStr: supposed,
correct: false,
})
incorrect = true
}
}

// 打印本次测试的结果
if incorrect {
_, _ = colors.Printf(colors.White, colors.Red, "%s: incorrect", testFileName)
} else {
_, _ = colors.Printf(colors.White, colors.Green, "%s passed", testFileName)
}
fmt.Println()

fmt.Printf("%-8s: ", "Index")
for i := 0; i < len(supposedList); i++ {
fmt.Printf("%-4d", i)
}
fmt.Println()

fmt.Printf("%-8s: ", "Output")
for _, eachBox := range boxes {
if eachBox.correct {
fmt.Printf("%-4s", eachBox.outputStr)
} else {
_, _ = colors.Printf(colors.Default, colors.Red, "%-4s", eachBox.outputStr)
}
}
fmt.Println()

fmt.Printf("%-8s: ", "Supposed")
for _, eachBox := range boxes {
if eachBox.correct {
fmt.Printf("%-4s", eachBox.supposedStr)
} else {
_, _ = colors.Printf(colors.Default, colors.Green, "%-4s", eachBox.supposedStr)
}
}
fmt.Println()
return nil
}

// repeatCheck 根据结果文件的行数运行结果检查
func repeatCheck() error {
correctResults, err := readCorrectResults()
if err != nil {
return err
}
fileNum := len(correctResults)

for i := 0; i < fileNum; i++ {
testFile := "./" + string(48+i) + ".txt"
ifExist := checkIfFileExists(testFile)
if !ifExist {
fmt.Printf("cannot find file %s\n", testFile)
continue
}
err = checkResult(testFile, correctResults[i])
if err != nil {
fmt.Println(err.Error())
continue
}
}
return nil
}
5 changes: 5 additions & 0 deletions source_code/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module exercises

go 1.13

require github.com/issue9/term v1.1.0
5 changes: 5 additions & 0 deletions source_code/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
github.com/issue9/assert v1.3.2/go.mod h1:9Ger+iz8X7r1zMYYwEhh++2wMGWcNN2oVI+zIQXxcio=
github.com/issue9/term v1.1.0 h1:vKTjD4c0rxw+B7LLfwSypMaVTkyP9DrqyLLgFWsVTVw=
github.com/issue9/term v1.1.0/go.mod h1:9/YTi/aRSKOIy44Wso2xDWXJvMLAEYWskjutTkyUBN0=
golang.org/x/sys v0.0.0-20190310054646-10058d7d4faa h1:lqti/xP+yD/6zH5TqEwx2MilNIJY5Vbc6Qr8J3qyPIQ=
golang.org/x/sys v0.0.0-20190310054646-10058d7d4faa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Loading

0 comments on commit f8d50a8

Please sign in to comment.