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 <stdlib.h>
21 #include <string.h>
22 #include <sys/debug.h>
23
24 static void
expect(const char * var,custr_t * cu,const char * str,const char * file,size_t line)25 expect(const char *var, custr_t *cu, const char *str, const char *file,
26 size_t line)
27 {
28 if (strcmp(custr_cstr(cu), str) == 0)
29 return;
30
31 char msgbuf[256];
32
33 (void) snprintf(msgbuf, sizeof (msgbuf), "%s == '%s' ('%s' == '%s')",
34 var, str, custr_cstr(cu), str);
35
36 (void) assfail(msgbuf, file, line);
37 }
38
39 #define EXPECT(_cu, _str) expect(#_cu, _cu, _str, __FILE__, __LINE__)
40 #define FAIL(_expr, _ev) \
41 VERIFY3S(_expr, ==, -1); \
42 VERIFY3S(errno, ==, (_ev))
43
44 int
main(void)45 main(void)
46 {
47 custr_t *cu;
48
49 VERIFY0(custr_alloc(&cu));
50
51 VERIFY0(custr_append(cu, "12345"));
52 EXPECT(cu, "12345");
53
54 FAIL(custr_trunc(cu, 6), EINVAL);
55 FAIL(custr_rtrunc(cu, 10), EINVAL);
56
57 VERIFY0(custr_trunc(cu, 3));
58 EXPECT(cu, "123");
59
60 VERIFY0(custr_rtrunc(cu, 1));
61 EXPECT(cu, "1");
62
63 return (0);
64 }
65