summaryrefslogtreecommitdiff
path: root/ccan/opt/test/run-consume_words.c
blob: 9f164656abd602d0b4fe414664bb51c498a62553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <ccan/tap/tap.h>
#include <ccan/opt/opt.c>
#include <ccan/opt/usage.c>
#include <ccan/opt/helpers.c>
#include <ccan/opt/parse.c>

/* Test consume_words helper. */
int main(int argc, char *argv[])
{
	size_t prefix, len;
	bool start = true;

	plan_tests(27);

	/* Every line over width. */
	len = consume_words("hello world", 1, &prefix, &start);
	ok1(prefix == 0);
	ok1(!start);
	ok1(len == strlen("hello"));
	len = consume_words(" world", 1, &prefix, &start);
	ok1(prefix == 1);
	ok1(len == strlen("world"));
	ok1(!start);
	ok1(consume_words("", 1, &prefix, &start) == 0);

	/* Same with width where won't both fit. */
	start = true;
	len = consume_words("hello world", 5, &prefix, &start);
	ok1(!start);
	ok1(prefix == 0);
	ok1(len == strlen("hello"));
	len = consume_words(" world", 5, &prefix, &start);
	ok1(!start);
	ok1(prefix == 1);
	ok1(len == strlen("world"));
	ok1(consume_words("", 5, &prefix, &start) == 0);

	start = true;
	len = consume_words("hello world", 11, &prefix, &start);
	ok1(!start);
	ok1(prefix == 0);
	ok1(len == strlen("hello world"));
	ok1(consume_words("", 11, &prefix, &start) == 0);

	/* Now try a literal, should not be broken */
	start = true;
	len = consume_words(" hello world", 5, &prefix, &start);
	ok1(!start);
	ok1(prefix == 1);
	ok1(len == strlen("hello world"));

	/* A literal after an explicit \n also not broken */
	start = true;
	len = consume_words("hi\n hello world", 5, &prefix, &start);
	ok1(start);
	ok1(prefix == 0);
	ok1(len == strlen("hi\n"));
	len = consume_words(" hello world", 5, &prefix, &start);
	ok1(!start);
	ok1(prefix == 1);
	ok1(len == strlen("hello world"));

	return exit_status();
}