iPXE
Data Structures | Macros | Functions | Variables
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(...)   { __VA_ARGS__ }
 Define an inline key sequence. More...
 
#define EDITSTRING_TEST(name, START, EXPECTED, KEYS)
 Define an editable string test. More...
 
#define editstring_ok(test)   editstring_okx ( test, __FILE__, __LINE__ )
 

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. More...
 
static void editstring_test_exec (void)
 Perform editable string self-tests. More...
 

Variables

struct self_test editstring_test __self_test
 Editable string self-test. More...
 

Detailed Description

Editable string tests.

Definition in file editstring_test.c.

Macro Definition Documentation

◆ KEYS

#define KEYS (   ...)    { __VA_ARGS__ }

Define an inline key sequence.

Definition at line 55 of file editstring_test.c.

◆ 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] ) ), \
};
#define EXPECTED(...)
Define inline expected HMAC.
Definition: hmac_test.c:49
const char * name
Definition: ath9k_hw.c:1984
An editable string test.
const char * expected
Expected result.
#define KEYS(...)
Define an inline key sequence.

Define an editable string test.

Definition at line 58 of file editstring_test.c.

◆ editstring_ok

#define editstring_ok (   test)    editstring_okx ( test, __FILE__, __LINE__ )

Definition at line 170 of file editstring_test.c.

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', '!')   
)

◆ EDITSTRING_TEST() [2/14]

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

◆ 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', ' ')   
)

◆ EDITSTRING_TEST() [4/14]

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

◆ EDITSTRING_TEST() [5/14]

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

◆ EDITSTRING_TEST() [6/14]

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

◆ EDITSTRING_TEST() [7/14]

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() [8/14]

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

◆ 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  
)

◆ EDITSTRING_TEST() [10/14]

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

◆ EDITSTRING_TEST() [11/14]

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

◆ EDITSTRING_TEST() [12/14]

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

◆ EDITSTRING_TEST() [13/14]

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

◆ EDITSTRING_TEST() [14/14]

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

◆ editstring_okx()

static 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 }
int replace_string(struct edit_string *string, const char *replacement)
Replace editable string.
Definition: editstring.c:228
unsigned int cursor
Cursor position.
Definition: editstring.h:17
#define DBGC(...)
Definition: compiler.h:505
uint32_t string
Definition: multiboot.h:14
static int isprint(int character)
Check if character is printable.
Definition: ctype.h:97
#define okx(success, file, line)
Report test result.
Definition: test.h:44
static __nonnull void init_editstring(struct edit_string *string, char **buf)
Initialise editable string.
Definition: editstring.h:46
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
An editable string.
Definition: editstring.h:13
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static int test
Definition: epic100.c:73
union @383 key
Sense key.
Definition: scsi.h:18
void * memset(void *dest, int character, size_t len) __nonnull

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

◆ editstring_test_exec()

static 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.