Error make:*** [unit_test] in googleTest - c++

I have a project named Movies and I run some tests from googletest on it. I included the whole project and started running tests:
#include "gtest/gtest.h"
#include "Counter.h"
#include <iostream>
using namespace std;
TEST(CodeTest, failTest)
{
Counter c;
EXPECT_EQ( 7, c.getValue() );
}
TEST(CodeTest, plusEqualsConstructor)
{
Counter f,g;
f=g+1;
EXPECT_FALSE(f==g);
}
and my main program is:
#include "gtest/gtest.h"
using namespace std;
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
return 0;
};
at first it worked as expected and then it gave me the following weird error:
make:*** [unit_test] Error 1
does anyone know what the problem can be? Thank you !!

Related

How to convert a command line argument to an int?

Im trying to convert the command line argument(*argv[]) to an integer using the atoi function
int main(int argc, char *argv[]) {
This is my attempt
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main(int argc, char *argv[]) {
int x = 0;
for ( x=0; x < argc; x++ )
{
int x = atoi(argv[1]);
cout << x;
}
return 0;
}
However this returns 0 and im unsure why. Thankyou
It's hard to say having the arguments you pass to your program, but there are few problems here.
Your loop goes from 0 to argc, but your inside your loop you always use argv[1], if you didn't pass any arguments you're going out of bounds, because argv[0] is always the path to your executable.
atoi is a function from C, and when it fails to parse it's argument as an int, it returns 0, replace it with std::stoi, and you will get and execption if the conversion failed. You can catch this exception with try/catch, and then check the string that you tried to convert to int.
Well, this
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main(int argc, char* argv[]) {
int x = 0;
for (x = 0; x < argc; x++)
{
cout << argv[x];
}
return 0;
}
just prints the path to the .exe, the path is a string, it has no numbers. And as I understood from my "research" about command line arguments, you need to use your program through a command line, a terminal, to initialise the argv argument.
Link : https://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm
Also, as I understood at least, the argv[0] is always the path of the .exe
I hope I will be of some help, if I am mistaken at something, pls tell me where and I will correct my self by editing the answer

Unit tests. How to run tests in the main()

I am trying to run a test in the main function, but the error "you cannot overload the main () function"is displayed.
#define CATCH_CONFIG_RUNNER // -- main() создавать нужно --
#include "catch.hpp"
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "Russian");
int result = Catch::Session().run(argc, argv);
system("pause");
return result;
}
You should use Catch in some other way. Something like that worked for me:
#include <iostream> // some standard includes, whatever you need
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("My first test") {
// --- test code here ---
}
TEST_CASE("My second test") {
// --- test code here ---
}
Try the framework's tutorial to learn more =)

Unable to conversion in Expected<unique_ptr<Module>> to Module

I am new to llvm , writing the program "main.cpp" of https://github.com/davidar/lljvm/blob/master/backend/main.cpp.
I stuck at the error while executing the command : "pm.run(m)"
error: no matching function for call to ‘llvm::legacy::PassManager::run(llvm::Expected<std::unique_ptr<llvm::Module> >&)
Here is my source code:
#include "backened.h"
#include <iostream>
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/PassManager.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/DataLayout.h"
#include <llvm/Transforms/Scalar.h>
#include "llvm/IR/LegacyPassManager.h"
using namespace llvm;
using namespace std;
static cl::opt<string> input(cl::Positional, cl::desc("Bitcode File.."),cl::Required);
static cl::opt<string> classname("classname",cl::desc("Binary name of the generated class..."));
int main(int argc, char** argv)
{
cl::ParseCommandLineOptions(argc, argv, "Hi..");
LLVMContext context;
ErrorOr<unique_ptr<MemoryBuffer>> mb = MemoryBuffer::getFile(input);
if(error_code ec = mb.getError()) {
errs() << ec.message();
return 1;
}
Expected<unique_ptr<Module>> m = parseBitcodeFile(mb->get()->getMemBufferRef(),context);
if(error_code ec= errorToErrorCode(m.takeError()) )
{
errs() <<"Unable to read bitcode file.." <<ec.message() ;
}
PassManager<Module> pm;
pm.add(createVerifierPass());
pm.add(createGCLoweringPass());
pm.add(createLowerSwitchPass());
pm.add(createCFGSimplificationPass());
pm.add(new JVMWriter(fouts(), classname, debugLevel));
pm.add(createGCInfoDeleter());
pm.run(*m);
return 0;
}
Please help me.

Unable to pass the arguments to R function from cpp code

My cpp source code:
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
cout<<"Before"<<endl;
system("Rscript /Desktop/R_TENS/rtest.R");
return 0;
}
rtest.R
rtest = function(input ,output){
a <- input
b<- output
outpath<-a*b
print(a*b)
return(outpath)
}
Compile: g++ name.cpp
This creates a executable file. But how the parameters (eg:2,3) is could be passed?
./a.out 2 3 is not working. Expected result for this input is 6.

ASSERT: "allArguments.size() == origArgc"

I'm having this error in my Qt Application:
Debug Error!
Program: C:\Qt\Qt5.1.1\5.1.1\msvc2012_64\bin\QtCored.dll
Module: 5.1.1 File: global\qglobal.cpp
Line: 2014
ASSERT: "allArguments.size() == origArgc" in file
kernel\qcoreapplication.cpp, line 2095
#include <QCoreApplication>
#include <QDebug>
#include <QStringList>
int main(int argc, char *argv[])
{
QCoreApplication app(argc,argv);
qDebug()<<"argc:" << argc;
qDebug()<<"arguments:"<<app.arguments().length();
return 0;
}
Why is that so?
The issue was that I was passing arguments with a new line character in it.
After I removed, it worked again.