简体   繁体   中英

C++ unit testing with Visual Studio test framework

I am having some issues setting up a sample test for a queue class I implemented.

Here is the queue class:

Queue.h:

    typedef float QueueInfoType;
    QueueInfoType x;
    class Queue
    {
      public:
        Queue(){front = rear = count = 0;}

        bool isEmpty();

        bool isFull();

        void add(QueueInfoType x);

        float remove();

        int numItems();

        enum {MAXQUEUE = 80};

      private:
        QueueInfoType values[MAXQUEUE];
        int front, rear, count;
    };

Queue.cpp:

    bool Queue::isEmpty()
    {
      return front == rear;
    }

    bool Queue::isFull()
    {
      return count >= MAXQUEUE;
    }

    void Queue::add(QueueInfoType x)
    {
      values[rear = (rear + 1) % MAXQUEUE] = x;
      count = count + 1;
    }

    float Queue::remove()
    {
      count = count - 1;
      return x = values[front = (front + 1) % MAXQUEUE];
    }

    int Queue::numItems()
    {
      return count;
    }

Test method:

    [TestMethod]
    void TestNumItems()
    {
        Queue q;
        for(int i = 0; i < 20; i++)
        {
            q.add(i);
        }
        int expected = 2;
        int actual = q.numItems();
        Assert::AreEqual(expected, actual,  "queue had: " + actual + " items");
    };

I'm obviously missing something, my count for the queue is never incremented when I call the add method to add an item to the queue, however items are added fine to the queue.

I am compiling my queue class in a static library and adding a reference to it in my test project.

Any ideas why the count for my queue never changes?

EDIT:

I am creating a circular queue with this class that has a max number of items defined by MAXQUEUE.

Above is how QueueInfoType is defined.

NOTE:

When I change the static library to an executable and add void main() to my queue.cpp and write code to test the queue's functions, it works just fine and count returns properly. Is there something happening when it is used as a static library by the test project?

I'm not sure of that, but a queue class must be implemented using pointers, so there isn't a size limit, and you can use something like that in the add function too:

void Queue::add(QueueInfoType x)
{
  count++;
  values[count] = x;
}

So the implementation is easier to understand, and about your mistake... I don't find it, it may work... I´ll continue think.

PD: sorry about my english

I think your expected should be "20".

Also, remove is returning a "float" but that should probably be a QueueInfoType.

I ran the test and it worked fine (other than testing 20 vs 2). actual was 20 for my test.

Also, the assignment of "x =" in remove doesn't seem right.

Try changing the for loop in the test routine to:

    for(int i = 0; i < 20; i++)
    {
        q.add(i + 100);
    }

If you end up getting a different number for your count, then its because you accidentally went out of bounds on your array and damaged the stack. Because of how the variables were defined, they might end up neighbors on the stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM