Im trying to learn how to use unity but Im with some doubts.
For example I have a c program that works like a very basic calculator where I have for example the division operation.
And I would like to test this basic program with unity to learn how unity works. Im using unity in code blocks and I started to do some basic exampels to learn the basics, for example:
void test1(void)
{
TEST_ASSERT(1==1);
TEST_ASSERT(2==2);
}
void test2(void)
{
TEST_ASSERT_EQUAL(2,2);
TEST_ASSERT_EQUAL_INT(2,3);
TEST_ASSERT_EQUAL_FLOAT(1.1,1.2);
}
int main()
{
UNITY_BEGIN();
RUN_TEST(teste_theFirst);
RUN_TEST(teste_2);
return UNITY_END();
}
So this example above is simple because we are saying the values directly. But how can we use unity to interact to a command line program? For example I have a part of the calculator program relative to the division operation, below:
calc.c:
void division();
printf("Enter / symbol for Division \n");
void division()
{
int a, b, d=0;
printf("\nPlease enter first number : ");
scanf("%d", &a);
printf("Please enter second number : ");
scanf("%d", &b);
d=a/b;
printf("\nDivision of entered numbers=%d\n",d);
}
Do you know how unity can interact with this division operation code? For example the calculator has some inputs and the output. In the case of the division there are two inputs and one output (the result).
The main of the calc program:
int main()
{
int X=1;
char Calc_oprn;
// Function call
calculator_operations();
while(X)
{
printf("%s : ", KEY);
Calc_oprn=getche();
switch(Calc_oprn)
{
case '+': addition();
break;
case '/': division();
break;
case 'Q':
case 'q': exit(0);
break;
case 'c':
case 'C': system("cls");
calculator_operations();
break;
default : system("cls");
printf("\n**********You have entered unavailable option");
printf("***********\n");
printf("\n*****Please Enter any one of below available ");
printf("options****\n");
calculator_operations();
}
}
}
Aucun commentaire:
Enregistrer un commentaire