Continuing with the series of Find the Bug, another simple question this time. A developer is asked to write a function which compares the two strings and and return true if two strings are same and false otherwise, developer ends up writing the following code:
public static bool Compare(string str1,string str2)
{
try
{
int size = str1.Length;
for (int i = 0; i < size; i++)
{
if (str1[i] != str2[i])
return false;
}
return true;
}
catch (IndexOutOfRangeException ex)
{
return false;
}
}
Under what all test cases this code will either fail are give wrong results.
Showing posts with label fib. Show all posts
Showing posts with label fib. Show all posts
Find the Bug - 2
Continuing with the series "Find the Bug", here is the next code snippet
.....
if(wrapstring.Length > n)
{
string tempwrapstring = "";
for(int i=0; i< i="i+n)"> wrapstring.Length)
tempwrapstring += wrapstring.Substring(i,(wrapstring.Length-i));
else
tempwrapstring += wrapstring.Substring(i, i+n) + "brtag";
}
wrapstring=tempwrapstring;
}
.........
The codesnippet is about wrapping the strings of length greater than n into multi-line strings using brtag(Line break in HTML). What is the bug in the above code snippet. What is the solution. Add your answers in the comments.
.....
if(wrapstring.Length > n)
{
string tempwrapstring = "";
for(int i=0; i< i="i+n)"> wrapstring.Length)
tempwrapstring += wrapstring.Substring(i,(wrapstring.Length-i));
else
tempwrapstring += wrapstring.Substring(i, i+n) + "brtag";
}
wrapstring=tempwrapstring;
}
.........
The codesnippet is about wrapping the strings of length greater than n into multi-line strings using brtag(Line break in HTML). What is the bug in the above code snippet. What is the solution. Add your answers in the comments.
Find the Bug - I
White box testing is not about finding obvious cases which any way will be caught during the Black box testing, but its about finding the rare cases where the software will break. Finding the cases where the software breaks by inspecting the code is always challenge. Here, I am adding a code snippet which has a bug, lets see how many testers can identify such bugs:
.............
int num=10;
ArrayList FullList = GetObjectsList();
ArrayList RandList = new ArrayList(num);
Random rand = new Random();
int index;
int maximum = FullList.Count;
int max_objs = (num > FullList.Count) ? FullList.Count : num;
while(RandList.Count < max_objs)
{
index = rand.Next(1, maximum);
//This returns rand value between 1 and maximum-1 (both inclusive)
if(!RandList.Contains(FullList[index]))
RandList.Add(FullList[index]);
}
.............
The code snippet is about getting 10 or less random objects from a list of objects. Under which scenario the code can be disastrous ? Add your solution to the comments.
.............
int num=10;
ArrayList FullList = GetObjectsList();
ArrayList RandList = new ArrayList(num);
Random rand = new Random();
int index;
int maximum = FullList.Count;
int max_objs = (num > FullList.Count) ? FullList.Count : num;
while(RandList.Count < max_objs)
{
index = rand.Next(1, maximum);
//This returns rand value between 1 and maximum-1 (both inclusive)
if(!RandList.Contains(FullList[index]))
RandList.Add(FullList[index]);
}
.............
The code snippet is about getting 10 or less random objects from a list of objects. Under which scenario the code can be disastrous ? Add your solution to the comments.
Subscribe to:
Posts (Atom)