File: TR_pattern.sh

package info (click to toggle)
cctools 9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,624 kB
  • sloc: ansic: 192,539; python: 20,827; cpp: 20,199; sh: 11,719; perl: 4,106; xml: 3,688; makefile: 1,224
file content (74 lines) | stat: -rwxr-xr-x 1,535 bytes parent folder | download | duplicates (4)
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
65
66
67
68
69
70
71
72
73
74
#!/bin/sh

. ../../dttools/test/test_runner_common.sh

exe="pattern.test"

prepare()
{
	${CC} -I../src/ -g $CCTOOLS_TEST_CCFLAGS -o "$exe" -x c - -x none ../src/libdttools.a -lm <<EOF
#include "pattern.h"
#include "debug.h"

#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define check(expected,rc) \\
	do {\\
		ptrdiff_t e = (expected);\\
		if (e != rc)\\
			fatal("[%s:%d]: unexpected failure: %d (got) -> %d (expected)\n\t%s", __FILE__, __LINE__, (int)e, rc, #expected);\\
	} while (0)

int main (int argc, char *argv[])
{
	char *cap1;
	char *cap2;
	size_t ncap;
	check(pattern_match("foo", "foo"), 0);
	check(pattern_match("foo", "o"), 1);
	check(pattern_match("foo", "oo"), 1);
	check(pattern_match("foo", "o$"), 2);
	check(pattern_match("foo", "^foo"), 0);
	check(pattern_match("foo", "^f"), 0);
	check(pattern_match("foo", "^"), 0);
	check(pattern_match("foo", "^o"), -1);
	check(pattern_match("foo", "(foo)", &cap1), 0);
	check(strcmp(cap1, "foo"), 0);
	free(cap1);
	check(pattern_match("foo", "bar"), -1);
	check(pattern_match("foo", "(foo)()", &cap1, &ncap), 0);
	check(strcmp(cap1, "foo"), 0);
	check(ncap, 3);
	free(cap1);
	check(pattern_match("foobar", "(foo)()b(.*)", &cap1, &ncap, &cap2), 0);
	check(strcmp(cap1, "foo"), 0);
	check(ncap, 3);
	check(strcmp(cap2, "ar"), 0);
	free(cap1);
	free(cap2);
	return 0;
}
EOF
	return $?
}

run()
{
	./"$exe"
	return $?
}

clean()
{
	rm -f "$exe"
	return 0
}

dispatch "$@"

# vim: set noexpandtab tabstop=4: