Software configuration - the experimental debug 4

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lll_90/article/details/92234523

Experience: debug The following process is actually achieved mainly by stepping eclipse static checking and breakpoints. As long as the general location based on the desired output and the actual output good location bug, and then carefully look at the code, determine the location of problems that may arise after the narrow range, and then stepping through, according to the program illogical place to find out where the bug.
In fact, this debug mode is the most basic and common, unlike the multi-threaded back, you need to understand the situation of the stack by the program at run time. But that part of the operation are not familiar with, you need to pay more consolidated. Well, here is the basis for debug sections:

3.6 Debugging

3.6.1 understand the code debugger is thought to be

FindMedianSortedArrays.Java
dichotomy, the position of the median calculated after combined array c: halfLen. When the length c is even, halfLen middle two numbers is the subscript large number, is odd, halfLen just subscript c is the median.
First take shorter median arrays A [i], where A is a smaller array so that, assuming that A [i] is the median of the combined array, then there must B halfLen-i-1 is less than the number of A [ I], in particular, when B is an odd number length, B is determined large number of halfLen-i B [halfLen-i-1] if [i] is greater than a.
A crop by dichotomy, while moving i, j positions, merge array c is determined by A, B in which segments constituting.
Continue two points A, known until you find division point of suitable size A [i], B [j ], so that A [i - 1] <B [j] and A [i]> B [j - 1].
Note here when the length a is even, a [I] is intermediate the larger of two numbers, B [j] is even longer, B [j] is critical in the larger of two numbers, m + n is therefore an odd number, the maximum number of the left side is the median.

RemoveComments.Java
with "/ " and " /" is comment identifier determination range, as a marker with inBlock ,, when inBlockfalse, reads the current character, when inBlocktrue, it will ignore the current character.
After each of the source comments section [i] and the exclusion screening added to the new list.

TopVotedCandidate.java
i-ticket everyone's existence List A.get (i) in. At query time, with time and orderly features, we can see each A.get (i) i of .get (0) .time in accordance with size i in ascending order, and the time of each i A.get (i) a A.get (i) .get (j) .time is ordered in accordance with the size of j.
Thus, by dichotomy .time first find the maximum of i is smaller than t A.get (i) .get (0) , and then looking for the maximum or less t j in A.get (i). When the two closest vote since the need is most frequently artificially recent vote winner, so long as the minimum is greater than t i find, j can be.

3.6.2 identify and locate the error process

FindMedianSortedArrays.java
first reference to the core idea of this algorithm online: binary search idea, the "intermediate element" a middle element of the array and the array b compared to the first half in order to delete an array of smaller and larger elements where the elements where half of the array.
Debug:
The writing test spec in examp found that the result is always obtained with the difference between the original number of 1,2, a good guess when untreated takes an intermediate value. The following analysis of the code:

  1. At the beginning, is not divisible by consideration of the characteristics, the figure should be changed to the red box statements = halfLen (m + n-+. 1) / 2;
    Here Insert Picture Description
    2. from 0 array in accordance with the standard properties divisible, A is median should be subscript I = int (iMin iMax +) / 2;
    3. Analyzing the left if the maximum number of parity according to the median of the combined array c. You should use the length c modulo 2.
    Here will Here Insert Picture Descriptioninstead Here Insert Picture DescriptionRemoveComments.Java

  2. IDEA static checking and found grammatical errors Here Insert Picture Description, and modify it asHere Insert Picture Description

  3. Run-time error occurs array bounds, an error is found:
    Here Insert Picture Description
    to change itHere Insert Picture Description

  4. When the addition was found during single source [i] inBlock condition setting is incorrect and should be replacedHere Insert Picture Description

  5. Continue to test, there will be found in the output character '/', analyzing the code, when read, "find /" the ' ' symbol when, inBlock has been revised to false, will read 'when /' to default to the need to read character, therefore, increase i ++; skipped the characterHere Insert Picture Description

  6. Continue testing, it was found, the program will read to comment "//" means, so increasing the screening of "//" annotation functionHere Insert Picture Description

Directly out of the loop, add the current row has been read part of the list.

TopVotedCandidate.java

According to the IDEA static checking, syntax error is found in the following figure:
Here Insert Picture Description

The first written test spec, in this experiment, I made exactly the same as the original test cases.
6 performed by the test array q:
* tickets 1.A, B did not vote
* 2.A, B have ticket, and A <B
* 3.A, B have ticket, and A = B, B votes obtained recently (two examples)
* 4.A, B have ticket, and A = B, A to obtain tickets recently
* 5.A, B have a ticket, and A> B
discovery into an infinite loop. Suspected dichotomy boundary is not handled correctly during use, then were examined according to the idea, the following error found.
1. easy to know, count for each candidate is counting, it was found, and no increase in C, modified as follows:
Here Insert Picture Description
2. Then, when using the binary method was found to i A.get (i), when A.get (mi) .get (0) .time too small and when lo = hi-1, due to the characteristics divisible, so lo not increase, here caught in an infinite loop. Below
Here Insert Picture Description
the lo = mi; = mi The +. 1 to LO;
3. apparent from judgment condition if (A.get (mi) .get ( 0) .time <= t), when A.get (mi) .get (0) .time equal to t, the query section is still right, the last known A.get (lo) .get (0) .time t should be greater than a; and A.get (lo) .get (0 ) .time is A.get (lo) in the minimum time, the request must be in the decomposition of votes is greater than the smallest integer i, t A.get (i) in the sequence.
It should be looking for (lo-1) in the sequence A.get.
The figure above int i = lo; instead int i = lo-1;
4. Since the point of time of the query is also counted the votes cast, so that the lo is A.get (i) .get (lo) is the smallest integer greater than t, t is not greater than or equal, where the lower figures condition red frame to A.get (i) .get (mi) .time <= t
Here Insert Picture Description

5. Finally, j is emulated, obviously, lo> = 0, the last sentence has no meaning, but considering the A.get (i) .get (0) .time may be less than equal to the maximum time t, at this time there are lo- 1 <0 understand that this is an extreme process conditions, to the original statement
int j = Math.max (lo-1 , 0);

3.6.3 How to fix errors

3.6.2 Details are
modified as follows:
FindMedianSortedArrays.java
1. The halfLen = (m + n) / 2; to = halfLen (m + n-+. 1) / 2;
2. the subscripts A request statement int array median i = (iMin + iMax + 1 ) / 2; to I = int (iMin iMax +) / 2;
3. after the required conditions merge array parity is determined if ((m + n +1) % 2 == 1) to if ((m + n)% 2 == 1)

Inter edit details RemoveComments.Java part of 3.6.2 RemoveComments.Java

TopVotedCandidate.java

  1. Here Insert Picture DescriptionChangedHere Insert Picture Description

2. Here Insert Picture DescriptionchangedHere Insert Picture Description

  1. Median too small for the movement, the lo = mi; to lo = mi + 1;
    4. Select the A.get (i), int i = lo; instead int i = lo-1;
    5. the trimming range, condition determination A.get (i) .get (mi) .time <t to A.get (i) .get (mi) .time <= t
    boundary when seeking 6. j processing int j = Math.max (lo, 0 ); instead int j = Math.max (lo-1 , 0);

Guess you like

Origin blog.csdn.net/lll_90/article/details/92234523