1#!/usr/bin/python2
2#
3# Post-process the output of test-dumpevents and check it for correctness.
4#
5
6import math
7import re
8import sys
9
10text = sys.stdin.readlines()
11
12try:
13    expect_inserted_pos = text.index("Inserted:\n")
14    expect_active_pos = text.index("Active:\n")
15    got_inserted_pos = text.index("Inserted events:\n")
16    got_active_pos = text.index("Active events:\n")
17except ValueError:
18    print >>sys.stderr, "Missing expected dividing line in dumpevents output"
19    sys.exit(1)
20
21if not (expect_inserted_pos < expect_active_pos <
22        got_inserted_pos < got_active_pos):
23    print >>sys.stderr, "Sections out of order in dumpevents output"
24    sys.exit(1)
25
26now,T= text[1].split()
27T = float(T)
28
29want_inserted = set(text[expect_inserted_pos+1:expect_active_pos])
30want_active = set(text[expect_active_pos+1:got_inserted_pos-1])
31got_inserted = set(text[got_inserted_pos+1:got_active_pos])
32got_active = set(text[got_active_pos+1:])
33
34pat = re.compile(r'Timeout=([0-9\.]+)')
35def replace_time(m):
36    t = float(m.group(1))
37    if .9 < abs(t-T) < 1.1:
38        return "Timeout=T+1"
39    elif 2.4 < abs(t-T) < 2.6:
40        return "Timeout=T+2.5"
41    else:
42        return m.group(0)
43
44cleaned_inserted = set( pat.sub(replace_time, s) for s in got_inserted
45                        if "Internal" not in s)
46
47if cleaned_inserted != want_inserted:
48    print >>sys.stderr, "Inserted event lists were not as expected!"
49    sys.exit(1)
50
51if set(got_active) != set(want_active):
52    print >>sys.stderr, "Active event lists were not as expected!"
53    sys.exit(1)
54
55