Test run in a global variable - global-variables

"Implement your algorithm in Java. Provide the argument for the test run in a global variable."
What does it mean to provide the argument for the test run in a global variable?

It means that your program should look more or less like this:
public class Test
{
private static final int INPUT_ARG = ...;
public static void main(String[] args) throws Exception
{
...
}
...
}
You can provide input arguments to a program in various ways:
Inside an input file.
Through the keyboard.
As command-line arguments.
Hard-coded within your program.
In this case, you are asked to use the last option mentioned above, and hard-code the input argument as a global variable within your code. In all other options, the user of your program can change the input arguments without having to rebuild the program. But in the latter case, only those who have access to the source code of your program can change the input arguments.

Related

In C both static local variable and static global variable memory allocation

In C both static local variable and static global variable with same name declaration are done in same file. They both are stored in data segment memory.
When I compile code why it is not throwing error?
In same memory 2 variable with same name can be stored?
Please find code below
#include <stdio.h>
static int x = 0;
void call()
{
printf("Adress of gloabl static =%p",&x);
}
int main()
{
static int x = 0;
printf("Adress of local static =%p",&x);
call();
}
There are two things going on here.
Variable scopes.
There is a public x and a local x in main. Say x and main:x. C defaults to the local one. As far as I am aware, C offers no method to access the global one when the reference has been overridden by a local one. (C++ does, ::)
Different meaning for the static keyword.
2.1 The static keyword in global scope means the object x may not be referenced from anywhere except this file. Even with extern it will throw you an error. This is great, as it prevents unintended usage of "private" objects in a module.
2.2 The static keyword in local scope means the object x will be allocated once, permanently. Any instance of main() uses the same x.
Like a global, while only being accessible from within the scope. x will persist even if you exit main. This also means you cannot use an initializer, it will error on the above if not 0. Since when should it do the initialization?
Standard specifies all static objects in local scope shall be initialized.
Local statics are great if you need to carry over data to the next time the functions runs, especially with interrupts, but do not want the data to be public.
Static is a great keyword to do basic data hiding for multi file microcontroller programs. Keeping your code clean and not littered with globals.
This programming question has already been answered in stackoverflow as this has nothing to do with electronics.
That said, local variables have precedence and will shadow a global variable. It is perfectly valid to do so. Some compilers might warn about this. If this bothers you, don't use global and local variables with same name then.

Separating data and test settings in QTests

