iPXE
editstring_test.c File Reference

Editable string tests. More...

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <ipxe/keys.h>
#include <ipxe/editstring.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  editstring_test
 An editable string test. More...

Macros

#define KEYS(...)
 Define an inline key sequence.
#define EDITSTRING_TEST(name, START, EXPECTED, KEYS)
 Define an editable string test.
#define editstring_ok(test)

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 EDITSTRING_TEST (simple, "", "hello world!", KEYS( 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'))
 EDITSTRING_TEST (simple_null, NULL, "hi there", KEYS( 'h', 'i', ' ', 't', 'h', 'e', 'r', 'e'))
 EDITSTRING_TEST (insert, "in middle", "in the middle", KEYS(KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, 't', 'h', 'e', ' '))
 EDITSTRING_TEST (backspace_end, "byebye", "bye", KEYS(KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE))
 EDITSTRING_TEST (backspace_all, "abc", "", KEYS(KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE))
 EDITSTRING_TEST (backspace_empty, NULL, "", KEYS(KEY_BACKSPACE))
 EDITSTRING_TEST (backspace_beyond, "too far", "", KEYS(KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE))
 EDITSTRING_TEST (delete_dc, "go away", "goaway", KEYS(KEY_HOME, KEY_RIGHT, KEY_RIGHT, KEY_DC))
 EDITSTRING_TEST (delete_ctrl_d, "not here", "nohere", KEYS(KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, CTRL_D, CTRL_D))
 EDITSTRING_TEST (word_end, "remove these two words", "remove these ", KEYS(CTRL_W, CTRL_W))
 EDITSTRING_TEST (word_start, "no word", "word", KEYS(CTRL_A, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, CTRL_W))
 EDITSTRING_TEST (word_mid, "delete this word", "delete word", KEYS(KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, CTRL_W))
 EDITSTRING_TEST (sol, "everything must go", "go", KEYS(KEY_LEFT, KEY_LEFT, CTRL_U))
 EDITSTRING_TEST (eol, "all is lost", "all", KEYS(KEY_HOME, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, CTRL_K))
static void editstring_okx (struct editstring_test *test, const char *file, unsigned int line)
 Report an editable string test result.
static void editstring_test_exec (void)
 Perform editable string self-tests.

Variables

struct self_test editstring_test __self_test
 Editable string self-test.

Detailed Description

Editable string tests.

Definition in file editstring_test.c.

Macro Definition Documentation

◆ KEYS

◆ EDITSTRING_TEST

#define EDITSTRING_TEST ( name,
START,
EXPECTED,
KEYS )
Value:
static const int name ## _keys[] = KEYS; \
static struct editstring_test name = { \
.start = START, \
.keys = name ## _keys, \
.count = ( sizeof ( name ## _keys ) / \
sizeof ( name ## _keys[0] ) ), \
.expected = EXPECTED, \
};
const char * name
Definition ath9k_hw.c:1986
#define KEYS(...)
Define an inline key sequence.
#define EXPECTED(...)
Define inline expected result point.
An editable string test.

Define an editable string test.

Definition at line 58 of file editstring_test.c.

58#define EDITSTRING_TEST( name, START, EXPECTED, KEYS ) \
59 static const int name ## _keys[] = KEYS; \
60 static struct editstring_test name = { \
61 .start = START, \
62 .keys = name ## _keys, \
63 .count = ( sizeof ( name ## _keys ) / \
64 sizeof ( name ## _keys[0] ) ), \
65 .expected = EXPECTED, \
66 };

◆ editstring_ok

#define editstring_ok ( test)
Value:
editstring_okx ( test, __FILE__, __LINE__ )
static void editstring_okx(struct editstring_test *test, const char *file, unsigned int line)
Report an editable string test result.
static int test
Definition epic100.c:73

Definition at line 170 of file editstring_test.c.

Referenced by editstring_test_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ EDITSTRING_TEST() [1/14]

EDITSTRING_TEST ( simple ,
"" ,
"hello world!" ,
KEYS('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!')  )

References KEYS.

◆ EDITSTRING_TEST() [2/14]

EDITSTRING_TEST ( simple_null ,
NULL ,
"hi there" ,
KEYS('h', 'i', ' ', 't', 'h', 'e', 'r', 'e')  )

References KEYS, and NULL.

◆ EDITSTRING_TEST() [3/14]

EDITSTRING_TEST ( insert ,
"in middle" ,
"in the middle" ,
KEYS(KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, 't', 'h', 'e', ' ')  )

References KEY_LEFT, and KEYS.

◆ EDITSTRING_TEST() [4/14]

EDITSTRING_TEST ( backspace_end ,
"byebye" ,
"bye" ,
KEYS(KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE)  )

References KEY_BACKSPACE, and KEYS.

◆ EDITSTRING_TEST() [5/14]

EDITSTRING_TEST ( backspace_all ,
"abc" ,
"" ,
KEYS(KEY_BACKSPACE, KEY_BACKSPACE, KEY_BACKSPACE)  )

References KEY_BACKSPACE, and KEYS.

◆ EDITSTRING_TEST() [6/14]

EDITSTRING_TEST ( backspace_empty ,
NULL ,
"" ,
KEYS(KEY_BACKSPACE)  )

References KEY_BACKSPACE, KEYS, and NULL.

◆ EDITSTRING_TEST() [7/14]

References KEY_BACKSPACE, and KEYS.

◆ EDITSTRING_TEST() [8/14]

EDITSTRING_TEST ( delete_dc ,
"go away" ,
"goaway" ,
KEYS(KEY_HOME, KEY_RIGHT, KEY_RIGHT, KEY_DC)  )

References KEY_DC, KEY_HOME, KEY_RIGHT, and KEYS.

◆ EDITSTRING_TEST() [9/14]

EDITSTRING_TEST ( delete_ctrl_d ,
"not here" ,
"nohere" ,
KEYS(KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, CTRL_D, CTRL_D)  )

References CTRL_D, KEY_LEFT, and KEYS.

◆ EDITSTRING_TEST() [10/14]

EDITSTRING_TEST ( word_end ,
"remove these two words" ,
"remove these " ,
KEYS(CTRL_W, CTRL_W)  )

References CTRL_W, and KEYS.

◆ EDITSTRING_TEST() [11/14]

EDITSTRING_TEST ( word_start ,
"no word" ,
"word" ,
KEYS(CTRL_A, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, CTRL_W)  )

References CTRL_A, CTRL_W, KEY_RIGHT, and KEYS.

◆ EDITSTRING_TEST() [12/14]

EDITSTRING_TEST ( word_mid ,
"delete this word" ,
"delete word" ,
KEYS(KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT, CTRL_W)  )

References CTRL_W, KEY_LEFT, and KEYS.

◆ EDITSTRING_TEST() [13/14]

EDITSTRING_TEST ( sol ,
"everything must go" ,
"go" ,
KEYS(KEY_LEFT, KEY_LEFT, CTRL_U)  )

References CTRL_U, KEY_LEFT, and KEYS.

◆ EDITSTRING_TEST() [14/14]

EDITSTRING_TEST ( eol ,
"all is lost" ,
"all" ,
KEYS(KEY_HOME, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, CTRL_K)  )

References CTRL_K, KEY_HOME, KEY_RIGHT, and KEYS.

◆ editstring_okx()

void editstring_okx ( struct editstring_test * test,
const char * file,
unsigned int line )
static

Report an editable string test result.

Parameters
testEditable string test
fileTest code file
lineTest code line

Definition at line 135 of file editstring_test.c.

136 {
137 struct edit_string string;
138 unsigned int i;
139 char *actual;
140 int key;
141
142 /* Initialise editable string */
143 memset ( &string, 0, sizeof ( string ) );
144 actual = NULL;
145 init_editstring ( &string, &actual );
146
147 /* Set initial string content */
148 okx ( replace_string ( &string, test->start ) == 0, file, line );
149 okx ( actual != NULL, file, line );
150 okx ( string.cursor == ( test->start ? strlen ( test->start ) : 0 ),
151 file, line );
152 DBGC ( test, "Initial string: \"%s\"\n", actual );
153
154 /* Inject keypresses */
155 for ( i = 0 ; i < test->count ; i++ ) {
156 key = test->keys[i];
157 okx ( edit_string ( &string, key ) == 0, file, line );
158 okx ( actual != NULL, file, line );
159 okx ( string.cursor <= strlen ( actual ), file, line );
160 DBGC ( test, "After key %#02x (%c): \"%s\"\n",
161 key, ( isprint ( key ) ? key : '.' ), actual );
162 }
163
164 /* Verify result string */
165 okx ( strcmp ( actual, test->expected ) == 0, file, line );
166
167 /* Free result string */
168 free ( actual );
169}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
static int isprint(int character)
Check if character is printable.
Definition ctype.h:98
int replace_string(struct edit_string *string, const char *replacement)
Replace editable string.
Definition editstring.c:229
static __nonnull void init_editstring(struct edit_string *string, char **buf)
Initialise editable string.
Definition editstring.h:47
#define DBGC(...)
Definition compiler.h:505
void * memset(void *dest, int character, size_t len) __nonnull
uint32_t string
Definition multiboot.h:2
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
An editable string.
Definition editstring.h:14
unsigned int cursor
Cursor position.
Definition editstring.h:18
#define okx(success, file, line)
Report test result.
Definition test.h:44

References edit_string::cursor, DBGC, free, init_editstring(), isprint(), key, memset(), NULL, okx, replace_string(), strcmp(), string, strlen(), and test.

◆ editstring_test_exec()

void editstring_test_exec ( void )
static

Perform editable string self-tests.

Definition at line 176 of file editstring_test.c.

176 {
177
178 editstring_ok ( &simple );
179 editstring_ok ( &simple_null );
180 editstring_ok ( &insert );
181 editstring_ok ( &backspace_end );
182 editstring_ok ( &backspace_all );
183 editstring_ok ( &backspace_empty );
184 editstring_ok ( &backspace_beyond );
185 editstring_ok ( &delete_dc );
186 editstring_ok ( &delete_ctrl_d );
187 editstring_ok ( &word_end );
188 editstring_ok ( &word_start );
189 editstring_ok ( &word_mid );
190 editstring_ok ( &sol );
191 editstring_ok ( &eol );
192}
#define editstring_ok(test)

References editstring_ok.

Variable Documentation

◆ __self_test

struct self_test editstring_test __self_test
Initial value:
= {
.name = "editstring",
}
static void editstring_test_exec(void)
Perform editable string self-tests.

Editable string self-test.

Definition at line 195 of file editstring_test.c.

195 {
196 .name = "editstring",
197 .exec = editstring_test_exec,
198};