-2

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 &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
}

My Test is look like this...

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)
        }
    }

}

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...

  • Is the `queue_test.go` file in the same package as the `queue.go` file ? (check the first line). – fzd Oct 29 '21 at 13:59
  • 2
    The only problem you are showing is the `go vet` failure. Fix the error shown in the output and run `go test` again. – JimB Oct 29 '21 at 14:11

1 Answers1

2

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("expected %v but recevied %v", 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.

Trevor Johnson
  • 874
  • 5
  • 19