I am currently using following pattern when creating tests with QTest.
One test class per production class.
If a class has some 'global' setting run the test class multiple times with each such setting.
Each production class method has one test method.
Each test method has _data method.
Each _data method specify settings and data to be used and names the cases.
This last point somewhat bothers me because I am not passing just data but also data to be used for initialising that particular test. Sometimes it looks weird and even though my tests are short they are not all that intuitive because of the initialisation logic.
The alternative pattern I know of is to split each test method (breaking my rule #3) based on this initialisation needs. On one hand it would eliminate a lot of _data test methods but it would also make the test classes much bigger and no longer easily relatable to the production class (the naming would help though). Most google tests are written like this.
Another alternative would be to use global state of the object much like I treat global settings. If the object is either valid or invalid then it would not be part of each _data method but rather setting of the test class that would run in either configuration.
My main concern is maintainability. With my current approach I sometimes struggle to understand the nuances of the settings I pass to the tests and I need some sensible way to separate them and not to burden myself even more by it.
For global settings you run the test class multiple times, so IMHO doing the same for local settings doesn't really "violate" your rule #3, it is more an extension of rule #2.
Alternatively you could make the initialization routine another thing that is part of the test data.
Something like
private slots:
void someMethodTest_data()
{
QTest::addColumn<QByteArray>("settings");
//....
QTest::addRow("case1") << "settings1" << ....
}
void someMethodTest()
{
Q_FETCH(QByteArray, settings);
const QByteArray initMethod = QTest::currentTestFuntion() + "_init_" + settings;
QMetaObject::invokeMethod(this, initMethod.constData(), Qt::DirectConnect);
// commence test
}
protected slots:
void someMethodTest_init_settings1();

warning: "Cannot find file '/3' locally. To fix it set server name by environment variable PHP_IDE_CONFIG and restart debug session."

Every time I step into the method (prepareStatusFilterQuery()) while unit testing with debug I got warning and debugger not stepping into the method. Warning:
Cannot find file '/3' locally. To fix it set server name by
environment variable PHP_IDE_CONFIG and restart debug session.
Other than this case debugger works fine.
This happens when you try to step into the code of a mock object.
There is no solution for it because the mock objects are instances of classes that are created during the execution of the test.
PHPUnit MockObjects uses reflection to gather information about the class you ask it to mock (the names and arguments of the public methods) then it generates the PHP code of a new class (that extends the mocked class) and runs it using eval().
The debugger is actually stepping into the method but PhpStorm cannot display the source code because there is no source code for it. Keep using the "Step Into" command and at some point the control will go back to code (of PHPUnit) whose source code is loaded from a file and PhpStorm can find it.
Just encountered this error. In my case it was caused by the method missing the private keyword:
class SomeClass {
function some_function_name () {
}
}
Should look like:
class SomeClass {
private function some_function_name () {
}
}

QPluginLoader.instance() - how does it really work?

I am currently working on a Qt project where dynamic plugin loading is central. I am loading DLLs by using Qt's QPluginLoader. The different plugins are accessed through CameraPluginStructs, defined as follows:
struct CameraPluginStruct
{
QString name;
QString filePath;
CameraPluginInterface* plugin;
QPluginLoader* pluginLoader;
CameraPluginStruct(QString filePath=nullptr, QString name=nullptr):
filePath(filepath), name(name), plugin(nullptr), pluginLoader(nullptr){}
};
To load a plugin, the following function is called:
void loadPlugin(CameraPluginStruct& p)
{
p.pluginLoader = new QPluginLoader(p.filePath);
p.pluginLoader->load();
QObject* possiblePlugin = p.pluginLoader->instance(); //QPluginLoader.instance();
if(possiblePlugin)
{
// cast from QObject to correct type:
p.plugin = qobjectcast<CameraPluginInterface>(possiblePlugin);
}
}
And to unload a plugin, I use this function:
void unloadPlugin(CameraPluginStruct& p)
{
p.pluginLoader->unload(); // QPluginLoader.unload();
p.pluginLoader->~QPluginLoader();
p.pluginLoader = nullptr;
p.plugin = nullptr;
}
I have made a few, simple test plugins that write messages to the console when the constructor is called. For simplicity, let's say that I have two test plugins, DLL A and DLL B. When I load A by using the loadPlugin() function, the constructor in the plugin is called, and the corresponding message is written in the console. I can do the same thing with B, and everything seems to work – B's constructor message is written to the console, and the other functions seem to work as well.
Now, the problem occurs when I try to create another CameraPluginStruct connected to either A or B. No message is written to the console, which leads me to think that the constructor is not called. Even though, I am able to call the other test functions in the plugin with success (DoSomething(), see below). If I unload all CameraPlugins connected to A or B, and then load the DLL again, the constructor is called again on the first load.
The QPluginLoader.instance() call is described as follows in the documentation:
"Returns the root component object of the plugin. (...) The component object is a QObject. Use qobject_cast() to access interfaces you are
interested in." http://doc.qt.io/qt-5/qpluginloader.html#instance
Wouldn't it then be natural that the constructor in the DLL would be called each time, and not only the first time?
As I've understood, a DLL is only loaded once for any program. Therefore I have also tried to use only one QPluginLoader per DLL file, with the same result. Qt also says that:
"Multiple instances of QPluginLoader can be used to access the same physical plugin." http://doc.qt.io/qt-5/qpluginloader.html#details
Therefore I can't see how this can be a source to the problem anyway.
I would really appreciate if anyone could clarify how the QPluginLoader.instance() really works. Why is the constructor – at least how it seems – only called the first time I use the instance() call?
Thank you!
Here is the code found in the DLLs (the output texts differ in A and B):
TestDLL::TestDLL()
{
std::cout << "This is written from the constructor in A \n";
}
QString TestDLL::Name() const
{
return "Hello, writing from Name() \n";
}
void TestDLL::DoSomething() const
{
qDebug() << "Hello, this text comes from DoSomething()"\n;
}
When your plugin is loaded (i.e. the first time QPluginLoader::instance() is called), then a single instance of it is created - this is your root instance. The root instance is the only instance QPluginLoader will ever create for you.
If you want more, then you create a createInstance() or clone() method for your plugin class, so that new instances can be created from the root instance. Or more conventionally, make your plugin class a factory for the class type you wish to expose.

qt tr() in static variable

I have problem concerning translations in qt. All translations in my porject work fine, but one, which is in a static variable of a class. Corresponding part of code looks as follows
The header file is similar to this:
typedef struct {
int type;
QString problematicString;
} info;
MyClass::QObject_Descendant
{
Q_OBJECT;
//some functions like constructor, destructor... etc.
....
static info myClassInfo;//class that makes problems
}
and in implementation file I initialize the variable as follows:
info MyClass::myClassInfo={
1,
tr("something to be translated")
};
And whatever I do (trying with QT_TR_NOOP, then tr() and others) I cannot get myClassInfo.problematicString translated. The weirdest thing is that the text "something to be translated"
appears in *.ts file.
If someone has any hints, please share them with me. Thanks in advance.
Chris.
Static variables are instantiated (and thus, constructor code run) before your int main function is run. The translation code is set up in the QApplication constructor (I believe), which isn't run until your int main function has been entered. Thus, you are trying to get the translation of a string before the code to support it has been initialized.
To avoid this, you could either accept that the given string isn't translated and explicitly translate it every time it is used, or use the Construct on First Use idiom instead of a static member variable.

Resources