Monday, 30 May 2016
Friday, 27 May 2016
Code Coverage Using Google Test for unmanaged code using Visual Studio
Folks I am back with a shout out. Today we
are going to walk on creating a static library in Visual c++ and will use
Google test to write unit test and perform code coverage.
In this small article I will be using a set of utilities
Visual studio 2013, Google Test Runner that integrate in
Visual studio for easy discovery of gtest and the finally the Google test
framework. I am writing this to get a quick revision if some day I walk back again to do it again.
The very first step was to download the tool shown above
I downloaded the gtest framework and compiled the library
using the following setting
Once the library is ready I created a dummy static library
to get some hands on testing and named it as Win32Project1 which simply had
two functions sum and sub.
To start writing some Google test code I created a another C++ console based application and configured it with the settings to get code coverage
by setting linker properties of the test project as
And advanced->profile as
Do you know that the above two settings will help me to get
coverage of the api I am going to test in a while.
One these are set I created a dummy Gtest which is very
basic and ran it using the test runner. Notice that I have to set of test
written one from Microsoft unit testing other from google test setting applied
before writing test was.
As the picture show sum is 100% covered however the sub is
not covered as I have no test written for it in Google Tests.
Wednesday, 1 July 2015
PTE- Academic Speaking Tips:
Machine learning: Given a task T with a performance measure P the machine learning experience E is said to be derived in such a way that the performance on Task T improves with experience E.
More the data, more the analysis ,more is the evaluation ,more is the probability of errors. Serious readers logically can understand the analysis on the input data and the results.
Here are some tips to go ahead
More the data, more the analysis ,more is the evaluation ,more is the probability of errors. Serious readers logically can understand the analysis on the input data and the results.
Here are some tips to go ahead
Check:
Before
the the test start. Check your headphones read some sentences from the section
which shows about the mic check steps. Try to read a sentence and two and
adjust your microphone properly don’t practice the old 123 mic check. Try to
read the way you will read in sections to come. Hear your voice and check is it
what you sound like and audible with no sound of your breath,if not adjust your MIC, even then if the issue exists
raise your hand and the ask for a replace. Time is not clocked for this activity so make sure things are the way you want before you begin.
1. Read aloud.
General Tip
In time to practice a sentence perform a
fake reading don’t concentrate on surrounding concentrate on your goal. Cool
your self down in this time, to clear your voice hold your tongue between your
teeth and then read ,when you will begin the actual reading your voice would be
very clear try this before you believe it.
Action
time :
Divide the sentence into meaning full
chunks and read in speed(dont compromise on the pronunciation with this speed) make sure you spend a little more time on the
important words usually nouns verbs in a sentence. Maintain speed or a rhythm, do
not rush but be confident much like:a news reader.
A note: Don’t doubt your self, computer
would only analyse what you will do, so do it the right way.
2. Repeat sentence
The short sentences are easy to remember
but some time some complex sentences come. When you speak, speak in one go don’t pause between words. Focuses on
finishing the sentence in one go no breaks anywhere.
3. Describe image
The more you speak the more is the chance
of error. Be specific and without hesitations speak the following about the image.
·
Introduce the image
·
Give trends for highest and lowest
·
Conclude what is over all idea
20-25 seconds will be consumed in all this,
go ahead the haunt is over.
When you construct sentences about graphs use the graph vocabulary like dramatic fall, significant decrease . For table what the table contains and the highest and lowest and then overall conclusion from the table .
Example image.
This image shows how a song can be played from the multilingualism website. The first step is to search the song. The second step is to download the song followed by saving the downloaded song in your hard drive . Finally user can play the saved song . Overall this image lists the steps how a user can download and play a song.
4. Retell
lecture
Make notes during the lecture. Speak in
general what the speaker was telling, get some data from the notes you wrote
add if example are provide. Try to
conclude if you understand. Don’t fumble
and note “the answers will be analysed base on your response so be correct in
what you speak?. Here I mean the correct English with no hesitations. The way
if some one asks what is your name and then comes your response. J… Machine learning.
Though this section appears complicated but the key
is what you write in your notes and how well you respond back. Create correct
and small sentences.
Notes:
Modern Cars
Cost of cars
Decreased over the time
Fuel efficient
Future :Advanced technology
Better safety
pollution free
More intelligent
Speech:
The lecture was about the modern cars . As per the speaker cars nowdays have become more fuel effiecent .Moreover there costs have decreased . However the speak expects that in future they would be pollution free with better safety as they will become more intelligent. Overall this lecture was about the future of modern cars.
5.
Answer short questions: once the recording start
speaks the answer usually they are single words or a group of words.
Monday, 12 May 2014
Model Based Testing
From couple of days i am researching on Model Based Testing. Really wanted to have good grasp on fundamentals of testing my code using model based testing.
Finally i landed on Microsoft Spec Explorer and N Model. Their are many commercial tools available in this subject , but as a Novice i opted to use above for my test purpose.
The integration of Spec Explorer with Visual studio really made my analysis easy , while using N Model i have to code more to generate tests, generate models and finally run the tests against implementation.
Abstraction plays a key role in designing models.
I created few test applications using c#, modeled the use case and generated offline tests using spec explorer.
At times To provide a good code coverage it becomes difficult , to unit tests but using Model Based testing i was able to get a good code coverage very fast , identified Dead code and finally wrote some better code.
Wednesday, 17 July 2013
Know about virtual functions in c++
// Base class is going to add a virtual pointer
// Every class having virtual functions have a vtable
// vtable contains the list of functions pointers which object of that class can call
// vptr points to the function defination of the class it points to.
* Class Test show the concept of virtual tables
// Derived class having overridden functions
// VPTR And the concept of virtual table which contains static array created at compile time listing all function pointer to
// virtual functions that can be called by the object of the class .
FUNCTION IS MAPPED TO THE CORRESPONDING FUNCTION OF THE CLASS
class test
{
int val;
public:
test()
{
cout<<"Default Constructor of base class"<<endl;
}
virtual void fun1()
{
cout<<"Base class function fun1"<<endl;
}
test(int x)
{
val=x;
}
virtual void fun2()
{
cout<<"Base class function fun2"<<endl;
}};class Housetest:public test
{
int houseval;public:
Housetest()
{
cout<<"I AM DEFAUTL CONSTRUCNTOR FOR HOUSE TEST"<<endl;
cout<<"House Test dont have fun1"<<endl;
}
Housetest(int x):test(x)
{
houseval=x;
cout<<"Value of x"<<houseval;
}
void fun1()
{
cout<<" I am fun1 from house test"<<endl;
}
};class classtest:public test
{int classval;
public:
classtest()
{
cout<<"Class test dont have fun1"<<endl;}
void fun2()
{
cout<<" I AM FUN2 FROM CLASS TEST"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
test *x= new classtest;
x->fun1();
x->fun2();
cout<<"----------------------------------------------------\n"<<endl;
cout<<"I am creating a new object of House test now"<<endl;
cout<<"----------------------------------------------------\n"<<endl;
x=new Housetest;
x->fun1();
x->fun2();
return 0;
}
// Every class having virtual functions have a vtable
// vtable contains the list of functions pointers which object of that class can call
// vptr points to the function defination of the class it points to.
* Class Test show the concept of virtual tables
// Derived class having overridden functions
// VPTR And the concept of virtual table which contains static array created at compile time listing all function pointer to
// virtual functions that can be called by the object of the class .
FUNCTION IS MAPPED TO THE CORRESPONDING FUNCTION OF THE CLASS
class test
{
int val;
public:
test()
{
cout<<"Default Constructor of base class"<<endl;
}
virtual void fun1()
{
cout<<"Base class function fun1"<<endl;
}
test(int x)
{
val=x;
}
virtual void fun2()
{
cout<<"Base class function fun2"<<endl;
}};class Housetest:public test
{
int houseval;public:
Housetest()
{
cout<<"I AM DEFAUTL CONSTRUCNTOR FOR HOUSE TEST"<<endl;
cout<<"House Test dont have fun1"<<endl;
}
Housetest(int x):test(x)
{
houseval=x;
cout<<"Value of x"<<houseval;
}
void fun1()
{
cout<<" I am fun1 from house test"<<endl;
}
};class classtest:public test
{int classval;
public:
classtest()
{
cout<<"Class test dont have fun1"<<endl;}
void fun2()
{
cout<<" I AM FUN2 FROM CLASS TEST"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
test *x= new classtest;
x->fun1();
x->fun2();
cout<<"----------------------------------------------------\n"<<endl;
cout<<"I am creating a new object of House test now"<<endl;
cout<<"----------------------------------------------------\n"<<endl;
x=new Housetest;
x->fun1();
x->fun2();
return 0;
}
Operator overloading in c++ with example
Operator overloading
This gives us flexibility to use operators with class objects.
Operator overloading is done by two ways
1. Member function : Take atmost one argument
2. Friend function: Take at most two arguments
This limits the overloading to apply for uninary and binary operators.
// The Test Class show the opertor overloading
// Overloads + operator using Friend function
// Overloads - operator using Member function
class test
{public:
int a;
int b;friend test operator +(test &, test &);
test operator -(test &);
test()
{
}test(int x, int y)
{
a=x;
b=y;}};test test::operator -(test &t1)
{test temp;
temp.a= t1.a-a;
temp.b= t1.b-b;return temp; }test operator +(test &t1, test &t2)
{test temp;
temp.a=t1.a+t2.a;
temp.b=t2.b+t2.b;
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{test t1(3000,2000);
test t2(1000,1000);test t3;t3= t1-t2;
cout<<t3.a<<t3.b<<endl;
t3=t1+t2;
cout<<t3.a<<t3.b<<endl;
return 0;
}
// Class Balance Perfom operator Overloading
/// Overloads + * using member function
// Overloads / - using friend function
class balance
{
public:
int start;
int end;
public:friend balance operator /(balance &, balance &);
balance()
{
start=end=0;
}balance(int x,int y)
{
start=x;
end=y;
}balance operator +(balance&);
friend balance operator -(balance &, balance &);
balance operator *(balance &);};
balance balance::operator*(balance &b1)
{
balance temp;
temp.start= b1.start*start;
temp.end=b1.end*end;
return temp; }balance operator /(balance &b1,balance &b2)
{
balance temp;
temp.start= b1.start / b2.start;
temp.end =b1.end /b2.end;return temp;
}
balance balance:: operator +(balance &b1)
{
balance temp;
temp.start=b1.start+start;
temp.end=b1.end+end;return temp;
}balance operator -(balance &t1, balance &t2)
{
balance temp;
temp.start=t1.start -t2.start;
temp.end=t1.end- t2.end;return temp; }int _tmain(int argc, _TCHAR* argv[])
{ balance b1(100,0);
balance b2(100,20);
balance b3;cout<<"------------START---------------------\n";
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;cout<<"------------add---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1+b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;cout<<"------------sub---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1-b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
cout<<"------------mul---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1*b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
cout<<"------------div---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1/b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
return 0;
}
This gives us flexibility to use operators with class objects.
Operator overloading is done by two ways
1. Member function : Take atmost one argument
2. Friend function: Take at most two arguments
This limits the overloading to apply for uninary and binary operators.
// The Test Class show the opertor overloading
// Overloads + operator using Friend function
// Overloads - operator using Member function
class test
{public:
int a;
int b;friend test operator +(test &, test &);
test operator -(test &);
test()
{
}test(int x, int y)
{
a=x;
b=y;}};test test::operator -(test &t1)
{test temp;
temp.a= t1.a-a;
temp.b= t1.b-b;return temp; }test operator +(test &t1, test &t2)
{test temp;
temp.a=t1.a+t2.a;
temp.b=t2.b+t2.b;
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{test t1(3000,2000);
test t2(1000,1000);test t3;t3= t1-t2;
cout<<t3.a<<t3.b<<endl;
t3=t1+t2;
cout<<t3.a<<t3.b<<endl;
return 0;
}
// Class Balance Perfom operator Overloading
/// Overloads + * using member function
// Overloads / - using friend function
class balance
{
public:
int start;
int end;
public:friend balance operator /(balance &, balance &);
balance()
{
start=end=0;
}balance(int x,int y)
{
start=x;
end=y;
}balance operator +(balance&);
friend balance operator -(balance &, balance &);
balance operator *(balance &);};
balance balance::operator*(balance &b1)
{
balance temp;
temp.start= b1.start*start;
temp.end=b1.end*end;
return temp; }balance operator /(balance &b1,balance &b2)
{
balance temp;
temp.start= b1.start / b2.start;
temp.end =b1.end /b2.end;return temp;
}
balance balance:: operator +(balance &b1)
{
balance temp;
temp.start=b1.start+start;
temp.end=b1.end+end;return temp;
}balance operator -(balance &t1, balance &t2)
{
balance temp;
temp.start=t1.start -t2.start;
temp.end=t1.end- t2.end;return temp; }int _tmain(int argc, _TCHAR* argv[])
{ balance b1(100,0);
balance b2(100,20);
balance b3;cout<<"------------START---------------------\n";
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;cout<<"------------add---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1+b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;cout<<"------------sub---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1-b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
cout<<"------------mul---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1*b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
cout<<"------------div---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1/b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
return 0;
}
Casting in c++
The below example demostrates the types of casting available with c++.
C++ is strongly types language.
The example below demostrates the diffrent.
Please read the comments to know some theory about the diffrent types of castings.
class base
{
public:C++ is strongly types language.
The example below demostrates the diffrent.
Please read the comments to know some theory about the diffrent types of castings.
class base
{
int a;base()
{
cout<<
"Base class object"<<endl;}
void fun(){
cout<<
"Function from base class"<<endl;}
};
class der:public base{
public:der()
{
cout<<
"Der class object"<<endl;}
int b;
void fder(){
cout<<
"Function from derieved"<<endl;}
};
class unrelated{
public:unrelated()
{
cout<<
"Unrelated class object"<<endl;}
void funrelelated(){
cout<<
"Function is unrelated"<<endl;}
};
int _tmain(int argc, _TCHAR* argv[]){
// created pointer and objects of each of the class we defined base *bp;
base bo;
der *dp;
der doo;
unrelated *up;
unrelated uo;
// Lets talk about Dynamic cast before we start implementing// 1. Applies to pointer and objects of related classe// 2. Allows downcasting fails in downcasting// 3 Always check that the object are converted or notbp= dynamic_cast<base * >(&doo);//dp= dynamic_cast<der *>(&bo);// This statement will fail or not compile
// Static cast can perform conversion between pointers to related clasees.
// No overhead of type safety checkder *stcst=static_cast<der *>(bp);// Reineterpretcast covnverts any pointer type to any other pointer typer even of unrelated classesbase *bp= new base;unrelated *ur=
reinterpret_cast<unrelated *> (bp);// Constant removes the constantness of object
return 0;}
Monday, 21 May 2012
Simulating Traffic lights on runtime not a ideal state machine
In this example i am trying to simulate traffic light systems, where the lights will glow based on the sensor update, the threads used are pthreads from posix library and i am using very basic of gcc compiler for compiling. This traffic light manages the four directions of traffic and gives each a time to relay the traffic.
#include<iostream>
#include<time.h>
#include<unistd.h>
#include<pthread.h>
#include<cstdlib>
using namespace std;
//class to simulate the timer
class timer
{
clock_t T1;
clock_t T2;
public:
void start()
{
T1=clock();
}
void stop()
{
T2=clock();
}
int get_time()
{
return ((T2-T1) * 60)/CLOCKS_PER_SEC ;
}
void reset()
{
// use this function to reset the timer
T1=0;
T2=0;
}
~timer() //oops a distructor
{
T1=0;
T2=0;
}
};
timer t1;
// real time volatile i dont let the compiler use assumptions for me, no //optimizations for me my dear
volatile bool g_red;
volatile bool g_green;
volatile bool g_yellow;
volatile bool stat=true;
volatile bool g_censor1;
volatile bool g_censor2;
volatile bool g_censor3;
volatile bool g_censor4;
volatile bool g_fyellow=true;
void check()
{
if(g_green && stat)
{
cout<<"Green Light"<<endl;
stat=false;
}
if(g_yellow && g_fyellow)
{
g_yellow=false;
cout<<"Yellow"<<endl;
if(g_censor1)
{
cout<<"++++++North get ready to stop"<<endl;
}
if(g_censor2)
{
cout<<"++++++South Get Ready to stop"<<endl;
}
if(g_censor3)
{
cout<<"++++++East Get Ready stop"<<endl;
}
if(g_censor4)
{
cout<<"++++++West Get Ready stop"<<endl;
}
}
if(g_red)
{
cout<<"=====RED=========="<<endl;
stat=true;
g_yellow=false;
g_green=false;
g_fyellow=true;
}
}
void north()
{
cout<<"\t\tControlling North"<<endl;
t1.start();
while(g_censor1 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=false;
g_censor2=true;
g_censor3=false;
g_censor4=false;
}
void south()
{
cout<<"\t\tControlling South"<<endl;
t1.start();
while(g_censor2 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=false;
g_censor2=false;
g_censor3=true;
g_censor4=false;
}
void east()
{
cout<<"\t\tControlling East"<<endl;
t1.start();
while(g_censor3 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=false;
g_censor2=false;
g_censor3=false;
g_censor4=true;
}
void west()
{
cout<<"\t\tControlling West"<<endl;
t1.start();
while(g_censor4 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=true;
g_censor2=false;
g_censor3=false;
g_censor4=false;
}
void *fun(void *arg)
{
cout<<"Censor Update"<<endl;
int num=rand();
if(num%2==0)
{
g_censor1=true;
g_censor2=false;
g_censor3=false;
g_censor4=false;
}
if(num%3==0)
{
g_censor1=false;
g_censor2=true;
g_censor3=false;
g_censor4=false;
}
if(num%5==0)
{
g_censor1=false;
g_censor2=false;
g_censor3=true;
g_censor4=false;
}
if(num%7==0)
{
g_censor1=false;
g_censor2=false;
g_censor3=false;
g_censor4=false;
}
if(g_censor1)
{
cout<<"++++++North Vechicle censor"<<endl;
}
if(g_censor2)
{
cout<<"++++++South Vechicle censor"<<endl;
}
if(g_censor3)
{
cout<<"++++++East Vechicle censor"<<endl;
}
if(g_censor4)
{
cout<<"++++++West Vechicle censor"<<endl;
}
}
int main()
{
//g_censor1=true;
pthread_t t1;
while(1)
{
try
{
//pthread_create(&t1,0,fun,0);
pthread_create(&t1,0,fun,0);
pthread_join(t1,0);
north();
//pthread_create(&t1,0,fun,0);
south();
pthread_create(&t1,0,fun,0);
pthread_join(t1,0);
east();
pthread_create(&t1,0,fun,0);
pthread_join(t1,0);
west();
//pthread_join(t1,0);
throw 1;
}
catch(int)
{
cout<<"exception";
pthread_create(&t1,0,fun,0);
}
}
return 0;
}
Tuesday, 1 May 2012
Some intresting questions
Explain the memory layout of a c program ? Explain in detail the role of each segment and type of variables it stores?
Explain the inter process communication mechanism? which all you have used and which one is the fastest?
Whats the need of volatile variable? Explain about the effects in terms of usage of a ordinary variable and a volatile variable?
what does a process control block saves and where does it save the context of the process?
Explain virtual functions ? how they are implemented and how many virtual tables does a derived class has?
Design a macro which reverse a number 1234 as 4321.
Dynamic memory allocation in c and c++? advantages and disadvantages?
How will u implement garbage collection in c++?
Explain the different types of constructors and whats the copy constructor?
How real time operating systems are different from GPOS?What makes a system real time?
How will u restrict a object to be created on stack and heap?
Explain the different types of casting available with c++? Explain with example each one of them and providing situations where they can be used?
what are static libraries and dynamic libraries? are they part of the user code?
why do we prefer using references?
How can you call a function before main ?
Explain the ways u can debug and application?
What approaches will u apply if the controller is not able to connect to the instrument?
Test case to test a scenario where a text box takes a valid value between 0 and 1000? Write test cases ?
Talk about Boundary value analysis?
Types of testing ?
Talk about the configuration management tools requirement management tools ?
Talk about the bug management tool and the different states of the bug tracking ?
Threads synchronisation mechanism ? Design you own semaphore and prove that it solves the design purpose?
Which all features you have used in c++?
Explain abstract classes and the pure virtual functions?
Explain mapping at run time how the run time binding happens in case of virtual functions?
Whats the size of class if i use a virtual function and if i don't use a virtual function?
Why virtual functions are considered slower? what is the hidden penalty?
Explain why you don't prefer using the heap memory in ur application this is a design specific questions?
how will u set , unset and determine which all bits are set in a value.
Explain big and little endian machines with few examples? where does it matter ?
what are events ?
Tuesday, 3 April 2012
Finite State Machine
#include<iostream>
#include<pthread.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
char x_buff[1000];
char t_buff[1000];
/*Call setup mode is used to establish SVCs between DTE devices. A PLP uses the X.121 addressing
scheme to set up the virtual circuit. The call setup mode is executed on a per-virtual circuit basis,
which means that one virtual circuit can be in call-setup mode while another is in data-transfer mode.
This mode is used only with SVCs, not with PVCs.
*/
/* data-transfer mode is used for transferring data between two DTE devices across a virtual circuit.
In this mode, PLP handles segmentation and reassembly, bit padding, and error and flow control.
This mode is executed on a per-virtual circuit basis and is used with both PVCs and SVCs.
Idle mode is used when a virtual circuit is established but data transfer is not occurring. It is executed
on a per-virtual circuit basis and is used only with SVCs.
Call-clearing mode is used to end communication sessions between DTE devices and to terminate
SVCs. This mode is executed on a per-virtual circuit basis and is used only with SVCs.
Restarting mode is used to synchronize transmission between a DTE device and a locally connected
DCE device. This mode is not executed on a per-virtual circuit basis. It affects all the DTE device’s
established virtual circuits
*/
/* ************************TYPES OF PACKETS */
/*Four types of PLP packet fields exist:
1 General Format Identifier (GFI)—Identifies packet parameters, such as whether the packet
carries user data or control information, what kind of windowing is being used, and whether
delivery confirmation is required.
2 Logical Channel Identifier (LCI)—Identifies the virtual circuit across the local DTE/DCE
interface.
3 Packet Type Identifier (PTI)—Identifies the packet as one of 17 different PLP packet types.
4 User Data—Contains encapsulated upper-layer information. This field is present only in data
packets. Otherwise, additional fields containing control information are added.
I bits
GFI 4
LCI 12
PTI 8
User Data variable
*/
class timer
{
clock_t T1;
clock_t T2;
public:
void start()
{
T1=clock();
}
void stop()
{
T2=clock();
}
int get_time()
{
return ((T2-T1) * 60)/CLOCKS_PER_SEC ;
}
void reset()
{
T1=0;
T2=0;
}
~timer()
{
T1=0;
T2=0;
}
};
enum PLP
{
IDLE=1,
CALL_SETUP,
CALL_CLEAR,
DATA_TRANSFER,
RESTART
};
void *changer(void * arg);
void *FSM(void * arg);
PLP packet;
void change_state(PLP nextstate)
{
packet=nextstate;
}
void call_setup()
{
cout<<"Now in call _setup function "<<endl;
FILE *fp;
char file_name[50];
cout<<"enter file name to open";
gets(file_name);
fp=fopen(file_name,"r");
if(fp==NULL)
{
cout<<"File open Failed"<<endl;
change_state(CALL_SETUP);
}
else
{
fread(t_buff,800,800,fp);
//cout<<"waiting for input"<<endl;
if(t_buff)
{
strcat(t_buff,"+CALL_SETUP_HEADER");
cout<<"-----------------------------------------"<<endl;
cout<<"Input data is "<<t_buff<<endl;
cout<<"-----------------------------------------"<<endl;
}
else
{
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+CALL_SETUP_HEADER");
cout<<"-----------------------------------------"<<endl;
}
change_state(IDLE);
}
}
void idle()
{
cout<<"Now in idle function "<<endl;
strcat(t_buff,"+IDLE_HEADER");
change_state(DATA_TRANSFER);
}
void call_clear()
{
cout<<"Now in call_clear function "<<endl;
strcpy(x_buff,t_buff);
cout<<x_buff<<endl;
strcpy(t_buff,"");
change_state(RESTART);
}
void restart()
{
cout<<"Inside restart function "<<endl;
//change_state(CALL_SETUP);
cout<<"setting value of state machine using thread"<<endl;
pthread_t t1;
pthread_create(&t1,0,changer,0);
pthread_join(t1,NULL);
cout<<x_buff<<endl;
//pthread_create(&t1,0,FSM,0);
//pthread_join(t1,NULL);
}
void data_transfer()
{
static int counter;
counter++;
cout<<"Now in data_transfer function"<<endl;
cout<<"Session =="<<counter<<endl;
if(counter < 6)
{
cout<<"NXT STATE WILL BE IDLE "<<endl;
char *p;
p=t_buff;
if(strlen(t_buff)<32)
{
strcpy(x_buff,p);
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+data_transfer_HEADER");
cout<<"-----------------------------------------"<<endl;
}
else
{
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+data_transfer_HEADER");
cout<<"-----------------------------------------"<<endl;
//cout<<"exceed"<<endl;
}
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
change_state(IDLE);
pthread_mutex_unlock( &mutex1 );
}
else
{
counter=0;
cout<<"NEXT STAE WILL BE CALL CLEAR max data _cycle Event"<<endl;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
change_state(CALL_CLEAR);
pthread_mutex_unlock( &mutex1 );
}
}
void *changer(void * arg)
{
cout<<"--------------CHANGER THREAD CALLED---------------------"<<endl;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
packet=CALL_SETUP;
pthread_mutex_unlock( &mutex1 );
}
void *FSM(void * arg)
{
static int co;
co++;
cout<<" Inside FSM CALL VALUE IS NOW==="<<co<<endl;
switch(packet)
{
case IDLE:
idle();
break;
case CALL_SETUP:
call_setup();
break;
case CALL_CLEAR:
call_clear();
break;
case DATA_TRANSFER:
data_transfer();
break;
case RESTART:
restart();
break;
default:
packet=CALL_SETUP;
break;
}
}
timer tim1; // hu ha global variables timer
int main()
{
pthread_t t;
tim1.start();
int i=0;
while(1)
{
try
{
if((tim1.get_time() > 300) && !(tim1.get_time() < 0 ))
{
//cout<<"Time value 150 elasped"<<tim1.get_time()<<endl;
if(tim1.get_time() < 300)
{
throw 1;
}
tim1.reset();
tim1.stop();
tim1.start();
i++;
pthread_create(&t,0,FSM,0);
pthread_join(t,NULL);
if(i%10 == 0)
{
pthread_t t1;
pthread_create(&t1,0,changer,0);
pthread_join(t1,NULL);
throw 1;
}
} //edif
else
{
static int x;
tim1.stop();
x++;
if(x==1)
{
for(int k=0;k<tim1.get_time();i++)
cout<<"--";
cout<<"SYSTEM INTIALIZED "<<endl;
}
}
}
catch(int)
{
cout<<"******************************************************************"<<endl;
cout<<"**********EXCEPTION HANDLED***************************************"<<endl;
cout<<"Current state is "<<packet<<endl;
cout<<"Current time is"<< tim1.get_time()<<endl;
cout<<"TIMER RESET CALLED "<<endl;
tim1.reset();
cout<<"TIMER RESTARTED"<<endl;
tim1.start();
}
catch(...)
{
cout<<"**********EXCEPTION HANDLED***************************************"<<endl;
cout<<"Current state is "<<packet<<endl;
cout<<"Current time is"<< tim1.get_time()<<endl;
cout<<"TIMER RESET CALLED "<<endl;
tim1.reset();
cout<<"TIMER RESTARTED"<<endl;
tim1.start();
}
}
//FSM();
return 0;
}
#include<pthread.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
char x_buff[1000];
char t_buff[1000];
/*Call setup mode is used to establish SVCs between DTE devices. A PLP uses the X.121 addressing
scheme to set up the virtual circuit. The call setup mode is executed on a per-virtual circuit basis,
which means that one virtual circuit can be in call-setup mode while another is in data-transfer mode.
This mode is used only with SVCs, not with PVCs.
*/
/* data-transfer mode is used for transferring data between two DTE devices across a virtual circuit.
In this mode, PLP handles segmentation and reassembly, bit padding, and error and flow control.
This mode is executed on a per-virtual circuit basis and is used with both PVCs and SVCs.
Idle mode is used when a virtual circuit is established but data transfer is not occurring. It is executed
on a per-virtual circuit basis and is used only with SVCs.
Call-clearing mode is used to end communication sessions between DTE devices and to terminate
SVCs. This mode is executed on a per-virtual circuit basis and is used only with SVCs.
Restarting mode is used to synchronize transmission between a DTE device and a locally connected
DCE device. This mode is not executed on a per-virtual circuit basis. It affects all the DTE device’s
established virtual circuits
*/
/* ************************TYPES OF PACKETS */
/*Four types of PLP packet fields exist:
1 General Format Identifier (GFI)—Identifies packet parameters, such as whether the packet
carries user data or control information, what kind of windowing is being used, and whether
delivery confirmation is required.
2 Logical Channel Identifier (LCI)—Identifies the virtual circuit across the local DTE/DCE
interface.
3 Packet Type Identifier (PTI)—Identifies the packet as one of 17 different PLP packet types.
4 User Data—Contains encapsulated upper-layer information. This field is present only in data
packets. Otherwise, additional fields containing control information are added.
I bits
GFI 4
LCI 12
PTI 8
User Data variable
*/
class timer
{
clock_t T1;
clock_t T2;
public:
void start()
{
T1=clock();
}
void stop()
{
T2=clock();
}
int get_time()
{
return ((T2-T1) * 60)/CLOCKS_PER_SEC ;
}
void reset()
{
T1=0;
T2=0;
}
~timer()
{
T1=0;
T2=0;
}
};
enum PLP
{
IDLE=1,
CALL_SETUP,
CALL_CLEAR,
DATA_TRANSFER,
RESTART
};
void *changer(void * arg);
void *FSM(void * arg);
PLP packet;
void change_state(PLP nextstate)
{
packet=nextstate;
}
void call_setup()
{
cout<<"Now in call _setup function "<<endl;
FILE *fp;
char file_name[50];
cout<<"enter file name to open";
gets(file_name);
fp=fopen(file_name,"r");
if(fp==NULL)
{
cout<<"File open Failed"<<endl;
change_state(CALL_SETUP);
}
else
{
fread(t_buff,800,800,fp);
//cout<<"waiting for input"<<endl;
if(t_buff)
{
strcat(t_buff,"+CALL_SETUP_HEADER");
cout<<"-----------------------------------------"<<endl;
cout<<"Input data is "<<t_buff<<endl;
cout<<"-----------------------------------------"<<endl;
}
else
{
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+CALL_SETUP_HEADER");
cout<<"-----------------------------------------"<<endl;
}
change_state(IDLE);
}
}
void idle()
{
cout<<"Now in idle function "<<endl;
strcat(t_buff,"+IDLE_HEADER");
change_state(DATA_TRANSFER);
}
void call_clear()
{
cout<<"Now in call_clear function "<<endl;
strcpy(x_buff,t_buff);
cout<<x_buff<<endl;
strcpy(t_buff,"");
change_state(RESTART);
}
void restart()
{
cout<<"Inside restart function "<<endl;
//change_state(CALL_SETUP);
cout<<"setting value of state machine using thread"<<endl;
pthread_t t1;
pthread_create(&t1,0,changer,0);
pthread_join(t1,NULL);
cout<<x_buff<<endl;
//pthread_create(&t1,0,FSM,0);
//pthread_join(t1,NULL);
}
void data_transfer()
{
static int counter;
counter++;
cout<<"Now in data_transfer function"<<endl;
cout<<"Session =="<<counter<<endl;
if(counter < 6)
{
cout<<"NXT STATE WILL BE IDLE "<<endl;
char *p;
p=t_buff;
if(strlen(t_buff)<32)
{
strcpy(x_buff,p);
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+data_transfer_HEADER");
cout<<"-----------------------------------------"<<endl;
}
else
{
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+data_transfer_HEADER");
cout<<"-----------------------------------------"<<endl;
//cout<<"exceed"<<endl;
}
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
change_state(IDLE);
pthread_mutex_unlock( &mutex1 );
}
else
{
counter=0;
cout<<"NEXT STAE WILL BE CALL CLEAR max data _cycle Event"<<endl;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
change_state(CALL_CLEAR);
pthread_mutex_unlock( &mutex1 );
}
}
void *changer(void * arg)
{
cout<<"--------------CHANGER THREAD CALLED---------------------"<<endl;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
packet=CALL_SETUP;
pthread_mutex_unlock( &mutex1 );
}
void *FSM(void * arg)
{
static int co;
co++;
cout<<" Inside FSM CALL VALUE IS NOW==="<<co<<endl;
switch(packet)
{
case IDLE:
idle();
break;
case CALL_SETUP:
call_setup();
break;
case CALL_CLEAR:
call_clear();
break;
case DATA_TRANSFER:
data_transfer();
break;
case RESTART:
restart();
break;
default:
packet=CALL_SETUP;
break;
}
}
timer tim1; // hu ha global variables timer
int main()
{
pthread_t t;
tim1.start();
int i=0;
while(1)
{
try
{
if((tim1.get_time() > 300) && !(tim1.get_time() < 0 ))
{
//cout<<"Time value 150 elasped"<<tim1.get_time()<<endl;
if(tim1.get_time() < 300)
{
throw 1;
}
tim1.reset();
tim1.stop();
tim1.start();
i++;
pthread_create(&t,0,FSM,0);
pthread_join(t,NULL);
if(i%10 == 0)
{
pthread_t t1;
pthread_create(&t1,0,changer,0);
pthread_join(t1,NULL);
throw 1;
}
} //edif
else
{
static int x;
tim1.stop();
x++;
if(x==1)
{
for(int k=0;k<tim1.get_time();i++)
cout<<"--";
cout<<"SYSTEM INTIALIZED "<<endl;
}
}
}
catch(int)
{
cout<<"******************************************************************"<<endl;
cout<<"**********EXCEPTION HANDLED***************************************"<<endl;
cout<<"Current state is "<<packet<<endl;
cout<<"Current time is"<< tim1.get_time()<<endl;
cout<<"TIMER RESET CALLED "<<endl;
tim1.reset();
cout<<"TIMER RESTARTED"<<endl;
tim1.start();
}
catch(...)
{
cout<<"**********EXCEPTION HANDLED***************************************"<<endl;
cout<<"Current state is "<<packet<<endl;
cout<<"Current time is"<< tim1.get_time()<<endl;
cout<<"TIMER RESET CALLED "<<endl;
tim1.reset();
cout<<"TIMER RESTARTED"<<endl;
tim1.start();
}
}
//FSM();
return 0;
}
Quick questions
- Why do we use reference and pointers in copy constructor?
- Can exception be thrown from constructor or ~Distrct?
- Main is a entry point of a program how can be call another function before main?
- Find mid point of linked list only transverse it once?
- What is differences between Mutex and semaphores?
- Explain resource sharing in multi threaded environment?
- How static and dynamic libraries are linked ?
- Are virtual constructor and distructors possible?
- Give syntax for operator overloading for == operator?
==================Level 2========================
Explain Socket binding.
Can two applications have binding on the the same port?
what is the disadvantage of dynamic memory allocation?
how you debug your applications in your current evironment ?+++++++++++++++++++++++++++++++++++++++++++++++++++
Write down a program to find prime numbers?
Explain why your RTOS is diffrent then a normal kernel?
Subscribe to:
Posts (Atom)
-
Folks I am back with a shout out. Today we are going to walk on creating a static library in Visual c++ and will use Google test to write ...
-
The below example demostrates the types of casting available with c++. C++ is strongly types language. The example below demostrates t...