1 #include <stdio.h>
2 #include "r_util.h"
3
4
check(const char * exp,const char * act,const char * desc)5 void check(const char *exp, const char *act, const char *desc) {
6 if (strcmp(exp, act) == 0)
7 printf("\x1b[34m[+][%s]\x1b[39;49m test passed\n", desc);
8 else
9 printf("\x1b[31m[-][%s]\x1b[39;49m test failed (actual = %s\x1b[39;49m, expected = %s\x1b[39;49m)\n", desc, act, exp);
10 }
11
check_n(const char * exp,const char * act,int len,const char * desc)12 void check_n(const char *exp, const char *act, int len, const char *desc) {
13 if (strncmp(exp, act, len) == 0)
14 printf("\x1b[34m[+][%s]\x1b[39;49m test passed\n", desc);
15 else
16 printf("\x1b[31m[-][%s]\x1b[39;49m test failed (actual = %s\x1b[39;49m, expected = %s\x1b[39;49m)\n", desc, act, exp);
17 }
18
check_int(int exp,int act,const char * desc)19 void check_int(int exp, int act, const char *desc) {
20 if (exp == act)
21 printf("\x1b[34m[+][%s]\x1b[39;49m test passed\n", desc);
22 else
23 printf("\x1b[31m[-][%s]\x1b[39;49m test failed (actual = %d\x1b[39;49m, expected = %d\x1b[39;49m)\n", desc, act, exp);
24 }
25
check_array(int * exp,int * act,int len,const char * desc)26 void check_array(int *exp, int *act, int len, const char *desc) {
27 int i, err_found = 0;
28 for (i = 0; i < len; i++) {
29 if (exp[i] != act[i]) {
30 printf("\x1b[31m[-][%s]\x1b[39;49m test failed element %d (actual = %d\x1b[39;49m, expected = %d\x1b[39;49m)\n", desc, i, act[i], exp[i]);
31 err_found = 1;
32 }
33 }
34 if (!err_found)
35 printf("\x1b[34m[+][%s]\x1b[39;49m test passed\n", desc);
36 else
37 printf("\x1b[31m[-][%s]\x1b[39;49m test failed\n", desc);
38 }
39
main(int argc,char * argv[])40 int main(int argc, char *argv[]) {
41 char head[] = " a";
42 char tail[] = "a ";
43 char head_tail[] = " a ";
44
45 check ("a", r_str_trim_head (head), "trim head \" a\"");
46 check ("a", r_str_trim_tail (tail), "trim tail \"a \"");
47 check ("a", r_str_trim (head_tail), "trim head tail \" a \"");
48
49 char *crop =
50 "This is the first line\n"
51 "This is the second\n"
52 "\n"
53 "And this is the last\n";
54 char *crop_exp =
55 "is is the se\n"
56 "\n"
57 "d this is th\n";
58 check (crop_exp, r_str_crop(crop, 2, 1, 14, 10), "crop text");
59 check ("", r_str_crop(NULL, 2, 1, 14, 10), "crop NULL");
60
61 char dst[256];
62 char src[] = "This is my text";
63 r_str_ncpy(dst, src, 4);
64 check ("This", dst, "r_str_ncpy");
65 dst[0] = '\0';
66 r_str_ncpy(dst, src, 100);
67 check ("This is my text", dst, "r_str_ncpy (n > src length)");
68
69 strcpy(dst, "This is a $hell < fin.txt");
70 r_str_sanitize(dst);
71 check("This is a _hell _ fin.txt", dst, "r_str_sanitize");
72
73 strcpy(dst, "\x1b[30mHel\x1b[28mlo");
74 check("lo", r_str_ansi_chrn(dst, 4), "r_str_ansi_chrn");
75
76 check_int(5, r_str_ansi_len(dst), "r_str_ansi_len 1");
77 strcpy(dst, "Hello");
78 check_int(5, r_str_ansi_len(dst), "r_str_ansi_len 2");
79 strcpy(dst, "\x1b[30m");
80 check_int(0, r_str_ansi_len(dst), "r_str_ansi_len 3");
81
82 strcpy(dst, "Hello");
83 check_int(5, r_str_ansi_chop(dst, -1, 5), "r_str_ansi_chop (normal string)");
84 check("Hello", dst, "r_str_ansi_chop (normal string)(str)");
85 strcpy(dst, "Hello");
86 check_int(2, r_str_ansi_chop(dst, -1, 2), "r_str_ansi_chop 2 (normal string)");
87 check("He", dst, "r_str_ansi_chop 2 (normal string)(str)");
88 strcpy(dst, "Hello");
89 check_int(0, r_str_ansi_chop(dst, -1, 0), "r_str_ansi_chop 3 (normal string)");
90 check("", dst, "r_str_ansi_chop 3 (normal string)(str)");
91
92 strcpy(dst, "\x1b[30mHel\x1b[29mlo");
93 check_int(0, r_str_ansi_chop(dst, -1, 0), "r_str_ansi_chop 1");
94 check("", dst, "r_str_ansi_chop 1 (str)");
95 strcpy(dst, "\x1b[30mHel\x1b[29mlo");
96 check_int(7, r_str_ansi_chop(dst, -1, 2), "r_str_ansi_chop 2");
97 check("\x1b[30mHe", dst, "r_str_ansi_chop 2 (str)");
98 strcpy(dst, "\x1b[30mHel\x1b[29mlo");
99 check_int(8, r_str_ansi_chop(dst, -1, 3), "r_str_ansi_chop 3");
100 check("\x1b[30mHel", dst, "r_str_ansi_chop 3 (str)");
101
102 char *orig;
103 int *cpos;
104 strcpy(dst, "Hello");
105 check_int(5, r_str_ansi_filter(dst, NULL, NULL, -1), "r_str_ansi_filter (normal string)");
106 check("Hello", dst, "r_str_ansi_filter (normal string(str)");
107 strcpy(dst, "\x1b[30mHel\x1b[29mlo\x1b[28m");
108 check_int(5, r_str_ansi_filter(dst, NULL, NULL, -1), "r_str_ansi_filter");
109 check("Hello", dst, "r_str_ansi_filter (str)");
110 strcpy(dst, "\x1b[30mHel\x1b[29mlo\x1b[28m");
111 check_int(3, r_str_ansi_filter(dst, NULL, NULL, 8), "r_str_ansi_filter (length)");
112 check_n("Hel", dst, 3, "r_str_ansi_filter (length)(str)");
113 strcpy(dst, "\x1b[30mHel\x1b[29mlo\x1b[28m");
114 r_str_ansi_filter(dst, &orig, NULL, -1);
115 check("\x1b[30mHel\x1b[29mlo\x1b[28m", orig, "r_str_ansi_filter out orig");
116 strcpy(dst, "\x1b[30mHel\x1b[29mlo\x1b[28m");
117 int res = r_str_ansi_filter(dst, NULL, &cpos, -1);
118 check_int(5, res, "r_str_ansi_filter res");
119 int exp_cpos[] = {5, 6, 7, 13, 14};
120 check_array(exp_cpos, cpos, res, "r_str_ansi_filter cpos");
121
122 char clean[256];
123 char *str = malloc(256);
124 strcpy(str, "\x1b[30mHell\x1b[32mo\nIt'\x1b[33ms a test\n");
125 strcpy(clean, "Hello\nIt's a test\n");
126 int thunk[] = {5, 6, 7, 8, 14, 15, 16, 17, 18, 24, 25, 26, 27, 28, 29, 30, 31, 32};
127 char *res_s = r_str_replace_thunked (str, clean, thunk, 18, "est", "\x1b[31mest\x1b[39;49m", 1);
128 check("\x1b[30mHell\x1b[32mo\nIt'\x1b[33ms a t\x1b[31mest\x1b[39;49m\n", res_s, "r_str_replace_thunked");
129
130 int l;
131 strcpy(str, "\x1b[30mHell\x1b[32mo\nIt'\x1b[33ms an hell\n");
132 l = r_str_ansi_filter (str, &orig, &cpos, 0);
133 res_s = r_str_replace_thunked(orig, str, cpos, l, "ell", "\x1b[31mell\x1b[39;49m", 1);
134 check("\x1b[30mH\x1b[31mell\x1b[39;49mo\nIt'\x1b[33ms an h\x1b[31mell\x1b[39;49m\n", res_s, "r_str_ansi_filter + replace_thunked");
135
136 crop =
137 "\x1b[30mThis is the \x1b[34mfirst line\n"
138 "\x1b[32mThis \x1b[31mis the\x1b[39;49m second\n"
139 "\n"
140 "And this is the \x1b[32mlast\n";
141 crop_exp =
142 "\x1b[30m\x1b[34m\x1b[32mis \x1b[31mis the\x1b[39;49m se\n"
143 "\n"
144 "d this is th\n";
145 check(crop_exp, r_str_ansi_crop(crop, 2, 1, 14, 10), "r_str_ansi_crop");
146
147 return 0;
148 }
149