在GO语言中测试列表

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

Testing lists in GO

问题

我正在尝试在GO中实现一个测试。但是我在结构体内部的列表语法方面遇到了困难。

package primeFactor

import "testing"

var testCases = []struct {
    p        int
    expected []int
}{
    {15, [3,5]},
    {26, [2,13]},
    {37, [37]},
    {42, [2,3,7]},
}

func TestPrimeFactor(t *testing.T) {
    for _, test := range testCases {
        observed := PrimeFactor(test.p)
        if observed != test.expected {
            t.Error("For p = %d, expected %t. Got %t.",
                test.p, test.expected, observed)
        }
    }
}

我得到的输出错误是:

expected ']', found ','
: expected operand, found '{'
: expected ';', found 'for'

我感谢你的帮助。谢谢。

英文:

I'm trying to implement a test in GO. But I'm struggling with the list's syntax inside the struct.

package primeFactor

import "testing"

var testCases = []struct {
	p        int
	expected []int
}{
	{15, [3,5]},
	{26, [2,13]},
	{37, [37]},
	{42, [2,3,7]},
}

func TestPrimeFactor(t *testing.T) {
	for _, test := range testCases {
		observed := PrimeFactor(test.p)
		if observed != test.expected {
			t.Error("For p = %d, expected %t. Got %t.",
				test.p, test.expected, observed)
		}
	}
}

The output error I have is:

expected ']', found ','
: expected operand, found '{'
: expected ';', found 'for'

I appreciate your help. Thanks.

答案1

得分: 4

Toni的回答解决了你的具体问题,但是为了解决比较切片的另一个问题,你需要使用reflect.DeepEqual

看看这个例子:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	observed := []int{1, 2}
	expected := []int{1, 3}

	if reflect.DeepEqual(observed, expected) {
		fmt.Println("切片相同")
	} else {
		fmt.Println("切片不同")
	}
}

你可以在这里运行这个例子:https://play.golang.org/p/_JRQ5bqmJf

英文:

Toni's answer addresses your specific problem but to address the other issue of comparing slices you'll want to use reflect.DeepEqual

Check out this example:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	observed := []int{1, 2}
	expected := []int{1, 3}

	if reflect.DeepEqual(observed, expected) {
		fmt.Println("Slices are the same")
	} else {
		fmt.Println("Slices are different")
	}
}

https://play.golang.org/p/_JRQ5bqmJf

答案2

得分: 3

为什么你一开始就写成那样?那不是Go语法。根据规范

> 切片字面量描述了整个底层数组字面量。因此,切片字面量的长度和容量是最大元素索引加一。切片字面量的形式为
>
> []T{x1, x2, … xn}

所以,在你的情况下:

var testCases = []struct {
    p        int
    expected []int
}{
    {15, []int{3, 5}},
    {26, []int{2, 13}},
    {37, []int{37}},
    {42, []int{2, 3, 7}},
}

这个规范非常易读,比人们想象的要简单。你可能想要全面阅读它,并将其作为参考保持在手边。

英文:

Why do you wrote that in the first place? That's not Go syntax. From the spec:

> A slice literal describes the entire underlying array literal. Thus, the length and capacity of a slice literal are the maximum element index plus one. A slice literal has the form
>
> []T{x1, x2, … xn}

So, in your case:

var testCases = []struct {
    p        int
    expected []int
}{
    {15, []int{3, 5}},
    {26, []int{2, 13}},
    {37, []int{37}},
    {42, []int{2, 3, 7}},
}

The spec is pretty readable and less scary than one might think. You may want to give it a full look and keep it close for reference.

答案3

得分: 2

...为了完整起见,这里只是一个简单的示例,展示如何编写自己的函数,供你的测试调用以比较切片:

func slicesMatch(a, b []int) bool {
    la := len(a)
    lb := len(b)

    if la != lb {
        return false
    }

    for i := 0; i < la; i++ {
        if a[i] != b[i] {
            return false
        }
    }

    return true
}

在 Playground 上查看

英文:

...and for completeness, here's just a simple example of writing your own function that your test can call to compare the slices:

func slicesMatch(a, b []int) bool {
	la := len(a)
	lb := len(b)

	if la != lb {
    	return false
    }

    for i := 0; i &lt; la; i++ {
    	if a[i] != b[i] {
	    	return false
	    }
    }

    return true
}

<kbd>View it on the Playground</kbd>

huangapple
  • 本文由 发表于 2015年1月27日 04:08:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/28157891.html
匿名

发表评论

匿名网友

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

确定