Go命令行输入测试

huangapple go评论88阅读模式
英文:

Go Command Line Input Testing

问题

有没有更好的方法来测试输入,比下面的方法更好?测试应该使用 fmt.Println("6 6 4") 来更好地模拟命令行输入吗?

main.go:

package main

import (
	"fmt"
	"math"
)

func input_calc(n, m, a float64) uint64 {

	fmt.Scanln(&n, &m, &a)

	a_in_n_ceil := uint64(math.Ceil(n / a))

	a_in_m_ceil := uint64(math.Ceil(m / a))

	a_in_n_and_m := a_in_n_ceil * a_in_m_ceil

	return a_in_n_and_m

}

func main() {

	fmt.Println(input_calc(0, 0, 0))

}

main_test.go:

package main

import "testing"

func TestMain(t *testing.T) {

	answer := input_calc(6, 6, 4)
	if answer != 4 {
		t.Log("错误应该是 4,但得到了", answer)
		t.Fail()
	}
}
英文:

is there a better way that might be recommend for testing input, than below? Should the test use fmt.Println("6 6 4") to better simulate the input from command line?

main.go:

package main

import (
	"fmt"
	"math"
)

func input_calc(n, m, a float64) uint64 {

	fmt.Scanln(&n, &m, &a)

	a_in_n_ceil := uint64(math.Ceil(n / a))

	a_in_m_ceil := uint64(math.Ceil(m / a))

	a_in_n_and_m := a_in_n_ceil * a_in_m_ceil

	return a_in_n_and_m

}

func main() {

	fmt.Println(input_calc(0, 0, 0))

}

main_test.go:

package main

import "testing"

func TestMain(t *testing.T) {

	answer := input_calc(6, 6, 4)
	if answer != 4 {
		t.Log("error should be 4, but got", answer)
		t.Fail()
	}
}

答案1

得分: 1

为了测试输入,可以使用io.Reader接口。对于你的例子,代码如下:

main.go

package main

import (
	"fmt"
	"io"
	"math"
	"os"
)

func input_calc(rdr io.Reader) (uint64, error) {
	var n, m, a float64
	_, err := fmt.Fscanln(rdr, &n, &m, &a)
	if err != nil {
		return 0, err
	}

	a_in_n_ceil := uint64(math.Ceil(n / a))
	a_in_m_ceil := uint64(math.Ceil(m / a))
	a_in_n_and_m := a_in_n_ceil * a_in_m_ceil

	return a_in_n_and_m, nil
}

func main() {
	rdr := os.Stdin
	answer, err := input_calc(rdr)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(answer)
}

main_test.go

package main

import (
	"strings"
	"testing"
)

func TestInputCalc(t *testing.T) {
	input := "6 6 4\n"
	rdr := strings.NewReader(input)
	answer, err := input_calc(rdr)
	if err != nil {
		t.Error(err)
	}
	if answer != 4 {
		t.Error("error should be 4, but got", answer)
	}
}

语句

input := "6 6 4\n"

input := fmt.Sprintln(6, 6, 4)

是等价的。

英文:

To test input, use the io.Reader interface. For your example,

main.go

package main  

import (
    "fmt"
    "io"
    "math"
    "os"
)

func input_calc(rdr io.Reader) (uint64, error) {
    var n, m, a float64
    _, err := fmt.Fscanln(rdr, &n, &m, &a)
    if err != nil {
	    return 0, err
    }

    a_in_n_ceil := uint64(math.Ceil(n / a))
    a_in_m_ceil := uint64(math.Ceil(m / a))
    a_in_n_and_m := a_in_n_ceil * a_in_m_ceil

    return a_in_n_and_m, nil
}

func main() {
    rdr := os.Stdin
    answer, err := input_calc(rdr)
    if err != nil {
	    fmt.Println(err)
	    return
    }
    fmt.Println(answer)
}

main_test.go

package main

import (
    "strings"
    "testing"
)

func TestInputCalc(t *testing.T) {
    input := "6 6 4\n"
    rdr := strings.NewReader(input)
    answer, err := input_calc(rdr)
    if err != nil {
	    t.Error(err)
    }
    if answer != 4 {
	    t.Error("error should be 4, but got", answer)
    }
}

The statements

input := "6 6 4\n"

and

input := fmt.Sprintln(6, 6, 4)

are equivalent.

huangapple
  • 本文由 发表于 2022年2月15日 09:08:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/71120070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定