C++ unit testing with Visual Studio test framework - c++

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

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

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.
 

Related

Error: Char 34: runtime error: addition of unsigned offset to 0x603000000070 overflowed to 0x60300000006c (stl_vector.h) (C++)

I have been trying to solve the sorted Squares leetcode problem (https://leetcode.com/explore/learn/card/fun-with-arrays/521/introduction/3240/), and I am mostly through it. However, I get the above error. Following is my code
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int start = 0;
int end = nums.size()-1;
vector<int> final(nums.size());
int finalIdx = final.size()-1;
int sqr = 0;
while(start<=end){
if (abs(nums[start])<abs(nums[end])){
sqr = nums[end]*nums[end];
final[finalIdx] = sqr;
finalIdx--;
end--;
}
if (abs(nums[start])>abs(nums[end])){
sqr = nums[start]*nums[start];
final[finalIdx] = sqr;
finalIdx--;
start++;
}
else if(abs(nums[start])==abs(nums[end])){
sqr = nums[end]*nums[end];
final[finalIdx] = sqr;
finalIdx--;
final[finalIdx] = sqr;
finalIdx--;
start++;
end--;
}
return final;
}
}
};
The issue lies in my loop condition I believe. When I change the condition to start<end, I have no compile error, but the first element of the output array (final) is always 0, which I assume is by default. However, when I try to do start<=end in order to add a condition that handles the start==end case, I get the above error. I would like to understand why this is happening so I can rectify the issue. Thanks!
First, that's not a "compile error" ; it's a runtime error (and the error message reported says as much.
That said, the issue stems from the condition of start <= end landing on the = part of that condition. Eventually that is guaranteed to happen, save for one very specific set of circumstances:
start = (end-1)
abs(num[start]) == abs(num[end])
When that happens, your code will dump two values to the output vector, and both increment start and decrement end. The start and end indexes effectively swap values, the while condition is no longer true, and the loop will now cleanly exit.
In all other circumstances start and end will eventually land on the same index. When that happens your dual-push logic will dump the same value twice into the target vector, and that is where the issue manifests. There is only one value left to push (and start and end both reference it by index). Therefore you're going to push one more value into your target vector than you have space for, and the runtime exception ensues.
The fix is simple. Stop trying to be smart about short circuiting in three different conditions when in reality you only need one and a master-else. The computational requirements are the same no matter what, and in the end all you need is this:
class Solution
{
public:
std::vector<int> sortedSquares(std::vector<int> const &nums)
{
std::vector<int> final(nums.size());
int start = 0;
int end = nums.size()-1;
int finalIdx = final.size()-1;
while(start<=end)
{
if (abs(nums[end]) < abs(nums[start]))
{
final[finalIdx--] = nums[start]*nums[start];
++start;
}
else
{
final[finalIdx--] = nums[end]*nums[end];
--end;
}
}
return final;
}
};
If you really want all three conditions in your code, it is possible, but not warranted, and the special case circumstances don't justify doing it. Regardless, see below:
class Solution
{
public:
std::vector<int> sortedSquares(std::vector<int> const &nums)
{
std::vector<int> final(nums.size());
int start = 0;
int end = nums.size() - 1;
int finalIdx = final.size() - 1;
while (start <= end)
{
if (abs(nums[start]) < abs(nums[end]))
{
final[finalIdx--] = nums[end] * nums[end];
end--;
}
else if (abs(nums[end]) < abs(nums[start]))
{
final[finalIdx--] = nums[start] * nums[start];
start++;
}
else // !(a<b || b<0) implies (a == b)
{
int sqr = final[finalIdx--] = nums[end] * nums[end];
if (end != start)
{
final[finalIdx--] = sqr;
}
--end;
++start;
}
}
return final;
}
};

Member variable resetting back to 0

When running through the test the count variable from the class stack1 gets reset back to 0 when using its pop function. Strangely however, during the push loop, the count increases as intended but when pop occurs, the count gets reset back to 0 and subtracts into the negatives from there. Is there something I'm forgetting?
#include <iostream>
using namespace std;
class TheStack
{
public:
TheStack();
void push(int);
int pop();
bool isEmpty();
private:
const int MaxSize = 10;
int arr[10];
int count;
};
TheStack::TheStack()
{
count = 0;
}
void TheStack::push(int userInput)
{
if (count >= MaxSize)
{
cout << "Stack is full." << endl;
}
else
{
arr[count] = userInput;
count+=1;
}
}
int TheStack::pop()
{
if (isEmpty())
{
cout << "Stack is empty." << endl;
}
else
{
int temp = arr[count];
arr[count] = NULL;
count-=1;
return temp;
}
}
bool TheStack::isEmpty()
{
if (count == 0)
{
return true;
}
else
{
return false;
}
}
int main()
{
TheStack stack1;
if (stack1.isEmpty())
{
cout << "isEmpty() works" << endl;
}
stack1.pop();
for (int i = 0; i < 10; i++)
{
stack1.push(i);
}
stack1.push(0);
stack1.pop();
stack1.pop();
stack1.pop();
stack1.pop();
system("pause");
}
When you do push you first save the data into the array and then increment count. This means that in order to properly do pop you need to work in reverse: first decrement count and only then read data from the array.
But in the code you are doing it backwards. When the stack is full, count is at max value (10 in your case), and your arr[count] = NULL; writes beyond the array boundary. This causes undefined behavior and, in particular, destroys your count value. (This is why it suddenly becomes 0.)
Also:
arr[count] = NULL; makes no sense. NULL is supposed to be used in pointer contexts, not in integer contexts. This is not even guaranteed to compile.
What is the point of that anyway? Initially your array contains garbage above the current top of the stack. Why do you suddenly care to clean it up after doing pop?
Not all control paths of pop() return value. This is undefined behavior in itself.
const int MaxSize = 10; in the class definition is a C++11 feature. Since you are already using C++11, you can do the same for count. Just do int count = 0; right inside the class definition and you will not have to write the constructor explicitly.
Although in your implementation MaxSize would make more sense as a static const class member. In that case you'll also be able to declare your array as int arr[MaxSize];.
You must first decrease count and then access arr[count] in int TheStack::pop(). Now you get access above the last pushed element, event out of bound of array if the stack is full.

Need to reference and update value from nested class C++

Bear with me, I'm new to C++. I'm trying to update a value which is stored in a vector, but I'm getting this error:
non-const lvalue reference to type 'Node'
I'm using a simple wrapper around std::vector so I can share methods like contains and others (similar to how the ArrayList is in Java).
#include <vector>
using namespace std;
template <class T> class NewFrames {
public:
// truncated ...
bool contains(T data) {
for(int i = 0; i < this->vec->size(); i++) {
if(this->vec->at(i) == data) {
return true;
}
}
return false;
}
int indexOf(T data) {
for(int i = 0; i < this->vec->size(); i++) {
if(this->vec->at(i) == data) {
return i;
}
}
return -1;
}
T get(int index) {
if(index > this->vec->size()) {
throw std::out_of_range("Cannot get index that exceeds the capacity");
}
return this->vec->at(index);
}
private:
vector<T> *vec;
};
#endif // A2_NEWFRAMES_H
The class which utilizes this wrapper is defined as follows:
#include "Page.h"
#include "NewFrames.h"
class Algo {
private:
typedef struct Node {
unsigned reference:1;
int data;
unsigned long _time;
Node() { }
Node(int data) {
this->data = data;
this->reference = 0;
this->_time = (unsigned long) time(NULL);
}
} Node;
unsigned _faults;
Page page;
NewFrames<Node> *frames;
};
I'm at a point where I need to reference one of the Node objects inside of the vector, but I need to be able to change reference to a different value. From what I've found on SO, I need to do this:
const Node &n = this->frames->get(this->frames->indexOf(data));
I've tried just using:
Node n = this->frames->get(this->frames->indexOf(data));
n.reference = 1;
and then viewing the data in the debugger, but the value is not updated when I check later on. Consider this:
const int data = this->page.pages[i];
const bool contains = this->frames->contains(Node(data));
Node node = this->frames->get(index);
for(unsigned i = 0; i < this->page.pages.size(); i++) {
if(node == NULL && !contains) {
// add node
} else if(contains) {
Node n = this->frames->get(this->frames->indexOf(data));
if(n.reference == 0) {
n.reference = 1;
} else {
n.reference = 0;
}
} else {
// do other stuff
}
}
With subsequent passes of the loop, the node with that particular data value is somehow different.
But if I attempt to change n.reference, I'll get an error because const is preventing the object from changing. Is there a way I can get this node so I can change it? I'm coming from the friendly Java world where something like this would work, but I want to know/understand why this doesn't work in C++.
Node n = this->frames->get(this->frames->indexOf(data));
n.reference = 1;
This copies the Node from frames and stores the copy as the object n. Modifying the copy does not change the original node.
The simplest "fix" is to use a reference. That means changing the return type of get from T to T&, and changing the previous two lines to
Node& n = this->frames->get(this->frames->indexOf(data));
n.reference = 1;
That should get the code to work. But there is so much indirection in the code that there are likely to be other problems that haven't shown up yet. As #nwp said in a comment, using vector<T> instead of vector<T>* will save you many headaches.
And while I'm giving style advice, get rid of those this->s; they're just noise. And simplify the belt-and-suspenders validity checks: when you loop from 0 to vec.size() you don't need to check that the index is okay when you access the element; change vec.at(i) to vec[i]. And in get, note that vec.at(index) will throw an exception if index is out of bounds, so you can either skip the initial range check or keep the check (after fixing it so that it checks the actual range) and, again, use vec[index] instead of vec.at(index).

C++ Memory Leak With STL Vector

I am building a templated Max Heap class in C++ for a datastructures class. The implementation demonstrates a Max Heap with a vector under the hood. There is an online submission associated with the assignment and when I submit mine, all the tests (push, pop, top, and size) pass and work (for the online unknown unit tests as well as all the ones I wrote) and I have no memory leaks with any of my tests, however I am failing the memory leak section with the online submission, indicating to me that my Bubble Up (Reheap Up) or Bubble Down (Reheap Down) algorithms are doing something funny with vector indices.
I noticed that I used the bracket operator a lot to mess with the vector, so I went through and changed all the brackets to .at() so I could see any suppressed out of bounds errors. Flying colors again, except for the memory leaks allegedly. I then figured well maybe one of the unit tests is adding sooo many values the vector fails to clear them all for some unknown reason...wasn't the case because I added so many values to a vector in my max heap class in my unit tests it took 90 seconds to finish and after all 52K allocations were made 52K deallocations were made as well and valgrind reported no errors.
Below is some of the main code for the class, if anyone could decide where some code is written that in some situation may warrant a memory leak that would be great!
template <class T>
class MaxHeap {
public:
MaxHeap(){
// TODO: Fill me in
}
~MaxHeap() {
data.clear();
}
void push(T value){
data.push_back(value);
bubbleUp(data.size()-1, value);
}
void pop(){
if(!size()) {
return;
}
T val = data.at(size()-1);
data.pop_back();
if(!size()) {
return;
}
data.at(0) = val;
bubbleDown(0, val);
}
T top(){
if(!data.size()) throw logic_error("Empty Heap");
return data.at(0);
}
unsigned int size(){
return data.size();
}
void print_vec() {
for (int i = 0; i < size(); ++i) {
cout << data.at(i) << " ";
}
cout << endl;
}
vector<T> getVec() {
return data;
}
private:
vector<T> data;
void bubbleUp(int idx, T value) {
int position = idx;
int parent_idx = parent(position);
while (data.at(parent_idx) < value) {
data.at(position) = data.at(parent_idx);
data.at(parent_idx) = value;
position = parent_idx;
parent_idx = parent(position);
}
}
void bubbleDown(int idx, T value) {
int left_child_idx = left_child(idx);
int right_child_idx = right_child(idx);
int max_child_idx;
if(left_child_idx <= size()-1) { // left child (consequently right child) in bounds of vector
if(left_child_idx == size()-1) { // no right child, left is maxchild
max_child_idx = left_child_idx;
} else {
max_child_idx = (data.at(left_child_idx) <= data.at(right_child_idx)) ? right_child_idx : left_child_idx;
}
if(data.at(idx) < data.at(max_child_idx)) {
data.at(idx) = data.at(max_child_idx);
data.at(max_child_idx) = value;
bubbleDown(max_child_idx, value);
}
}
}
int left_child(int idx) {return (idx*2+1);}
int right_child(int idx) {return (idx*2+2);}
int parent(int idx) {return ((idx-1)/2);}
};
Warning: this is only a theory, since it is improbable that the source of leak is in the code shown here.
If T is a malformed type, that does not release it's memory when using the assignment operator, then this might be the part that trigger this bad behvior:
T swap; // initialized to something. perhaps using new
while (data.at(parent_idx) < value) {
swap = data.at(parent_idx); //assume no delete in T.operator=()
data.at(parent_idx) = value;
data.at(position) = swap;
position = parent_idx;
parent_idx = parent(position);
}
This is not a problem in this code. However, you might still be able to patch it here. Why is T defined outside the loop?
while (data.at(parent_idx) < value) {
T swap = data.at(parent_idx); // no assignment here
data.at(parent_idx) = value;
data.at(position) = swap;
position = parent_idx;
parent_idx = parent(position);
}
===
Unrelated but better - don't use the unnecessary intermediate variable, and mix in move semantics:
while (data.at(parent_idx) < value) {
data.at(position) = std::move(data.at(parent_idx));
data.at(parent_idx) = value;
position = parent_idx;
parent_idx = parent(position);
}

C++ Lock free producer/consumer queue

I was looking at the sample code for a lock-free queue at:
http://drdobbs.com/high-performance-computing/210604448?pgno=2
(Also reference in many SO questions such as Is there a production ready lock-free queue or hash implementation in C++)
This looks like it should work for a single producer/consumer, although there are a number of typos in the code. I've updated the code to read as shown below, but it's crashing on me. Anybody have suggestions why?
In particular, should divider and last be declared as something like:
atomic<Node *> divider, last; // shared
I don't have a compiler supporting C++0x on this machine, so perhaps that's all I need...
// Implementation from http://drdobbs.com/high-performance-computing/210604448
// Note that the code in that article (10/26/11) is broken.
// The attempted fixed version is below.
template <typename T>
class LockFreeQueue {
private:
struct Node {
Node( T val ) : value(val), next(0) { }
T value;
Node* next;
};
Node *first, // for producer only
*divider, *last; // shared
public:
LockFreeQueue()
{
first = divider = last = new Node(T()); // add dummy separator
}
~LockFreeQueue()
{
while( first != 0 ) // release the list
{
Node* tmp = first;
first = tmp->next;
delete tmp;
}
}
void Produce( const T& t )
{
last->next = new Node(t); // add the new item
last = last->next; // publish it
while (first != divider) // trim unused nodes
{
Node* tmp = first;
first = first->next;
delete tmp;
}
}
bool Consume( T& result )
{
if (divider != last) // if queue is nonempty
{
result = divider->next->value; // C: copy it back
divider = divider->next; // D: publish that we took it
return true; // and report success
}
return false; // else report empty
}
};
I wrote the following code to test this. Main (not shown) just calls TestQ().
#include "LockFreeQueue.h"
const int numThreads = 1;
std::vector<LockFreeQueue<int> > q(numThreads);
void *Solver(void *whichID)
{
int id = (long)whichID;
printf("Thread %d initialized\n", id);
int result = 0;
do {
if (q[id].Consume(result))
{
int y = 0;
for (int x = 0; x < result; x++)
{ y++; }
y = 0;
}
} while (result != -1);
return 0;
}
void TestQ()
{
std::vector<pthread_t> threads;
for (int x = 0; x < numThreads; x++)
{
pthread_t thread;
pthread_create(&thread, NULL, Solver, (void *)x);
threads.push_back(thread);
}
for (int y = 0; y < 1000000; y++)
{
for (unsigned int x = 0; x < threads.size(); x++)
{
q[x].Produce(y);
}
}
for (unsigned int x = 0; x < threads.size(); x++)
{
q[x].Produce(-1);
}
for (unsigned int x = 0; x < threads.size(); x++)
pthread_join(threads[x], 0);
}
Update: It ends up that the crash is being caused by the queue declaration:
std::vector<LockFreeQueue<int> > q(numThreads);
When I change this to be a simple array, it runs fine. (I implemented a version with locks and it was crashing too.) I see that the destructor is being called immediate after the constructor, resulting in doubly-freed memory. But, does anyone know WHY the destructor would be called immediately with a std::vector?
You'll need to make several of the pointers std::atomic, as you note, and you'll need to use compare_exchange_weak in a loop to update them atomically. Otherwise, multiple consumers might consume the same node and multiple producers might corrupt the list.
It's critically important that these writes (just one example from your code) occur in order:
last->next = new Node(t); // add the new item
last = last->next; // publish it
That's not guaranteed by C++ -- the optimizer can rearrange things however it likes, as long as the current thread always acts as-if the program ran exactly the way you wrote it. And then the CPU cache can come along and reorder things further.
You need memory fences. Making the pointers use the atomic type should have that effect.
This could be totally off the mark, but I can't help but wonder whether you're having some sort of static initialization related issue... For laughs, try declaring q as a pointer to a vector of lock-free queues and allocating it on the heap in main().