I need to avoid(mock) unit testing of line of code displaying messagebox using FakeItEasy framework.
Code is here,
public class XYZ
{
public static int nValue = 0;
public void AddInetegers(int i)
{
int j = i * 100;
int k = j * 30 / 100;
MessageBox.Show("Value Here {0}", k.ToString());
nValue = k;
}
}
MessageBox.Show is a static member, and cannot be faked by FakeItEasy. In these situations, typically the code that accesses the non-fakeable service is isolated and wrapped in an interface.
Here's how I would do it:
interface IAlerter
{
void Alert(string text, string caption);
}
public class XYZ
{
public static int nValue = 0;
private readonly IAlerter _alerter;
public XYZ(IAlerter alerter)
{
_alerter = alerter;
}
public void AddInetegers(int i)
{
int j = i * 100;
int k = j * 30 / 100;
_alerter.Show("Value Here {0}", k.ToString());
nValue = k;
}
}
[TestMethod]
public void Test_Using_FakeItEasy()
{
var alerter = A.Fake<IAlerter>();
// No need to configure alerter.Alert(), since it doesn't return a value
var xyz = new XYZ(alerter);
xyz.AddInetegers(3);
Assert.AreEqual(90, XYZ.nValue);
}
Related
Hi I am coding a synth in the Juce framework.
When my adsr is running I need to override isVoiceActive() and set it to true from inside the voice. This is a public member function of the SynthesiserVoice class.
virtual bool SynthesiserVoice::isVoiceActive ( ) const
Returns true if this voice is currently busy playing a sound.
By default this just checks the getCurrentlyPlayingNote() value, but
can be overridden for more advanced checking.
So in the voice I have another member function virtual void renderNextBlock() and from inside it I want to override isVoiceActive
class SynthVoice : public SynthesiserVoice
{
public:
void renderNextBlock (AudioBuffer <float> &outputBuffer, int startSample, int numSamples) override
{
for (int sample = startSample; sample < (startSample + numSamples); ++sample)
{
float env_value = adsr.getNextSample();
if(env_value > 0)
isVoiceActive = true; //???????
...
you cannot do this because isVoiceActive is not an attribute it's a function, you can use your isVoiceActive as a data member and not as a function member so you can assign it true or false. or if you want to work with function then you have to add '&' so you can assign to that function a value (if you don't use '&' then you cannot do what you want to do.
you have 2 choices:
1/ to use isVoiceActivat as a data member and you can do like this:
class SynthesiserVoice
{
public:
bool isVoiceActive;
};
class SynthVoice :public SynthesiserVoice
{
public:
oid renderNextBlock (AudioBuffer <float> &outputBuffer, int startSample, int numSamples) override
{
for (int sample = startSample; sample < (startSample + numSamples); ++sample)
{
float env_value = adsr.getNextSample();
if(env_value > 0)
isVoiceActive = true;
}
}
};
2/ to do the following:
class SynthesiserVoice
{
public:
bool i = false;
virtual bool& isVoiceActive() { return i; } // or virtual bool& isVoiceActive()=0;
};
class SynthVoice :public SynthesiserVoice
{
public:
bool& isVoiceActive()override { return i; }
void renderNextBlock (AudioBuffer <float> &outputBuffer, int startSample, int numSamples) override
{
for (int sample = startSample; sample < (startSample + numSamples); ++sample)
{
float env_value = adsr.getNextSample();
if(env_value > 0)
isVoiceActive ()= true;
}
}
};
Now you have to implement this to suit your functions and data.
Hope it's clear and helpful
I am trying to understand what is the best way to update structure passed from C# to C++ to modify. Following code shows my intentions.
I have c++ side structure as below:
// data.cpp
struct Pos {
int x;
int y;
}
struct PointInfo {
int count;
Pos positions[10]; // Ideally I wanted vector<Pos>
}
// data.cs
[StructLayout (LayoutKind.Sequential)]
[System.Serializable]
public struct Pos {
int X;
int Y;
}
[StructLayout (LayoutKind.Sequential)]
[System.Serializable]
public struct PositionInfo {
int count;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 10)]
public Pos positions;
}
// In C# side only
public PositionInfo pInfo = new PositionInfo();
pInfo.positions = new Pos[10];
// In C# Interop function
[DllImport (dllName)]
private static extern void GetPositions(ref PositionInfo pInfo);
This method in implemented in c++ side (unmanaged plugin) as:
extern "C" void INTERFACE_EXPORT
GetPositions(PositionInfo* pinfo)
{
pinfo.count = 3;
for (int i = 0; i < 3; ++i) {
pinfo.pos[i].x = i*2;
pinfo.pos[i].y = i*4;
}
}
This does not work. If I replace Pos with Int then it works. I am trying to understand what is the best way to pass above structure from C# to be filled by C++ plugin.
There is a problem in the simple code snippet below that i can't uncover even with a debugger. Thanks for your help in advance.
Command.h
class word{
private : static int next_word;
private : int word_id;
public : word();
public : void grabIt();
public : void raiseIt();
public : void putItDown();
};
class Command{
public : typedef void(word::*Method)();
private : word* w;
private : Method m;
public : Command(word *w, Method m);
public : void execute();
};
template<typename T>
class Queue{
private : enum{
SIZE=9
};
private : T* commandArray[SIZE];
private : int m_added = 0, m_removed = 0;
public : void enqueue(T* t){
commandArray[m_added] = t;
m_added = (m_added + 1) % SIZE;
}
public : T* dequeue(){
int temp = m_removed;
m_removed = (m_removed + 1) % SIZE;
return commandArray[temp];
}
};
Command.cpp
int word::next_word = 0;
word::word(){
this->word_id = next_word++;
}
void word::grabIt(){
std::cout << "Grabbed it" << std::endl;
}
void word::raiseIt(){
std::cout << "Raised it" << std::endl;
}
void word::putItDown(){
std::cout << "Put it down" << std::endl;
}
Command::Command(word *w, Method m){
w = w;
m = m;
}
void Command::execute(){
(w->*m)(); // -------------->>>>> Causing Seg-Fault
}
Main.cpp
Queue<Command> queue;
Command *commandArray[9];
Command command1(new word, &word::grabIt); commandArray[0] = &command1;
Command command2(new word, &word::raiseIt); commandArray[1] = &command2;
Command command3(new word, &word::putItDown); commandArray[2] = &command3;
Command command4(new word, &word::grabIt); commandArray[3] = &command4;
Command command5(new word, &word::raiseIt); commandArray[4] = &command5;
Command command6(new word, &word::putItDown); commandArray[5] = &command6;
Command command7(new word, &word::grabIt); commandArray[6] = &command7;
Command command8(new word, &word::raiseIt); commandArray[7] = &command8;
Command command9(new word, &word::putItDown); commandArray[8] = &command9;
for( int i=0 ; i < 9; i++){
queue.enqueue(commandArray[i]);
}
for( int i=2 ; i < 6 ; i++){
queue.dequeue()->execute();
}
Some word objects inside the queue appear NULL in the debugger, so cause Seg-Fault.
To fix the problem, change
Command::Command(word *w, Method m){
w = w;
m = m;
}
to
Command::Command(word *w, Method m) : w(w), m(m){}
For the exact reasoning, look at Can I use identical names for fields and constructor parameters?
I am making a game that includes orcish enemies. I have one problem, how do initialize their HP, MP, Attack, defense, and speed without writing different code for each like this:
int orcishHP = 50;
int orcishMP = 5;
int orcishAttack = 15;
int orcishDef = 10;
int orcishSpeed = 20;
Isn't there some way to initialize all that when I refer to the orc, like this:
int orcishStats(){
int HP = 50
etc...
}
So instead of calling orcish MP HP and all that stuff, I have all of it in one one place.
If this made sense, please help. If this didn't make sense, don't help.
Here's the correct way to do this:
You define your attributes in a class/struct, and when that struct is initialized, the constructor(OrcishAttributes()) is called:
struct OrcishAttributes
{
int m_orcishHP;
int m_orcishMP;
int m_orcishAttack;
int m_orcishDef;
int m_orcishSpeed;
OrcishAttributes(int orcishHP, int orcishMP, int orcishAttack, int orcishDef, int orcishSpeed)
{
m_orcishHP=orcishHP;
m_orcishMP = orcishMP;
m_orcishAttack = orcishAttack
m_orcishDef = orcishDef;
m_orcishSpeed = orcishSpeed;
}
}
class Orc
{
public:
Orc(OrcishAttributes oa)
{
m_orcAttributes = oa;
}
private:
OrcishAttributes m_orcAttributes;
}
int main()
{
OrcishAttributes oa(50,5,15,10,20);
Orc* o = new Orc(oa);
}
That's why we have OOP.
You can define a base class, lets say Unit. It can store common members like fields or methods:
class Unit
{
protected:
int HP;
int MP;
int Attack;
int Def;
int Speed;
public:
int getHPplusMP()
{
return HP + MP;
}
};
Then, you can create inherited classes and initialize these values in its constructors:
class OrcUnit : public Unit
{
public:
OrcUnit()
{
HP = 20;
MP = 0;
Attack = 13;
Def = 4;
Speed = 5;
}
};
class ElfUnit : public Unit
{
public:
ElfUnit()
{
HP = 25;
MP = 15;
Attack = 15;
Def = 1;
Speed = 8;
}
};
Then, you will be able to work with it:
ElfUnit elf;
OrcUnit orc;
Unit u = elf;
cout << u.getHPplusMP();
Unit u = orc;
cout << u.getHPplusMP();
In general, it is definitely a good idea to learn OOP very well before trying to implement games.
I implemented a googletest, with fixture class UnitTest_solver. Implementation for the fixture is the following. It contains helper functions
class UnitTest_solver : public ::testing::Test
{
protected:
static void SetUpTestCase()
{
// setup table with data
m_col = 2;
m_row = 100;
// other things to initialize m_test_data
}
static void TearDownTestCase()
{
for(int i = 0 ; i < m_row ; i++)
delete[] m_test_data[i];
delete[] m_test_data;
}
static double chi_sqr(double* x)
{
if(m_col < 2)
return 0;
double fx = 0;
double * row_i = new double[m_col - 1];
for(int i = 0 ; i < m_row ; i++)
{
for(int j = 0 ; j < m_col - 1 ; j++)
row_i[j] = m_test_data[i][j];
fx += pow(m_test_data[i][0] - func_1(x, row_i, m_col - 1), 2.0);
}
return fx;
}
static double func_1(double* x, double* dbl, int nb_param)
{
if(nb_param != 2)
return 0;
return x[0] * exp(-1 * x[1] * dbl[0]);
}
static double UnitTest_solver::functPtr( double * parameters, void * userinfo)
{
return chi_sqr(parameters);
}
static ofstream thing;
static double** m_test_data;
static int m_col, m_row;
};
Also, out of the fixture scope, i initialize static variables. Last is function pointer. is definition syntax ok ?
double** UnitTest_solver::m_test_data = 0;
int UnitTest_solver::m_col = 0;
int UnitTest_solver::m_row = 0;
double (UnitTest_solver::*functPtr)(double * , void *) = 0;
then, i have a test case, with link to fixture UnitTest_solver.
TEST_F(UnitTest_solver, testFunc1)
{
Solver* solversqp = new Solver();
solversqp->set_pointer_to_function(UnitTest_solver::functPtr, (void*)0);
//...
}
second line show compile time error with UnitTest_solver::functPtr : when mouse is over the error, info is 'function defined at line xxx is unacessible', with xxx pointing to functPtr definition inside the fixture.
If i run the ggltest commenting the last line, solversqp->set_pointer_to_function(UnitTest_solver::functPtr, (void*)0);, test is finishing (if i put a trivial ASSERT, it is successful).
Whats wrong with my function pointer definition.
I do not see full code, therefore this is just a guess.
Everything in class UnitTest_solver is protected, therefore everything (other then classes inheriting for this class) do not have access to it's members. Change it to public, and your problem will be solved :
class UnitTest_solver : public ::testing::Test
{
// protected:
public: