-3

这是我的代码

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 : returns the next element in the queue
func (q *QueueData) Peek() (interface{}, error) {
    if len(q.data) == 0 {
        return 0, fmt.Errorf("Queue is empty")
    }
    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() < 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("Queue is empty")
        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 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("current: ", q.Keys())

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

        if i < 5 && q.Len() != (i+1) {
            t.Errorf("expected length %d but actual: %d", i+1, q.Len())
        } else if i >= 5 && q.Len() != 5 {
            t.Errorf("expexted length: %d but actual: %d", 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() > 0 {
        t.Log("current: ", q.Keys())

        v := q.Pop()

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

}

测试返回

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

我不明白如何从 _test.go 文件中制作代码。还需要解释如何制作 _test.go。只是给这个测试一些参考。就像我添加 EvictPolicy 函数时一样,它未声明...

4

1 回答 1

1

您看到的错误

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

看起来是指这条线


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

在这一行中,您调用t.Error以打印错误消息并将expectand vvars 格式化为错误字符串。但是,t.Error不会格式化附加参数。它只是打印它们。

您应该t.Errorf像处理其他错误消息一样调用。

于 2021-10-30T06:21:24.973 回答