1 #include "tests.h"
2 
3 static const pl_str null = {0};
4 static const pl_str test = PL_STR0("test");
5 static const pl_str empty = PL_STR0("");
6 
is_null(pl_str str)7 static inline bool is_null(pl_str str)
8 {
9     return !str.len && !str.buf;
10 }
11 
is_empty(pl_str str)12 static inline bool is_empty(pl_str str)
13 {
14     return !str.len;
15 }
16 
main()17 int main()
18 {
19     void *tmp = pl_tmp(NULL);
20 
21     REQUIRE(is_null(pl_str0(NULL)));
22     REQUIRE(is_null(pl_strdup(tmp, null)));
23     char *empty0 = pl_strdup0(tmp, null);
24     REQUIRE(empty0 && !empty0[0]);
25     REQUIRE(pl_str_equals0(empty, empty0));
26 
27     pl_str buf = {0};
28     pl_str_append(tmp, &buf, null);
29     REQUIRE(is_null(buf));
30     pl_str_append_asprintf(tmp, &buf, "%.*s", PL_STR_FMT(test));
31     REQUIRE(pl_str_equals(buf, test));
32 
33     REQUIRE(pl_strchr(null, ' ') < 0);
34     REQUIRE(pl_strspn(null, " ") == 0);
35     REQUIRE(pl_strcspn(null, " ") == 0);
36     REQUIRE(is_null(pl_str_strip(null)));
37 
38     REQUIRE(pl_strchr(test, 's') == 2);
39     REQUIRE(pl_strspn(test, "et") == 2);
40     REQUIRE(pl_strcspn(test, "xs") == 2);
41 
42     REQUIRE(is_null(pl_str_take(null, 10)));
43     REQUIRE(is_empty(pl_str_take(test, 0)));
44     REQUIRE(is_null(pl_str_drop(null, 10)));
45     REQUIRE(is_null(pl_str_drop(test, test.len)));
46     REQUIRE(pl_str_equals(pl_str_drop(test, 0), test));
47 
48     REQUIRE(pl_str_find(null, test) < 0);
49     REQUIRE(pl_str_find(null, null) == 0);
50     REQUIRE(pl_str_find(test, null) == 0);
51     REQUIRE(pl_str_find(test, test) == 0);
52 
53     pl_str rest;
54     REQUIRE(is_null(pl_str_split_char(null, ' ', &rest)) && is_null(rest));
55     REQUIRE(is_null(pl_str_split_str(null, test, &rest)) && is_null(rest));
56     REQUIRE(is_empty(pl_str_split_str(test, test, &rest)) && is_empty(rest));
57     REQUIRE(is_null(pl_str_getline(null, &rest)) && is_null(rest));
58 
59     pl_str right, left = pl_str_split_char(pl_str0("left right"), ' ', &right);
60     REQUIRE(pl_str_equals0(left, "left"));
61     REQUIRE(pl_str_equals0(right, "right"));
62 
63     left = pl_str_split_str0(pl_str0("leftTESTright"), "TEST", &right);
64     REQUIRE(pl_str_equals0(left, "left"));
65     REQUIRE(pl_str_equals0(right, "right"));
66 
67     pl_str out;
68     REQUIRE(pl_str_decode_hex(tmp, null, &out) && is_empty(out));
69     REQUIRE(!pl_str_decode_hex(tmp, pl_str0("invalid"), &out));
70 
71     REQUIRE(pl_str_equals(null, null));
72     REQUIRE(pl_str_equals(null, empty));
73     REQUIRE(pl_str_startswith(null, null));
74     REQUIRE(pl_str_startswith(test, null));
75     REQUIRE(pl_str_startswith(test, test));
76     REQUIRE(pl_str_endswith(null, null));
77     REQUIRE(pl_str_endswith(test, null));
78     REQUIRE(pl_str_endswith(test, test));
79 
80     float f;
81     int i;
82     REQUIRE(pl_str_parse_float(pl_str0("1.3984"), &f) && feq(f, 1.3984f, 1e-8));
83     REQUIRE(pl_str_parse_float(pl_str0("-8.9100083"), &f) && feq(f, -8.9100083f, 1e-8));
84     REQUIRE(pl_str_parse_float(pl_str0("-0"), &f) && feq(f, 0.0f, 1e-8));
85     REQUIRE(pl_str_parse_float(pl_str0("-3.14e20"), &f) && feq(f, -3.14e20f, 1e-8));
86     REQUIRE(pl_str_parse_float(pl_str0("0.5e-5"), &f) && feq(f, 0.5e-5f, 1e-8));
87     REQUIRE(pl_str_parse_int(pl_str0("64239"), &i) && i == 64239);
88     REQUIRE(pl_str_parse_int(pl_str0("-102"), &i) && i == -102);
89     REQUIRE(pl_str_parse_int(pl_str0("-0"), &i) && i == 0);
90     REQUIRE(!pl_str_parse_float(null, &f));
91     REQUIRE(!pl_str_parse_float(test, &f));
92     REQUIRE(!pl_str_parse_float(empty, &f));
93     REQUIRE(!pl_str_parse_int(null, &i));
94     REQUIRE(!pl_str_parse_int(test, &i));
95     REQUIRE(!pl_str_parse_int(empty, &i));
96 
97     pl_free(tmp);
98     return 0;
99 }
100