xref: /illumos-gate/usr/src/test/util-tests/tests/libcustr/custr_remove.c (revision fa4a3e77edc40df6d92e8da6fc4961d275e9896d)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2019 Joyent, Inc.
14  */
15 
16 #include <errno.h>
17 #include <libcustr.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/debug.h>
22 
23 static void
24 expect(const char *var, custr_t *cu, const char *str, const char *file,
25     size_t line)
26 {
27 	if (strcmp(custr_cstr(cu), str) == 0)
28 		return;
29 
30 	char msgbuf[256];
31 
32 	(void) snprintf(msgbuf, sizeof (msgbuf), "%s == '%s' ('%s' == '%s')",
33 	    var, str, custr_cstr(cu), str);
34 
35 	(void) assfail(msgbuf, file, line);
36 }
37 
38 #define	EXPECT(_cu, _str) expect(#_cu, _cu, _str, __FILE__, __LINE__)
39 #define	FAIL(_expr, _ev)		\
40 	VERIFY3S(_expr, ==, -1);	\
41 	VERIFY3S(errno, ==, (_ev))
42 
43 int
44 main(void)
45 {
46 	custr_t *cu;
47 
48 	VERIFY0(custr_alloc(&cu));
49 
50 	VERIFY0(custr_append(cu, "12345"));
51 	EXPECT(cu, "12345");
52 
53 	FAIL(custr_remove(cu, 6, 2), EINVAL);
54 	FAIL(custr_remove(cu, 2, 10), EINVAL);
55 	FAIL(custr_rremove(cu, 6, 2), EINVAL);
56 	FAIL(custr_rremove(cu, 2, 10), EINVAL);
57 
58 	VERIFY0(custr_remove(cu, 0, 1));
59 	EXPECT(cu, "2345");
60 	VERIFY0(custr_rremove(cu, 1, 2));
61 	EXPECT(cu, "23");
62 
63 	VERIFY0(custr_append(cu, "456"));
64 	EXPECT(cu, "23456");
65 
66 	VERIFY0(custr_remove(cu, 1, 2));
67 	EXPECT(cu, "256");
68 
69 	VERIFY0(custr_rremove(cu, 1, 2));
70 	EXPECT(cu, "2");
71 
72 	return (0);
73 }
74