1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-26 11:27:38 +00:00

Refactor and add tests for TextBuffer (#738)

This commit is contained in:
Ronan Hanley
2021-03-16 21:19:54 +00:00
committed by Jummit
parent a28e7e2db3
commit 8984ebcf80
2 changed files with 202 additions and 136 deletions

View File

@@ -3,168 +3,84 @@
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission. * Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com * Send enquiries to dratcliffe@gmail.com
*/ */
package dan200.computercraft.core.terminal; package dan200.computercraft.core.terminal;
public class TextBuffer { public class TextBuffer
private final char[] m_text; {
private final char[] text;
public TextBuffer(char c, int length) { public TextBuffer( char c, int length )
this.m_text = new char[length]; {
for (int i = 0; i < length; i++) { text = new char[length];
this.m_text[i] = c; this.fill( c );
}
} }
public TextBuffer(String text) { public TextBuffer( String text )
this(text, 1); {
this.text = text.toCharArray();
} }
public TextBuffer(String text, int repetitions) { public int length()
int textLength = text.length(); {
this.m_text = new char[textLength * repetitions]; return text.length;
for (int i = 0; i < repetitions; i++) {
for (int j = 0; j < textLength; j++) {
this.m_text[j + i * textLength] = text.charAt(j);
}
}
} }
public TextBuffer(TextBuffer text) { public void write( String text )
this(text, 1); {
write( text, 0 );
} }
public TextBuffer(TextBuffer text, int repetitions) { public void write( String text, int start )
int textLength = text.length(); {
this.m_text = new char[textLength * repetitions];
for (int i = 0; i < repetitions; i++) {
for (int j = 0; j < textLength; j++) {
this.m_text[j + i * textLength] = text.charAt(j);
}
}
}
public int length() {
return this.m_text.length;
}
public char charAt(int i) {
return this.m_text[i];
}
public String read() {
return this.read(0, this.m_text.length);
}
public String read(int start, int end) {
start = Math.max(start, 0);
end = Math.min(end, this.m_text.length);
int textLength = Math.max(end - start, 0);
return new String(this.m_text, start, textLength);
}
public String read(int start) {
return this.read(start, this.m_text.length);
}
public void write(String text) {
this.write(text, 0, text.length());
}
public void write(String text, int start, int end) {
int pos = start; int pos = start;
start = Math.max(start, 0); start = Math.max( start, 0 );
end = Math.min(end, pos + text.length()); int end = Math.min( start + text.length(), pos + text.length() );
end = Math.min(end, this.m_text.length); end = Math.min( end, this.text.length );
for (int i = start; i < end; i++) { for( int i = start; i < end; i++ )
this.m_text[i] = text.charAt(i - pos); {
this.text[i] = text.charAt( i - pos );
} }
} }
public void write(String text, int start) { public void write( TextBuffer text )
this.write(text, start, start + text.length()); {
} int end = Math.min( text.length(), this.text.length );
for( int i = 0; i < end; i++ )
public void write(TextBuffer text) { {
this.write(text, 0, text.length()); this.text[i] = text.charAt( i );
}
public void write(TextBuffer text, int start, int end) {
int pos = start;
start = Math.max(start, 0);
end = Math.min(end, pos + text.length());
end = Math.min(end, this.m_text.length);
for (int i = start; i < end; i++) {
this.m_text[i] = text.charAt(i - pos);
} }
} }
public void write(TextBuffer text, int start) { public void fill( char c )
this.write(text, start, start + text.length()); {
fill( c, 0, text.length );
} }
public void fill(char c) { public void fill( char c, int start, int end )
this.fill(c, 0, this.m_text.length); {
} start = Math.max( start, 0 );
end = Math.min( end, text.length );
public void fill(char c, int start, int end) { for( int i = start; i < end; i++ )
start = Math.max(start, 0); {
end = Math.min(end, this.m_text.length); text[i] = c;
for (int i = start; i < end; i++) {
this.m_text[i] = c;
} }
} }
public void fill(char c, int start) { public char charAt( int i )
this.fill(c, start, this.m_text.length); {
return text[i];
} }
public void fill(String text) { public void setChar( int i, char c )
this.fill(text, 0, this.m_text.length); {
} if( i >= 0 && i < text.length )
{
public void fill(String text, int start, int end) { text[i] = c;
int pos = start;
start = Math.max(start, 0);
end = Math.min(end, this.m_text.length);
int textLength = text.length();
for (int i = start; i < end; i++) {
this.m_text[i] = text.charAt((i - pos) % textLength);
} }
} }
public void fill(String text, int start) { public String toString()
this.fill(text, start, this.m_text.length); {
return new String( text );
} }
}
public void fill(TextBuffer text) {
this.fill(text, 0, this.m_text.length);
}
public void fill(TextBuffer text, int start, int end) {
int pos = start;
start = Math.max(start, 0);
end = Math.min(end, this.m_text.length);
int textLength = text.length();
for (int i = start; i < end; i++) {
this.m_text[i] = text.charAt((i - pos) % textLength);
}
}
public void fill(TextBuffer text, int start) {
this.fill(text, start, this.m_text.length);
}
public void setChar(int i, char c) {
if (i >= 0 && i < this.m_text.length) {
this.m_text[i] = c;
}
}
@Override
public String toString() {
return new String(this.m_text);
}
}

View File

@@ -0,0 +1,150 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.terminal;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class TextBufferTest
{
@Test
void testStringConstructor()
{
TextBuffer textBuffer = new TextBuffer( "test" );
assertEquals( "test", textBuffer.toString() );
}
@Test
void testCharRepetitionConstructor()
{
TextBuffer textBuffer = new TextBuffer( 'a', 5 );
assertEquals( "aaaaa", textBuffer.toString() );
}
@Test
void testLength()
{
TextBuffer textBuffer = new TextBuffer( "test" );
assertEquals( 4, textBuffer.length() );
}
@Test
void testWrite()
{
TextBuffer textBuffer = new TextBuffer( ' ', 4 );
textBuffer.write( "test" );
assertEquals( "test", textBuffer.toString() );
}
@Test
void testWriteTextBuffer()
{
TextBuffer source = new TextBuffer( "test" );
TextBuffer target = new TextBuffer( " " );
target.write( source );
assertEquals( "test", target.toString() );
}
@Test
void testWriteFromPos()
{
TextBuffer textBuffer = new TextBuffer( "test" );
textBuffer.write( "il", 1 );
assertEquals( "tilt", textBuffer.toString() );
}
@Test
void testWriteOutOfBounds()
{
TextBuffer textBuffer = new TextBuffer( "test" );
textBuffer.write( "abcdefghijklmnop", -5 );
assertEquals( "fghi", textBuffer.toString() );
}
@Test
void testWriteOutOfBounds2()
{
TextBuffer textBuffer = new TextBuffer( " " );
textBuffer.write( "Hello, world!", -3 );
assertEquals( "lo, world! ", textBuffer.toString() );
}
@Test
void testFill()
{
TextBuffer textBuffer = new TextBuffer( "test" );
textBuffer.fill( 'c' );
assertEquals( "cccc", textBuffer.toString() );
}
@Test
void testFillSubstring()
{
TextBuffer textBuffer = new TextBuffer( "test" );
textBuffer.fill( 'c', 1, 3 );
assertEquals( "tcct", textBuffer.toString() );
}
@Test
void testFillOutOfBounds()
{
TextBuffer textBuffer = new TextBuffer( "test" );
textBuffer.fill( 'c', -5, 5 );
assertEquals( "cccc", textBuffer.toString() );
}
@Test
void testCharAt()
{
TextBuffer textBuffer = new TextBuffer( "test" );
assertEquals( 'e', textBuffer.charAt( 1 ) );
}
@Test
void testSetChar()
{
TextBuffer textBuffer = new TextBuffer( "test" );
textBuffer.setChar( 2, 'n' );
assertEquals( "tent", textBuffer.toString() );
}
@Test
void testSetCharWithNegativeIndex()
{
TextBuffer textBuffer = new TextBuffer( "test" );
textBuffer.setChar( -5, 'n' );
assertEquals( "test", textBuffer.toString(), "Buffer should not change after setting char with negative index." );
}
@Test
void testSetCharWithIndexBeyondBufferEnd()
{
TextBuffer textBuffer = new TextBuffer( "test" );
textBuffer.setChar( 10, 'n' );
assertEquals( "test", textBuffer.toString(), "Buffer should not change after setting char beyond buffer end." );
}
@Test
void testMultipleOperations()
{
TextBuffer textBuffer = new TextBuffer( ' ', 5 );
textBuffer.setChar( 0, 'H' );
textBuffer.setChar( 1, 'e' );
textBuffer.setChar( 2, 'l' );
textBuffer.write( "lo", 3 );
assertEquals( "Hello", textBuffer.toString(), "TextBuffer failed to persist over multiple operations." );
}
@Test
void testEmptyBuffer()
{
TextBuffer textBuffer = new TextBuffer( "" );
// exception on writing to empty buffer would fail the test
textBuffer.write( "test" );
assertEquals( "", textBuffer.toString() );
}
}