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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------
# drawElements Quality Program utilities
# --------------------------------------
#
# Copyright 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-------------------------------------------------------------------------
import sys
import random
import string
import subprocess
from optparse import OptionParser
def all (results, predicate):
for result in results:
if not predicate(result):
return False
return True
def any (results, predicate):
for result in results:
if predicate(result):
return True
return False
class FilterRule:
def __init__ (self, name, description, filters):
self.name = name
self.description = description
self.filters = filters
class TestCaseResult:
def __init__ (self, name, results):
self.name = name
self.results = results
class Group:
def __init__ (self, name):
self.name = name
self.cases = []
def readCaseList (filename):
f = open(filename, 'rb')
cases = []
for line in f:
if line[:6] == "TEST: ":
case = line[6:].strip()
if len(case) > 0:
cases.append(case)
return cases
def toResultList (caselist):
results = []
for case in caselist:
results.append(TestCaseResult(case, []))
return results
def addResultsToCaseList (caselist, results):
resultMap = {}
caseListRes = toResultList(caselist)
for res in caseListRes:
resultMap[res.name] = res
for result in results:
if result.name in resultMap:
resultMap[result.name].results += result.results
return caseListRes
def readTestResults (filename):
f = open(filename, 'rb')
csvData = f.read()
csvLines = csvData.splitlines()
results = []
f.close()
for line in csvLines[1:]:
args = line.split(',')
if len(args) == 1:
continue # Ignore
results.append(TestCaseResult(args[0], args[1:]))
if len(results) == 0:
raise Exception("Empty result list")
# Sanity check for results
numResultItems = len(results[0].results)
seenResults = set()
for result in results:
if result.name in seenResults:
raise Exception("Duplicate result row for test case '%s'" % result.name)
if len(result.results) != numResultItems:
raise Exception("Found %d results for test case '%s', expected %d" % (len(result.results), result.name, numResultItems))
seenResults.add(result.name)
return results
def readGroupList (filename):
f = open(filename, 'rb')
groups = []
for line in f:
group = line.strip()
if group != "":
groups.append(group)
return groups
def createGroups (results, groupNames):
groups = []
matched = set()
for groupName in groupNames:
group = Group(groupName)
groups.append(group)
prefix = groupName + "."
prefixLen = len(prefix)
for case in results:
if case.name[:prefixLen] == prefix:
if case in matched:
die("Case '%s' matched by multiple groups (when processing '%s')" % (case.name, group.name))
group.cases.append(case)
matched.add(case)
return groups
def createLeafGroups (results):
groups = []
groupMap = {}
for case in results:
parts = case.name.split('.')
groupName = string.join(parts[:-1], ".")
if not groupName in groupMap:
group = Group(groupName)
groups.append(group)
groupMap[groupName] = group
else:
group = groupMap[groupName]
group.cases.append(case)
return groups
def filterList (results, condition):
filtered = []
for case in results:
if condition(case.results):
filtered.append(case)
return filtered
def getFilter (list, name):
for filter in list:
if filter.name == name:
return filter
return None
def getNumCasesInGroups (groups):
numCases = 0
for group in groups:
numCases += len(group.cases)
return numCases
def getCasesInSet (results, caseSet):
filtered = []
for case in results:
if case in caseSet:
filtered.append(case)
return filtered
def selectCasesInGroups (results, groups):
casesInGroups = set()
for group in groups:
for case in group.cases:
casesInGroups.add(case)
return getCasesInSet(results, casesInGroups)
def selectRandomSubset (results, groups, limit, seed):
selectedCases = set()
numSelect = min(limit, getNumCasesInGroups(groups))
random.seed(seed)
random.shuffle(groups)
groupNdx = 0
while len(selectedCases) < numSelect:
group = groups[groupNdx]
if len(group.cases) == 0:
del groups[groupNdx]
if groupNdx == len(groups):
groupNdx -= 1
continue # Try next
selected = random.choice(group.cases)
selectedCases.add(selected)
group.cases.remove(selected)
groupNdx = (groupNdx + 1) % len(groups)
return getCasesInSet(results, selectedCases)
def die (msg):
print(msg)
sys.exit(-1)
# Named filter lists
FILTER_RULES = [
FilterRule("all", "No filtering", []),
FilterRule("all-pass", "All results must be 'Pass'", [lambda l: all(l, lambda r: r == 'Pass')]),
FilterRule("any-pass", "Any of results is 'Pass'", [lambda l: any(l, lambda r: r == 'Pass')]),
FilterRule("any-fail", "Any of results is not 'Pass' or 'NotSupported'", [lambda l: not all(l, lambda r: r == 'Pass' or r == 'NotSupported')]),
FilterRule("prev-failing", "Any except last result is failure", [lambda l: l[-1] == 'Pass' and not all(l[:-1], lambda r: r == 'Pass')]),
FilterRule("prev-passing", "Any except last result is 'Pass'", [lambda l: l[-1] != 'Pass' and any(l[:-1], lambda r: r == 'Pass')])
]
if __name__ == "__main__":
parser = OptionParser(usage = "usage: %prog [options] [caselist] [result csv file]")
parser.add_option("-f", "--filter", dest="filter", default="all", help="filter rule name")
parser.add_option("-l", "--list", action="store_true", dest="list", default=False, help="list available rules")
parser.add_option("-n", "--num", dest="limit", default=0, help="limit number of cases")
parser.add_option("-s", "--seed", dest="seed", default=0, help="use selected seed for random selection")
parser.add_option("-g", "--groups", dest="groups_file", default=None, help="select cases based on group list file")
(options, args) = parser.parse_args()
if options.list:
print("Available filter rules:")
for filter in FILTER_RULES:
print(" %s: %s" % (filter.name, filter.description))
sys.exit(0)
if len(args) == 0:
die("No input files specified")
elif len(args) > 2:
die("Too many arguments")
# Fetch filter
filter = getFilter(FILTER_RULES, options.filter)
if filter == None:
die("Unknown filter '%s'" % options.filter)
# Read case list
caselist = readCaseList(args[0])
if len(args) > 1:
results = readTestResults(args[1])
results = addResultsToCaseList(caselist, results)
else:
results = toResultList(caselist)
# Execute filters for results
for rule in filter.filters:
results = filterList(results, rule)
if options.limit != 0:
if options.groups_file != None:
groups = createGroups(results, readGroupList(options.groups_file))
else:
groups = createLeafGroups(results)
results = selectRandomSubset(results, groups, int(options.limit), int(options.seed))
elif options.groups_file != None:
groups = createGroups(results, readGroupList(options.groups_file))
results = selectCasesInGroups(results, groups)
# Print test set
for result in results:
print(result.name)
|