Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

Below is Lexer.java, Token.java, StringHandler.java, and Main.java. I need help in writing test cases. Please write unit tests for lexer.java and make sure to show the full code with the screenshot of the output. Make sure to include the test cases as well. 

Lexer.java

import java.util.LinkedList;
 
public class Lexer {
private StringHandler stringHandler;
    private int lineNumber;
    private int charPosition;
    
 
 
    public Lexer(String input) {
        stringHandler = new StringHandler(input);
        lineNumber = 1;
        charPosition = 0;
    }
 
    public LinkedList<Token> lex() {
        LinkedList<Token> tokens = new LinkedList<>();
        
     
        while (!stringHandler.isDone()) {
            char c = stringHandler.peek(0);
 
            if (c == ' ' || c == '\t') {
                stringHandler.swallow(1);
                charPosition++;
            } else if (c == '\n') {
                tokens.add(new Token(TokenType.SEPARATOR, lineNumber, charPosition));
                stringHandler.swallow(1);
                lineNumber++;
                charPosition = 0;
               
            } else if (Character.isLetter(c)) {
                tokens.add(processWord());
            } else if (Character.isDigit(c)) {
                tokens.add(processNumber());
            } else {
                throw new RuntimeException("Unrecognized character: " + c);
            }
        }
 
        return tokens;
    }
 
    private Token processWord() {
        StringBuilder value = new StringBuilder();
        while (!stringHandler.isDone() && (Character.isLetterOrDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '_' || stringHandler.peek(0) == ',')) {
            value.append(stringHandler.getChar());
            charPosition++;
        }
        return new Token(TokenType.WORD, value.toString(), lineNumber, charPosition - value.length());
    }
 
   
    private Token processNumber() {
        StringBuilder value = new StringBuilder();
        while (!stringHandler.isDone() && (Character.isDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '.')) {
            value.append(stringHandler.getChar());
            charPosition++;
        }
        return new Token(TokenType.NUMBER, value.toString(), lineNumber, charPosition - value.length());
    }
}
 
Token.java
 
enum TokenType {
    WORD, NUMBER, SEPARATOR
}
public class Token {
private TokenType type;
    private String value;
    private int lineNumber;
    private int charPosition;
 
 
    public Token(TokenType type, int lineNumber, int charPosition) {
        this.type = type;
        this.lineNumber = lineNumber;
        this.charPosition = charPosition;
    }
 
 
    public Token(TokenType type, String value, int lineNumber, int charPosition) {
        this(type, lineNumber, charPosition);
        this.value = value;
    }
 
    public String toString() {
        if (value == null) {
            return type.toString();
        } else {
            return type.toString() + "(" + value + ")";
        }
    }
}
 
 
 
 
Main.java
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
 
public class Main {
 
public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Usage: java Main <filename>");
            return;
        }
 
   
        try {
            String content = new String(Files.readAllBytes(Paths.get(args[0])));
            Lexer lexer = new Lexer(content);
            LinkedList<Token> tokens = lexer.lex();
 
            for (Token token : tokens) {
                System.out.println(token);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
 
StringHandler.java
 
public class StringHandler {
 
 
private String document;
    private int index;
    
 
    public StringHandler(String document) {
        this.document = document;
        this.index = 0;
    }
 
 
    public char peek(int i) {
        return document.charAt(index + i);
    }
 
    public String peekString(int i) {
        return document.substring(index, index + i);
    }
    
 
    public char getChar() {
        char c = document.charAt(index);
        index++;
        return c;
    }
 
 
    public void swallow(int i) {
        index += i;
    }
 
 
    public boolean isDone() {
        return index >= document.length();
    }
 
 
    public String remainder() {
        return document.substring(index);
    }
 
 
Expert Solution
Check Mark
Step 1: To write unit tests for the Lexer class in your code, you can use a testing framework like JUnit

Example of how you can write test cases for the Lexer class:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class LexerTest {

    @Test
    public void testLexSimpleInput() {
        String input = "Hello, World!\n123";
        Lexer lexer = new Lexer(input);
        LinkedList<Token> tokens = lexer.lex();

        // Check the first token
        Token token = tokens.removeFirst();
        assertEquals(TokenType.WORD, token.getType());
        assertEquals("Hello", token.getValue());
        assertEquals(1, token.getLineNumber());
        assertEquals(0, token.getCharPosition());

        // Check the second token
        token = tokens.removeFirst();
        assertEquals(TokenType.SEPARATOR, token.getType());
        assertEquals("\n", token.getValue());
        assertEquals(1, token.getLineNumber());
        assertEquals(5, token.getCharPosition());

        // Check the third token
        token = tokens.removeFirst();
        assertEquals(TokenType.WORD, token.getType());
        assertEquals("World", token.getValue());
        assertEquals(2, token.getLineNumber());
        assertEquals(0, token.getCharPosition());

        // Check the fourth token
        token = tokens.removeFirst();
        assertEquals(TokenType.WORD, token.getType());
        assertEquals("123", token.getValue());
        assertEquals(2, token.getLineNumber());
        assertEquals(6, token.getCharPosition());

        // Check that all tokens have been processed
        assertTrue(tokens.isEmpty());
    }

    @Test
    public void testLexEmptyInput() {
        String input = "";
        Lexer lexer = new Lexer(input);
        LinkedList<Token> tokens = lexer.lex();

        // Check that no tokens were generated for an empty input
        assertTrue(tokens.isEmpty());
    }

    @Test
    public void testLexInvalidCharacter() {
        String input = "Hello @ World!";
        Lexer lexer = new Lexer(input);

        // Check that an exception is thrown for an unrecognized character
        assertThrows(RuntimeException.class, () -> lexer.lex());
    }
}

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education