如何使函数适应_test文件?

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

How to make the function suit the _test file?

问题

这是我的代码:

type Queue interface {
	Push(key interface{})
	Pop() interface{}
	Contains(key interface{}) bool
	Len() int
	Keys() []interface{}
}

type QueueData struct {
	size int
	data []interface{}
}

func New(size int) *QueueData {
	return &QueueData{
		size: size,
	}
}

func (q *QueueData) IsEmpty() bool {
	return len(q.data) == 0
}

// Peek : 返回队列中的下一个元素
func (q *QueueData) Peek() (interface{}, error) {
	if len(q.data) == 0 {
		return 0, fmt.Errorf("队列为空")
	}
	return q.data[0], nil
}

// Queue : 将元素添加到队列中,并返回指向当前队列的指针
func (q *QueueData) Push(n interface{}) *QueueData {
	if q.Len() < q.size {
		q.data = append(q.data, n)
	} else {
		q.Pop()
		q.Push(n)
	}
	return q
}

// Dequeue : 从队列中移除下一个元素并返回其值
//func (q *QueueFix) Pop() (interface{}, error) {
func (q *QueueData) Pop() interface{} {
	if len(q.data) == 0 {
		//return 0, fmt.Errorf("队列为空")
		return 0
	}
	element := q.data[0]
	q.data = q.data[1:]
	//return element, nil
	return element
}

func (q *QueueData) Len() int {
	return len(q.data)
}

func (q *QueueData) Keys() []interface{} {
	return q.data
}

func (q *QueueData) Contains(key interface{}) bool {
	cont := false
	for i := 0; i < q.Len(); i++ {
		if q.data[i] == key {
			cont = true
		}
	}
	return cont
}

我的测试代码如下:

var testValues = []interface{}{
	"lorem",
	"ipsum",
	1,
	2,
	3,
	"jack",
	"jill",
	"felix",
	"donking",
}

// TestPush 验证淘汰旧项目策略
func TestEvictPolicy(t *testing.T) {
	size := 5
	q := New(size)

	for i, v := range testValues {
		q.Push(v)

		t.Log("当前队列: ", q.Keys())

		// 验证
		// 项目存在性
		if !q.Contains(v) {
			t.Errorf("策略: 新插入的 %v 必须存在", v)
		}

		if i < 5 && q.Len() != (i+1) {
			t.Errorf("期望长度 %d,实际长度: %d", i+1, q.Len())
		} else if i >= 5 && q.Len() != 5 {
			t.Errorf("期望长度: %d,实际长度: %d", size, q.Len())
		}
	}
}

// TestPop 验证弹出项目策略
func TestPop(t *testing.T) {
	size := 5
	q := New(size)

	for _, v := range testValues {
		q.Push(v)
	}

	for q.Len() > 0 {
		t.Log("当前队列: ", q.Keys())

		v := q.Pop()

		// 验证
		expect := testValues[len(testValues)-(q.Len()+1)]
		if v != expect {
			t.Errorf("期望值 %v,实际值 %v", expect, v)
		}
	}

}

测试返回了以下错误:

.\Queue_test.go:60:4: 错误调用可能有格式化指令 %v
FAIL /C/Users/richa/go/src [构建失败]

我不明白如何从_test.go文件中创建代码。需要解释如何创建_test.go文件。只需给出一些关于此测试的参考资料,例如当我添加EvictPolicy函数时,它说未声明...

英文:

Here was my code

type Queue interface {
	Push(key interface{})
	Pop() interface{}
	Contains(key interface{}) bool
	Len() int
	Keys() []interface{}
}

type QueueData struct {
	size int
	data []interface{}
}

func New(size int) *QueueData {
	return &amp;QueueData{
		size: size,
	}
}

func (q *QueueData) IsEmpty() bool {
	return len(q.data) == 0
}

// Peek : returns the next element in the queue
func (q *QueueData) Peek() (interface{}, error) {
	if len(q.data) == 0 {
		return 0, fmt.Errorf(&quot;Queue is empty&quot;)
	}
	return q.data[0], nil
}

