File: TR_path_collapse.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 (99 lines) | stat: -rwxr-xr-x 2,265 bytes parent folder | download | duplicates (3)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh

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

exe="path_collapse.test"

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

void test (const char *in, const char *expected, int dots)
{
	static char out[4096];
	memset(out, 0, sizeof(out));
	path_collapse(in, out, dots);
	if (strcmp(out, expected) != 0) {
		fprintf(stderr, "for %s, got %s expected %s (dots = %d)\n", in, out, expected, dots);
		exit(EXIT_FAILURE);
	}
}
int main (int argc, char *argv[])
{
	test("/foo/bar", "/foo/bar", 1);
	test("/foo/bar", "/foo/bar", 0);
	test("/foo//bar", "/foo/bar", 0);
	test("/foo//bar//", "/foo/bar/", 0);
	test("/foo//bar/.", "/foo/bar/", 0);
	test("/foo//../.", "/foo/..", 0);
	test("/foo//../.", "/", 1);
	test("/foo//../.././/", "/foo/../..", 0);
	test("/foo//../.././/", "/", 1);
	test("/foo//../.././..//", "/foo/../../..", 0);
	test("/foo//../.././..//", "/", 1);
	test("/foo//.././..", "/foo/../..", 0);
	test("/foo//.././.././/", "/foo/../..", 0);
	test("/foo//.././.././/.", "/foo/../..", 0);
	test("/foo//.././.././/.//", "/foo/../..", 0);
	test("/.//foo/bar", "/foo/bar", 0);
	test("/.//foo/bar", "/foo/bar", 1);

	test("foo/bar", "foo/bar", 0);
	test("foo/bar", "foo/bar", 1);
	test("./foo/bar", "./foo/bar", 0);
	test("./foo/bar", "./foo/bar", 1);
	test(".//foo/bar", "./foo/bar", 0);
	test(".//foo/bar", "./foo/bar", 1);
	test(".//foo/bar/", "./foo/bar/", 0);
	test(".//foo/bar/", "./foo/bar/", 1);
	test(".//foo/bar//", "./foo/bar/", 0);
	test(".//foo/bar//", "./foo/bar/", 1);

	test("", "/", 0);
	test("/", "/", 0);
	test("/.", "/", 0);
	test("/./", "/", 0);

	test(".", ".", 0);
	test("./", ".", 0);

	test("...", "...", 0);
	test("/...", "/...", 0);
	test("/.../", "/.../", 0);
	test("./...", "./...", 0);
	test("./.../", "./.../", 0);

	test("./../", "./..", 0);
	test("./../", "/", 1);
	test("foo/../", "/", 1);
	test("./foo/../", ".", 1);
	test("./foo/../..", "/", 1);
	test("./foo/../../", "/", 1);
	test("foo/../..", "/", 1);
	test("foo/../../", "/", 1);

	return 0;
}
EOF
	return $?
}

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

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

dispatch "$@"

# vim: set noexpandtab tabstop=4: