Program to Increment the parameter when the parameter has more digits

I have a Parameter. I want to increase the parameter value by 1.
Below is the code to capture the parameter and increment it by 1.
Action()
{
long double i;   
lr_save_string("1234567899""id");
i = atol(lr_eval_string("{id}"));
i =i+1;   
lr_save_int(i, "id");
lr_output_message("Value is: %s"lr_eval_string("{id}"));
return 0;
}
Output:
Starting action Action.
Action.c(12): Value is: 1234567900
Ending action Action.
Ending Vuser...


If we increase the Length of parameter by one more digit (here 11 digits).  The below logic fails as shown below. L L

Action()
{
long double i;   
lr_save_string("12345678999""id");
i = atol(lr_eval_string("{id}"));
i =i+1;   
lr_save_int(i, "id");
lr_output_message("Value is: %s"lr_eval_string("{id}"));
return 0;
}
Starting action Action.
Action.c(12): Value is: -2147483648
Ending action Action.


So, the solution to handle this type of issues is as below:

Action()
{
   
char *ptr, *id;
   
lr_save_string("1234567890123499999""id");

id = lr_eval_string("{id}"); 

ptr =id+strlen(id)-1;

lr_output_message("value of ptr = %s", ptr );

lr_output_message("Original value = %s", id );

/* While Loop to continue adding if digit is 9 */
while (++*ptr > '9')
{
    *ptr-- = '0';
}

lr_output_message("Incremented value = %s", id);

return 0;
}
Output:
Starting action Action.
Action.c(12): value of ptr = 9
Action.c(14): Original value = 1234567890123499999
Action.c(22): Incremented value = 1234567890123500000

Ending action Action.

Comments

Popular posts from this blog

How to use a value between two different threads in Jmeter

Steps to Analyze AWR Report in Oracle

Correlation and checking in Results