lchat

A line oriented chat front end for ii.
git clone git://git.suckless.org/lchat
Log | Files | Refs | README

sl_test.c (2560B)


      1 #include <assert.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #include "slackline.h"
      7 
      8 static void
      9 strokes(struct slackline *sl, const char *str)
     10 {
     11 	for (const char *c = str; *c != '\0'; c++)
     12 		sl_keystroke(sl, *c);
     13 }
     14 
     15 static void
     16 print_state(struct slackline *sl)
     17 {
     18 	size_t l = sl->last - sl->buf;
     19 
     20 	printf("rcur: %zu bcur: %zu rlen: %zu blen: %zu l: %zu buf: \"%s\" ",
     21 	    sl->rcur, sl->bcur, sl->rlen, sl->blen,     l, sl->buf);
     22 
     23 	for (size_t i = 0; i < strlen(sl->buf); i++)
     24 		printf("%X", sl->buf[i] & 0xff);
     25 
     26 	putchar('\n');
     27 }
     28 
     29 static void
     30 check_init(struct slackline *sl)
     31 {
     32 	assert(sl != NULL);
     33 	assert(sl->buf != NULL);
     34 	assert(sl->ptr == sl->buf);
     35 	assert(sl->rlen == 0);
     36 	assert(sl->blen == 0);
     37 	assert(sl->rcur == 0);
     38 	assert(sl->bcur == 0);
     39 	assert(sl->last == sl->buf);
     40 }
     41 
     42 static void
     43 check_ascii(struct slackline *sl)
     44 {
     45 	strokes(sl, "abc");
     46 	assert(sl->blen == 3);
     47 	assert(sl->rlen == 3);
     48 	assert(sl->bcur == 3);
     49 	assert(sl->rcur == 3);
     50 	assert(sl->last - sl->buf == sl->blen);
     51 
     52 	strokes(sl, "\x08");	/* backspace */
     53 	assert(sl->blen == 2);
     54 	assert(sl->rlen == 2);
     55 	assert(sl->bcur == 2);
     56 	assert(sl->rcur == 2);
     57 	assert(sl->last - sl->buf == sl->blen);
     58 
     59 	strokes(sl, "\x1b[D");	/* left arrow key */
     60 	assert(sl->blen == 2);
     61 	assert(sl->rlen == 2);
     62 	assert(sl->bcur == 1);
     63 	assert(sl->rcur == 1);
     64 	assert(sl->last - sl->buf == sl->blen);
     65 }
     66 
     67 static void
     68 check_utf8(struct slackline *sl)
     69 {
     70 	            /*   ae  |   oe  |   ue  |   ss  */
     71 	strokes(sl, "\xC3\xA4\xC3\xBC\xC3\xB6\xC3\x9F");
     72 	assert(sl->blen == 8);
     73 	assert(sl->rlen == 4);
     74 	assert(sl->bcur == 8);
     75 	assert(sl->rcur == 4);
     76 	assert(sl->last - sl->buf == sl->blen);
     77 
     78 	strokes(sl, "\x08");	/* backspace */
     79 	assert(sl->blen == 6);
     80 	assert(sl->rlen == 3);
     81 	assert(sl->bcur == 6);
     82 	assert(sl->rcur == 3);
     83 	assert(sl->last - sl->buf == sl->blen);
     84 
     85 	strokes(sl, "\x1b[D");	/* left arrow key */
     86 	assert(sl->blen == 6);
     87 	assert(sl->rlen == 3);
     88 	assert(sl->bcur == 4);
     89 	assert(sl->rcur == 2);
     90 	assert(sl->last - sl->buf == sl->blen);
     91 
     92 	strokes(sl, "\x08");	/* backspace */
     93 	assert(sl->blen == 4);
     94 	assert(sl->rlen == 2);
     95 	assert(sl->bcur == 2);
     96 	assert(sl->rcur == 1);
     97 	assert(sl->last - sl->buf == sl->blen);
     98 
     99 	strokes(sl, "\x1b[C");	/* right arrow key */
    100 	assert(sl->blen == 4);
    101 	assert(sl->rlen == 2);
    102 	assert(sl->bcur == 4);
    103 	assert(sl->rcur == 2);
    104 	assert(sl->last - sl->buf == sl->blen);
    105 }
    106 
    107 int
    108 main(void)
    109 {
    110 	struct slackline *sl;
    111 
    112 	sl = sl_init();
    113 	check_init(sl);
    114 	check_ascii(sl);
    115 
    116 	sl_reset(sl);
    117 	check_init(sl);
    118 	check_utf8(sl);
    119 
    120 	sl_free(sl);
    121 
    122 	return EXIT_SUCCESS;
    123 }