Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose that David needs to test a function (named, compdate ) that compares dat

ID: 3735521 • Letter: S

Question

Suppose that David needs to test a function (named, compdate) that compares dates with the following specification:

int compdate(int year1, int month1, int day1, int year2, int month2, int day2);

/* compdate returns 0 if date1 = date2, -1 if date1 before date2, 1 if date1 after date2,
-2 if any element of any date is invalid;
valid years are all integers (<0 = BC); valid month is 1-12; valid day is
1-{28,29,30,31} depending on the year and month */


David decides to use a random testing scheme to test the compdate function, he also decides to run 200 tests where each test is performed by the following function:

int randomtest_compdate() {

          int i;

for (i=0; i<200; i++){
int y1 = random_int(); /* returns a random 32 bit value */
int y2 = random_int();
  int m1 = (random_int() % 11) + 1;
  int m2 = (random_int() % 11) + 1;
  int d1 = (random_int() % 30) + 1;
  int d2 = (random_int() % 30) + 1;
  int c = compdate(y1,m1,d1,y2,m2,d2);
  oracle(y1,m1,d1,y2,m2,d2,c);

}

}

Assuming that oracle function does a good job of checking compdate’s correctness:

Discuss the random testing approach and propose how to improve it.

For each improvement, explain the weakness of the original scheme.

Explanation / Answer

Random testing mostly depends upon the executing machine and the implementation of the random function. A random number is generated by using system time, some encoding/decoding algorithm, CPU clock speed, number of cpu cores for asynchronocity to name a few. Some machines have good enough implementation of random function so that it can return different numbers in same clock frame but on general purpose machines it might not be the case.

So taking the above explanation of random functions into consideration, the random testing might generate similar date1 and date2 and for each test case (out of 200) also be similar. So most of the time it will test dates which are relatively close i.e., date1 = 10/01/2018, date2=11/04/2018. dates with a lot of time gap might not be tested much.

Also edge cases may or may not be tested since random will be random.

We can test edge cases separately by making one date constant and othor random or both constant.

For rest of the testing the generation of dates can be done in different frames hence it will cause generation of better created random dates. (For generatng dates in different frames means - generate one date than break function flow by either sleep or something else and then get to function again and generate another date and compare).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote