summaryrefslogtreecommitdiff
path: root/regress/usr.bin/tsort/tsort-check
blob: f42342bce8e5c0ab3fc381c7649132086e1722d9 (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
#!/usr/bin/perl

# Written by Marc Espie, 2001.
# Public domain

%order=();

%exception=();
%ok=();


open(SORTED, shift) or die "No sorted output\n";
while(<SORTED>) {
	chomp;
	while (m/^tsort: cycle in data/) {
		@list = ();
		while (<SORTED>) {
			chomp;
			last if m/^tsort: cycle in data/;
			last unless m/^tsort:\s+/;
			push(@list, $');
		}
		for $i (1 .. @list) {
			$exception{$list[$i-1].' '.$list[$i % @list]} = 1;
		}
		$break{$list[1]} = 1;
	}
	$order{$_} = $i++;
}
close(SORTED);

@pairs=();

open(PAIRS, shift) or die "No pairs\n";
while (<PAIRS>) {
	chomp;
	push(@pairs, split(/\s+/, $_));
	while (@pairs >= 2) {
	    $b = pop @pairs;
	    $a = pop @pairs;
	    if (defined $exception{"$a $b"}) {
	    	$ok{"$a $b"} = 1;
	    }
	    next if $break{$a} = 1;
	    next unless $order{$a} < $order{$b};
	    die "Bad pair $a $b\n";
    	}
}
close(PAIRS);

while (($key, $v) = each %exception) {
	next if $v != 1;
	die "Bogus cycle edge $key\n" unless $ok{$key};
}