From 52a777598ac5313a022b1236d39b1e4be30407fe Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Wed, 15 Jun 2022 23:31:28 +0200 Subject: [PATCH] Added task 2 --- .../Solution_Phase04_MichaelChen.tex | 61 ++++++++++++++++--- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/project_task_sheets/phase_04/project_phase04_tasks/Solution_Phase04_MichaelChen.tex b/project_task_sheets/phase_04/project_phase04_tasks/Solution_Phase04_MichaelChen.tex index 28f268a..012f4d9 100644 --- a/project_task_sheets/phase_04/project_phase04_tasks/Solution_Phase04_MichaelChen.tex +++ b/project_task_sheets/phase_04/project_phase04_tasks/Solution_Phase04_MichaelChen.tex @@ -126,42 +126,85 @@ \begin{enumerate}[topsep=0pt, leftmargin=*] \item Name and briefly describe the specific lines that (directly or indirectly) influence the boolean results of the predicates and clauses in line \textit{10}, \textit{12}, \textit{17}, \textit{20}, \textit{22}, \textit{30}, and \textit{32}. \begin{answer} - [TODO: Add answer here] + In this table I listed all dependencies the predicates. + \begin{center} + \begin{tabular}{r|c|c} + Line & Directly & Indirectly \\ \hline \hline + \textit{10} & \texttt{s} \\ + \textit{12} & \texttt{start}, \texttt{end}, \texttt{s} \\ + \textit{17} & \texttt{len} & \texttt{start}, \texttt{end} \\ + \textit{20} & \texttt{value}, \texttt{newCount} & \texttt{count}, \texttt{len}, (\texttt{start}, \texttt{end}) \\ + \textit{22} & \texttt{i}, \texttt{end} & \texttt{start} \\ + \textit{30} & \texttt{newCapacity} & \texttt{value} \\ + \textit{32} & \texttt{minimumCapacity}, \texttt{newCapacity} & \texttt{value} + \end{tabular} + \end{center} \end{answer} \item To which extent are the predicates influenced by internal states? Which conclusions do you draw for the application of logic coverage criteria to source code in general? \begin{answer} - [TODO: Add answer here] + The predicates in this examples are not influenced by the internal state of the object that much, except for the buffer size check of the character array. The internal state of the function influences the variables a lot, because the input parameters are used in operations that declare new internal variables. Finding variables for logic coverage from source code is a rather easy task, however finding input parameters to cover the test requirements is a very difficult task and sometimes not even possible. \end{answer} \item Provide a set of tests that cover the following predicates and clauses: \begin{enumerate} \item \texttt{(start < 0) || (end < 0) || (start > end) || (end > s.length())} (l.12) based on the \textit{Correlated Active Clause Coverage} (CACC) criterion \begin{answer} - [TODO: Add answer here] - \begin{lstlisting}[language=Java,belowskip=-0.8\baselineskip] - /* Add code here */ +@Test +public void caccBoundCheck() { + String s = "01234"; + // base case + new StringBuilder(4).append(s, 0, s.length()); + // start < 0 + new StringBuilder(4).append(s, -1, s.length()); + // start > end + new StringBuilder(4).append(s, 6, s.length()); + // end > s.length() + new StringBuilder(4).append(s, 0, s.length() + 1); + // end < 0 + new StringBuilder(4).append(s, -2, -1); +} \end{lstlisting} % or: \lstinputlisting[language=Java,belowskip=-0.8\baselineskip]{file_name.java} \end{answer} \item \texttt{newCount > value.length} (l.20) for an evaluation to both \textit{true} and \textit{false} \begin{answer} - [TODO: Add answer here] + This predicate determines if the buffer needs to be expanded. To achieve this we can input more and less than the current capacity. \begin{lstlisting}[language=Java,belowskip=-0.8\baselineskip] - /* Add code here */ +@Test +public void requireExpansion() { + String s = "01234"; + // Predicate is true, so buffer will expand + new StringBuilder(4).append(s, 0, s.length()); +} + +@Test +public void requireExpansion() { + String s = "01234"; + // Predicate is false, so buffer will not expand + new StringBuilder(5).append(s, 0, s.length()); +} \end{lstlisting} % or: \lstinputlisting[language=Java,belowskip=-0.8\baselineskip]{file_name.java} \end{answer} \item \texttt{newCapacity < 0} (l.30) and \texttt{minimumCapacity > newCapacity} (l.32) for an evaluation to \textit{true}. Which requirement(s) does \texttt{value} have to meet so that \texttt{newCapacity < 0} will be satisfied? \begin{answer} - [TODO: Add answer here] + In order for \texttt{newCapacity < 0} to be satisfied, the monotonously increasing value of \texttt{value.length} (buffer size) has to overflow the integer size and become negative. The lowest value that could happen would be $2^30 - 1 = 1073741823$, which would allocate more than 1 gigabyte of memory for the buffer. \begin{lstlisting}[language=Java,belowskip=-0.8\baselineskip] - /* Add code here */ +@Test +public void newCapOverflow() { + new StringBuilder(1073741823).expandCapacity(0); +} + +@Test +public void lowerThanMin() { + new StringBuilder(2).expandCapacity(7); +} \end{lstlisting} % or: \lstinputlisting[language=Java,belowskip=-0.8\baselineskip]{file_name.java} \end{answer}