From 89e4bb84eef5af58ef5bdf8f749fe876a6777e1f Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Thu, 19 May 2022 08:07:46 +0200 Subject: [PATCH] Solved task 3 --- .../JSONScannerTest2.java | 88 +++++++++++++++++++ .../phase_03/project_phase03_tasks/Makefile | 2 +- .../Solution_Phase03_MichaelChen.tex | 8 +- 3 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 project_task_sheets/phase_03/project_phase03_tasks/JSONScannerTest2.java diff --git a/project_task_sheets/phase_03/project_phase03_tasks/JSONScannerTest2.java b/project_task_sheets/phase_03/project_phase03_tasks/JSONScannerTest2.java new file mode 100644 index 0000000..ff220ca --- /dev/null +++ b/project_task_sheets/phase_03/project_phase03_tasks/JSONScannerTest2.java @@ -0,0 +1,88 @@ +package com.alibaba.fastjson.parser; + +import com.alibaba.fastjson.JSONException; +import org.junit.Assert; +import org.junit.Test; + + +public class JSONScannerTest2 { + @Test + public void stringUtf8BomTest() throws Throwable { + JSONScanner scanner = new JSONScanner("\uFEFF,"); + // After construction the UTF-8 BOM should be safely skipped + scanner.nextToken(); + Assert.assertEquals(JSONToken.COMMA, scanner.token()); + } + + @Test + public void stringHasSpecialTest() throws Throwable { + JSONScanner scanner = new JSONScanner("\"\\b\\n\\t\\\"\\uABED\""); + // After construction the UTF-8 BOM should be safely skipped + scanner.nextToken(); + Assert.assertEquals(JSONToken.LITERAL_STRING, scanner.token()); + Assert.assertEquals("\b\n\t\"\uABED", scanner.stringVal()); + } + + @Test + public void stringWithIso8601Date() throws Throwable { + JSONScanner scanner = new JSONScanner("2012-04-23T18:25:43.511Z"); + // After construction the UTF-8 BOM should be safely skipped + Assert.assertTrue(scanner.scanISO8601DateIfMatch()); + } + + @Test + public void scanSingleComment() throws Throwable { + JSONScanner scanner = new JSONScanner("//dsajfklsjfk"); + // After construction the UTF-8 BOM should be safely skipped + scanner.nextToken(); + Assert.assertTrue(scanner.isEOF()); + } + + @Test + public void scanMultiComment() throws Throwable { + JSONScanner scanner = new JSONScanner("/*dsajfklsjfk*/"); + // After construction the UTF-8 BOM should be safely skipped + scanner.nextToken(); + Assert.assertTrue(scanner.isEOF()); + } + + @Test + public void scannerSkipWhitespace() throws Throwable { + JSONScanner scanner = new JSONScanner("\n\t\t \r\b\f232"); + scanner.nextToken(); + Assert.assertEquals(JSONToken.LITERAL_INT, scanner.token()); + Assert.assertEquals(232, scanner.intValue()); + } + + @Test + public void inputTokenTest_Null() throws Throwable { + JSONScanner scanner = new JSONScanner("null"); + // After construction the UTF-8 BOM should be safely skipped + scanner.nextToken(); + Assert.assertEquals(JSONToken.NULL, scanner.token()); + } + + @Test + public void inputTokenTest_Undefined() throws Throwable { + JSONScanner scanner = new JSONScanner("undefined"); + // After construction the UTF-8 BOM should be safely skipped + scanner.nextToken(); + Assert.assertEquals(JSONToken.UNDEFINED, scanner.token()); + } + + @Test + public void inputTokenTest_Undefined() throws Throwable { + JSONScanner scanner = new JSONScanner("undefined"); + // After construction the UTF-8 BOM should be safely skipped + scanner.nextToken(); + Assert.assertEquals(JSONToken.UNDEFINED, scanner.token()); + } + + @Test + public void inputTokenTest_ExplPosInt() throws Throwable { + JSONScanner scanner = new JSONScanner("+7531"); // explicit positive with sign + scanner.nextToken(); + Assert.assertEquals(JSONToken.LITERAL_INT, scanner.token()); + Assert.assertEquals(7531, scanner.intValue()); + } +} diff --git a/project_task_sheets/phase_03/project_phase03_tasks/Makefile b/project_task_sheets/phase_03/project_phase03_tasks/Makefile index eeda579..ce3b251 100644 --- a/project_task_sheets/phase_03/project_phase03_tasks/Makefile +++ b/project_task_sheets/phase_03/project_phase03_tasks/Makefile @@ -4,7 +4,7 @@ name = MichaelChen solutionname = Solution_Phase$(phase)_$(name) target = $(solutionname)_V$(version).zip -package = $(solutionname).pdf +package = $(solutionname).pdf HashMap.java JSONScannerTest2.java latexmkflags = .PHONY : all dev diff --git a/project_task_sheets/phase_03/project_phase03_tasks/Solution_Phase03_MichaelChen.tex b/project_task_sheets/phase_03/project_phase03_tasks/Solution_Phase03_MichaelChen.tex index 4045dd1..3137c9b 100644 --- a/project_task_sheets/phase_03/project_phase03_tasks/Solution_Phase03_MichaelChen.tex +++ b/project_task_sheets/phase_03/project_phase03_tasks/Solution_Phase03_MichaelChen.tex @@ -210,7 +210,7 @@ \begin{enumerate}[topsep=0pt, leftmargin=*] \item Measure the coverage of your given project test suite (which includes the existing test suite as well as the tests that you created in previous project phases) by three graph coverage criteria which you can freely choose. Describe each individual result in 2-3 sentences. \begin{answer} - [TODO: Add answer here] + My test suite for the \texttt{JSONScanner} class so far covers 1\% of the class and 5\% of the \texttt{JsonLexerBase} base class, that does most of the work for basic JSON strings, in terms of instruction coverage. The fact that roughly similar coverage values are achieved with branch coverage using my small test suite is explained by the fact that most instructions that i have not covered are not covered because of missing tests that handle edge case (in this case mostly different JSON token alternatives) branches. My method coverage currently is at about 14\% and 12\% for the base class. Similar to the above tests, I am missing lots of tests for function calls that retrieve specific token values, however the coverage is higher than the branch coverage because the token alternatives are handled within the lexing method call, thus the method coverage is higher. \end{answer} \item Extend the test suite with own tests, which have to fullfil \textbf{one} of the following criteria: @@ -219,11 +219,7 @@ \item Reveal a new bug in the software project (describe the bug, its context, and a potential fix in detail) \end{enumerate} \begin{answer} - [TODO: Add answer here] - - \begin{lstlisting}[language=Java,belowskip=-0.8\baselineskip] -/* Add code here */ - \end{lstlisting} + See file \texttt{JSONScannerTest2.java}. Using this test suite I increased my test instruction coverage from 1\% to more than 9\% and my branch coverage from .5\% to almost 5\% (factor 10). I almost doubled the method coverage, which matches the test suite, because I roughly doubled the amount of different methods I called for different edge cases. \end{answer} \end{enumerate}