I am trying to find the set difference of two vectors, so i do something like this:
std::vector<sha1_hash> first_vec, second_vec, difference_vec;
// populate first_vec and second_vec ...
std::sort(first_vec.begin(),first_vec.end());
std::sort(second_vec.begin(),second_vec.end());
std::set_difference(first_vec.begin(),first_vec.end(),
second_vec.begin(),second_vec.end(),
difference_vec.begin());
When i run this in debug, i get the following run-time assertion failure (in 'vector'):
_SCL_SECURE_VALIDATE_RANGE(_Myptr < ((_Myvec *)(this->_Getmycont()))->_Mylast);
I am using VS 2008.
Any ideas on what can trigger this?
Like most c++ algorithms, set_difference does not create new entries in the output vector where none existed before. You ned to create space in the output to hold the results.
Edit: Or use an an insert iterator (following untested):
back_insert_iterator< std::vector<sha1_hash> > bi( difference_vec );
std::set_difference(first_vec.begin(),first_vec.end(),
second_vec.begin(),second_vec.end(),
bi);
Related
i use the newer version of the v8 library. When i run demo from this page(process.cc),
my program crash when GetInternalField is called in UnwrapMap function. Does anyone have a similar problem, or does they know how to solve it?
Here example:
map<string, string>* JsHttpRequestProcessor::UnwrapMap(Local<Object> obj) {
Local<External> field = Local<External>::Cast(obj->GetInternalField(0)); // here segmentation fault
void* ptr = field->Value();
return static_cast<map<string, string>*>(ptr);
}
EDIT:
I solved my problem. I had to insert macro V8_COMPRESS_POINTERS = 1. Thank you
The provided sample works fine here; did you modify it in any way? How exactly are you running it?
You can only call obj->GetInternalField(0) if you know that obj is an object with internal fields. You can use obj->InternalFieldCount() to check. Objects only have internal fields if you, the embedder, gave them any. If you are trying to convert a regular JavaScript object to a C++ map, you'll have to iterate over its properties, not its internal fields.
Please,I was searching it here, but couldn't do it correctly.
So I have a function which returns to me the vector of sortig numbers. Then I tried to create a using test for this very vector.
Here is what I have right now:
#include "stdafx.h"
#include "CppUnitTest.h"
#include "Finder.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace FinderUnitTest
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
Finder f;
std::vector<int> v1 = f.find_words();
//find_words(); is working okay without tests
for (int i=0;i<1;i++)
Assert::AreEqual(57,v1[i]);
}
};
}
It really doesn't matter, how many time for goes. I'd like to get the unit test without mistake, I have one right now, it is:
Message: Invalid parameter detected in function std::vector >::operator [], c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\vector line 1795. Expression: "vector subscript out of range"
How I get that when I build my two projects, my fuction, which returns the vector of sorting numbers,doesn't have any data because when I run the test,there is empty. Am I right?
I just want to compare two first numbers of my vector with 57.
If you are expecting find_words to return a vector like [57, 57, ...] then this test should fail. However, it should not error, but rather it should Assert. You want to fix your checks so they detect the problem as an assert violation.
Finder f;
std::vector<int> v1 = f.find_words();
Assert::IsTrue(v1.size() >= 2); // there are at least two entries
Assert::AreEqual(57,v1[0]); // the first is 57
Assert::AreEqual(57,v1[1]); // the second is 57
I don't see where you gave finder anything to search, but if you say it should find 57's, you're the boss. Just be sure to check that. Once the unittest gives this assert, the unittest has done its job, and you can go back and see if you gave Finder the right inputs, or if there is a bug inside Finder.
X21's comment was good programming practice about how to detect and avoid the crash. It fixes the crash by not checking the values at all, since to do so would be an error. It was not directed at writing a unit test. The unit test must detect and assert when the output is not what you expect. Crashing would be better than not checking at all, inside a test.
I am writing an application for the iPad using Xcode 5.0
I have tried to implement a category that will allow shuffling of an NSMutableArray. I'm using Test Driven Development, and I wrote a test like the following using Objective-C++:
size_t randomInteger(size_t);
#implementation ShuffleArrayTests
- (void)testsShufflesAnArray
{
NSMutableArray* array = [#[#"one", #"two", #"three", #"four", #"five",
#"six", #"seven", #"eight", #"nine", #"ten"] mutableCopy];
std::vector<__unsafe_unretained id> values(array.count);
[array getObjects:&values[0] range:NSMakeRange(0, array.count)];
::srandom(0);
std::random_shuffle(values.begin(), values.end(), randomInteger);
NSMutableArray* expectedValues =
[NSMutableArray arrayWithObjects:&values[0] count:values.size()];
::srandom(0);
[array shuffle];
XCTAssertEqualObjects(expectedValues, array);
}
The implementation for the shuffle category method is written as follows, also in Objective-C++:
- (void)shuffle
{
std::vector<__unsafe_unretained id> buffer(self.count);
[self getObjects:&buffer[0] range:NSMakeRange(0, self.count)];
std::random_shuffle(buffer.begin(), buffer.end(), randomInteger);
[self setArray:[NSMutableArray arrayWithObjects:&(buffer[0])
count:buffer.size()]];
}
and randomInteger is basically implemented like this:
size_t randomInteger(size_t limit)
{
return ::random() % limit;
}
One would think that be cause the same seed value is set before performing each random shuffle that the test would pass, and the expected array would match the actual array.
The test is failing on the iOS simulator, however, and it had baffled me for many days as to why. I finally figured out that the test is calling a different version of std::random_shuffle than what is used in the category implementation. I'm not sure why this is happening.
What can be done to make the test and implementation code use the same std::random_shuffle algorithm?
It seems to me that you've got a logic problem in your test.
First, you create array and put a bunch of values into it. So far so good.
Second, you copy them into values and shuffle them. Still fine.
Third, you copy the elements from values into expectedValues, and shuffle them. OK
Finally, you expect that the contents of array and expectedValues are the same. Why do you think that this would be the case? expectedValues is basically array, shuffled twice.
If I understand what you're trying to test, I think you want to copy from array into expectedValues and shuffle that, then compare expectedValues and values.
expectedValues is not shuffled twice. The contents of array are copied and shuffled. The results of the copied shuffle are store in expected results
The random seed is then reset so it will produce the same shuffled results and then array, which is completely independent of of expected results is shuffled, and the two arrays are compared.
I believe I found the problem. This project was originally created using Xcode 4.6 running on Mac OS X 10.7. I upgraded to Xcode 5 running on OS X 10.9, and it looks like some settings that should have been changed did not get changed. I created a new application from scratch, and put the test in that application and it passed fine. I was then able to look at the differences between the project.
For the application itself I found that in the build settings under Apple LLVM 5.0 - Language - C++, the C++ Standard Library was set to Compiler Default; I changed it to libc++ (LLVM C++ standard library with C++11 support) and that appears to fix the issue.
I am trying to insert data into a 2d map but the following code gives me a Access violation error.
void StateManager::AddState(string stateName, map<string, IBaseComponent*> stateComponents)
{
// Add the state to the states map
m_States.insert(pair<string, map<string, IBaseComponent*>>(stateName,stateComponents));
}
Can someone point out what I am doing wrong and how I can fix it ?
Edit: I tried inserting some test values into the map and that works fine. Must be a problem with the data I am inserting.
Turns out I was using the function before initializing the object that contains it. So m_States wasn't actually created yet. Hence the access violation error.
I'm not getting any error messages, simply my vector is not populating. Looking at the vector in the watch list, nothing is being copied. Why is this?
I've tried two ways.
The first
std::vector<Point3D*> hitpoints;
local_hit_point = sr.local_hit_point; //local_hit_point class Point3D
hitpoints.push_back(local_hit_point);
The second way I tried to use pointers
std::vector<Point3D*> hitpoints;
Point3D* hittingpoint_ptr = new Point3D;
local_hit_point = sr.local_hit_point;
hittingpoint_ptr = &local_hit_point;
hitpoints.push_back(hittingpoint_ptr);
I got vectors in other places in my code which work. Am I really just being daft, but I can't seem to figure out why its not working.
My best guess is that you have an issue with you debugger..
First Suggestion;
Clear everything in your watchlist because they can change the behaviour of the execution
check it again..
Second suggestion;
Create a new project and write a simple code like the one above and see whether your vector is populating..If this simple project works, you should provide us more code and details..
simply my vector is not populating.
It is populating. However
Looking at the vector in the watch list ...
I used hitpoint.size()
Results of function/method calls (size() is a method) are not automatically updated in visual studio watch list (because you haven't told what os/compiler you use I had to assume it is visual studio). I.e. if you enter function call into watch list, it'll calculate results, but will not call function again until you manually refresh it. Instead of function call, add vector itself into watch list.