In regards to C what is a stub routine? Also an example would be much appreciated as well.
A method stub or simply stub in software development is a piece of code used to stand in for some other programming functionality. A stub may simulate the behavior of existing code (such as a procedure on a remote machine; such methods are often called mocks) or be a temporary substitute for yet-to-be-developed code.
A stub is a small piece of code that takes the place of another component during testing. The benefit of using a stub is that it returns consistent results, making the test easier to write. And you can run tests even if the other components are not working yet.
A stub is a class supposed to return data from its methods and functions. The return value is hard-coded. Stubs are used inside unit tests when we are testing that a class or method delivers expected output for a known input. They are easy to use in testing, and involve no extra dependencies for unit testing.
Plesk Extensions API stubs is a set of files with all classes definitions and methods prototypes, but without implementation of these methods. You can use API stubs for quicker development.
A stub routine can be one of (at least) two things.
First, it can be a place-holder routine which you quickly develop to test a higher level routine, with the intention of substituting a real version later on. This is typically used with top-down development (coding the higher levels first then working your way down to the more detailed stuff) and can be as simple as:
int getCount (void) { return 7; } // just return fixed value for testing.
or slightly more complex:
// Cycle through values for some variety.
int getCount (void) {
static int retvals[] = {2,7,1,8,2,8,1,8,2,8,4,5,9};
static int pos = -1;
pos = (pos + 1) % (sizeof (retvals) / sizeof (*retvals));
return retvals[pos];
}
Of course, once the stub gets complex enough, you may as well just implement the real thing :-)
Secondly, it's commonly used in remote procedure call (RPC) environments. A stub there is used for marshalling data at one end and communicating it to a server at the other end.
RPC needs to create stub functions for the client and a server. It's very similar to a function prototype in C but the end result is slightly different, such as:
+----------------+
| Client |
| +----------+ | +---------------+
| | caller | | | Server |
| |----------| | | +----------+ |
| | stub_cli |---- (over the wire) --->| stub_svr | |
| +----------+ | | |----------| |
+----------------+ | | function | |
| +----------+ |
+---------------+
In this example, instead of calling function
in the same program, the caller calls a client stub function (with the same prototype as function
), which is responsible for packaging up the information and getting it across the wire to another process.
This can be the same machine or a different machine, it doesn't really matter - one of the advantages of RPC is to be able to move servers around at will.
In the server, there's a 'listener' process that will receive that information and pass it to the server. The server's stub receives the information, unpacks it, and passes it to the real function.
The real function then does what it needs to and returns to the server stub which can package up the return information and pass it back to the client stub.
The client stub then unpacks that and passes it back to the caller.
It is a function with the same signature as the real function but it does nothing, and can be compiled and executed just like the real thing. e.g.
int MyStub(char * str)
{
/* Stub - Does Nothing */
return 0;
}
These are often used as placeholders, so that overall program structure can be worked out first, and then the details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With