Is it possible to not stub mongo-c-driver whiling using GTests? - googletest

I wrote some basic funtions that uses mongodb-c-driver. and I tried to write some tests with GTest.
in each test case I init and destroy mongoc with ( mongoc_init() and mongoc_cleanup()).
When I run one test case, everything goes fine but when I run two tests or more Iā€™m getting some invalid reads :
==3726== Invalid read of size 1
==3726== at 0x4842B60: memmove (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==3726== by 0x4A81C82: memcpy (string_fortified.h:34)
==3726== by 0x4A81C82: bson_string_append (bson-string.c:143)
==3726== by 0x49F9C72: _append_platform_field (mongoc-handshake.c:495)
==3726== by 0x49F9C72: _mongoc_handshake_build_doc_with_application (mongoc-handshake.c:560)
==3726== by 0x4A1A11A: _build_ismaster_with_handshake (mongoc-topology-scanner.c:232)
==3726== by 0x4A1A11A: _mongoc_topology_scanner_get_ismaster (mongoc-topology-scanner.c:263)
==3726== by 0x4A1A2BF: _begin_ismaster_cmd (mongoc-topology-scanner.c:291)
==3726== by 0x4A1AC78: mongoc_topology_scanner_node_setup_tcp (mongoc-topology-scanner.c:836)
==3726== by 0x4A1AF57: mongoc_topology_scanner_node_setup (mongoc-topology-scanner.c:960)
==3726== by 0x4A1B18E: mongoc_topology_scanner_start (mongoc-topology-scanner.c:1083)
==3726== by 0x4A15126: mongoc_topology_scan_once (mongoc-topology.c:765)
==3726== by 0x4A15126: _mongoc_topology_do_blocking_scan (mongoc-topology.c:797)
==3726== by 0x4A157F4: mongoc_topology_select_server_id (mongoc-topology.c:1030)
==3726== by 0x49DCD90: _mongoc_cluster_select_server_id (mongoc-cluster.c:2704)
==3726== by 0x49E169F: _mongoc_cluster_stream_for_optype (mongoc-cluster.c:2750)
Do I need to stub all mongoc-driver fuctions to do some unit/integration tests to my functions ?
Mongo Init Function :
mongoc_init();
uri = mongoc_uri_new_with_error("mongodb://127.0.0.1:27017", &error);
return mongoc_client_new_from_uri(uri);
Mongo Cleanup function
mongoc_client_destroy(mongoClient);
mongoc_cleanup();
Functions to test
return mongoc_client_get_collection(mongoClient, database, collectionName);

To answer my question, the probleme was calling mongoc_cleanup() at the end of each test case.
Once mongoc_cleanup() is called, it is invalid to call mongoc_init() again. http://mongoc.org/libmongoc/current/init-cleanup.html#synopsis
mongoc_init() should be called at the start of the test sheet, and mongoc_cleanup() at the end test sheet.

Related

Converting Polyhedron object to Nef_polyhedron object

I want to use the CGAL library to calculate the Boolean difference between two polyhedrons. Both polyhedrons are in the form of .off files. My idea is to first read these two files into two Polyhedron type variables p1 and p2, then initialize Nef_polyhedron type variables nef1 and nef2 with p1 and p2 respectively, and finally find the Boolean difference between nef1 and nef2.
My code is as follows:
#include<fstream>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Nef_polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/draw_nef_3.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
typedef CGAL::Nef_polyhedron_3<Kernel> Nef_polyhedron;
int main() {
Polyhedron p1, p2, res;
std::ifstream fin1("blank.off");
std::ifstream fin2("svreal.off");
fin1 >> p1;
fin2 >> p2;
fin1.close();
fin2.close();
Nef_polyhedron nef1(p1);
Nef_polyhedron nef2(p2);
Nef_polyhedron nef = nef1 - nef2;
nef.convert_to_polyhedron(res);
std::ofstream fout("res2.off");
fout << res;
return 0;
}
However, when the code executes to "Nef_polyhedron nef2(p2);", the following exception is thrown: boost::wrapexcept<boost::bad_any_cast>. This may mean an error in constructing Nef_polyhedron nef2 with Polyhedron object p2. But I really cant't figure out why such a mistake happened. The Polyhedron p1 and p2 are both valid. And it looks nice when I open file svreal.off which is the .off file form of Polyhedron object p2.
I would appreciate it if you could help me solve this problem.
Your input "svreal.off" contains some degenerate faces. You can remove them using CGAL::Polygon_mesh_processing::remove_degenerate_faces() from CGAL/Polygon_mesh_processing/repair_degeneracies.h or use the function OFF_to_nef_3() directly from the istream.

i have error its meaning :Error C2440 'return': cannot convert from 'void (__cdecl &)(yield_context)' to 'void (&)(yield_context)'

I am making program in which i try to make boost packaged_task then take its future in vector and launch it with asio post.
when i try to make packaged_task ,it gives me this error:
Error C2440 'return': cannot convert from 'void (__cdecl &)(boost::asio::yield_context)' to 'void (&)(boost::asio::yield_context)'
this is the relevant code:
typedef boost::packaged_task<std::string(boost::asio::yield_context)> task_t;
boost::shared_ptr<task_t> task = boost::make_shared<task_t>(boost::bind(&HTTPRequest::Execute, mClientRequestsVariables[m_HttpClient_request_name]->shared_from_this() , boost::placeholders::_1, m_HttpClient_request_name, Get_mHTTPClient_Responses_Map_shared_pointer()));
boost::future<std::string> fut = (task->get_future());
mPendingData.push_back(std::move(fut)); // C++11 possible: (std::move(fut) when fut is a unique_future);
mIos.post(boost::bind(&task_t::operator(), task));
and this is the definition of HTTPRequst::Execute :
HTTPRequest::HTTPRequest(boost::asio::io_service& ios, unsigned int id, std::string URL, const HTTPServiceResolve& resolve_addr, boost::shared_ptr<LoggingClass_2> mHTTPRequest_LoggingInstance_shared_pointer)
and the class HTTPRequest is derived from enable_shared_from_this .
the error is in bind.hpp so i find in vs output windows clues to the included part of code.
why is this error happening?and what is the solution??

How to prevent cppcheck (v1.72) warnings for %"PRIi64" (%lld) with a int64_t variable (cpcheck thinks it is a signed int) of stdint.h

For a sequence of
typedef int64_t I64;
I64 i=5;
printf("%"PRIi64",i);
cppcheck gives the warning below:
warning: %lld in format string (no. 1) requires 'long long' but the argument type is 'signed int'.
The makro PRIi64 is resolved by lld, this is correct, but the 64 bit integer type is not accepted as long long int.
I hope there is a way to resolve this, because we get a lot of such warnings in our project and don't see the real bugs anymore.
The latest version of Cppcheck does not shown a warning about following example code:
void f(void)
{
typedef int64_t I64;
I64 i=5;
printf("%"PRIi64",i);
}

Objective-C Travis issue for boolean values in XCTAssertEqual: ("NO") is not equal to ("0")

here's my test code which succeeds locally:
- (void)setUp {
restroom = [[Restroom alloc] initWithName:#"Target" andIsAccessible:FALSE andIsUnisex:TRUE];
}
- (void)tearDown {
restroom = nil;
}
- (void)testThatARestroomCanBeCreated
{
XCTAssertNotNil(restroom, #"Should be able to create a Restroom instance.");
}
- (void)testThatRestroomHasAName
{
XCTAssertEqualObjects(restroom.name, #"Target", #"Restroom should have the name given when initialized.");
}
- (void)testThatRestroomHasAFlagForAccessibility
{
XCTAssertEqual(restroom.isAccessible, FALSE, #"Restroom should have the accessibility flag given when initialized.");
}
- (void)testThatRestroomHasAFlagForUnisex
{
XCTAssertEqual(restroom.isUnisex, TRUE, #"Restroom should have the unisex flag given when initialized.");
}
however, when Travis processes it, i get error for only the tests relating to boolean values:
āœ— -[RRiOSAppTests testThatRestroomHasAFlagForAccessibility] (0 ms) (0)
-[RRiOSAppTests testThatRestroomHasAFlagForUnisex]
/Users/travis/build/.../RRiOSApp/RRiOSAppTests/RRiOSAppTests.m:91: ((restroom.isUnisex) equal to (1)) failed: ("YES") is not equal to ("1") - Restroom should have the unisex flag given when initialized.:
...
āœ— -[RRiOSAppTests testThatRestroomHasAFlagForUnisex] (0 ms) (1)
āœ“ -[RRiOSAppTests testThatRestroomHasAName] (0 ms)
/Users/travis/build/.../RRiOSApp/RRiOSAppTests/RRiOSAppTests.m:86: ((restroom.isAccessible) equal to (0)) failed: ("NO") is not equal to ("0") - Restroom should have the accessibility flag given when initialized.
I tried replacing TRUE and FALSE with 1 and 0 - but the same error came about.
Here's my .travis.yml file:
language: objective-c
install:
- brew remove --force xctool
- brew install xctool --HEAD
script:
- xctool test -project RRiOSApp/RRiOSApp.xcodeproj -scheme RRiOSApp -sdk iphonesimulator7.0
And, I'm using Xcode 6
BOOL is either YES or NO, not TRUE or FALSE. TRUE is not the same type as YES, which is why the test fails. There is also true, which is a completely different type.
TRUE is defined in Core Foundation as:
#define TRUE 1
YES is defined in the ObjC runtime as:
#define YES (BOOL)1
true is defined differently depending on whether you're compiling as C99 or C++. You can look it up in stdbool.h if you're curious.
But in any case, you shouldn't test equality to BOOL (ever, not just in tests). You should test truth. The tests you want here is XCTAssertTrue and XCTAssertFalse. There are many "true" values that are not equal to YES or TRUE, which is why testing equality on BOOL is error-prone.
I just randomly found the answer to the question myself while reading a textbook on TDD. I had to cast the booleans in XCTAssertEqual (e.g. XCTAssertEqual(restroom.isAccessible, (BOOL)FALSE, #"Restroom should have the accessibility flag given when initialized.");). The book is 'Test-Driven iOS Development' by Graham Lee - highly recommended!

Valgrind Error Involving Uninitialised String: False Flag?

When running valgrind to check for errors in a program written in C89/90, it comes up with a Uninitialised value was created by a heap allocation error for a strToUpper() function I wrote, despite the string being initialised.
I'm using this function to compare strings ignoring case. Unfortunately, C89 doesn't seem to include the strcasecmp() function in <string.h>, so I've written my own, which calls the strToUpper() and strcmp() functions.
CODE
char* strToUpper(char* inStr)
{
int i;
char *upperStr;
size_t strLen = strlen(inStr);
upperStr = (char*)malloc(sizeof(char) * (strLen + 1));
/* Does this for loop not initialise upperStr? */
for (i = 0; i < strLen; i++)
upperStr[i] = toupper(inStr[i]);
return upperStr;
}
VALGRIND ERROR
==27== Conditional jump or move depends on uninitialised value(s)
==27== at 0x4C31FAA: strcmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==27== by 0x406649: strcasecmp (stringPlus.c:178)
==27== ...
==27== by 0x400FEB: main (main.c:58)
==27== Uninitialised value was created by a heap allocation
==27== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==27== by 0x4062E4: strToUpper (stringPlus.c:58)
==27== by 0x406622: strcasecmp (stringPlus.c:175)
==27== ...
==27== by 0x400FEB: main (main.c:58)
Any ideas?
You are not terminating your copied string.
Add something like
upperStr[i] = '\0';
after your for loop.
Instead of using malloc(blah) I always use calloc(1,blah). The latter sets all allocated memory to zero.