// Queue : adds an element onto the queue and returns an pointer to the current queue
func (q *QueueData) Push(n interface{}) *QueueData {
	if q.Len() &lt; q.size {
		q.data = append(q.data, n)
	} else {
		q.Pop()
		q.Push(n)
	}
	return q
}

// Dequeue : removes the next element from the queue and returns its value
//func (q *QueueFix) Pop() (interface{}, error) {
func (q *QueueData) Pop() interface{} {
	if len(q.data) == 0 {
		//return 0, fmt.Errorf(&quot;Queue is empty&quot;)
		return 0
	}
	element := q.data[0]
	q.data = q.data[1:]
	//return element, nil
	return element
}

func (q *QueueData) Len() int {
	return len(q.data)
}

func (q *QueueData) Keys() []interface{} {
	return q.data
}

func (q *QueueData) Contains(key interface{}) bool {
	cont := false
	for i := 0; i &lt; q.Len(); i++ {
		if q.data[i] == key {
			cont = true
		}
	}
	return cont
}

My Test is look like this...

var testValues = []interface{}{
	&quot;lorem&quot;,
	&quot;ipsum&quot;,
	1,
	2,
	3,
	&quot;jack&quot;,
	&quot;jill&quot;,
	&quot;felix&quot;,
	&quot;donking&quot;,
}

// TestPush validate evict old item policy
func TestEvictPolicy(t *testing.T) {
	size := 5
	q := New(size)

	for i, v := range testValues {
		q.Push(v)

		t.Log(&quot;current: &quot;, q.Keys())

		// validate
		// item existence
		if !q.Contains(v) {
			t.Errorf(&quot;policy: newly inserted %v must be exists&quot;, v)
		}

		if i &lt; 5 &amp;&amp; q.Len() != (i+1) {
			t.Errorf(&quot;expected length %d but actual: %d&quot;, i+1, q.Len())
		} else if i &gt;= 5 &amp;&amp; q.Len() != 5 {
			t.Errorf(&quot;expexted length: %d but actual: %d&quot;, size, q.Len())
		}
	}
}

// TestPop validate pop item policy
func TestPop(t *testing.T) {
	size := 5
	q := New(size)

	for _, v := range testValues {
		q.Push(v)
	}

	for q.Len() &gt; 0 {
		t.Log(&quot;current: &quot;, q.Keys())

		v := q.Pop()

		// validate
		expect := testValues[len(testValues)-(q.Len()+1)]
		if v != expect {
			t.Error(&quot;expected %v but recevied %v&quot;, expect, v)
		}
	}

}

The test returned

> .\Queue_test.go:60:4: Error call has possible formatting directive %v
> FAIL /C/Users/richa/go/src [build failed]

I didn't i understand how to make an code from _test.go file. Need an explain how to make the _test.go aswell. Just give some reference for this testing. like when i add EvictPolicy Function, itsay undeclared...

答案1

得分: 2

你看到的错误信息是这样的:

Queue_test.go:60:4: Error call has possible formatting directive %v FAIL

看起来是指的这一行代码:

t.Error("expected %v but recevied %v", expect, v)

在这一行中,你调用了t.Error来打印错误信息,并将expectv变量格式化为错误字符串。但是,t.Error并不会对额外的参数进行格式化,它只是将它们打印出来。

你应该像对其他错误信息一样调用t.Errorf

英文:

The error you are seeing

Queue_test.go:60:4: Error call has possible formatting directive %v FAIL

Looks to be referring to this line


t.Error(&quot;expected %v but recevied %v&quot;, expect, v)

On this line you called t.Error to print the error message and format the expect and v vars into the error string. But, t.Error does not format the additional arguments. It just prints them.

You should call t.Errorf like you did for your other error messages.

huangapple
  • 本文由 发表于 2021年10月29日 21:51:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/69770094.html
匿名

发表评论

匿名网友

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

确定