Unit test failing on two equal Qstrings (one is read from a file)? - qt

I'm trying to compare two string which are supposed to be the same. The test however fails.
I'm testing if the file get set correctly by the router.setForwarding(true) method.
Here is the code of the test.
void router_test::testSetForwarding_true()
{
QFile myfile("/proc/sys/net/ipv4/ip_forward");
myfile.open(QIODevice::ReadOnly | QIODevice::Text);
router->setForwarding(true);
QString forward = QString(myfile.readAll());
QCOMPARE(QString("1"),forward);
}
As a result I get:
FAIL! : router_test::testSetForwarding_true() Compared values are not the same
Actual (QString("1")): 1
Expected (forward): 1
Why aren't they equal?

As you can glean from the output, you have interchanged the actual and expected values. You're also comparing the newline-terminated output against one without a newline.
This should work:
QCOMPARE(forward, QString("1\n"));
or
QCOMPARE(forward[0], QChar('1'));

Related

Comparison of Hex Numbers in Tcl

I am trying to compare the two following hex numbers in a TCL Script:
0x00001111
32'h00001111
They should be equal to each other, but I am having trouble comparing them because of the difference in formatting.
I have tried using "scan" command to convert, but have not gotten the correct output I am looking for.
Code snippet:
set read1 0x00001111
set read2 32'h00001111
set new_read [scan $read2 %x]
if ($read1 == $read2) {
puts "Two values equal"
}
This code does not work and sets new_read to 4369. Any help is appreciated
You want to change the format specifier to scan to sth. like:
if {[scan $read2 "%d'h%x" bw new_read] == 2} {
# on success
} else {
# on failure
}
This way, you will be able to compare scan $read1 %x and $new_read using == & friends.

How does the assignment part work in the following line of code in Angular2?

I am learning from the project angular2-rxjs-chat application ong github. In the code here there is a line of code given below:
threads[message.thread.id] = threads[message.thread.id] ||
message.thread;
where threads has earlier been defined on line 29 in the code as shown below:
let threads: {[key: string]: Thread} = {};
The comments in the code states that "store the message's thread in our acuuculator 'threads'. I need a little bit explanation of how does the assignment works on line 31 as on both sides of the assignment operator we have the same thing i.e., threads[message.thread.id]. If the statement on line 31 was like
(threads[message.thread.id] = message.thread;)
then I would explain it as a value is being assigned to a key in the map "threads". But I don't understand the full line.
This means if threads[message.thread.id] already has a value then keep it, otherwise set the value to meassage.thread.
If the part before || evaluates to a value that is truthy (not null, undefined, false, ...)then the part after||is not evaluated and the result from the part before||is returned otherwise the result from the expression after||` is returned.
You could also write it as
if(!threads[message.thread.id]) {
threads[message.thread.id] = message.thread;
}

Get cell types when reading and parsing excel files

I am trying to read and parse and excel and some unclear things come into play as usual for me.
Here is what i have:
while (true)
{
comVariantCell1 = cells.item(row, 1).value().variantType();
comVariantCell2 = cells.item(row, 2).value().variantType();
//if an empty cell is found, processing will stop and user will get an error message in order to solve the inconsistency.
if (comVariantCell1 != COMVariantType::VT_EMPTY && comVariantCell2 != COMVariantType::VT_EMPTY)
{
//both cells have values, check their types.
importedLine = conNull();
progress1.setText(strfmt("Importing row %1", row));
if (cells.item(row, 1).value().variantType() == COMVariantType::VT_BSTR)
{
importedLine += cells.item(row, 1).value().bStr();
}
else
{
importedLine += cells.item(row, 1).value().double();
}
importedLine += cells.item(row, 2).value().double();
importedLinesCollection += [importedLine]; //conIns(importedLinesCollection, row - 1, (importedLine));
row++;
}
else
{
info (strFmt("Empty cell found at line %1 - import will not continue and no records were saved.", row));
break;
}
}
Excel format:
Item number Transfer Qty
a100 50.5
a101 10
a102 25
This worked well to check if the cell type is string: COMVariantType::VT_BSTR
but what should i use to check for a real or integer value ?
I am pretty sure in this case, the quantity will be not contain real values but anyway, it could be useful in the future to make the difference between these two types.
I have to mention that, even if i have an int value and I use cells.item(row, 1).value().int() it won't work. I can't see why.
Why do i want to make the difference? Because if it's forbidden to have real values in the quantity column ( at least in my case ), i want to check that and give the user the opportunity to put a correct value in that place and maybe further investigate why that happened to be there.
Take a look on how it is done in \Classes\SysDataExcelCOM\readRow.
It is basically using switch to test the type. This is really boring!
Also take a look on ExcelIO, a class I made some years ago. It reads Excel and returns each row as a container. This is a more high-level approach.
As a last resort you could save the Excel as a tab separated file. Then use TextIO to read the content. This will be at least 10 times faster than using Excel!

trouble passing parameters to console application

I have trouble with correctly launching cloc 1.62 from windows command line using qprocess. Here is what i have:
A QStringList with all the languages cloc recognizes;
QStringList languages;
languages<<"\"ABAP\""<<"\"ActionScript\""<<"\"Ada\""<<"\"ADSO/IDSM\""<<"\"AMPLE\""<<"\"Ant\""<<"\"Apex Trigger\"";
Then i create a qstring consisting of all of the list's elements separated by comma, exept for one, which is stored in a lang variable;
QString langsinuse;
for (int i=0;i<languages.length();i++)
{
if (languages.at(i) != lang)
{
if (langsinuse.isEmpty())
{
langsinuse=langsinuse+languages.at(i);
}
else
{
langsinuse=langsinuse+','+languages.at(i);
}
}
}
then i build an arguments stringlist and launch the process:
QStringList arguments;
QProcess cloc;
QString params="cloc-1.62.exe --xml --by-file --report-file="+
list1.at(1).trimmed()+'/'+name+'/'+"report.xml"+" --exclude-lang="+langsinuse+" "+distr;
arguments<<"/k"<<params;
cloc.startDetached("cmd.exe",arguments,"cloc/");
But somehow the spaces in the languages names are not considered escaped and every word separated by space considered a different parameter for cloc, even though both words are in double quotes (for example "\"Apex Trigger\"") and clock produces a bunch of errors.
(50 errors:
Unable to read: Trigger","Arduino
Unable to read: Sketch","ASP","ASP.Net","Assembly","AutoHotkey","awk","Bourne)
It's the error that happens when you don't put a language's name that contains spaces in double quotes, int the --exclude-lang= option (for example --exclude-lang=Apex Trigger will cause an error, --exclude-lang="Apex Trigger" won't)
However if i just save the whole command i build in qt and save it in some batch file it runs just fine.
Am i missing something in escaping the double quotes correctly?
Ok i just had to pass arguments separately and not as a single string.
arguments<<"/k"<<"cloc-1.62.exe" <<"--xml"<<"--by-file"<<"--report-file="+
list1.at(1).trimmed()+'/'+name+'/'+"report.xml"<<"--exclude-lang="+langsinuse<<distr;

why does this value get cut off?

So I'm trying to print out hex values in c++ using printf, and well, I want the output of the following string to be val:0x0366 including that leading 0
Here's the code
int poo = 0x00000366;
printf("val:0x%x \n",poo);
Here's the output:
0x366
Have you tried printf("val:0x%04x \n",poo);?
According to printf reference,
A format specifier follows this prototype:
%[flags][width][.precision][length]specifier
In your case:
%[0][4][default precision][default lenght]x

Resources