Original author: Pierre Sarrazin. Source: http://sarrazip.com/dev/ebitor-0.1.1.tar.gz Archive SHA-256: 6911a9d2ee80925622f1d82db1b77710a1f896865e63d29a04dc16489b150e83
819 lines
24 KiB
C
819 lines
24 KiB
C
/* ebittest.c
|
|
|
|
This file is in the public domain.
|
|
*/
|
|
|
|
#undef NDEBUG /* enable asserts */
|
|
|
|
#include "sg4term.h"
|
|
#include "Ebitor_private.h"
|
|
|
|
|
|
///////////////////////////////////////////////////////////////
|
|
|
|
|
|
static const char *predefinedKeyPresses = "";
|
|
|
|
|
|
static byte
|
|
readPredefinedKeyOrKeyboard(void)
|
|
{
|
|
if (*predefinedKeyPresses != '\0')
|
|
return *predefinedKeyPresses++;
|
|
return 0; // not supposed to be reached
|
|
}
|
|
|
|
|
|
static const ebitor_Window testWindow = { 32, 15, 0, 0, 0, 0 };
|
|
|
|
|
|
static size_t currentTestIndex;
|
|
|
|
|
|
static byte
|
|
inkey1(void *cookie)
|
|
{
|
|
byte code = readPredefinedKeyOrKeyboard();
|
|
return code;
|
|
}
|
|
|
|
|
|
// Declarations of private functions used by the self tests.
|
|
//
|
|
// char Ebitor_isRegionVisible(size_t regionOffset, size_t regionLength, byte cursorCol, byte cursorRow,
|
|
// size_t *homeOffset, size_t *firstOffScreenOffset);
|
|
// size_t Ebitor_countRowsToScrollUp(size_t logicalOffsetToShow, size_t firstOffScreenOffset);
|
|
// size_t Ebitor_countRowsToScrollDown(size_t logicalOffsetToShow, size_t homeOffset, size_t *logicalOffsetAtStartOfTermRow);
|
|
|
|
|
|
static char textBuffer[600];
|
|
|
|
|
|
static const char *initContents = ""
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ01234\r"
|
|
"L0\r"
|
|
"L1\r"
|
|
"L2\r"
|
|
"L3\r"
|
|
"L4\r"
|
|
"L5\r"
|
|
"L6\r"
|
|
"L7\r"
|
|
"L8\r"
|
|
"L9\r"
|
|
"L10\r"
|
|
"L11\r"
|
|
"L12\r"
|
|
"L13\r"
|
|
"L14ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz\r"
|
|
"L15\r"
|
|
"L16\r"
|
|
"L17\r"
|
|
;
|
|
|
|
|
|
// Uses initContents. Fills textBuffer. Calls Ebitor_setContentLength().
|
|
//
|
|
static void
|
|
setRepeatedTestContent(size_t numRepetitions)
|
|
{
|
|
size_t initContentLength = strlen(initContents);
|
|
assert(initContentLength < sizeof(textBuffer) / numRepetitions);
|
|
char *dest = textBuffer;
|
|
for (size_t i = numRepetitions; i--; dest += initContentLength)
|
|
memcpy(dest, initContents, initContentLength);
|
|
Ebitor_setContentLength(initContentLength * numRepetitions);
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_setContentLength
|
|
//
|
|
static void
|
|
setTestContent(void)
|
|
{
|
|
setRepeatedTestContent(1);
|
|
}
|
|
|
|
|
|
static void
|
|
test_scrollWindowUp(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
setTestContent();
|
|
|
|
// Show the text.
|
|
predefinedKeyPresses = "\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
char text[3];
|
|
sg4term_getChars(0, 14, text, sizeof(text));
|
|
assert(memcmp(text, "L13", sizeof(text)) == 0);
|
|
|
|
sg4term_scrollWindowUp(1);
|
|
|
|
sg4term_getChars(0, 13, text, sizeof(text));
|
|
assert(memcmp(text, "L13", sizeof(text)) == 0); // L13 moved one row up
|
|
sg4term_getChars(0, 14, text, sizeof(text));
|
|
assert(memcmp(text, " ", sizeof(text)) == 0); // bottom row cleared
|
|
}
|
|
|
|
|
|
static void
|
|
test_scrollWindowDown(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
setTestContent();
|
|
|
|
// Show the text.
|
|
predefinedKeyPresses = "\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
char text[3];
|
|
sg4term_getChars(0, 0, text, sizeof(text));
|
|
assert(memcmp(text, "ABC", sizeof(text)) == 0);
|
|
|
|
sg4term_scrollWindowDown(1);
|
|
|
|
sg4term_getChars(0, 1, text, sizeof(text));
|
|
assert(memcmp(text, "ABC", sizeof(text)) == 0); // ABC moved one row down
|
|
sg4term_getChars(0, 0, text, sizeof(text));
|
|
assert(memcmp(text, " ", sizeof(text)) == 0); // top row cleared
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_isRegionVisible
|
|
//: Tested: Ebitor_countRowsToScrollUp
|
|
//
|
|
static void
|
|
test_countRowsToScrollUp(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
setTestContent();
|
|
|
|
size_t homeOffset = 0xFEDC, firstOffScreenOffset = 0xEDCB; // dummy values to be overwritten
|
|
|
|
// Check that a substring near the end of the text is not displayed.
|
|
//
|
|
char *p = strstr(editor->bufferBegin, "L16\r");
|
|
assert(p);
|
|
size_t regionOffset = p - editor->bufferBegin;
|
|
printf("[regionOffset=%u]", regionOffset);
|
|
char code = Ebitor_isRegionVisible(regionOffset, 3, editor->savedCursorCol, editor->savedCursorRow,
|
|
&homeOffset, &firstOffScreenOffset);
|
|
printf("[C=%d,H=%u,F=%u]", code, homeOffset, firstOffScreenOffset);
|
|
assert(code == 1); // substring comes after the displayed text
|
|
assert(homeOffset == 0); // top of screen shows top of text
|
|
size_t expectedFirstOffScreenOffset = strstr(editor->bufferBegin, "L14ABC") - editor->bufferBegin;
|
|
assert(firstOffScreenOffset == expectedFirstOffScreenOffset);
|
|
|
|
// Test that a character not at the start of a terminal row can be scrolled up to.
|
|
//
|
|
size_t logicalOffsetToShow = regionOffset + 1;
|
|
size_t numTermRows = Ebitor_countRowsToScrollUp(logicalOffsetToShow, firstOffScreenOffset);
|
|
printf("[N=%u]", numTermRows);
|
|
assert(numTermRows == 5);
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_isRegionVisible
|
|
//: Tested: Ebitor_countRowsToScrollDown
|
|
//
|
|
static void
|
|
test_countRowsToScrollDown(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
setTestContent();
|
|
|
|
// Show the text and move the cursor below the initially displayed text.
|
|
predefinedKeyPresses = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
// Cursor is at bottom left of editable area.
|
|
assert(editor->savedCursorCol == 0);
|
|
assert(editor->savedCursorRow == 14);
|
|
|
|
// Logical cursor is at end of text.
|
|
assert(editor->cursorOffset == editor->bufferFilledSize);
|
|
|
|
size_t homeOffset = 0, firstOffScreenOffset = 0xFEDC;
|
|
|
|
// Check that a substring near the beginning of the text is not displayed.
|
|
//
|
|
char *p = strstr(editor->bufferBegin, "L2\r");
|
|
assert(p);
|
|
size_t regionOffset = p - editor->bufferBegin;
|
|
printf("[regionOffset=%u]", regionOffset);
|
|
char code = Ebitor_isRegionVisible(regionOffset, 2, editor->savedCursorCol, editor->savedCursorRow,
|
|
&homeOffset, &firstOffScreenOffset);
|
|
printf("[C=%d,H=%u,F=%u]", code, homeOffset, firstOffScreenOffset);
|
|
assert(code == -1); // substring comes before the displayed text
|
|
assert(homeOffset == strstr(editor->bufferBegin, "L6\r") - editor->bufferBegin);
|
|
assert(firstOffScreenOffset == 0xFEDC); // dummy value not touched
|
|
|
|
size_t logicalOffsetToShow = regionOffset + 1;
|
|
size_t logicalOffsetAtStartOfTermRow = 0;
|
|
size_t numTermRows = Ebitor_countRowsToScrollDown(logicalOffsetToShow, homeOffset, &logicalOffsetAtStartOfTermRow);
|
|
printf("[N=%u,A=%u]", numTermRows, logicalOffsetAtStartOfTermRow);
|
|
assert(numTermRows == 4);
|
|
assert(logicalOffsetAtStartOfTermRow == regionOffset); // b/c regionOffset is at start of line ("L2")
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_scrollToShowLogicalOffset
|
|
//: Tested: Ebitor_getLogicalOffsetAtHome
|
|
//: Tested: Ebitor_isRegionVisible
|
|
//
|
|
static void
|
|
test_scrollToShowLogicalOffset(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
setTestContent();
|
|
|
|
// Initial screen shows top of text.
|
|
// Target a line that is below the screen.
|
|
//
|
|
char *p = strstr(editor->bufferBegin, "L16\r");
|
|
assert(p);
|
|
size_t offsetInLine = 1;
|
|
size_t regionOffset = p - editor->bufferBegin + offsetInLine;
|
|
size_t regionLen = 2;
|
|
//printf("[regionOffset=%u]", regionOffset);
|
|
|
|
// Show the text.
|
|
predefinedKeyPresses = "\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
Ebitor_scrollToShowLogicalOffset(regionOffset, regionLen);
|
|
|
|
byte cursorCol, cursorRow;
|
|
editor->getCursorPos(&cursorCol, &cursorRow);
|
|
//printf("[%u,%u]", cursorCol, cursorRow);
|
|
assert(cursorCol == offsetInLine);
|
|
assert(cursorRow == editor->termHeight - 1); // last window row
|
|
|
|
size_t h = Ebitor_getLogicalOffsetAtHome(cursorCol, cursorRow);
|
|
//printf("[HO=%u]", h);
|
|
assert(h == strstr(editor->bufferBegin, "L4\r") - editor->bufferBegin);
|
|
|
|
size_t homeOffset = 0xFFFF, firstOffScreenOffset = 0xFFFF;
|
|
char code = Ebitor_isRegionVisible(regionOffset, regionLen, cursorCol, cursorRow,
|
|
&homeOffset, &firstOffScreenOffset);
|
|
//printf("[%d,%u,%u]", code, homeOffset, firstOffScreenOffset);
|
|
assert(code == 0); // region now visible
|
|
assert(homeOffset == h);
|
|
assert(firstOffScreenOffset == strstr(editor->bufferBegin, "L17\r") - editor->bufferBegin);
|
|
|
|
// Check that terminal contents got scrolled up.
|
|
char text[3];
|
|
sg4term_getChars(0, 7, text, sizeof(text));
|
|
assert(memcmp(text, "L11", sizeof(text)) == 0);
|
|
|
|
// Now scroll up to a line that is currently above the displayed text.
|
|
//
|
|
regionOffset = strstr(editor->bufferBegin, "L2\r") - editor->bufferBegin + offsetInLine;
|
|
regionLen = 1; // just the "2"
|
|
//printf("[regionOffset=%u]", regionOffset);
|
|
code = Ebitor_isRegionVisible(regionOffset, regionLen, cursorCol, cursorRow,
|
|
&homeOffset, &firstOffScreenOffset);
|
|
//printf("[%d,%u,%u]", code, homeOffset, firstOffScreenOffset);
|
|
assert(code == -1); // "L2" line is above the displayed text
|
|
assert(homeOffset == h);
|
|
assert(firstOffScreenOffset == strstr(editor->bufferBegin, "L17\r") - editor->bufferBegin);
|
|
|
|
Ebitor_scrollToShowLogicalOffset(regionOffset, regionLen);
|
|
|
|
editor->getCursorPos(&cursorCol, &cursorRow);
|
|
//printf("[%u,%u]", cursorCol, cursorRow);
|
|
assert(cursorCol == offsetInLine);
|
|
assert(cursorRow == 0);
|
|
|
|
homeOffset = 0xFFFF, firstOffScreenOffset = 0xFFFF;
|
|
code = Ebitor_isRegionVisible(regionOffset, regionLen, cursorCol, cursorRow,
|
|
&homeOffset, &firstOffScreenOffset);
|
|
//printf("[%d,%u,%u]", code, homeOffset, firstOffScreenOffset);
|
|
assert(code == 0); // region now visible
|
|
assert(homeOffset == regionOffset - offsetInLine);
|
|
assert(firstOffScreenOffset == strstr(editor->bufferBegin, "L15\r") - editor->bufferBegin);
|
|
|
|
// Check that terminal contents got scrolled down.
|
|
sg4term_getChars(0, 7, text, sizeof(text));
|
|
assert(memcmp(text, "L9 ", sizeof(text)) == 0);
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_findStringAndScrollToShow
|
|
//
|
|
static void
|
|
test_findStringAndScrollToShow(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
setTestContent();
|
|
|
|
// Show the text.
|
|
predefinedKeyPresses = "\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
BOOL found;
|
|
size_t foundLogicalOffset;
|
|
|
|
// Search for a string below the displayed text.
|
|
|
|
found = Ebitor_findStringAndScrollToShow(0, "l16", &foundLogicalOffset);
|
|
assert(found);
|
|
|
|
char text[3];
|
|
sg4term_getChars(0, 14, text, sizeof(text));
|
|
assert(memcmp(text, "L16", sizeof(text)) == 0);
|
|
|
|
byte cursorCol, cursorRow;
|
|
editor->getCursorPos(&cursorCol, &cursorRow);
|
|
assert(cursorCol == 0 && cursorRow == 14);
|
|
assert(memcmp(editor->bufferBegin + editor->cursorOffset, "L16\r", 4) == 0);
|
|
|
|
// Search for a string above the displayed text.
|
|
|
|
found = Ebitor_findStringAndScrollToShow(0, "l2", &foundLogicalOffset);
|
|
assert(found);
|
|
|
|
sg4term_getChars(0, 0, text, sizeof(text));
|
|
assert(memcmp(text, "L2 ", sizeof(text)) == 0);
|
|
|
|
editor->getCursorPos(&cursorCol, &cursorRow);
|
|
assert(cursorCol == 0 && cursorRow == 0);
|
|
assert(memcmp(editor->bufferBegin + editor->cursorOffset, "L2\r", 3) == 0);
|
|
|
|
// Search for a string already in the displayed text.
|
|
found = Ebitor_findStringAndScrollToShow(0, "678", &foundLogicalOffset);
|
|
assert(found);
|
|
|
|
sg4term_getChars(3, 13, text, sizeof(text));
|
|
assert(memcmp(text, "678", sizeof(text)) == 0);
|
|
assert(memcmp(editor->bufferBegin + editor->cursorOffset, "678", 3) == 0);
|
|
|
|
// Search for a string that is not in the text.
|
|
|
|
found = Ebitor_findStringAndScrollToShow(0, "ABSENT", &foundLogicalOffset);
|
|
assert(!found);
|
|
|
|
sg4term_getChars(3, 13, text, sizeof(text));
|
|
assert(memcmp(text, "678", sizeof(text)) == 0);
|
|
|
|
editor->getCursorPos(&cursorCol, &cursorRow);
|
|
assert(cursorCol == 3 && cursorRow == 13);
|
|
assert(memcmp(editor->bufferBegin + editor->cursorOffset, "678", 3) == 0);
|
|
|
|
size_t homeOffset = Ebitor_getLogicalOffsetAtHome(cursorCol, cursorRow);
|
|
//printf("[homeOffset=%u]", homeOffset);
|
|
assert(homeOffset == strlen("ABCDEFGHIJKLMNOPQRSTUVWXYZ01234\r" "L0\r" "L1\r"));
|
|
|
|
// Search for a string that appears more than once.
|
|
size_t numOccurrences = 0;
|
|
size_t startLogicalOffset = 0;
|
|
for (;;)
|
|
{
|
|
found = Ebitor_findStringAndScrollToShow(startLogicalOffset, "L1", &foundLogicalOffset);
|
|
//printf("[%d,%u]", found, foundLogicalOffset); waitkey(1);
|
|
if (!found)
|
|
break;
|
|
assert(startLogicalOffset <= foundLogicalOffset);
|
|
assert(foundLogicalOffset <= editor->bufferFilledSize - 2);
|
|
++numOccurrences;
|
|
assert(numOccurrences <= 9);
|
|
|
|
editor->getCursorPos(&cursorCol, &cursorRow);
|
|
assert(memcmp(editor->bufferBegin + editor->cursorOffset, "L1", 2) == 0);
|
|
|
|
// Try to find another occurrence.
|
|
startLogicalOffset = foundLogicalOffset + 1;
|
|
}
|
|
assert(numOccurrences == 9);
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_findStringAndScrollToShow
|
|
//: Tested: Ebitor_processEditing
|
|
//
|
|
static void
|
|
test_findDisplayedLineAfterScrollDown(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
setTestContent();
|
|
|
|
// Show the text.
|
|
predefinedKeyPresses = "\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
assert(editor->savedCursorCol == 0 && editor->savedCursorRow == 0);
|
|
|
|
// Find "L3", which is displayed.
|
|
//
|
|
size_t foundLogicalOffset = 0xFFFF;
|
|
BOOL found = Ebitor_findStringAndScrollToShow(0, "L3", &foundLogicalOffset);
|
|
assert(found);
|
|
assert(foundLogicalOffset == 41);
|
|
assert(editor->cursorOffset == 41);
|
|
|
|
byte cursorCol, cursorRow;
|
|
sg4term_getCursorPos(&cursorCol, &cursorRow);
|
|
assert(cursorCol == 0);
|
|
assert(cursorRow == 4);
|
|
|
|
assert(editor->savedCursorCol == 0 && editor->savedCursorRow == 4);
|
|
|
|
// Go down enough to screen window up one row.
|
|
// FALSE means no refresh, to check that Ebitor_findStringAndScrollToShow()
|
|
// did NOT scroll the window.
|
|
//
|
|
predefinedKeyPresses = "\n\n\n\n\n\n\n\n\n\n\n\x03";
|
|
Ebitor_processEditing(FALSE);
|
|
|
|
assert(editor->savedCursorCol == 0 && editor->savedCursorRow == testWindow.height - 1);
|
|
|
|
char text[3];
|
|
sg4term_getChars(0, testWindow.height - 1, text, sizeof(text));
|
|
assert(memcmp(text, "L14", sizeof(text)) == 0);
|
|
|
|
// Find "L3" again and check that the screen has not scrolled.
|
|
//
|
|
found = Ebitor_findStringAndScrollToShow(0, "L3", &foundLogicalOffset);
|
|
assert(found);
|
|
assert(foundLogicalOffset == 41);
|
|
assert(editor->cursorOffset == 41);
|
|
|
|
sg4term_getCursorPos(&cursorCol, &cursorRow);
|
|
assert(cursorCol == 0);
|
|
assert(cursorRow == 3);
|
|
|
|
assert(editor->savedCursorCol == 0 && editor->savedCursorRow == 3);
|
|
|
|
sg4term_getChars(0, testWindow.height - 1, text, sizeof(text));
|
|
assert(memcmp(text, "L14", sizeof(text)) == 0);
|
|
|
|
// Process nothing, but check that the screen has been redisplayed correctly.
|
|
//
|
|
predefinedKeyPresses = "\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
sg4term_getChars(0, testWindow.height - 1, text, sizeof(text));
|
|
assert(memcmp(text, "L14", sizeof(text)) == 0);
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_processUpOrDownArrowsAndRefresh
|
|
//
|
|
static void
|
|
test_goPageUpOnEmptyBuffer(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
predefinedKeyPresses = "\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
// Page up:
|
|
Ebitor_processUpOrDownArrowsAndRefresh(TRUE, testWindow.height);
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_processUpOrDownArrowsAndRefresh
|
|
//
|
|
static void
|
|
test_goPageUpOnSingleLine(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
predefinedKeyPresses = "\r\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
// Page up:
|
|
Ebitor_processUpOrDownArrowsAndRefresh(TRUE, testWindow.height);
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_scrollToShowLogicalOffset
|
|
//
|
|
static void
|
|
test_goTwoPagesUp(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
setRepeatedTestContent(2);
|
|
|
|
// Show the text.
|
|
predefinedKeyPresses =
|
|
"\n\n\n\n\n\n\n\n\n\n" // go to bottom of text (scrolls down 2 pages)
|
|
"\n\n\n\n\n\n\n\n\n\n"
|
|
"\n\n\n\n\n\n\n\n\n\n"
|
|
"\n\n\n\n\n\n\n\n\n\n"
|
|
"\n\n"
|
|
"\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
// Go back up to the first "L2" line.
|
|
size_t targetOffset = 32 + 3 + 3;
|
|
Ebitor_scrollToShowLogicalOffset(targetOffset, 2);
|
|
|
|
assert(editor->cursorOffset == targetOffset);
|
|
assert(editor->savedCursorCol == 0 && editor->savedCursorRow == 0);
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_scrollToShowLogicalOffset
|
|
//
|
|
static void
|
|
test_goTwoPagesDown(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
setRepeatedTestContent(2);
|
|
|
|
// Show the text.
|
|
predefinedKeyPresses = "\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
// Scroll down to the second "L13" line.
|
|
//
|
|
const char *target = strstr(textBuffer, "L11\r");
|
|
assert(target);
|
|
target = strstr(target + 1, "L11\r");
|
|
assert(target);
|
|
|
|
size_t targetOffset = target - textBuffer;
|
|
Ebitor_scrollToShowLogicalOffset(targetOffset, 3);
|
|
|
|
assert(editor->cursorOffset == targetOffset);
|
|
assert(editor->savedCursorCol == 0 && editor->savedCursorRow == testWindow.height - 1);
|
|
|
|
// Check that the scrolling was correct.
|
|
//
|
|
char text[3];
|
|
sg4term_getChars(0, testWindow.height - 1, text, sizeof(text));
|
|
assert(memcmp(text, "L11", sizeof(text)) == 0);
|
|
sg4term_getChars(0, 2, text, sizeof(text));
|
|
assert(memcmp(text, "ABC", sizeof(text)) == 0);
|
|
}
|
|
|
|
|
|
// Checks that the terminal cursor goes down when the user presses the down arrow key.
|
|
//
|
|
static void
|
|
test_goOneRowDown(void)
|
|
{
|
|
setRepeatedTestContent(2);
|
|
|
|
// Show the text.
|
|
predefinedKeyPresses = "\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
// Down arrow, then return from Ebitor_processEditing().
|
|
predefinedKeyPresses = "\x0A\x03";
|
|
Ebitor_processEditing(FALSE);
|
|
|
|
// Check that cursor went down one row.
|
|
byte column, row;
|
|
sg4term_getCursorPos(&column, &row);
|
|
assert(column == 0);
|
|
assert(row == 1);
|
|
|
|
// Check that logical offset is at start of 2nd logical line.
|
|
assert(Ebitor_getLogicalCursorOffset() == strstr(textBuffer, "\r") + 1 - textBuffer);
|
|
}
|
|
|
|
|
|
//: Tested: Ebitor_setContentLength
|
|
//: Tested: Ebitor_processEditing
|
|
//
|
|
static void
|
|
test_bufferFill(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
|
|
// Fill textBuffer (with letters) almost to the limit.
|
|
//
|
|
size_t filledSize = sizeof(textBuffer) - 5;
|
|
for (size_t i = 0; i < filledSize; ++i)
|
|
textBuffer[i] = 'A' + (char) (i % 26);
|
|
Ebitor_setContentLength(filledSize);
|
|
|
|
assert(editor->bufferFilledSize == sizeof(textBuffer) - 5);
|
|
|
|
// Interactively add too many chars.
|
|
//
|
|
predefinedKeyPresses = "12345678\x03";
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
assert(editor->bufferFilledSize == sizeof(textBuffer)); // no room for "678"
|
|
|
|
char text[7];
|
|
sg4term_getChars(0, 0, text, sizeof(text));
|
|
assert(memcmp(text, "12345AB", sizeof(text)) == 0);
|
|
|
|
// Backspace.
|
|
//
|
|
predefinedKeyPresses = "\f\x03";
|
|
Ebitor_processEditing(FALSE);
|
|
|
|
assert(editor->bufferFilledSize == sizeof(textBuffer) - 1);
|
|
|
|
sg4term_getChars(0, 0, text, sizeof(text));
|
|
assert(memcmp(text, "1234ABC", sizeof(text)) == 0);
|
|
|
|
// Backspace too much.
|
|
//
|
|
predefinedKeyPresses = "\f\f\f\f\f\f\f\x03";
|
|
Ebitor_processEditing(FALSE);
|
|
|
|
assert(editor->bufferFilledSize == sizeof(textBuffer) - 5);
|
|
|
|
sg4term_getChars(0, 0, text, sizeof(text));
|
|
assert(memcmp(text, "ABCDEFG", sizeof(text)) == 0);
|
|
}
|
|
|
|
|
|
// Tests a fix made in Ebitor_processPrintableChar() on 2025-01-09
|
|
// re: not calling Ebitor_fillTerminalToBottom() with firstRowToFill
|
|
// equal to Ebitor_instance.termHeight.
|
|
// In this test, the last visible row has 1 character less than the
|
|
// terminal width, then the user adds a character to the start of
|
|
// that row.
|
|
//
|
|
//: Tested: Ebitor_setContentLength
|
|
//: Tested: Ebitor_processEditing
|
|
//
|
|
static void
|
|
test_addCharToLastVisibleRowWithAlmostFullLine(void)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
|
|
// Fill textBuffer (with letters) almost to the limit.
|
|
//
|
|
strcpy(textBuffer,
|
|
"L0\r"
|
|
"L1\r"
|
|
"L2\r"
|
|
"L3\r"
|
|
"L4\r"
|
|
"L5\r"
|
|
"L6\r"
|
|
"L7\r"
|
|
"L8\r"
|
|
"L9\r"
|
|
"L10\r"
|
|
"L11\r"
|
|
"L12\r"
|
|
"L13\r"
|
|
"L14--+----1----+----2----+----3\r" // last visible row, initially; 1 char short of filling row
|
|
"L15\r");
|
|
Ebitor_setContentLength(strlen(textBuffer));
|
|
|
|
// Interactively add a char at start of last visible row.
|
|
//
|
|
predefinedKeyPresses =
|
|
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n" // go from row 0 to row 14
|
|
"*" // add printable char
|
|
"\x03" // return from Ebitor_processEditing()
|
|
;
|
|
Ebitor_processEditing(TRUE);
|
|
|
|
// Check content of last 3 visible rows.
|
|
//
|
|
char text[32 * 3 + 1];
|
|
sg4term_getChars(0, testWindow.height - 3, text, sizeof(text) - 1);
|
|
text[32 * 3] = '\0';
|
|
|
|
assert(strcmp(text,
|
|
"L13 "
|
|
"*L14--+----1----+----2----+----3"
|
|
" ") == 0);
|
|
|
|
// 2 down arrows, then check that L15 appears.
|
|
//
|
|
predefinedKeyPresses = "\n\n\x03";
|
|
Ebitor_processEditing(FALSE);
|
|
sg4term_getChars(0, testWindow.height - 3, text, sizeof(text) - 1);
|
|
assert(strcmp(text,
|
|
"*L14--+----1----+----2----+----3"
|
|
" "
|
|
"L15 ") == 0);
|
|
}
|
|
|
|
|
|
#ifdef USIM
|
|
char screenBuffer[512];
|
|
word timer;
|
|
#define TIMER_ADDRESS (&timer)
|
|
#else /* CoCo: */
|
|
char *screenBuffer = (char *) 0x0400;
|
|
#define TIMER_ADDRESS ((word *) 0x112)
|
|
#endif
|
|
|
|
|
|
// screenMode: 32, 40, 64, 80.
|
|
//
|
|
static void
|
|
initEbitor(byte screenMode, const ebitor_Window *window)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
|
|
word irqServiceRoutineAddress = * (word *) 0xFFF8;
|
|
BOOL isCoCo3 = (irqServiceRoutineAddress == 0xFEF7);
|
|
|
|
if (screenMode != 32 && !isCoCo3)
|
|
{
|
|
printf("MODE %u IS ONLY SUPPORTED ON\nA COCO 3.\n", screenMode);
|
|
return;
|
|
}
|
|
|
|
switch (screenMode)
|
|
{
|
|
case 32:
|
|
#ifdef _COCO_BASIC_
|
|
width(32); // by SECB
|
|
cls(3);
|
|
#endif
|
|
sg4term_init(screenBuffer, window, inkey1, NULL, TIMER_ADDRESS);
|
|
|
|
Ebitor_init(textBuffer, sizeof(textBuffer),
|
|
window->width, window->height,
|
|
sg4term_clear,
|
|
sg4term_locate,
|
|
sg4term_getCursorPos,
|
|
sg4term_print,
|
|
sg4term_printPrefix,
|
|
sg4term_clrtoeol,
|
|
sg4term_scrollWindowUp,
|
|
sg4term_scrollWindowDown,
|
|
sg4term_waitEditorKey);
|
|
break;
|
|
|
|
default:
|
|
assert(0);
|
|
}
|
|
}
|
|
|
|
|
|
typedef void (*TestFunction)(void);
|
|
|
|
|
|
static void
|
|
runTest(TestFunction testFunc)
|
|
{
|
|
byte screenMode = 32;
|
|
#ifdef _COCO_BASIC_
|
|
width(32);
|
|
locate(0, 0);
|
|
#endif
|
|
initEbitor(screenMode, &testWindow);
|
|
testFunc();
|
|
Ebitor_shutdown();
|
|
}
|
|
|
|
|
|
TestFunction tests[] =
|
|
{
|
|
test_scrollWindowUp,
|
|
test_scrollWindowDown,
|
|
test_countRowsToScrollUp,
|
|
test_countRowsToScrollDown,
|
|
test_scrollToShowLogicalOffset,
|
|
test_findStringAndScrollToShow,
|
|
test_findDisplayedLineAfterScrollDown,
|
|
test_goPageUpOnEmptyBuffer,
|
|
test_goPageUpOnSingleLine,
|
|
test_goTwoPagesUp,
|
|
test_goTwoPagesDown,
|
|
test_bufferFill,
|
|
test_goOneRowDown,
|
|
test_addCharToLastVisibleRowWithAlmostFullLine,
|
|
};
|
|
|
|
|
|
static void
|
|
runSelfTests(void)
|
|
{
|
|
size_t numTests = sizeof(tests) / sizeof(tests[0]);
|
|
for (currentTestIndex = 0; currentTestIndex < numTests; ++currentTestIndex)
|
|
runTest(tests[currentTestIndex]);
|
|
#ifdef _COCO_BASIC_
|
|
cls(255);
|
|
#endif
|
|
printf("\n* RAN %u TEST(S) *\n", numTests);
|
|
}
|
|
|
|
|
|
static void
|
|
failedAssertHandler(const char *file, int lineno, const char *condition)
|
|
{
|
|
printf("[***FAILED ASSERT IN TEST #%u: %s:%d: %s]",
|
|
currentTestIndex, file, lineno, condition);
|
|
for (;;)
|
|
;
|
|
}
|
|
|
|
|
|
int
|
|
main()
|
|
{
|
|
#ifdef _COCO_BASIC_
|
|
rgb();
|
|
#endif
|
|
_SetFailedAssertHandler(failedAssertHandler);
|
|
runSelfTests();
|
|
return 0;
|
|
}
|